Skip to content

Commit

Permalink
Merge pull request #21448 from charris/backport-21306
Browse files Browse the repository at this point in the history
ENH: Introduce numpy.core.setup_common.NPY_CXX_FLAGS
  • Loading branch information
charris committed May 5, 2022
2 parents 65bbf0c + a284f54 commit fdb5393
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions doc/source/user/building.rst
Expand Up @@ -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.
Expand Down
24 changes: 15 additions & 9 deletions numpy/core/setup.py
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 #
Expand Down
8 changes: 8 additions & 0 deletions numpy/distutils/ccompiler_opt.py
Expand Up @@ -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`,
Expand Down
2 changes: 2 additions & 0 deletions numpy/linalg/setup.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit fdb5393

Please sign in to comment.