summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorMatthew Brett <matthew.brett@gmail.com>2021-10-23 16:14:43 +0100
committerMatthew Brett <matthew.brett@gmail.com>2021-10-23 18:33:16 +0100
commit07778e58fbb5bd0e5172a9c1a99c391144ed41de (patch)
tree404f6243cf10b9e0a9f6ee0786962eaf76061c33 /numpy
parent6409a721c17aae94cfd55ccbcf26a95494dd1cf8 (diff)
downloadnumpy-07778e58fbb5bd0e5172a9c1a99c391144ed41de.tar.gz
Allow clib callable build flags
At the moment, we are guessing whether we have the MSVC compiler, by looking at what Python was originally compiled for. That works only if we are using the same compiler, but this is not the case when we compile with e.g. mingw-w64 using Python.org Python. Unfortunately, at the time we are specifying build flags, we don't know what compiler we are using. Allow build flags to clib to be callables that return lists of strings, instead of strings, where the callables can do work like inspecting the compiler, at build time. Use this to check for MSVC at build time, when specifying the `/GL-` flag. See gh-9977 for a related discussion about these flags.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/setup.py17
-rw-r--r--numpy/distutils/command/build_clib.py34
-rw-r--r--numpy/random/setup.py14
3 files changed, 56 insertions, 9 deletions
diff --git a/numpy/core/setup.py b/numpy/core/setup.py
index 6800f65e9..7bf70ed88 100644
--- a/numpy/core/setup.py
+++ b/numpy/core/setup.py
@@ -696,16 +696,23 @@ def configuration(parent_package='',top_path=None):
join('src', 'npymath', 'halffloat.c')
]
- # Must be true for CRT compilers but not MinGW/cygwin. See gh-9977.
- # Intel and Clang also don't seem happy with /GL
- is_msvc = (platform.platform().startswith('Windows') and
- platform.python_compiler().startswith('MS'))
+ def gl_if_msvc(build_cmd):
+ """ Add flag if we are using MSVC compiler
+
+ We can't see this in our scope, because we have not initialized the
+ distutils build command, so use this deferred calculation to run when
+ we are building the library.
+ """
+ if build_cmd.compiler.compiler_type == 'msvc':
+ return ['/GL-']
+ return []
+
config.add_installed_library('npymath',
sources=npymath_sources + [get_mathlib_info],
install_dir='lib',
build_info={
'include_dirs' : [], # empty list required for creating npy_math_internal.h
- 'extra_compiler_args' : (['/GL-'] if is_msvc else []),
+ 'extra_compiler_args': [gl_if_msvc],
})
config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config",
subst_dict)
diff --git a/numpy/distutils/command/build_clib.py b/numpy/distutils/command/build_clib.py
index a481758c1..45201f98f 100644
--- a/numpy/distutils/command/build_clib.py
+++ b/numpy/distutils/command/build_clib.py
@@ -185,6 +185,30 @@ class build_clib(old_build_clib):
for (lib_name, build_info) in libraries:
self.build_a_library(build_info, lib_name, libraries)
+ def assemble_flags(self, in_flags):
+ """ Assemble flags from flag list
+
+ Parameters
+ ----------
+ in_flags : None or sequence
+ None corresponds to empty list. Sequence elements can be strings
+ or callables that return lists of strings. Callable takes `self` as
+ single parameter.
+
+ Returns
+ -------
+ out_flags : list
+ """
+ if in_flags is None:
+ return []
+ out_flags = []
+ for in_flag in in_flags:
+ if callable(in_flag):
+ out_flags += in_flag(self)
+ else:
+ out_flags.append(in_flag)
+ return out_flags
+
def build_a_library(self, build_info, lib_name, libraries):
# default compilers
compiler = self.compiler
@@ -263,9 +287,13 @@ class build_clib(old_build_clib):
include_dirs = build_info.get('include_dirs')
if include_dirs is None:
include_dirs = []
- extra_postargs = build_info.get('extra_compiler_args') or []
- extra_cflags = build_info.get('extra_cflags') or []
- extra_cxxflags = build_info.get('extra_cxxflags') or []
+ # Flags can be strings, or callables that return a list of strings.
+ extra_postargs = self.assemble_flags(
+ build_info.get('extra_compiler_args'))
+ extra_cflags = self.assemble_flags(
+ build_info.get('extra_cflags'))
+ extra_cxxflags = self.assemble_flags(
+ build_info.get('extra_cxxflags'))
include_dirs.extend(get_numpy_include_dirs())
# where compiled F90 module files are:
diff --git a/numpy/random/setup.py b/numpy/random/setup.py
index dce9a101e..59a30d2b6 100644
--- a/numpy/random/setup.py
+++ b/numpy/random/setup.py
@@ -65,12 +65,24 @@ def configuration(parent_package='', top_path=None):
'src/distributions/random_mvhg_marginals.c',
'src/distributions/random_hypergeometric.c',
]
+
+ def gl_if_msvc(build_cmd):
+ """ Add flag if we are using MSVC compiler
+
+ We can't see this in our scope, because we have not initialized the
+ distutils build command, so use this deferred calculation to run when
+ we are building the library.
+ """
+ if build_cmd.compiler.compiler_type == 'msvc':
+ return ['/GL-']
+ return []
+
config.add_installed_library('npyrandom',
sources=npyrandom_sources,
install_dir='lib',
build_info={
'include_dirs' : [], # empty list required for creating npyrandom.h
- 'extra_compiler_args' : (['/GL-'] if is_msvc else []),
+ 'extra_compiler_args': [gl_if_msvc],
})
for gen in ['mt19937']: