Skip to content

Commit 88758ae

Browse files
authored
Fix default target triple for cargo libcnb package (#922)
* Allow tests to target a specific platform Also picks better defaults for the test runner and `libcnb package`. Fixes #843 Fixes #869 * Update CHANGELOG.md * Revert libcnb-test changes, limit scope to just determining a default target triple. Fixes #843 Fixes #869 * Update CHANGELOG.md Signed-off-by: Colin Casey <[email protected]> * Update CHANGELOG.md Signed-off-by: Colin Casey <[email protected]> * Update CHANGELOG.md Signed-off-by: Colin Casey <[email protected]> --------- Signed-off-by: Colin Casey <[email protected]>
1 parent 3b88883 commit 88758ae

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased]
1111

12+
### Changed
13+
14+
- `libcnb-cargo`
15+
- The default for the `--target` argument of `cargo libcnb package` will now be based on an architecture that matches the host machine instead of `x86_64-unknown-linux-musl`. ([#922](https://github.com/heroku/libcnb.rs/pull/922))
1216

1317
## [0.28.1] - 2025-03-25
1418

libcnb-cargo/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub(crate) struct PackageArgs {
2424
#[arg(long)]
2525
pub(crate) release: bool,
2626
/// Build for the target triple
27-
#[arg(long, default_value = "x86_64-unknown-linux-musl")]
28-
pub(crate) target: String,
27+
#[arg(long)]
28+
pub(crate) target: Option<String>,
2929
/// Directory for packaged buildpacks, defaults to 'packaged' in Cargo workspace root
3030
#[arg(long)]
3131
pub(crate) package_dir: Option<PathBuf>,

libcnb-cargo/src/package/command.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::cli::PackageArgs;
22
use crate::package::error::Error;
33
use libcnb_data::buildpack::BuildpackId;
44
use libcnb_package::buildpack_dependency_graph::build_libcnb_buildpacks_dependency_graph;
5-
use libcnb_package::cross_compile::{CrossCompileAssistance, cross_compile_assistance};
5+
use libcnb_package::cross_compile::{
6+
AARCH64_UNKNOWN_LINUX_MUSL, CrossCompileAssistance, X86_64_UNKNOWN_LINUX_MUSL,
7+
cross_compile_assistance,
8+
};
69
use libcnb_package::dependency_graph::get_dependencies;
710
use libcnb_package::output::create_packaged_buildpack_dir_resolver;
811
use libcnb_package::util::absolutize_path;
@@ -21,6 +24,11 @@ pub(crate) fn execute(args: &PackageArgs) -> Result<(), Error> {
2124
CargoProfile::Dev
2225
};
2326

27+
let target_triple = match &args.target {
28+
Some(target) => target.to_string(),
29+
None => determine_target_triple_from_host_arch()?,
30+
};
31+
2432
let workspace_root_path =
2533
find_cargo_workspace_root_dir(&current_dir).map_err(Error::CannotFindCargoWorkspaceRoot)?;
2634

@@ -37,18 +45,17 @@ pub(crate) fn execute(args: &PackageArgs) -> Result<(), Error> {
3745
.map_err(|error| Error::CannotCreatePackageDirectory(package_dir.clone(), error))?;
3846

3947
let buildpack_dir_resolver =
40-
create_packaged_buildpack_dir_resolver(&package_dir, cargo_profile, &args.target);
48+
create_packaged_buildpack_dir_resolver(&package_dir, cargo_profile, &target_triple);
4149

42-
eprintln!("🖥️ Gathering Cargo configuration (for {})", args.target);
50+
eprintln!("🖥️ Gathering Cargo configuration (for {target_triple})");
4351
let cargo_build_env = if args.no_cross_compile_assistance {
4452
Vec::new()
4553
} else {
46-
match cross_compile_assistance(&args.target) {
54+
match cross_compile_assistance(&target_triple) {
4755
CrossCompileAssistance::Configuration { cargo_env } => cargo_env,
4856
CrossCompileAssistance::NoAssistance => {
4957
eprintln!(
50-
"Couldn't determine automatic cross-compile settings for target triple {}.",
51-
args.target
58+
"Couldn't determine automatic cross-compile settings for target triple {target_triple}."
5259
);
5360
eprintln!(
5461
"This is not an error, but without proper cross-compile settings in your Cargo manifest and locally installed toolchains, compilation might fail."
@@ -110,7 +117,7 @@ pub(crate) fn execute(args: &PackageArgs) -> Result<(), Error> {
110117
libcnb_package::package::package_buildpack(
111118
&node.path,
112119
cargo_profile,
113-
&args.target,
120+
&target_triple,
114121
&cargo_build_env,
115122
&buildpack_destination_dir,
116123
&packaged_buildpack_dirs,
@@ -195,3 +202,14 @@ fn calculate_dir_size(path: impl AsRef<Path>) -> std::io::Result<u64> {
195202

196203
Ok(size_in_bytes)
197204
}
205+
206+
// NOTE: The target OS is always assumed to be linux based
207+
fn determine_target_triple_from_host_arch() -> Result<String, Error> {
208+
match std::env::consts::ARCH {
209+
"amd64" | "x86_64" => Ok(X86_64_UNKNOWN_LINUX_MUSL.to_string()),
210+
"arm64" | "aarch64" => Ok(AARCH64_UNKNOWN_LINUX_MUSL.to_string()),
211+
arch => Err(Error::CouldNotDetermineDefaultTargetForArch(
212+
arch.to_string(),
213+
)),
214+
}
215+
}

libcnb-cargo/src/package/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ pub(crate) enum Error {
2424
CannotConfigureCrossCompilation,
2525
#[error("No buildpacks found!")]
2626
NoBuildpacksFound,
27+
#[error(
28+
"Could not determine a default target triple from the current architecture ({0}), you must explicitly provide the --target argument"
29+
)]
30+
CouldNotDetermineDefaultTargetForArch(String),
2731
}

libcnb-package/src/cross_compile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ pub enum CrossCompileAssistance {
108108
}
109109

110110
// Constants for supported target triples
111-
const AARCH64_UNKNOWN_LINUX_MUSL: &str = "aarch64-unknown-linux-musl";
112-
const X86_64_UNKNOWN_LINUX_MUSL: &str = "x86_64-unknown-linux-musl";
111+
pub const AARCH64_UNKNOWN_LINUX_MUSL: &str = "aarch64-unknown-linux-musl";
112+
pub const X86_64_UNKNOWN_LINUX_MUSL: &str = "x86_64-unknown-linux-musl";
113113

114114
// Constants for `std::env::consts::OS` and `std::env::consts::ARCH`
115115
const OS_LINUX: &str = "linux";

0 commit comments

Comments
 (0)