diff --git a/mesonbuild/build.py b/mesonbuild/build.py index a60b2b7ae..13eb0c1c7 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -206,21 +206,14 @@ class Build: self.projects = {} self.targets: T.MutableMapping[str, 'Target'] = OrderedDict() self.run_target_names: T.Set[T.Tuple[str, str]] = set() - - global_args: PerMachineDefaultable[T.Dict[str, T.List[str]]] = PerMachineDefaultable({}) - global_link_args: PerMachineDefaultable[T.Dict[str, T.List[str]]] = PerMachineDefaultable({}) - project_args: PerMachineDefaultable[T.Dict[str, T.List[str]]] = PerMachineDefaultable({}) - project_link_args: PerMachineDefaultable[T.Dict[str, T.List[str]]] = PerMachineDefaultable({}) - if environment.is_cross_build(): - global_args.host = {} - global_link_args.host = {} - project_args.host = {} - project_link_args.host = {} - self.global_args = global_args.default_missing() - self.projects_args = project_args.default_missing() - self.global_link_args = global_link_args.default_missing() - self.projects_link_args = project_link_args.default_missing() - + self.global_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachineDefaultable.default( + environment.is_cross_build(), {}, {}) + self.global_link_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachineDefaultable.default( + environment.is_cross_build(), {}, {}) + self.projects_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachineDefaultable.default( + environment.is_cross_build(), {}, {}) + self.projects_link_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachineDefaultable.default( + environment.is_cross_build(), {}, {}) self.tests: T.List['Test'] = [] self.benchmarks: T.List['Test'] = [] self.headers: T.List[Headers] = [] @@ -243,10 +236,8 @@ class Build: # If we are doing a cross build we need two caches, if we're doing a # build == host compilation the both caches should point to the same place. - dependency_overrides: PerMachineDefaultable[T.Dict[T.Tuple, DependencyOverride]] = PerMachineDefaultable({}) - if environment.is_cross_build(): - dependency_overrides.host = {} - self.dependency_overrides = dependency_overrides.default_missing() + self.dependency_overrides: PerMachine[T.Dict[T.Tuple, DependencyOverride]] = PerMachineDefaultable.default( + environment.is_cross_build(), {}, {}) self.devenv: T.List[EnvironmentVariables] = [] def get_build_targets(self): diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 7f94d1837..5e726a462 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -407,11 +407,10 @@ class CoreData: self.initialized_subprojects: T.Set[str] = set() # For host == build configuraitons these caches should be the same. - deps: PerMachineDefaultable[DependencyCache] = PerMachineDefaultable( - DependencyCache(self.options, MachineChoice.BUILD)) - if self.is_cross_build(): - deps.host = DependencyCache(self.options, MachineChoice.HOST) - self.deps = deps.default_missing() + self.deps: PerMachine[DependencyCache] = PerMachineDefaultable.default( + self.is_cross_build(), + DependencyCache(self.options, MachineChoice.BUILD), + DependencyCache(self.options, MachineChoice.HOST)) self.compiler_check_cache = OrderedDict() # type: T.Dict[CompilerCheckCacheKey, compiler.CompileResult] diff --git a/mesonbuild/mesonlib/universal.py b/mesonbuild/mesonlib/universal.py index 684d2239e..66149f9d0 100644 --- a/mesonbuild/mesonlib/universal.py +++ b/mesonbuild/mesonlib/universal.py @@ -547,6 +547,21 @@ class PerMachineDefaultable(PerMachine[T.Optional[_T]]): def __repr__(self) -> str: return f'PerMachineDefaultable({self.build!r}, {self.host!r})' + @classmethod + def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]: + """Easy way to get a defaulted value + + This allows simplifying the case where you can control whether host and + build are separate or not with a boolean. If the is_cross value is set + to true then the optional host value will be used, otherwise the host + will be set to the build value. + """ + m = cls(build) + if is_cross: + m.host = host + return m.default_missing() + + class PerThreeMachineDefaultable(PerMachineDefaultable, PerThreeMachine[T.Optional[_T]]): """Extends `PerThreeMachine` with the ability to default from `None`s.