setup.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is based on
  5. https://github.com/FedericoStra/cython-package-example/blob/master/setup.py
  6. """
  7. """
  8. MIT License
  9. Copyright (c) 2019 Federico Stra
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in all
  17. copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. SOFTWARE.
  25. """
  26. """
  27. Copyright 2021 anonimous <shkolnick-kun@gmail.com> and contributors.
  28. Licensed under the Apache License, Version 2.0 (the "License");
  29. you may not use this file except in compliance with the License.
  30. You may obtain a copy of the License at
  31. http://www.apache.org/licenses/LICENSE-2.0
  32. Unless required by applicable law or agreed to in writing,
  33. software distributed under the License is distributed on an "AS IS" BASIS,
  34. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  35. See the License for the specific language governing permissions
  36. """
  37. import numpy
  38. import os
  39. from os.path import dirname, isfile, join, splitext
  40. from setuptools import Extension, setup
  41. try:
  42. from Cython.Build import cythonize
  43. from Cython.Compiler.Main import default_options
  44. except ImportError:
  45. cythonize = None
  46. # https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#distributing-cython-modules
  47. def no_cythonize(extensions, **_ignore):
  48. for extension in extensions:
  49. sources = []
  50. for sfile in extension.sources:
  51. path, ext = splitext(sfile)
  52. if ext in (".pyx", ".py"):
  53. if extension.language == "c++":
  54. ext = ".cpp"
  55. else:
  56. ext = ".c"
  57. sfile = path + ext
  58. sources.append(sfile)
  59. extension.sources[:] = sources
  60. return extensions
  61. EXT_NAME = "yaflpy"
  62. SETUP_DIR = dirname(__file__)
  63. SRC_DIR = join(SETUP_DIR, "src")
  64. EXT_DIR = join(SRC_DIR, EXT_NAME)
  65. DEFINES = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
  66. CT_ENV = {"YAFLPY_USE_64_BIT":False}
  67. if int(os.getenv("YAFL_64", 0)):
  68. #Override defaults in 64 bit case
  69. print('YAFL: The extensuion will be compiled in 64 bit variant!!!')
  70. CT_ENV = {"YAFLPY_USE_64_BIT":True}
  71. DEFINES += [("YAFL_USE_64_BIT",1)]
  72. extensions = [
  73. Extension(EXT_NAME, [join(EXT_DIR, EXT_NAME + ".pyx")],
  74. language='c',
  75. include_dirs=[numpy.get_include(), SRC_DIR, EXT_DIR],
  76. define_macros=DEFINES
  77. )
  78. ]
  79. if not isfile(join(EXT_DIR, EXT_NAME + ".c")):
  80. CYTHONIZE = cythonize is not None
  81. else:
  82. CYTHONIZE = bool(int(os.getenv("CYTHONIZE", 0))) and cythonize is not None
  83. if CYTHONIZE:
  84. default_options["compile_time_env"] = CT_ENV
  85. compiler_directives = {
  86. "language_level": 3,
  87. "embedsignature": True
  88. }
  89. extensions = cythonize(extensions, compiler_directives=compiler_directives)
  90. else:
  91. extensions = no_cythonize(extensions)
  92. with open("requirements.txt") as fp:
  93. install_requires = fp.read().strip().split("\n")
  94. with open("requirements-dev.txt") as fp:
  95. dev_requires = fp.read().strip().split("\n")
  96. setup(
  97. ext_modules=extensions,
  98. install_requires=install_requires,
  99. extras_require={
  100. "dev": dev_requires,
  101. "docs": ["sphinx", "sphinx-rtd-theme"]
  102. },
  103. )