setup.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import os
  2. import sys
  3. from os.path import join
  4. from numpy.distutils.system_info import platform_bits
  5. from numpy.distutils.msvccompiler import lib_opts_if_msvc
  6. def configuration(parent_package='', top_path=None):
  7. from numpy.distutils.misc_util import Configuration, get_mathlibs
  8. config = Configuration('random', parent_package, top_path)
  9. def generate_libraries(ext, build_dir):
  10. config_cmd = config.get_config_cmd()
  11. libs = get_mathlibs()
  12. if sys.platform == 'win32':
  13. libs.extend(['Advapi32', 'Kernel32'])
  14. ext.libraries.extend(libs)
  15. return None
  16. # enable unix large file support on 32 bit systems
  17. # (64 bit off_t, lseek -> lseek64 etc.)
  18. if sys.platform[:3] == 'aix':
  19. defs = [('_LARGE_FILES', None)]
  20. else:
  21. defs = [('_FILE_OFFSET_BITS', '64'),
  22. ('_LARGEFILE_SOURCE', '1'),
  23. ('_LARGEFILE64_SOURCE', '1')]
  24. defs.append(('NPY_NO_DEPRECATED_API', 0))
  25. config.add_subpackage('tests')
  26. config.add_data_dir('tests/data')
  27. config.add_data_dir('_examples')
  28. EXTRA_LINK_ARGS = []
  29. EXTRA_LIBRARIES = ['npyrandom']
  30. if os.name != 'nt':
  31. # Math lib
  32. EXTRA_LIBRARIES.append('m')
  33. # Some bit generators exclude GCC inlining
  34. EXTRA_COMPILE_ARGS = ['-U__GNUC_GNU_INLINE__']
  35. if sys.platform == 'cygwin':
  36. # Export symbols without __declspec(dllexport) for using by cython.
  37. # Using __declspec(dllexport) does not export other necessary symbols
  38. # in Cygwin package's Cython environment, making it impossible to
  39. # import modules.
  40. EXTRA_LINK_ARGS += ['-Wl,--export-all-symbols']
  41. # Use legacy integer variable sizes
  42. LEGACY_DEFS = [('NP_RANDOM_LEGACY', '1')]
  43. PCG64_DEFS = []
  44. # One can force emulated 128-bit arithmetic if one wants.
  45. #PCG64_DEFS += [('PCG_FORCE_EMULATED_128BIT_MATH', '1')]
  46. depends = ['__init__.pxd', 'c_distributions.pxd', 'bit_generator.pxd']
  47. # npyrandom - a library like npymath
  48. npyrandom_sources = [
  49. 'src/distributions/logfactorial.c',
  50. 'src/distributions/distributions.c',
  51. 'src/distributions/random_mvhg_count.c',
  52. 'src/distributions/random_mvhg_marginals.c',
  53. 'src/distributions/random_hypergeometric.c',
  54. ]
  55. def lib_opts(build_cmd):
  56. """ Add flags that depend on the compiler.
  57. We can't see which compiler we are using in our scope, because we have
  58. not initialized the distutils build command, so use this deferred
  59. calculation to run when we are building the library.
  60. """
  61. opts = lib_opts_if_msvc(build_cmd)
  62. if build_cmd.compiler.compiler_type != 'msvc':
  63. # Some bit generators require c99
  64. opts.append('-std=c99')
  65. return opts
  66. config.add_installed_library('npyrandom',
  67. sources=npyrandom_sources,
  68. install_dir='lib',
  69. build_info={
  70. 'include_dirs' : [], # empty list required for creating npyrandom.h
  71. 'extra_compiler_args': [lib_opts],
  72. })
  73. for gen in ['mt19937']:
  74. # gen.pyx, src/gen/gen.c, src/gen/gen-jump.c
  75. config.add_extension(f'_{gen}',
  76. sources=[f'_{gen}.c',
  77. f'src/{gen}/{gen}.c',
  78. f'src/{gen}/{gen}-jump.c'],
  79. include_dirs=['.', 'src', join('src', gen)],
  80. libraries=EXTRA_LIBRARIES,
  81. extra_compile_args=EXTRA_COMPILE_ARGS,
  82. extra_link_args=EXTRA_LINK_ARGS,
  83. depends=depends + [f'_{gen}.pyx'],
  84. define_macros=defs,
  85. )
  86. for gen in ['philox', 'pcg64', 'sfc64']:
  87. # gen.pyx, src/gen/gen.c
  88. _defs = defs + PCG64_DEFS if gen == 'pcg64' else defs
  89. config.add_extension(f'_{gen}',
  90. sources=[f'_{gen}.c',
  91. f'src/{gen}/{gen}.c'],
  92. include_dirs=['.', 'src', join('src', gen)],
  93. libraries=EXTRA_LIBRARIES,
  94. extra_compile_args=EXTRA_COMPILE_ARGS,
  95. extra_link_args=EXTRA_LINK_ARGS,
  96. depends=depends + [f'_{gen}.pyx',
  97. 'bit_generator.pyx', 'bit_generator.pxd'],
  98. define_macros=_defs,
  99. )
  100. for gen in ['_common', 'bit_generator']:
  101. # gen.pyx
  102. config.add_extension(gen,
  103. sources=[f'{gen}.c'],
  104. libraries=EXTRA_LIBRARIES,
  105. extra_compile_args=EXTRA_COMPILE_ARGS,
  106. extra_link_args=EXTRA_LINK_ARGS,
  107. include_dirs=['.', 'src'],
  108. depends=depends + [f'{gen}.pyx', f'{gen}.pxd',],
  109. define_macros=defs,
  110. )
  111. config.add_data_files(f'{gen}.pxd')
  112. for gen in ['_generator', '_bounded_integers']:
  113. # gen.pyx, src/distributions/distributions.c
  114. config.add_extension(gen,
  115. sources=[f'{gen}.c'],
  116. libraries=EXTRA_LIBRARIES + ['npymath'],
  117. extra_compile_args=EXTRA_COMPILE_ARGS,
  118. include_dirs=['.', 'src'],
  119. extra_link_args=EXTRA_LINK_ARGS,
  120. depends=depends + [f'{gen}.pyx'],
  121. define_macros=defs,
  122. )
  123. config.add_data_files('_bounded_integers.pxd')
  124. mtrand_libs = ['m', 'npymath'] if os.name != 'nt' else ['npymath']
  125. config.add_extension('mtrand',
  126. sources=['mtrand.c',
  127. 'src/legacy/legacy-distributions.c',
  128. 'src/distributions/distributions.c',
  129. ],
  130. include_dirs=['.', 'src', 'src/legacy'],
  131. libraries=mtrand_libs,
  132. extra_compile_args=EXTRA_COMPILE_ARGS,
  133. extra_link_args=EXTRA_LINK_ARGS,
  134. depends=depends + ['mtrand.pyx'],
  135. define_macros=defs + LEGACY_DEFS,
  136. )
  137. config.add_data_files(*depends)
  138. config.add_data_files('*.pyi')
  139. return config
  140. if __name__ == '__main__':
  141. from numpy.distutils.core import setup
  142. setup(configuration=configuration)