Skip to content

Commit

Permalink
Introduce numpy.core.setup_common.NPY_CXX_FLAGS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
serge-sans-paille authored and charris committed May 5, 2022
1 parent 65bbf0c commit 270b833
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 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
15 changes: 10 additions & 5 deletions numpy/core/setup.py
Expand Up @@ -464,6 +464,8 @@ def configuration(parent_package='',top_path=None):
from numpy.distutils.misc_util import Configuration, dot_join
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
Expand Down Expand Up @@ -738,11 +740,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 +1088,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 270b833

Please sign in to comment.