diff options
-rw-r--r-- | mesonbuild/compilers/compilers.py | 53 | ||||
-rw-r--r-- | mesonbuild/environment.py | 53 | ||||
-rw-r--r-- | mesonbuild/interpreter.py | 2 | ||||
-rwxr-xr-x | run_unittests.py | 2 |
4 files changed, 54 insertions, 56 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 6a310ba..7ac0753 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import contextlib, os.path, re, tempfile +import contextlib, os.path, re, tempfile, shlex import subprocess from ..linkers import StaticLinker @@ -57,6 +57,15 @@ clike_suffixes += ('h', 'll', 's') soregex = re.compile(r'.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$') +# Environment variables that each lang uses. +cflags_mapping = {'c': 'CFLAGS', + 'cpp': 'CXXFLAGS', + 'objc': 'OBJCFLAGS', + 'objcpp': 'OBJCXXFLAGS', + 'fortran': 'FFLAGS', + 'd': 'DFLAGS', + 'vala': 'VALAFLAGS'} + # All these are only for C-like languages; see `clike_langs` above. def sort_clike(lang): @@ -716,6 +725,48 @@ class Compiler: """ return [] + def get_args_from_envvars(self): + """ + Returns a tuple of (compile_flags, link_flags) for the specified language + from the inherited environment + """ + def log_var(var, val): + if val: + mlog.log('Appending {} from environment: {!r}'.format(var, val)) + + lang = self.get_language() + compiler_is_linker = False + if hasattr(self, 'get_linker_exelist'): + compiler_is_linker = (self.get_exelist() == self.get_linker_exelist()) + + if lang not in cflags_mapping: + return [], [], [] + + compile_flags = os.environ.get(cflags_mapping[lang], '') + log_var(cflags_mapping[lang], compile_flags) + compile_flags = shlex.split(compile_flags) + + # Link flags (same for all languages) + link_flags = os.environ.get('LDFLAGS', '') + log_var('LDFLAGS', link_flags) + link_flags = shlex.split(link_flags) + if compiler_is_linker: + # When the compiler is used as a wrapper around the linker (such as + # with GCC and Clang), the compile flags can be needed while linking + # too. This is also what Autotools does. However, we don't want to do + # this when the linker is stand-alone such as with MSVC C/C++, etc. + link_flags = compile_flags + link_flags + + # Pre-processor flags (not for fortran or D) + preproc_flags = '' + if lang in ('c', 'cpp', 'objc', 'objcpp'): + preproc_flags = os.environ.get('CPPFLAGS', '') + log_var('CPPFLAGS', preproc_flags) + preproc_flags = shlex.split(preproc_flags) + compile_flags += preproc_flags + + return preproc_flags, compile_flags, link_flags + def get_options(self): return {} # build afresh every time diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 2453f51..fb1e070 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -72,15 +72,6 @@ from .compilers import ( build_filename = 'meson.build' -# Environment variables that each lang uses. -cflags_mapping = {'c': 'CFLAGS', - 'cpp': 'CXXFLAGS', - 'objc': 'OBJCFLAGS', - 'objcpp': 'OBJCXXFLAGS', - 'fortran': 'FFLAGS', - 'd': 'DFLAGS', - 'vala': 'VALAFLAGS'} - def detect_gcovr(version='3.1', log=False): gcovr_exe = 'gcovr' try: @@ -952,50 +943,6 @@ class Environment: out = out.split('\n')[index].lstrip('libraries: =').split(':') return [os.path.normpath(p) for p in out] -def get_args_from_envvars(compiler): - """ - @compiler: Compiler to fetch environment flags for - - Returns a tuple of (compile_flags, link_flags) for the specified language - from the inherited environment - """ - def log_var(var, val): - if val: - mlog.log('Appending {} from environment: {!r}'.format(var, val)) - - lang = compiler.get_language() - compiler_is_linker = False - if hasattr(compiler, 'get_linker_exelist'): - compiler_is_linker = (compiler.get_exelist() == compiler.get_linker_exelist()) - - if lang not in cflags_mapping: - return [], [], [] - - compile_flags = os.environ.get(cflags_mapping[lang], '') - log_var(cflags_mapping[lang], compile_flags) - compile_flags = shlex.split(compile_flags) - - # Link flags (same for all languages) - link_flags = os.environ.get('LDFLAGS', '') - log_var('LDFLAGS', link_flags) - link_flags = shlex.split(link_flags) - if compiler_is_linker: - # When the compiler is used as a wrapper around the linker (such as - # with GCC and Clang), the compile flags can be needed while linking - # too. This is also what Autotools does. However, we don't want to do - # this when the linker is stand-alone such as with MSVC C/C++, etc. - link_flags = compile_flags + link_flags - - # Pre-processor flags (not for fortran or D) - preproc_flags = '' - if lang in ('c', 'cpp', 'objc', 'objcpp'): - preproc_flags = os.environ.get('CPPFLAGS', '') - log_var('CPPFLAGS', preproc_flags) - preproc_flags = shlex.split(preproc_flags) - compile_flags += preproc_flags - - return preproc_flags, compile_flags, link_flags - class CrossBuildInfo: def __init__(self, filename): self.config = {'properties': {}} diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index cd608db..dcd54a3 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2471,7 +2471,7 @@ to directly access options of other subprojects.''') # If <language>_args/_link_args settings are given on the # command line, use them, otherwise take them from env. - (preproc_args, compile_args, link_args) = environment.get_args_from_envvars(comp) + (preproc_args, compile_args, link_args) = comp.get_args_from_envvars() for optspec in self.build.environment.cmd_line_options.projectoptions: (optname, optvalue) = optspec.split('=', maxsplit=1) if optname == lang + '_link_args': diff --git a/run_unittests.py b/run_unittests.py index 1400db7..79799ea 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -3427,7 +3427,7 @@ def unset_envs(): # For unit tests we must fully control all command lines # so that there are no unexpected changes coming from the # environment, for example when doing a package build. - varnames = ['CPPFLAGS', 'LDFLAGS'] + list(mesonbuild.environment.cflags_mapping.values()) + varnames = ['CPPFLAGS', 'LDFLAGS'] + list(mesonbuild.compilers.compilers.cflags_mapping.values()) for v in varnames: if v in os.environ: del os.environ[v] |