Skip to content

Commit

Permalink
Add env kwarg in generator.process()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomura-RH committed Oct 2, 2023
1 parent 9bccc96 commit 1595c6d
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/markdown/snippets/env_kwarg_generator_process.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## generator.process() gains 'env' keyword argument

Like the kwarg of the same name in `custom_target()`, `env` allows
you to set the environment in which the generator will process inputs.
9 changes: 9 additions & 0 deletions docs/yaml/objects/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ methods:
`subdir/one.input` is processed it generates a file `{target private
directory}/subdir/one.out` as opposed to `{target private
directory}/one.out`.
env:
type: env | list[str] | dict[str]
since: 1.3.0
description: |
environment variables to set, such as
`{'NAME1': 'value1', 'NAME2': 'value2'}` or `['NAME1=value1', 'NAME2=value2']`,
or an [[@env]] object which allows more
sophisticated environment juggling.
3 changes: 2 additions & 1 deletion mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,8 @@ def generate_genlist_for_target(self, genlist: build.GeneratedList, target: buil
args = self.replace_paths(target, args, override_subdir=subdir)
cmdlist, reason = self.as_meson_exe_cmdline(exe,
self.replace_extra_args(args, genlist),
capture=outfiles[0] if generator.capture else None)
capture=outfiles[0] if generator.capture else None,
env=genlist.env)
abs_pdir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(target))
os.makedirs(abs_pdir, exist_ok=True)

Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def generate_genlist_for_target(self, genlist: T.Union[build.GeneratedList, buil
self.replace_extra_args(args, genlist),
workdir=tdir_abs,
capture=outfiles[0] if generator.capture else None,
force_serialize=True
force_serialize=True,
env=genlist.env
)
deps = cmd[-1:] + deps
abs_pdir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(target))
Expand Down
14 changes: 12 additions & 2 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1809,8 +1809,14 @@ def is_parent_path(parent: str, trial: str) -> bool:
def process_files(self, files: T.Iterable[T.Union[str, File, 'CustomTarget', 'CustomTargetIndex', 'GeneratedList']],
state: T.Union['Interpreter', 'ModuleState'],
preserve_path_from: T.Optional[str] = None,
extra_args: T.Optional[T.List[str]] = None) -> 'GeneratedList':
output = GeneratedList(self, state.subdir, preserve_path_from, extra_args=extra_args if extra_args is not None else [])
extra_args: T.Optional[T.List[str]] = None,
env: T.Optional[EnvironmentVariables] = None) -> 'GeneratedList':
output = GeneratedList(
self,
state.subdir,
preserve_path_from,
extra_args=extra_args if extra_args is not None else [],
env=env if env is not None else EnvironmentVariables())

for e in files:
if isinstance(e, CustomTarget):
Expand Down Expand Up @@ -1849,6 +1855,7 @@ class GeneratedList(HoldableObject):
subdir: str
preserve_path_from: T.Optional[str]
extra_args: T.List[str]
env: T.Optional[EnvironmentVariables]

def __post_init__(self) -> None:
self.name = self.generator.exe
Expand All @@ -1862,6 +1869,9 @@ def __post_init__(self) -> None:
if self.extra_args is None:
self.extra_args: T.List[str] = []

if self.env is None:
self.env: EnvironmentVariables = EnvironmentVariables()

if isinstance(self.generator.exe, programs.ExternalProgram):
if not self.generator.exe.found():
raise InvalidArguments('Tried to use not-found external program as generator')
Expand Down
5 changes: 3 additions & 2 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
typed_pos_args, typed_kwargs, typed_operator,
noArgsFlattening, noPosargs, noKwargs, unholder_return,
flatten, resolve_second_level_holders, InterpreterException, InvalidArguments, InvalidCode)
from ..interpreter.type_checking import NoneType, ENV_SEPARATOR_KW, PKGCONFIG_DEFINE_KW
from ..interpreter.type_checking import NoneType, ENV_KW, ENV_SEPARATOR_KW, PKGCONFIG_DEFINE_KW
from ..dependencies import Dependency, ExternalLibrary, InternalDependency
from ..programs import ExternalProgram
from ..mesonlib import HoldableObject, OptionKey, listify, Popen_safe
Expand Down Expand Up @@ -1043,6 +1043,7 @@ def __init__(self, gen: build.Generator, interpreter: 'Interpreter'):
'generator.process',
KwargInfo('preserve_path_from', (str, NoneType), since='0.45.0'),
KwargInfo('extra_args', ContainerTypeInfo(list, str), listify=True, default=[]),
ENV_KW.evolve(since='1.3.0')
)
def process_method(self,
args: T.Tuple[T.List[T.Union[str, mesonlib.File, 'build.GeneratedTypes']]],
Expand All @@ -1060,7 +1061,7 @@ def process_method(self,
'0.57.0', self.interpreter.subproject)

gl = self.held_object.process_files(args[0], self.interpreter,
preserve_path_from, extra_args=kwargs['extra_args'])
preserve_path_from, extra_args=kwargs['extra_args'], env=kwargs['env'])

return gl

Expand Down
1 change: 1 addition & 0 deletions mesonbuild/interpreter/kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class GeneratorProcess(TypedDict):

preserve_path_from: T.Optional[str]
extra_args: T.List[str]
env: EnvironmentVariables

class DependencyMethodPartialDependency(TypedDict):

Expand Down
11 changes: 11 additions & 0 deletions test cases/common/272 env in generator.process/generate_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
import os
import sys

ENV_VAR_VALUE = os.environ.get('ENV_VAR_VALUE')
assert ENV_VAR_VALUE is not None

with open(sys.argv[1], 'r') as infile, \
open(sys.argv[2], 'w') as outfile:

outfile.write(infile.read().replace('ENV_VAR_VALUE', ENV_VAR_VALUE))
3 changes: 3 additions & 0 deletions test cases/common/272 env in generator.process/main.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main(void) {
return ENV_VAR_VALUE;
}
21 changes: 21 additions & 0 deletions test cases/common/272 env in generator.process/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
project('test_env_in_generator_process', 'c')

generate_main_py = find_program('generate_main.py')

main_generator = generator(generate_main_py,
arguments: ['@INPUT@', '@OUTPUT@'],
output: '@BASENAME@' + '.c'
)

main_template = files('main.template')

# With explicit values
my_executable = executable('myexecutable', main_generator.process(main_template, env: {'ENV_VAR_VALUE': '0'}))
test('explicit_value', my_executable)

# With env object
env = environment()
env.set('ENV_VAR_VALUE', '0')

my_executable2 = executable('myexecutable2', main_generator.process(main_template, env: env))
test('env_object', my_executable2)

0 comments on commit 1595c6d

Please sign in to comment.