aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2023-02-10 12:39:12 -0500
committerXavier Claessens <xclaesse@gmail.com>2023-02-15 15:12:34 -0500
commitc2a55bfe43fae1b44cf49a083297d6755c89e1cc (patch)
tree8186d1f785861ac6b7da1e548034f05ba493472c
parent04f233a80d570c90c1a152114cbf3bdad6a61607 (diff)
downloadmeson-c2a55bfe43fae1b44cf49a083297d6755c89e1cc.zip
meson-c2a55bfe43fae1b44cf49a083297d6755c89e1cc.tar.gz
meson-c2a55bfe43fae1b44cf49a083297d6755c89e1cc.tar.bz2
preprocess: Allow custom_tgt, custom_idx and generated_list
It was documented to be supported but only File and str were actually working.
-rw-r--r--mesonbuild/build.py22
-rw-r--r--mesonbuild/interpreter/compiler.py15
-rw-r--r--test cases/common/259 preprocess/bar.c2
-rw-r--r--test cases/common/259 preprocess/meson.build11
4 files changed, 39 insertions, 11 deletions
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index ad3b4e4..d954e43 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -2609,9 +2609,10 @@ class CompileTarget(BuildTarget):
subdir: str,
subproject: str,
environment: environment.Environment,
- sources: T.List[File],
+ sources: T.List['SourceOutputs'],
output_templ: str,
compiler: Compiler,
+ backend: Backend,
kwargs):
compilers = {compiler.get_language(): compiler}
super().__init__(name, subdir, subproject, compiler.for_machine,
@@ -2620,11 +2621,13 @@ class CompileTarget(BuildTarget):
self.compiler = compiler
self.output_templ = output_templ
self.outputs = []
- for f in sources:
- plainname = os.path.basename(f.fname)
- basename = os.path.splitext(plainname)[0]
- self.outputs.append(output_templ.replace('@BASENAME@', basename).replace('@PLAINNAME@', plainname))
- self.sources_map = dict(zip(sources, self.outputs))
+ self.sources_map: T.Dict[File, str] = {}
+ for f in self.sources:
+ self._add_output(f)
+ for gensrc in self.generated:
+ for s in gensrc.get_outputs():
+ rel_src = backend.get_target_generated_dir(self, gensrc, s)
+ self._add_output(File.from_built_relative(rel_src))
def type_suffix(self) -> str:
return "@compile"
@@ -2633,6 +2636,13 @@ class CompileTarget(BuildTarget):
def is_unity(self) -> bool:
return False
+ def _add_output(self, f: File) -> None:
+ plainname = os.path.basename(f.fname)
+ basename = os.path.splitext(plainname)[0]
+ o = self.output_templ.replace('@BASENAME@', basename).replace('@PLAINNAME@', plainname)
+ self.outputs.append(o)
+ self.sources_map[f] = o
+
class RunTarget(Target, CommandBase):
diff --git a/mesonbuild/interpreter/compiler.py b/mesonbuild/interpreter/compiler.py
index 95126cf..d27fc20 100644
--- a/mesonbuild/interpreter/compiler.py
+++ b/mesonbuild/interpreter/compiler.py
@@ -6,6 +6,7 @@ from __future__ import annotations
import enum
import functools
import os
+import itertools
import typing as T
from .. import build
@@ -28,6 +29,7 @@ if T.TYPE_CHECKING:
from ..compilers import Compiler, RunResult
from ..interpreterbase import TYPE_var, TYPE_kwargs
from .kwargs import ExtractRequired, ExtractSearchDirs
+ from .interpreter.interpreter import SourceOutputs
from typing_extensions import TypedDict, Literal
@@ -163,6 +165,8 @@ _COMPILES_KWS: T.List[KwargInfo] = [_NAME_KW, _ARGS_KW, _DEPENDENCIES_KW, _INCLU
_HEADER_KWS: T.List[KwargInfo] = [REQUIRED_KW.evolve(since='0.50.0', default=False), *_COMMON_KWS]
class CompilerHolder(ObjectHolder['Compiler']):
+ preprocess_uid = itertools.count()
+
def __init__(self, compiler: 'Compiler', interpreter: 'Interpreter'):
super().__init__(compiler, interpreter)
self.environment = self.env
@@ -750,7 +754,7 @@ class CompilerHolder(ObjectHolder['Compiler']):
return self.compiler.get_argument_syntax()
@FeatureNew('compiler.preprocess', '0.64.0')
- @typed_pos_args('compiler.preprocess', varargs=(mesonlib.File, str), min_varargs=1)
+ @typed_pos_args('compiler.preprocess', varargs=(str, mesonlib.File, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList), min_varargs=1)
@typed_kwargs(
'compiler.preprocess',
KwargInfo('output', str, default='@PLAINNAME@.i'),
@@ -759,20 +763,25 @@ class CompilerHolder(ObjectHolder['Compiler']):
)
def preprocess_method(self, args: T.Tuple[T.List['mesonlib.FileOrString']], kwargs: 'PreprocessKW') -> T.List[build.CustomTargetIndex]:
compiler = self.compiler.get_preprocessor()
- sources = self.interpreter.source_strings_to_files(args[0])
+ sources: 'SourceOutputs' = self.interpreter.source_strings_to_files(args[0])
+ if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in sources):
+ FeatureNew.single_use('compiler.preprocess with generated sources', '1.1.0', self.subproject,
+ location=self.current_node)
tg_kwargs = {
f'{self.compiler.language}_args': kwargs['compile_args'],
'build_by_default': False,
'include_directories': kwargs['include_directories'],
}
+ tg_name = f'preprocessor_{next(self.preprocess_uid)}'
tg = build.CompileTarget(
- 'preprocessor',
+ tg_name,
self.interpreter.subdir,
self.subproject,
self.environment,
sources,
kwargs['output'],
compiler,
+ self.interpreter.backend,
tg_kwargs)
self.interpreter.add_target(tg.name, tg)
# Expose this target as list of its outputs, so user can pass them to
diff --git a/test cases/common/259 preprocess/bar.c b/test cases/common/259 preprocess/bar.c
index 43737b9..47d4ba1 100644
--- a/test cases/common/259 preprocess/bar.c
+++ b/test cases/common/259 preprocess/bar.c
@@ -1,3 +1,3 @@
-int bar(void) {
+int @BAR@(void) {
return BAR;
}
diff --git a/test cases/common/259 preprocess/meson.build b/test cases/common/259 preprocess/meson.build
index 4824598..9627b4f 100644
--- a/test cases/common/259 preprocess/meson.build
+++ b/test cases/common/259 preprocess/meson.build
@@ -4,7 +4,16 @@ cc = meson.get_compiler('c')
add_project_arguments(['-DFOO=0', '-DBAR=0'], language: 'c')
-pp_files = cc.preprocess('foo.c', 'bar.c', output: '@PLAINNAME@')
+fs = import('fs')
+bar_content = fs.read('bar.c')
+bar_c = custom_target(
+ input: 'bar.c',
+ output: 'bar.c',
+ command: ['python3', '-c', '''import sys;print(sys.argv[1].replace('@BAR@', 'bar'))''', bar_content],
+ capture: true,
+)
+
+pp_files = cc.preprocess('foo.c', bar_c, output: '@PLAINNAME@')
foreach f : pp_files
message(f.full_path())