From 76135bccfb5f8a9ef93ed46e4abc1bbcaaedec55 Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Fri, 19 May 2023 12:40:28 -0400 Subject: [PATCH] Add --profile command line option When expanding an asset path, currently only the "target/release/" prefix could be remapped. By comprehending the build profile, path expansion can happen for "target/". This enables use of both alternative target-triple and build profiles. For example, given the following asset configuration in Cargo.toml: [package.metadata.generate-rpm] assets = [ { source = "target/my-profile/my-exe", dest = "/usr/bin/", mode = "755" }, ] rpms can be generated for multiple target triples, such as: cargo generate-rpm --profile my-profile --target x86_64-unknown-linux-musl cargo generate-rpm --profile my-profile --target i668-unknown-linux-musl with the paths for the "my-exe" executable expanded correctly to: target/x86_64-unknown-linux-musl/my-profile/my-exe target/i686-unknown-linux-musl/my-profile/my-exe This behavior is consistent with cargo-deb, which also has a --profile option for the same purpose. Signed-off-by: Peter Grayson --- README.md | 5 ++++- src/build_target.rs | 1 + src/config/file_info.rs | 24 ++++++++++++++++-------- src/main.rs | 9 +++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fe1429a..3562c4e 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ Because all packages in the workspace share a common output directory that is lo ### Cross compilation -This command supports `--target-dir` and `--target` option like `cargo build`. +This command supports `--target-dir`, `--target`, and `--profile` options like `cargo build`. Depending on these options, this command changes the RPM package file location and replaces `target/release/` of the source locations of the assets. @@ -189,6 +189,9 @@ In this case, the source of the asset `{ source = "target/release/XXX", dest = " You can use `CARGO_BUILD_TARGET` environment variable instead of `--target` option and `CARGO_BUILD_TARGET_DIR` or `CARGO_TARGET_DIR` instead of `--target-dir`. +Similarly, if using a custom build profile with, for example, `--profile custom` the source of the asset +`{ source = "target/release/XXX" }` will be treated as `target/custom/XXX`. + ### Payload compress type The default payload compress type of the generated RPM file is zstd. diff --git a/src/build_target.rs b/src/build_target.rs index 44f6e66..bde7c4e 100644 --- a/src/build_target.rs +++ b/src/build_target.rs @@ -5,6 +5,7 @@ use std::path::{Path, PathBuf}; pub struct BuildTarget { pub target_dir: Option, pub target: Option, + pub profile: Option, pub arch: Option, } diff --git a/src/config/file_info.rs b/src/config/file_info.rs index 4f22b4e..b423d1f 100644 --- a/src/config/file_info.rs +++ b/src/config/file_info.rs @@ -108,12 +108,14 @@ impl FileInfo<'_, '_, '_, '_> { parent: P, idx: usize, ) -> Result, ConfigError> { + let profile = build_target.profile.as_deref().unwrap_or("release"); let source = self .source .strip_prefix("target/release/") + .or_else(|| self.source.strip_prefix(&format!("target/{profile}/"))) .and_then(|rel_path| { build_target - .target_path("release") + .target_path(profile) .join(rel_path) .to_str() .map(|v| v.to_string()) @@ -411,11 +413,16 @@ mod test { )] ); - std::fs::create_dir_all(tempdir.path().join("target/foobarbaz/release")).unwrap(); - File::create(tempdir.path().join("target/foobarbaz/release/foobarbaz")).unwrap(); + std::fs::create_dir_all(tempdir.path().join("target/target-triple/my-profile")).unwrap(); + File::create( + tempdir + .path() + .join("target/target-triple/my-profile/my-bin"), + ) + .unwrap(); let file_info = FileInfo { - source: "target/release/foobarbaz".into(), - dest: "/usr/bin/foobarbaz".into(), + source: "target/release/my-bin".into(), + dest: "/usr/bin/my-bin".into(), user: None, group: None, mode: None, @@ -432,7 +439,8 @@ mod test { .unwrap() .to_string(), ), - target: Some("foobarbaz".to_string()), + target: Some("target-triple".to_string()), + profile: Some("my-profile".to_string()), ..Default::default() }; let expanded = file_info @@ -447,7 +455,7 @@ mod test { Some( tempdir .path() - .join("target/foobarbaz/release/foobarbaz") + .join("target/target-triple/my-profile/my-bin") .to_str() .unwrap() ), @@ -490,7 +498,7 @@ mod test { "/usr/share/doc/cargo-generate-rpm/", // specifying directory 0 ) - .unwrap(), + .unwrap(), vec![( PathBuf::from("README.md"), "/usr/share/doc/cargo-generate-rpm/README.md".into() diff --git a/src/main.rs b/src/main.rs index 73b2119..1c784fdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,12 @@ fn parse_arg() -> Result<(BuildTarget, Option, Option, CliSetti May be specified with CARGO_BUILD_TARGET_DIR or CARGO_TARGET_DIR environment variables.", "DIRECTORY", ); + opts.optopt( + "", + "profile", + "Select which build profile to package. Defaults to \"release\".", + "PROFILE", + ); opts.optopt( "", "payload-compress", @@ -179,6 +185,9 @@ fn parse_arg() -> Result<(BuildTarget, Option, Option, CliSetti if let Some(target_dir) = opt_matches.opt_str("target-dir") { build_target.target_dir = Some(target_dir); } + if let Some(profile) = opt_matches.opt_str("profile") { + build_target.profile = Some(profile); + } let payload_compress = opt_matches .opt_str("payload-compress") .unwrap_or("zstd".to_string());