diff options
| author | Xavier Claessens <xclaessens@netflix.com> | 2025-10-19 19:45:02 -0400 |
|---|---|---|
| committer | Xavier Claessens <xclaesse@gmail.com> | 2025-10-23 17:42:00 +0100 |
| commit | c63cfd95b3b93a25f1c8ac490603c37124ba20f0 (patch) | |
| tree | e7379dc98ca9a1ee56c8932e4a8083301767edd9 | |
| parent | af4886d59cffa673305d94e19e40112a126c6e52 (diff) | |
| download | meson-c63cfd95b3b93a25f1c8ac490603c37124ba20f0.tar.gz | |
cargo: Use --env-set when we have nightly rustc
| -rw-r--r-- | mesonbuild/cargo/interpreter.py | 52 | ||||
| -rw-r--r-- | mesonbuild/compilers/rust.py | 9 |
2 files changed, 61 insertions, 0 deletions
diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py index 241009a08..04a0adeed 100644 --- a/mesonbuild/cargo/interpreter.py +++ b/mesonbuild/cargo/interpreter.py @@ -139,6 +139,7 @@ class Interpreter: ] ast += self._create_dependencies(pkg, build) ast += self._create_meson_subdir(build) + ast += self._create_env_args(pkg, build, subdir) if pkg.manifest.lib: crate_type = pkg.manifest.lib.crate_type @@ -596,6 +597,56 @@ class Interpreter: build.block([build.function('subdir', [build.string('meson')])])) ] + def _pkg_common_env(self, pkg: PackageState, subdir: str) -> T.Dict[str, str]: + # Common variables for build.rs and crates + # https://doc.rust-lang.org/cargo/reference/environment-variables.html + # OUT_DIR is the directory where build.rs generate files. In our case, + # it's the directory where meson/meson.build places generated files. + out_dir = os.path.join(self.environment.build_dir, subdir, 'meson') + os.makedirs(out_dir, exist_ok=True) + version_arr = pkg.manifest.package.version.split('.') + version_arr += ['' * (4 - len(version_arr))] + return { + 'OUT_DIR': out_dir, + 'CARGO_MANIFEST_DIR': os.path.join(self.environment.source_dir, subdir), + 'CARGO_MANIFEST_PATH': os.path.join(self.environment.source_dir, subdir, 'Cargo.toml'), + 'CARGO_PKG_VERSION': pkg.manifest.package.version, + 'CARGO_PKG_VERSION_MAJOR': version_arr[0], + 'CARGO_PKG_VERSION_MINOR': version_arr[1], + 'CARGO_PKG_VERSION_PATCH': version_arr[2], + 'CARGO_PKG_VERSION_PRE': version_arr[3], + 'CARGO_PKG_AUTHORS': ','.join(pkg.manifest.package.authors), + 'CARGO_PKG_NAME': pkg.manifest.package.name, + # FIXME: description can contain newlines which breaks ninja. + #'CARGO_PKG_DESCRIPTION': pkg.manifest.package.description or '', + 'CARGO_PKG_HOMEPAGE': pkg.manifest.package.homepage or '', + 'CARGO_PKG_REPOSITORY': pkg.manifest.package.repository or '', + 'CARGO_PKG_LICENSE': pkg.manifest.package.license or '', + 'CARGO_PKG_LICENSE_FILE': pkg.manifest.package.license_file or '', + 'CARGO_PKG_RUST_VERSION': pkg.manifest.package.rust_version or '', + 'CARGO_PKG_README': pkg.manifest.package.readme or '', + } + + def _create_env_args(self, pkg: PackageState, build: builder.Builder, subdir: str) -> T.List[mparser.BaseNode]: + host_rustc = T.cast('RustCompiler', self.environment.coredata.compilers[MachineChoice.HOST]['rust']) + enable_env_set_args = host_rustc.enable_env_set_args() + if enable_env_set_args is None: + return [build.assign(build.array([]), 'env_args')] + # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates + env_dict = self._pkg_common_env(pkg, subdir) + env_dict.update({ + 'CARGO_CRATE_NAME': fixup_meson_varname(pkg.manifest.package.name), + #FIXME: TODO + #CARGO_BIN_NAME + #CARGO_BIN_EXE_<name> + #CARGO_PRIMARY_PACKAGE + #CARGO_TARGET_TMPDIR + }) + env_args: T.List[mparser.BaseNode] = [build.string(a) for a in enable_env_set_args] + for k, v in env_dict.items(): + env_args += [build.string('--env-set'), build.string(f'{k}={v}')] + return [build.assign(build.array(env_args), 'env_args')] + def _create_lib(self, pkg: PackageState, build: builder.Builder, lib_type: Literal['rust', 'c', 'proc-macro'], static: bool = False, shared: bool = False) -> T.List[mparser.BaseNode]: @@ -616,6 +667,7 @@ class Interpreter: build.identifier('features_args'), build.identifier(_extra_args_varname()), build.identifier('system_deps_args'), + build.identifier('env_args'), ] dependencies.append(build.identifier(_extra_deps_varname())) diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index 39280d584..fadf9772f 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -399,6 +399,15 @@ class RustCompiler(Compiler): self.is_cross, self.info, full_version=self.full_version, linker=self.linker, rustc=self) + def enable_env_set_args(self) -> T.Optional[T.List[str]]: + '''Extra arguments to enable --env-set support in rustc. + Returns None if not supported. + ''' + if version_compare(self.version, '>= 1.76') and self.allow_nightly: + return ['-Z', 'unstable-options'] + return None + + class ClippyRustCompiler(RustCompiler): """Clippy is a linter that wraps Rustc. |
