Skip to content

Commit 2d1c67f

Browse files
bonzinieli-schwartz
authored andcommitted
options: restore special behavior of CFLAGS vs. c_args
For compatibility with Autotools, CFLAGS is added to the linker command line if the compiler acts as a linker driver. However, this behavior was lost in commit d37d649 ("Make all Meson level options overridable per subproject.", 2025-02-13). The issue is that (for example) c_link_args is stored in env.options, and from that point on it is treated as a machine-file option. This includes not being able to override it in compilers.get_global_options: - initialize_from_top_level_project_call places it in pending_options - add_lang_args passes the right value to add_compiler_option - add_compiler_option calls add_system_option_internal - add_system_option_internal fishes the value out of pending_options and ignores what get_global_options provided. Instead, store the putative values of the compiler options coming from the environment in a separate dictionary, that is only accessed by get_global_options. This way it never appears in pending_options, and also there is no internal *_env_args variable anymore. Fixes: mesonbuild#14533 Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d11fa36 commit 2d1c67f

3 files changed

Lines changed: 43 additions & 25 deletions

File tree

‎mesonbuild/compilers/compilers.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,12 +1424,19 @@ def get_global_options(lang: str,
14241424
description = f'Extra arguments passed to the {lang}'
14251425
argkey = OptionKey(f'{lang}_args', machine=for_machine)
14261426
largkey = OptionKey(f'{lang}_link_args', machine=for_machine)
1427-
envkey = OptionKey(f'{lang}_env_args', machine=for_machine)
14281427

1429-
comp_key = argkey if argkey in env.options else envkey
1428+
comp_args_from_envvar = False
1429+
comp_options = env.coredata.optstore.get_pending_value(argkey)
1430+
if comp_options is None:
1431+
comp_args_from_envvar = True
1432+
comp_options = env.env_opts.get(argkey, [])
1433+
1434+
link_args_from_envvar = False
1435+
link_options = env.coredata.optstore.get_pending_value(largkey)
1436+
if link_options is None:
1437+
link_args_from_envvar = True
1438+
link_options = env.env_opts.get(largkey, [])
14301439

1431-
comp_options = env.options.get(comp_key, [])
1432-
link_options = env.options.get(largkey, [])
14331440
assert isinstance(comp_options, (str, list)), 'for mypy'
14341441
assert isinstance(link_options, (str, list)), 'for mypy'
14351442

@@ -1443,7 +1450,7 @@ def get_global_options(lang: str,
14431450
description + ' linker',
14441451
link_options, split_args=True, allow_dups=True)
14451452

1446-
if comp.INVOKES_LINKER and comp_key == envkey:
1453+
if comp.INVOKES_LINKER and comp_args_from_envvar and link_args_from_envvar:
14471454
# If the compiler acts as a linker driver, and we're using the
14481455
# environment variable flags for both the compiler and linker
14491456
# arguments, then put the compiler flags in the linker flags as well.

‎mesonbuild/environment.py‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ def __init__(self, source_dir: str, build_dir: T.Optional[str], cmd_options: cor
648648
# 'optimization' and 'debug' keys, it override them.
649649
self.options: T.MutableMapping[OptionKey, ElementaryOptionValues] = collections.OrderedDict()
650650

651+
# Environment variables with the name converted into an OptionKey type.
652+
# These have subtly different behavior compared to machine files, so do
653+
# not store them in self.options. See _set_default_options_from_env.
654+
self.env_opts: T.MutableMapping[OptionKey, ElementaryOptionValues] = {}
655+
651656
self.machinestore = machinefile.MachineFileStore(self.coredata.config_files, self.coredata.cross_files, self.source_dir)
652657

653658
## Read in native file(s) to override build machine configuration
@@ -819,35 +824,35 @@ def _set_default_options_from_env(self) -> None:
819824
env_opts[key].extend(p_list)
820825
elif keyname == 'cppflags':
821826
for lang in compilers.compilers.LANGUAGES_USING_CPPFLAGS:
822-
key = OptionKey(f'{lang}_env_args', machine=for_machine)
827+
key = OptionKey(f'{lang}_args', machine=for_machine)
823828
env_opts[key].extend(p_list)
824829
else:
825830
key = OptionKey.from_string(keyname).evolve(machine=for_machine)
826831
if evar in compilers.compilers.CFLAGS_MAPPING.values():
827-
# If this is an environment variable, we have to
828-
# store it separately until the compiler is
829-
# instantiated, as we don't know whether the
830-
# compiler will want to use these arguments at link
831-
# time and compile time (instead of just at compile
832-
# time) until we're instantiating that `Compiler`
833-
# object. This is required so that passing
834-
# `-Dc_args=` on the command line and `$CFLAGS`
835-
# have subtly different behavior. `$CFLAGS` will be
836-
# added to the linker command line if the compiler
837-
# acts as a linker driver, `-Dc_args` will not.
838-
#
839-
# We still use the original key as the base here, as
840-
# we want to inherit the machine and the compiler
841-
# language
842832
lang = key.name.split('_', 1)[0]
843-
key = key.evolve(f'{lang}_env_args')
833+
key = key.evolve(f'{lang}_args')
844834
env_opts[key].extend(p_list)
845835

846-
for k, v in env_opts.items():
836+
# If this is an environment variable, we have to
837+
# store it separately until the compiler is
838+
# instantiated, as we don't know whether the
839+
# compiler will want to use these arguments at link
840+
# time and compile time (instead of just at compile
841+
# time) until we're instantiating that `Compiler`
842+
# object. This is required so that passing
843+
# `-Dc_args=` on the command line and `$CFLAGS`
844+
# have subtly different behavior. `$CFLAGS` will be
845+
# added to the linker command line if the compiler
846+
# acts as a linker driver, `-Dc_args` will not.
847+
for (_, keyname), for_machine in itertools.product(NON_LANG_ENV_OPTIONS, MachineChoice):
848+
key = OptionKey.from_string(keyname).evolve(machine=for_machine)
847849
# Only store options that are not already in self.options,
848850
# otherwise we'd override the machine files
849-
if k not in self.options:
850-
self.options[k] = v
851+
if key in env_opts and key not in self.options:
852+
self.options[key] = env_opts[key]
853+
del env_opts[key]
854+
855+
self.env_opts.update(env_opts)
851856

852857
def _set_default_binaries_from_env(self) -> None:
853858
"""Set default binaries from the environment.

‎mesonbuild/options.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,12 @@ def ensure_and_validate_key(self, key: T.Union[OptionKey, str]) -> OptionKey:
838838
key = key.as_host()
839839
return key
840840

841+
def get_pending_value(self, key: T.Union[OptionKey, str], default: T.Optional[ElementaryOptionValues] = None) -> ElementaryOptionValues:
842+
key = self.ensure_and_validate_key(key)
843+
if key in self.options:
844+
return self.options[key].value
845+
return self.pending_options.get(key, default)
846+
841847
def get_value(self, key: T.Union[OptionKey, str]) -> ElementaryOptionValues:
842848
return self.get_value_object(key).value
843849

0 commit comments

Comments
 (0)