Skip to content

Commit

Permalink
cargo: Set CARGO_PKG_* in env when building and running build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xclaesse committed Nov 24, 2023
1 parent 08272e4 commit de93962
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
28 changes: 25 additions & 3 deletions mesonbuild/cargo/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,16 +522,37 @@ def _create_features(cargo: Manifest, build: builder.Builder) -> T.List[mparser.
def _create_cfg(cargo: Manifest, build: builder.Builder) -> T.List[mparser.BaseNode]:
# Allow Cargo subprojects to add extra Rust args in meson/meson.build file.
# This is used to replace build.rs logic.

# cargo_info = {'CARGO_...': 'value', ...}
# extra_args = []
# extra_deps = []
# fs = import('fs')
# has_meson_build = fs.is_dir('meson')
# if has_meson_build
# subdir('meson')
# endif
# cfg = rust.cargo_cfg(features, skip_build_rs: has_meson_build)
# cfg = rust.cargo_cfg(features, skip_build_rs: has_meson_build, info: cargo_info)
version_arr = cargo.package.version.split('.')
version_arr += ['' * (4 - len(version_arr))]
return [
build.assign(build.dict({
# https://doc.rust-lang.org/cargo/reference/environment-variables.html
build.string('CARGO_MANIFEST_DIR'): build.string(os.path.join(cargo.subdir, cargo.path)),
build.string('CARGO_PKG_VERSION'): build.string(cargo.package.version),
build.string('CARGO_PKG_VERSION_MAJOR'): build.string(version_arr[0]),
build.string('CARGO_PKG_VERSION_MINOR'): build.string(version_arr[1]),
build.string('CARGO_PKG_VERSION_PATCH'): build.string(version_arr[2]),
build.string('CARGO_PKG_VERSION_PRE'): build.string(version_arr[3]),
build.string('CARGO_PKG_AUTHORS'): build.string(','.join(cargo.package.authors)),
build.string('CARGO_PKG_NAME'): build.string(cargo.package.name),
build.string('CARGO_PKG_DESCRIPTION'): build.string(cargo.package.description or ''),
build.string('CARGO_PKG_HOMEPAGE'): build.string(cargo.package.homepage or ''),
build.string('CARGO_PKG_REPOSITORY'): build.string(cargo.package.repository or ''),
build.string('CARGO_PKG_LICENSE'): build.string(cargo.package.license or ''),
build.string('CARGO_PKG_LICENSE_FILE'): build.string(cargo.package.license_file or ''),
build.string('CARGO_PKG_RUST_VERSION'): build.string(cargo.package.rust_version or ''),
build.string('CARGO_PKG_README'): build.string(cargo.package.readme or ''),
}),
'cargo_info'),
build.assign(build.array([]), _extra_args_varname()),
build.assign(build.array([]), _extra_deps_varname()),
build.assign(build.function('import', [build.string('fs')]), 'fs'),
Expand All @@ -543,7 +564,8 @@ def _create_cfg(cargo: Manifest, build: builder.Builder) -> T.List[mparser.BaseN
'cargo_cfg',
build.identifier('rust'),
[build.method('keys', build.identifier('features'))],
{'skip_build_rs': build.identifier('has_meson_build')},
{'skip_build_rs': build.identifier('has_meson_build'),
'info': build.identifier('cargo_info')},
),
'cfg'),
]
Expand Down
14 changes: 11 additions & 3 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FuncBindgen(TypedDict):

class FuncCargoCfg(TypedDict):
skip_build_rs: bool
info: T.Dict[str, str]


class RustModule(ExtensionModule):
Expand Down Expand Up @@ -326,7 +327,7 @@ def _split_cfg(cfg: str) -> T.Tuple[str, str]:
return pair[0], value

@staticmethod
def _get_build_rs_env(state: ModuleState, cfgs: T.Dict[str, str], features: T.List[str]) -> T.Dict[str, str]:
def _get_build_rs_env(state: ModuleState, cfgs: T.Dict[str, str], features: T.List[str], info: T.Dict[str, str]) -> T.Dict[str, str]:
# https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
env = os.environ.copy()
def conv(k: str) -> str:
Expand All @@ -341,6 +342,7 @@ def conv(k: str) -> str:
rustc_cmd = rustc.get_exelist(ccache=False)
env['RUSTC'] = rustc_cmd[0]
env['TARGET'] = rustc.get_target_triplet()
env.update(info)
return env

CARGO_CFG_PREFIX = 'cargo:rustc-cfg='
Expand All @@ -350,6 +352,7 @@ def conv(k: str) -> str:
@typed_kwargs(
'rust.cargo_cfg',
KwargInfo('skip_build_rs', bool, default=False),
KwargInfo('info', ContainerTypeInfo(dict, str), default={}),
)
def cargo_cfg(self, state: ModuleState, args: T.Tuple[T.List[str]], kwargs: FuncCargoCfg) -> T.Dict[str, str]:
cfgs = dict(self._split_cfg(i) for i in self._get_cfgs(state, MachineChoice.HOST))
Expand All @@ -360,9 +363,14 @@ def cargo_cfg(self, state: ModuleState, args: T.Tuple[T.List[str]], kwargs: Func
rustc = state.get_compiler('rust', MachineChoice.BUILD)
if not rustc:
raise InterpreterException(f'build.rs file requires rust language for build machine')
env = self._get_build_rs_env(state, cfgs, args[0])
env = self._get_build_rs_env(state, cfgs, args[0], kwargs['info'])
cwd = os.path.join(state.environment.source_dir, state.subdir)
res = rustc.run(build_rs_file, state.environment, run_env=env, run_cwd=cwd)
try:
orig_env = os.environ
os.environ = env
res = rustc.run(build_rs_file, state.environment, run_cwd=cwd)
finally:
os.environ = orig_env
if res.returncode == 0:
compiler_args: T.List[str] = []
for line in res.stdout.splitlines():
Expand Down

0 comments on commit de93962

Please sign in to comment.