From 179355bf18f3c4216e8bb820fc19567e2a4bb640 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 28 Sep 2023 15:37:02 -0700 Subject: [PATCH] interpreter: use typed_kwargs for "sources" keyword argument of build targets --- mesonbuild/interpreter/interpreter.py | 4 ++-- mesonbuild/interpreter/kwargs.py | 4 +++- mesonbuild/interpreter/type_checking.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index a56e922e6..05581afeb 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -3265,8 +3265,8 @@ class Interpreter(InterpreterBase, HoldableObject): # Silently force to native because that's the only sensible value # and rust_crate_type is deprecated any way. for_machine = MachineChoice.BUILD - if 'sources' in kwargs: - sources += listify(kwargs['sources']) + # Avoid mutating, since there could be other references to sources + sources = sources + kwargs['sources'] if any(isinstance(s, build.BuildTarget) for s in sources): FeatureBroken.single_use('passing references to built targets as a source file', '1.1.0', self.subproject, 'Consider using `link_with` or `link_whole` if you meant to link, or dropping them as otherwise they are ignored.', diff --git a/mesonbuild/interpreter/kwargs.py b/mesonbuild/interpreter/kwargs.py index 015886ecf..7396229db 100644 --- a/mesonbuild/interpreter/kwargs.py +++ b/mesonbuild/interpreter/kwargs.py @@ -16,7 +16,7 @@ from ..dependencies.base import Dependency from ..mesonlib import EnvironmentVariables, MachineChoice, File, FileMode, FileOrString, OptionKey from ..modules.cmake import CMakeSubprojectOptions from ..programs import ExternalProgram -from .type_checking import PkgConfigDefineType +from .type_checking import PkgConfigDefineType, SourcesVarargsType class FuncAddProjectArgs(TypedDict): @@ -332,6 +332,7 @@ class _BuildTarget(_BaseBuildTarget): """Arguments shared by non-JAR functions""" rust_dependency_map: T.Dict[str, str] + sources: SourcesVarargsType class _LibraryMixin(TypedDict): @@ -390,6 +391,7 @@ class Jar(_BaseBuildTarget): main_class: str java_resources: T.Optional[build.StructuredSources] + sources: T.Union[str, File, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList, build.ExtractedObjects, build.BuildTarget] class FuncDeclareDependency(TypedDict): diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py index 166cb58e9..27a05523f 100644 --- a/mesonbuild/interpreter/type_checking.py +++ b/mesonbuild/interpreter/type_checking.py @@ -467,6 +467,13 @@ DEPENDENCY_SOURCES_KW: KwargInfo[T.List[T.Union[str, File, CustomTarget, CustomT SOURCES_VARARGS = (str, File, CustomTarget, CustomTargetIndex, GeneratedList, StructuredSources, ExtractedObjects, BuildTarget) +BT_SOURCES_KW: KwargInfo[SourcesVarargsType] = KwargInfo( + 'sources', + ContainerTypeInfo(list, SOURCES_VARARGS), + listify=True, + default=[], +) + VARIABLES_KW: KwargInfo[T.Dict[str, str]] = KwargInfo( 'variables', # str is listified by validator/convertor, cannot use listify=True here because @@ -527,6 +534,7 @@ _ALL_TARGET_KWS: T.List[KwargInfo] = [ # Applies to all build_target classes except jar _BUILD_TARGET_KWS: T.List[KwargInfo] = [ *_ALL_TARGET_KWS, + BT_SOURCES_KW, RUST_CRATE_TYPE_KW, KwargInfo( 'rust_dependency_map', @@ -665,6 +673,12 @@ _EXCLUSIVE_JAR_KWS: T.List[KwargInfo] = [ JAR_KWS = [ *_ALL_TARGET_KWS, *_EXCLUSIVE_JAR_KWS, + KwargInfo( + 'sources', + ContainerTypeInfo(list, (str, File, CustomTarget, CustomTargetIndex, GeneratedList, ExtractedObjects, BuildTarget)), + listify=True, + default=[], + ) ] # Arguments used by both_library and library