diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2025-10-24 10:34:34 +0200 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-10-29 11:34:19 -0700 |
| commit | b713d88a3b938665ed43652565254fbbd87aa2d1 (patch) | |
| tree | 54d057cc8b69c5a191f8078a3a8ec8286b4fb063 /mesonbuild/cargo | |
| parent | 77c41f3d61817b1c4022a31a1a24dd377e14925e (diff) | |
| download | meson-b713d88a3b938665ed43652565254fbbd87aa2d1.tar.gz | |
cargo: move environment generation to PackageState
Generating environment variables needs the Environment, not the
whole cargo interpreter state. Move it out of Interpreter.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'mesonbuild/cargo')
| -rw-r--r-- | mesonbuild/cargo/interpreter.py | 76 |
1 files changed, 37 insertions, 39 deletions
diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py index 9c239759f..7d473acb8 100644 --- a/mesonbuild/cargo/interpreter.py +++ b/mesonbuild/cargo/interpreter.py @@ -79,6 +79,39 @@ class PackageState: # Package configuration state cfg: T.Optional[PackageConfiguration] = None + def get_env_dict(self, environment: Environment, subdir: str) -> T.Dict[str, str]: + """Get environment variables for this package.""" + # 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(environment.build_dir, subdir, 'meson') + os.makedirs(out_dir, exist_ok=True) + version_arr = self.manifest.package.version.split('.') + version_arr += [''] * (4 - len(version_arr)) + + return { + 'OUT_DIR': out_dir, + 'CARGO_MANIFEST_DIR': os.path.join(environment.source_dir, subdir), + 'CARGO_MANIFEST_PATH': os.path.join(environment.source_dir, subdir, 'Cargo.toml'), + 'CARGO_PKG_VERSION': self.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(self.manifest.package.authors), + 'CARGO_PKG_NAME': self.manifest.package.name, + # FIXME: description can contain newlines which breaks ninja. + #'CARGO_PKG_DESCRIPTION': self.manifest.package.description or '', + 'CARGO_PKG_HOMEPAGE': self.manifest.package.homepage or '', + 'CARGO_PKG_REPOSITORY': self.manifest.package.repository or '', + 'CARGO_PKG_LICENSE': self.manifest.package.license or '', + 'CARGO_PKG_LICENSE_FILE': self.manifest.package.license_file or '', + 'CARGO_PKG_RUST_VERSION': self.manifest.package.rust_version or '', + 'CARGO_PKG_README': self.manifest.package.readme or '', + 'CARGO_CRATE_NAME': fixup_meson_varname(self.manifest.package.name), + } + @dataclasses.dataclass(frozen=True) class PackageKey: @@ -620,51 +653,16 @@ 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_dict = pkg.get_env_dict(self.environment, subdir) + # TODO: additional env vars that are not in the common set + # 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}')] |
