@@ -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
@@ -573,6 +574,56 @@ def _create_meson_subdir(self, build: builder.Builder) -> T.List[mparser.BaseNod
573574 build .block ([build .function ('subdir' , [build .string ('meson' )])]))
574575 ]
575576
577+ def _pkg_common_env (self , pkg : PackageState , subdir : str ) -> T .Dict [str , str ]:
578+ # Common variables for build.rs and crates
579+ # https://doc.rust-lang.org/cargo/reference/environment-variables.html
580+ # OUT_DIR is the directory where build.rs generate files. In our case,
581+ # it's the directory where meson/meson.build places generated files.
582+ out_dir = os .path .join (self .environment .build_dir , subdir , 'meson' )
583+ os .makedirs (out_dir , exist_ok = True )
584+ version_arr = pkg .manifest .package .version .split ('.' )
585+ version_arr += ['' * (4 - len (version_arr ))]
586+ return {
587+ 'OUT_DIR' : out_dir ,
588+ 'CARGO_MANIFEST_DIR' : os .path .join (self .environment .source_dir , subdir ),
589+ 'CARGO_MANIFEST_PATH' : os .path .join (self .environment .source_dir , subdir , 'Cargo.toml' ),
590+ 'CARGO_PKG_VERSION' : pkg .manifest .package .version ,
591+ 'CARGO_PKG_VERSION_MAJOR' : version_arr [0 ],
592+ 'CARGO_PKG_VERSION_MINOR' : version_arr [1 ],
593+ 'CARGO_PKG_VERSION_PATCH' : version_arr [2 ],
594+ 'CARGO_PKG_VERSION_PRE' : version_arr [3 ],
595+ 'CARGO_PKG_AUTHORS' : ',' .join (pkg .manifest .package .authors ),
596+ 'CARGO_PKG_NAME' : pkg .manifest .package .name ,
597+ # FIXME: description can contain newlines which breaks ninja.
598+ #'CARGO_PKG_DESCRIPTION': pkg.manifest.package.description or '',
599+ 'CARGO_PKG_HOMEPAGE' : pkg .manifest .package .homepage or '' ,
600+ 'CARGO_PKG_REPOSITORY' : pkg .manifest .package .repository or '' ,
601+ 'CARGO_PKG_LICENSE' : pkg .manifest .package .license or '' ,
602+ 'CARGO_PKG_LICENSE_FILE' : pkg .manifest .package .license_file or '' ,
603+ 'CARGO_PKG_RUST_VERSION' : pkg .manifest .package .rust_version or '' ,
604+ 'CARGO_PKG_README' : pkg .manifest .package .readme or '' ,
605+ }
606+
607+ def _create_env_args (self , pkg : PackageState , build : builder .Builder , subdir : str ) -> T .List [mparser .BaseNode ]:
608+ host_rustc = T .cast ('RustCompiler' , self .environment .coredata .compilers [MachineChoice .HOST ]['rust' ])
609+ enable_env_set_args = host_rustc .enable_env_set_args ()
610+ if enable_env_set_args is None :
611+ return [build .assign (build .array ([]), 'env_args' )]
612+ # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
613+ env_dict = self ._pkg_common_env (pkg , subdir )
614+ env_dict .update ({
615+ 'CARGO_CRATE_NAME' : fixup_meson_varname (pkg .manifest .package .name ),
616+ #FIXME: TODO
617+ #CARGO_BIN_NAME
618+ #CARGO_BIN_EXE_<name>
619+ #CARGO_PRIMARY_PACKAGE
620+ #CARGO_TARGET_TMPDIR
621+ })
622+ env_args : T .List [mparser .BaseNode ] = [build .string (a ) for a in enable_env_set_args ]
623+ for k , v in env_dict .items ():
624+ env_args += [build .string ('--env-set' ), build .string (f'{ k } ={ v } ' )]
625+ return [build .assign (build .array (env_args ), 'env_args' )]
626+
576627 def _create_lib (self , pkg : PackageState , build : builder .Builder ,
577628 lib_type : Literal ['rust' , 'c' , 'proc-macro' ],
578629 static : bool = False , shared : bool = False ) -> T .List [mparser .BaseNode ]:
@@ -593,6 +644,7 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder,
593644 build .identifier ('features_args' ),
594645 build .identifier (_extra_args_varname ()),
595646 build .identifier ('system_deps_args' ),
647+ build .identifier ('env_args' ),
596648 ]
597649
598650 dependencies .append (build .identifier (_extra_deps_varname ()))
0 commit comments