setup.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import os
  2. import platform
  3. import sys
  4. from os.path import join
  5. from numpy.distutils.system_info import platform_bits
  6. is_msvc = (platform.platform().startswith('Windows') and
  7. platform.python_compiler().startswith('MS'))
  8. def configuration(parent_package='', top_path=None):
  9. from numpy.distutils.misc_util import Configuration, get_mathlibs
  10. config = Configuration('random', parent_package, top_path)
  11. def generate_libraries(ext, build_dir):
  12. config_cmd = config.get_config_cmd()
  13. libs = get_mathlibs()
  14. if sys.platform == 'win32':
  15. libs.extend(['Advapi32', 'Kernel32'])
  16. ext.libraries.extend(libs)
  17. return None
  18. # enable unix large file support on 32 bit systems
  19. # (64 bit off_t, lseek -> lseek64 etc.)
  20. if sys.platform[:3] == 'aix':
  21. defs = [('_LARGE_FILES', None)]
  22. else:
  23. defs = [('_FILE_OFFSET_BITS', '64'),
  24. ('_LARGEFILE_SOURCE', '1'),
  25. ('_LARGEFILE64_SOURCE', '1')]
  26. defs.append(('NPY_NO_DEPRECATED_API', 0))
  27. config.add_subpackage('tests')
  28. config.add_data_dir('tests/data')
  29. config.add_data_dir('_examples')
  30. EXTRA_LINK_ARGS = []
  31. EXTRA_LIBRARIES = ['npyrandom']
  32. if os.name != 'nt':
  33. # Math lib
  34. EXTRA_LIBRARIES.append('m')
  35. # Some bit generators exclude GCC inlining
  36. EXTRA_COMPILE_ARGS = ['-U__GNUC_GNU_INLINE__']
  37. if is_msvc and platform_bits == 32:
  38. # 32-bit windows requires explicit sse2 option
  39. EXTRA_COMPILE_ARGS += ['/arch:SSE2']
  40. elif not is_msvc:
  41. # Some bit generators require c99
  42. EXTRA_COMPILE_ARGS += ['-std=c99']
  43. # Use legacy integer variable sizes
  44. LEGACY_DEFS = [('NP_RANDOM_LEGACY', '1')]
  45. PCG64_DEFS = []
  46. # One can force emulated 128-bit arithmetic if one wants.
  47. #PCG64_DEFS += [('PCG_FORCE_EMULATED_128BIT_MATH', '1')]
  48. depends = ['__init__.pxd', 'c_distributions.pxd', 'bit_generator.pxd']
  49. # npyrandom - a library like npymath
  50. npyrandom_sources = [
  51. 'src/distributions/logfactorial.c',
  52. 'src/distributions/distributions.c',
  53. 'src/distributions/random_mvhg_count.c',
  54. 'src/distributions/random_mvhg_marginals.c',
  55. 'src/distributions/random_hypergeometric.c',
  56. ]
  57. config.add_installed_library('npyrandom',
  58. sources=npyrandom_sources,
  59. install_dir='lib',
  60. build_info={
  61. 'include_dirs' : [], # empty list required for creating npyrandom.h
  62. 'extra_compiler_args' : (['/GL-'] if is_msvc else []),
  63. })
  64. for gen in ['mt19937']:
  65. # gen.pyx, src/gen/gen.c, src/gen/gen-jump.c
  66. config.add_extension(f'_{gen}',
  67. sources=[f'_{gen}.c',
  68. f'src/{gen}/{gen}.c',
  69. f'src/{gen}/{gen}-jump.c'],
  70. include_dirs=['.', 'src', join('src', gen)],
  71. libraries=EXTRA_LIBRARIES,
  72. extra_compile_args=EXTRA_COMPILE_ARGS,
  73. extra_link_args=EXTRA_LINK_ARGS,
  74. depends=depends + [f'_{gen}.pyx'],
  75. define_macros=defs,
  76. )
  77. for gen in ['philox', 'pcg64', 'sfc64']:
  78. # gen.pyx, src/gen/gen.c
  79. _defs = defs + PCG64_DEFS if gen == 'pcg64' else defs
  80. config.add_extension(f'_{gen}',
  81. sources=[f'_{gen}.c',
  82. f'src/{gen}/{gen}.c'],
  83. include_dirs=['.', 'src', join('src', gen)],
  84. libraries=EXTRA_LIBRARIES,
  85. extra_compile_args=EXTRA_COMPILE_ARGS,
  86. extra_link_args=EXTRA_LINK_ARGS,
  87. depends=depends + [f'_{gen}.pyx',
  88. 'bit_generator.pyx', 'bit_generator.pxd'],
  89. define_macros=_defs,
  90. )
  91. for gen in ['_common', 'bit_generator']:
  92. # gen.pyx
  93. config.add_extension(gen,
  94. sources=[f'{gen}.c'],
  95. libraries=EXTRA_LIBRARIES,
  96. extra_compile_args=EXTRA_COMPILE_ARGS,
  97. extra_link_args=EXTRA_LINK_ARGS,
  98. include_dirs=['.', 'src'],
  99. depends=depends + [f'{gen}.pyx', f'{gen}.pxd',],
  100. define_macros=defs,
  101. )
  102. config.add_data_files(f'{gen}.pxd')
  103. for gen in ['_generator', '_bounded_integers']:
  104. # gen.pyx, src/distributions/distributions.c
  105. config.add_extension(gen,
  106. sources=[f'{gen}.c'],
  107. libraries=EXTRA_LIBRARIES + ['npymath'],
  108. extra_compile_args=EXTRA_COMPILE_ARGS,
  109. include_dirs=['.', 'src'],
  110. extra_link_args=EXTRA_LINK_ARGS,
  111. depends=depends + [f'{gen}.pyx'],
  112. define_macros=defs,
  113. )
  114. config.add_data_files('_bounded_integers.pxd')
  115. mtrand_libs = ['m', 'npymath'] if os.name != 'nt' else ['npymath']
  116. config.add_extension('mtrand',
  117. sources=['mtrand.c',
  118. 'src/legacy/legacy-distributions.c',
  119. 'src/distributions/distributions.c',
  120. ],
  121. include_dirs=['.', 'src', 'src/legacy'],
  122. libraries=mtrand_libs,
  123. extra_compile_args=EXTRA_COMPILE_ARGS,
  124. extra_link_args=EXTRA_LINK_ARGS,
  125. depends=depends + ['mtrand.pyx'],
  126. define_macros=defs + LEGACY_DEFS,
  127. )
  128. config.add_data_files(*depends)
  129. config.add_data_files('*.pyi')
  130. return config
  131. if __name__ == '__main__':
  132. from numpy.distutils.core import setup
  133. setup(configuration=configuration)