Skip to content

Commit

Permalink
Add --profile command line option
Browse files Browse the repository at this point in the history
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/<profile>". 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 <pete@jpgrayson.net>
  • Loading branch information
jpgrayson committed May 20, 2023
1 parent edf7f6a commit 76135bc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/build_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
pub struct BuildTarget {
pub target_dir: Option<String>,
pub target: Option<String>,
pub profile: Option<String>,
pub arch: Option<String>,
}

Expand Down
24 changes: 16 additions & 8 deletions src/config/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ impl FileInfo<'_, '_, '_, '_> {
parent: P,
idx: usize,
) -> Result<Vec<(PathBuf, String)>, 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())
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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()
),
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, 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",
Expand Down Expand Up @@ -179,6 +185,9 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, 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());
Expand Down

0 comments on commit 76135bc

Please sign in to comment.