From a284f54c793675ecad496960d0e2360251e59891 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 7 Apr 2022 07:55:46 +0200 Subject: [PATCH] Introduce numpy.core.setup_common.NPY_CXX_FLAGS Group all C++ flags in one location. This avoids redundancy and makes sure we test the flags we use, and use the flags we test. Fix #21302 --- doc/source/user/building.rst | 3 +++ numpy/core/setup.py | 24 +++++++++++++++--------- numpy/distutils/ccompiler_opt.py | 8 ++++++++ numpy/linalg/setup.py | 2 ++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/doc/source/user/building.rst b/doc/source/user/building.rst index 22efca4a6f83..01ec65d3b6b3 100644 --- a/doc/source/user/building.rst +++ b/doc/source/user/building.rst @@ -48,6 +48,9 @@ Building NumPy requires the following software installed: Much of NumPy is written in C. You will need a C compiler that complies with the C99 standard. + Part of Numpy is now written in C++. You will also need a C++ compiler that + complies with the C++11 standard. + While a FORTRAN 77 compiler is not necessary for building NumPy, it is needed to run the ``numpy.f2py`` tests. These tests are skipped if the compiler is not auto-detected. diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 136492dd5e86..157c825d2a1e 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -461,15 +461,18 @@ def visibility_define(config): return '' def configuration(parent_package='',top_path=None): - from numpy.distutils.misc_util import Configuration, dot_join + from numpy.distutils.misc_util import (Configuration, dot_join, + exec_mod_from_location) from numpy.distutils.system_info import (get_info, blas_opt_info, lapack_opt_info) + from numpy.distutils.ccompiler_opt import NPY_CXX_FLAGS + from numpy.version import release as is_released config = Configuration('core', parent_package, top_path) local_dir = config.local_path codegen_dir = join(local_dir, 'code_generators') - if is_released(config): + if is_released: warnings.simplefilter('error', MismatchCAPIWarning) # Check whether we have a mismatch between the set C API VERSION and the @@ -478,8 +481,8 @@ def configuration(parent_package='',top_path=None): generate_umath_py = join(codegen_dir, 'generate_umath.py') n = dot_join(config.name, 'generate_umath') - generate_umath = npy_load_module('_'.join(n.split('.')), - generate_umath_py, ('.py', 'U', 1)) + generate_umath = exec_mod_from_location('_'.join(n.split('.')), + generate_umath_py) header_dir = 'include/numpy' # this is relative to config.path_in_package @@ -738,11 +741,17 @@ def get_mathlib_info(*args): ): is_cpp = lang == 'c++' if is_cpp: - # this a workround to get rid of invalid c++ flags + # this a workaround to get rid of invalid c++ flags # without doing big changes to config. # c tested first, compiler should be here bk_c = config_cmd.compiler config_cmd.compiler = bk_c.cxx_compiler() + + # Check that Linux compiler actually support the default flags + if hasattr(config_cmd.compiler, 'compiler'): + config_cmd.compiler.compiler.extend(NPY_CXX_FLAGS) + config_cmd.compiler.compiler_so.extend(NPY_CXX_FLAGS) + st = config_cmd.try_link(test_code, lang=lang) if not st: # rerun the failing command in verbose mode @@ -1080,10 +1089,7 @@ def generate_umath_c(ext, build_dir): libraries=['npymath'], extra_objects=svml_objs, extra_info=extra_info, - extra_cxx_compile_args=['-std=c++11', - '-D__STDC_VERSION__=0', - '-fno-exceptions', - '-fno-rtti']) + extra_cxx_compile_args=NPY_CXX_FLAGS) ####################################################################### # umath_tests module # diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index b38e47c13a94..205a8e060f69 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -16,6 +16,14 @@ import subprocess import textwrap +# These flags are used to compile any C++ source within Numpy. +# They are chosen to have very few runtime dependencies. +NPY_CXX_FLAGS = [ + '-std=c++11', # Minimal standard version + '-D__STDC_VERSION__=0', # for compatibility with C headers + '-fno-exceptions', # no exception support + '-fno-rtti'] # no runtime type information + class _Config: """An abstract class holds all configurable attributes of `CCompilerOpt`, diff --git a/numpy/linalg/setup.py b/numpy/linalg/setup.py index 94536bb2c055..73c386f1ccbf 100644 --- a/numpy/linalg/setup.py +++ b/numpy/linalg/setup.py @@ -3,6 +3,7 @@ def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration + from numpy.distutils.ccompiler_opt import NPY_CXX_FLAGS from numpy.distutils.system_info import get_info, system_info config = Configuration('linalg', parent_package, top_path) @@ -72,6 +73,7 @@ def get_lapack_lite_sources(ext, build_dir): sources=['umath_linalg.c.src', get_lapack_lite_sources], depends=['lapack_lite/f2c.h'], extra_info=lapack_info, + extra_cxx_compile_args=NPY_CXX_FLAGS, libraries=['npymath'], ) config.add_data_files('*.pyi')