Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip buildpack binaries when packaging #445

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ INFO - Building binaries (x86_64-unknown-linux-musl)...
# Omitting compilation output...
Finished dev [unoptimized + debuginfo] target(s) in 4.29s
INFO - Writing buildpack directory...
INFO - Successfully wrote buildpack directory: target/buildpack/debug/libcnb-examples_my-buildpack (27.30 MiB)
INFO - Successfully wrote buildpack directory: target/buildpack/debug/libcnb-examples_my-buildpack (3.26 MiB)
INFO - Packaging successfully finished!
INFO - Hint: To test your buildpack locally with pack, run: pack build my-image --buildpack target/buildpack/debug/libcnb-examples_my-buildpack --path /path/to/application
```
Expand Down
2 changes: 1 addition & 1 deletion examples/basics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "examples-basics"
version = "0.0.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/execd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "examples-execd"
version = "0.0.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/ruby-sample/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "examples-ruby-sample"
version = "0.0.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
publish = false

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions libcnb-cargo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix the packaged buildpack size reported by `cargo libcnb package` ([#442](https://github.com/heroku/libcnb.rs/pull/442)).
- Reduce number of dependencies to improve installation time. ([#442](https://github.com/heroku/libcnb.rs/pull/442) and [#443](https://github.com/heroku/libcnb.rs/pull/443))
- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

## [0.4.1] 2022-06-24

Expand Down
2 changes: 1 addition & 1 deletion libcnb-cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb-cargo"
version = "0.4.1"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "Cargo command for managing buildpacks written with libcnb.rs"
keywords = ["buildpacks", "CNB"]
Expand Down
2 changes: 1 addition & 1 deletion libcnb-cargo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn handle_libcnb_package(args: PackageArgs) {
&current_dir,
&cargo_metadata,
cargo_profile,
cargo_build_env,
&cargo_build_env,
&target_triple,
) {
Ok(binaries) => binaries,
Expand Down
1 change: 1 addition & 0 deletions libcnb-data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Disable `fancy-regex` default features (such as unused unicode support) to reduce buildpack binary size. ([#439](https://github.com/heroku/libcnb.rs/pull/439))
- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

# [0.7.0] 2022-06-24

Expand Down
2 changes: 1 addition & 1 deletion libcnb-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb-data"
version = "0.7.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "Types for data formats specified in the Cloud Native Buildpack specification, used by libcnb.rs"
keywords = ["buildpacks", "CNB"]
Expand Down
4 changes: 4 additions & 0 deletions libcnb-package/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

- Strip buildpack binaries when packaging, to reduce buildpack size. This not only reduces the size of production builder images, but also speeds up workflows that involve non-release builds (such as integration tests). ([#445](https://github.com/heroku/libcnb.rs/pull/445))
- The type of the `cargo_env` argument to `build::build_buildpack_binaries` and `build::build_binary` has changed. ([#445](https://github.com/heroku/libcnb.rs/pull/445))
- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

## [0.1.2] 2022-06-24

- Only create `.libcnb-cargo/additional-bin` if there are additional binaries to bundle ([#413](https://github.com/heroku/libcnb.rs/pull/413)).
Expand Down
2 changes: 1 addition & 1 deletion libcnb-package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb-package"
version = "0.1.2"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "Library for cross-compiling and packaging buildpacks written with libcnb.rs"
keywords = ["buildpacks", "CNB"]
Expand Down
47 changes: 32 additions & 15 deletions libcnb-package/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::config::{config_from_metadata, ConfigError};
use crate::CargoProfile;
use cargo_metadata::Metadata;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};

Expand All @@ -17,28 +17,22 @@ use std::process::{Command, ExitStatus};
///
/// Will return `Err` if any build did not finish successfully, the configuration cannot be
/// read or the configured main buildpack binary does not exist.
pub fn build_buildpack_binaries<
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr> + Clone,
V: AsRef<OsStr> + Clone,
>(
pub fn build_buildpack_binaries(
project_path: impl AsRef<Path>,
cargo_metadata: &Metadata,
cargo_profile: CargoProfile,
cargo_env: I,
cargo_env: &[(OsString, OsString)],
target_triple: impl AsRef<str>,
) -> Result<BuildpackBinaries, BuildBinariesError> {
let binary_target_names = binary_target_names(cargo_metadata);
let config = config_from_metadata(cargo_metadata).map_err(BuildBinariesError::ConfigError)?;

let cargo_env: Vec<(K, V)> = cargo_env.into_iter().collect();

let buildpack_target_binary_path = if binary_target_names.contains(&config.buildpack_target) {
build_binary(
project_path.as_ref(),
cargo_metadata,
cargo_profile,
cargo_env.clone(),
cargo_env.to_owned(),
target_triple.as_ref(),
&config.buildpack_target,
)
Expand All @@ -60,7 +54,7 @@ pub fn build_buildpack_binaries<
project_path.as_ref(),
cargo_metadata,
cargo_profile,
cargo_env.clone(),
cargo_env.to_owned(),
target_triple.as_ref(),
additional_binary_target_name,
)
Expand Down Expand Up @@ -97,18 +91,41 @@ pub fn build_buildpack_binaries<
/// # Errors
///
/// Will return `Err` if the build did not finish successfully.
pub fn build_binary<I: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>>(
pub fn build_binary(
project_path: impl AsRef<Path>,
cargo_metadata: &Metadata,
cargo_profile: CargoProfile,
cargo_env: I,
mut cargo_env: Vec<(OsString, OsString)>,
target_triple: impl AsRef<str>,
target_name: impl AsRef<str>,
) -> Result<PathBuf, BuildError> {
let mut cargo_args = vec!["build", "--target", target_triple.as_ref()];
match cargo_profile {
CargoProfile::Dev => {}
CargoProfile::Release => cargo_args.push("--release"),
CargoProfile::Dev => {
// We enable stripping for dev builds too, since debug builds are extremely
// large and can otherwise take a long time to be Docker copied into the
// ephemeral builder image created by `pack build` for local development
// and integration testing workflows. Since we are stripping the builds,
// we also disable debug symbols to improve performance slightly, since
// they will only be stripped out at the end of the build anyway.
cargo_env.append(&mut vec![
(
OsString::from("CARGO_PROFILE_DEV_DEBUG"),
OsString::from("false"),
),
(
OsString::from("CARGO_PROFILE_DEV_STRIP"),
OsString::from("true"),
),
]);
}
CargoProfile::Release => {
cargo_args.push("--release");
cargo_env.push((
OsString::from("CARGO_PROFILE_RELEASE_STRIP"),
OsString::from("true"),
));
edmorley marked this conversation as resolved.
Show resolved Hide resolved
}
}

let exit_status = Command::new("cargo")
Expand Down
1 change: 1 addition & 0 deletions libcnb-proc-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Disable `fancy-regex` default features (such as unused unicode support) to reduce buildpack binary size. ([#439](https://github.com/heroku/libcnb.rs/pull/439))
- Fix `verify_regex`'s error handling when the regex could not be compiled. ([#438](https://github.com/heroku/libcnb.rs/pull/438))
- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

## [0.2.2] 2022-06-24

Expand Down
2 changes: 1 addition & 1 deletion libcnb-proc-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb-proc-macros"
version = "0.2.2"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "Procedural macros used within libcnb.rs"
repository = "https://github.com/heroku/libcnb.rs/tree/main/libcnb-proc-macros"
Expand Down
1 change: 1 addition & 0 deletions libcnb-test/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Fix rustdocs for `LogOutput`. ([#440](https://github.com/heroku/libcnb.rs/pull/440)).
- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

## [0.4.0] 2022-06-24

Expand Down
2 changes: 1 addition & 1 deletion libcnb-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb-test"
version = "0.4.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "An integration testing framework for buildpacks written with libcnb.rs"
keywords = ["buildpacks", "CNB"]
Expand Down
2 changes: 1 addition & 1 deletion libcnb-test/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn package_crate_buildpack(
&cargo_manifest_dir,
&cargo_metadata,
CargoProfile::Dev,
cargo_env,
&cargo_env,
target_triple.as_ref(),
)
.map_err(PackageCrateBuildpackError::BuildBinariesError)?;
Expand Down
2 changes: 2 additions & 0 deletions libcnb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Increase minimum supported Rust version from 1.58 to 1.59. ([#445](https://github.com/heroku/libcnb.rs/pull/445))

## [0.8.0] 2022-06-24

- Make the "Buildpack API version mismatch" check still work when `buildpack.toml` doesn't match the spec or custom buildpack type ([#421](https://github.com/heroku/libcnb.rs/pull/421)).
Expand Down
2 changes: 1 addition & 1 deletion libcnb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libcnb"
version = "0.8.0"
edition = "2021"
rust-version = "1.58"
rust-version = "1.59"
license = "BSD-3-Clause"
description = "A framework for writing Cloud Native Buildpacks in Rust"
keywords = ["buildpacks", "CNB"]
Expand Down