@@ -192,6 +192,9 @@ impl CargoWorkspace {
192
192
193
193
meta. packages . sort_by ( |a, b| a. id . cmp ( & b. id ) ) ;
194
194
for meta_pkg in meta. packages {
195
+ let id = meta_pkg. id . clone ( ) ;
196
+ inject_cargo_env ( & meta_pkg, envs. entry ( id) . or_default ( ) ) ;
197
+
195
198
let cargo_metadata:: Package { id, edition, name, manifest_path, version, .. } =
196
199
meta_pkg;
197
200
let is_member = ws_members. contains ( & id) ;
@@ -385,3 +388,39 @@ fn is_dylib(path: &Path) -> bool {
385
388
Some ( ext) => matches ! ( ext. as_str( ) , "dll" | "dylib" | "so" ) ,
386
389
}
387
390
}
391
+
392
+ /// Recreates the compile-time environment variables that Cargo sets.
393
+ ///
394
+ /// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
395
+ fn inject_cargo_env ( package : & cargo_metadata:: Package , env : & mut Vec < ( String , String ) > ) {
396
+ // FIXME: Missing variables:
397
+ // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
398
+
399
+ let mut manifest_dir = package. manifest_path . clone ( ) ;
400
+ manifest_dir. pop ( ) ;
401
+ if let Some ( cargo_manifest_dir) = manifest_dir. to_str ( ) {
402
+ env. push ( ( "CARGO_MANIFEST_DIR" . into ( ) , cargo_manifest_dir. into ( ) ) ) ;
403
+ }
404
+
405
+ env. push ( ( "CARGO_PKG_VERSION" . into ( ) , package. version . to_string ( ) ) ) ;
406
+ env. push ( ( "CARGO_PKG_VERSION_MAJOR" . into ( ) , package. version . major . to_string ( ) ) ) ;
407
+ env. push ( ( "CARGO_PKG_VERSION_MINOR" . into ( ) , package. version . minor . to_string ( ) ) ) ;
408
+ env. push ( ( "CARGO_PKG_VERSION_PATCH" . into ( ) , package. version . patch . to_string ( ) ) ) ;
409
+
410
+ let pre = package. version . pre . iter ( ) . map ( |id| id. to_string ( ) ) . collect :: < Vec < _ > > ( ) ;
411
+ let pre = pre. join ( "." ) ;
412
+ env. push ( ( "CARGO_PKG_VERSION_PRE" . into ( ) , pre) ) ;
413
+
414
+ let authors = package. authors . join ( ";" ) ;
415
+ env. push ( ( "CARGO_PKG_AUTHORS" . into ( ) , authors) ) ;
416
+
417
+ env. push ( ( "CARGO_PKG_NAME" . into ( ) , package. name . clone ( ) ) ) ;
418
+ env. push ( ( "CARGO_PKG_DESCRIPTION" . into ( ) , package. description . clone ( ) . unwrap_or_default ( ) ) ) ;
419
+ //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
420
+ env. push ( ( "CARGO_PKG_REPOSITORY" . into ( ) , package. repository . clone ( ) . unwrap_or_default ( ) ) ) ;
421
+ env. push ( ( "CARGO_PKG_LICENSE" . into ( ) , package. license . clone ( ) . unwrap_or_default ( ) ) ) ;
422
+
423
+ let license_file =
424
+ package. license_file . as_ref ( ) . map ( |buf| buf. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
425
+ env. push ( ( "CARGO_PKG_LICENSE_FILE" . into ( ) , license_file) ) ;
426
+ }
0 commit comments