diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2025-09-23 10:28:16 -0700 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-10-15 10:21:46 -0700 |
| commit | d9af7164899fbd2b75310009d1467892faecad2a (patch) | |
| tree | 7a33b81cca13efd6a9807bf3f8bde50a480361ce | |
| parent | 3471ea270015f5edc2668e482494021723ed8d46 (diff) | |
| download | meson-d9af7164899fbd2b75310009d1467892faecad2a.tar.gz | |
build: move pic/pie to their proper classes
It's a smell for `BuildTarget` to be checking if it's a `StaticLibrary`
and then doing code. This also allows us to tighten the type checking a
bit.
| -rw-r--r-- | mesonbuild/build.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 6f280280d..722171f1a 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -1325,10 +1325,6 @@ class BuildTarget(Target): 'for each platform pass `[]` (empty array)') self.suffix = name_suffix self.name_suffix_set = True - if isinstance(self, StaticLibrary): - self.pic = self._extract_pic_pie(kwargs, 'pic', 'b_staticpic') - if isinstance(self, Executable) or (isinstance(self, StaticLibrary) and not self.pic): - self.pie = self._extract_pic_pie(kwargs, 'pie', 'b_pie') self.implicit_include_directories = kwargs.get('implicit_include_directories', True) if not isinstance(self.implicit_include_directories, bool): raise InvalidArguments('Implicit_include_directories must be a boolean.') @@ -1353,7 +1349,16 @@ class BuildTarget(Target): if self.swift_module_name == '': self.swift_module_name = self.name - def _extract_pic_pie(self, kwargs: BuildTargetKeywordArguments, arg: str, option: str) -> bool: + @T.overload + def _extract_pic_pie(self, kwargs: StaticLibraryKeywordArguments, arg: Literal['pic'], + option: Literal['b_staticpic']) -> bool: ... + + @T.overload + def _extract_pic_pie(self, kwargs: T.Union[StaticLibraryKeywordArguments, ExecutableKeywordArguments], + arg: Literal['pie'], option: Literal['b_pie']) -> bool: ... + + def _extract_pic_pie(self, kwargs: T.Union[StaticLibraryKeywordArguments, ExecutableKeywordArguments], + arg: Literal['pic', 'pie'], option: Literal['b_staticpic', 'b_pie']) -> bool: # You can't disable PIC on OS X. The compiler ignores -fno-PIC. # PIC is always on for Windows (all code is position-independent # since library loading is done differently) @@ -1374,9 +1379,9 @@ class BuildTarget(Target): k = OptionKey(option) if kwargs.get(arg) is not None: - val = T.cast('bool', kwargs[arg]) + val = kwargs[arg] elif k in self.environment.coredata.optstore: - val = self.environment.coredata.optstore.get_value_for(k.name, k.subproject) + val = self.environment.coredata.get_option_for_target(self, k) else: val = False @@ -2213,12 +2218,10 @@ class Executable(BuildTarget): environment: environment.Environment, compilers: T.Dict[str, 'Compiler'], kwargs: ExecutableKeywordArguments): - key = OptionKey('b_pie') - if 'pie' not in kwargs and key in environment.coredata.optstore: - kwargs['pie'] = environment.coredata.optstore.get_value_for(key) super().__init__(name, subdir, subproject, for_machine, sources, structured_sources, objects, environment, compilers, kwargs) self.win_subsystem = kwargs.get('win_subsystem') or 'console' + self.pie = self._extract_pic_pie(kwargs, 'pie', 'b_pie') assert kwargs.get('android_exe_type') is None or kwargs.get('android_exe_type') in {'application', 'executable'} # Check for export_dynamic self.export_dynamic = kwargs.get('export_dynamic', False) @@ -2352,6 +2355,9 @@ class StaticLibrary(BuildTarget): self.prelink = kwargs.get('prelink', False) super().__init__(name, subdir, subproject, for_machine, sources, structured_sources, objects, environment, compilers, kwargs) + self.pic = self._extract_pic_pie(kwargs, 'pic', 'b_staticpic') + if not self.pic: + self.pie = self._extract_pic_pie(kwargs, 'pie', 'b_pie') def post_init(self) -> None: super().post_init() |
