@@ -139,6 +139,7 @@ def _create_package(self, pkg: PackageState, build: builder.Builder, subdir: str
139139 ]
140140 ast += self ._create_dependencies (pkg , build )
141141 ast += self ._create_meson_subdir (build )
142+ ast += self ._create_env_args (pkg , build , subdir )
142143
143144 if pkg .manifest .lib :
144145 crate_type = pkg .manifest .lib .crate_type
@@ -596,6 +597,56 @@ def _create_meson_subdir(self, build: builder.Builder) -> T.List[mparser.BaseNod
596597 build .block ([build .function ('subdir' , [build .string ('meson' )])]))
597598 ]
598599
600+ def _pkg_common_env (self , pkg : PackageState , subdir : str ) -> T .Dict [str , str ]:
601+ # Common variables for build.rs and crates
602+ # https://doc.rust-lang.org/cargo/reference/environment-variables.html
603+ # OUT_DIR is the directory where build.rs generate files. In our case,
604+ # it's the directory where meson/meson.build places generated files.
605+ out_dir = os .path .join (self .environment .build_dir , subdir , 'meson' )
606+ os .makedirs (out_dir , exist_ok = True )
607+ version_arr = pkg .manifest .package .version .split ('.' )
608+ version_arr += ['' * (4 - len (version_arr ))]
609+ return {
610+ 'OUT_DIR' : out_dir ,
611+ 'CARGO_MANIFEST_DIR' : os .path .join (self .environment .source_dir , subdir ),
612+ 'CARGO_MANIFEST_PATH' : os .path .join (self .environment .source_dir , subdir , 'Cargo.toml' ),
613+ 'CARGO_PKG_VERSION' : pkg .manifest .package .version ,
614+ 'CARGO_PKG_VERSION_MAJOR' : version_arr [0 ],
615+ 'CARGO_PKG_VERSION_MINOR' : version_arr [1 ],
616+ 'CARGO_PKG_VERSION_PATCH' : version_arr [2 ],
617+ 'CARGO_PKG_VERSION_PRE' : version_arr [3 ],
618+ 'CARGO_PKG_AUTHORS' : ',' .join (pkg .manifest .package .authors ),
619+ 'CARGO_PKG_NAME' : pkg .manifest .package .name ,
620+ # FIXME: description can contain newlines which breaks ninja.
621+ #'CARGO_PKG_DESCRIPTION': pkg.manifest.package.description or '',
622+ 'CARGO_PKG_HOMEPAGE' : pkg .manifest .package .homepage or '' ,
623+ 'CARGO_PKG_REPOSITORY' : pkg .manifest .package .repository or '' ,
624+ 'CARGO_PKG_LICENSE' : pkg .manifest .package .license or '' ,
625+ 'CARGO_PKG_LICENSE_FILE' : pkg .manifest .package .license_file or '' ,
626+ 'CARGO_PKG_RUST_VERSION' : pkg .manifest .package .rust_version or '' ,
627+ 'CARGO_PKG_README' : pkg .manifest .package .readme or '' ,
628+ }
629+
630+ def _create_env_args (self , pkg : PackageState , build : builder .Builder , subdir : str ) -> T .List [mparser .BaseNode ]:
631+ host_rustc = T .cast ('RustCompiler' , self .environment .coredata .compilers [MachineChoice .HOST ]['rust' ])
632+ enable_env_set_args = host_rustc .enable_env_set_args ()
633+ if enable_env_set_args is None :
634+ return [build .assign (build .array ([]), 'env_args' )]
635+ # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
636+ env_dict = self ._pkg_common_env (pkg , subdir )
637+ env_dict .update ({
638+ 'CARGO_CRATE_NAME' : fixup_meson_varname (pkg .manifest .package .name ),
639+ #FIXME: TODO
640+ #CARGO_BIN_NAME
641+ #CARGO_BIN_EXE_<name>
642+ #CARGO_PRIMARY_PACKAGE
643+ #CARGO_TARGET_TMPDIR
644+ })
645+ env_args : T .List [mparser .BaseNode ] = [build .string (a ) for a in enable_env_set_args ]
646+ for k , v in env_dict .items ():
647+ env_args += [build .string ('--env-set' ), build .string (f'{ k } ={ v } ' )]
648+ return [build .assign (build .array (env_args ), 'env_args' )]
649+
599650 def _create_lib (self , pkg : PackageState , build : builder .Builder ,
600651 lib_type : Literal ['rust' , 'c' , 'proc-macro' ],
601652 static : bool = False , shared : bool = False ) -> T .List [mparser .BaseNode ]:
@@ -616,6 +667,7 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder,
616667 build .identifier ('features_args' ),
617668 build .identifier (_extra_args_varname ()),
618669 build .identifier ('system_deps_args' ),
670+ build .identifier ('env_args' ),
619671 ]
620672
621673 dependencies .append (build .identifier (_extra_deps_varname ()))
0 commit comments