Skip to content

Commit

Permalink
Set lto = "thin" for metadata build to fix linkme on macOS (#1200)
Browse files Browse the repository at this point in the history
* Set lto of metadata release profile to Thin

* Fmt

* Use profile defaults if not specified
  • Loading branch information
ascjones authored Jul 24, 2023
1 parent f37dfbc commit 96092f5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
3 changes: 3 additions & 0 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ pub use self::{
OptimizationResult,
},
workspace::{
Lto,
Manifest,
ManifestPath,
OptLevel,
PanicStrategy,
Profile,
Workspace,
},
Expand Down
7 changes: 6 additions & 1 deletion crates/build/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ use crate::{
BuildMode,
BuildSteps,
Features,
Lto,
Network,
OptimizationPasses,
Profile,
UnstableFlags,
Verbosity,
};
Expand Down Expand Up @@ -180,7 +182,10 @@ pub fn execute(
.with_root_package_manifest(|manifest| {
manifest
.with_added_crate_type("rlib")?
.with_profile_release_lto(false)?
.with_profile_release_defaults(Profile {
lto: Some(Lto::Thin),
..Profile::default()
})?
.with_empty_workspace();
Ok(())
})?
Expand Down
10 changes: 0 additions & 10 deletions crates/build/src/workspace/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,6 @@ impl Manifest {
.map(Into::into)
}

/// Set `[profile.release]` lto flag
pub fn with_profile_release_lto(&mut self, enabled: bool) -> Result<&mut Self> {
let lto = self
.profile_release_table_mut()?
.entry("lto")
.or_insert(enabled.into());
*lto = enabled.into();
Ok(self)
}

/// Set preferred defaults for the `[profile.release]` section
///
/// # Note
Expand Down
7 changes: 6 additions & 1 deletion crates/build/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ pub use self::{
Manifest,
ManifestPath,
},
profile::Profile,
profile::{
Lto,
OptLevel,
PanicStrategy,
Profile,
},
};

use anyhow::Result;
Expand Down
58 changes: 38 additions & 20 deletions crates/build/src/workspace/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@

use toml::value;

/// Subset of cargo profile settings to configure defaults for building contracts
/// Subset of cargo profile settings to configure defaults for building contracts.
///
/// All fields are optional, and if not set, the default value from cargo will be used.
/// See https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles.
#[derive(Default)]
pub struct Profile {
opt_level: OptLevel,
lto: Lto,
pub opt_level: Option<OptLevel>,
pub lto: Option<Lto>,
// `None` means use rustc default.
codegen_units: Option<u32>,
overflow_checks: bool,
panic: PanicStrategy,
pub codegen_units: Option<u32>,
pub overflow_checks: Option<bool>,
pub panic: Option<PanicStrategy>,
}

impl Profile {
/// The preferred set of defaults for compiling a release build of a contract
pub fn default_contract_release() -> Profile {
Profile {
opt_level: OptLevel::Z,
lto: Lto::Fat,
opt_level: Some(OptLevel::Z),
lto: Some(Lto::Fat),
codegen_units: Some(1),
overflow_checks: true,
panic: PanicStrategy::Abort,
overflow_checks: Some(true),
panic: Some(PanicStrategy::Abort),
}
}

Expand All @@ -46,18 +50,32 @@ impl Profile {
/// - If a profile setting is not defined, the value from this profile instance will
/// be added
pub(super) fn merge(&self, profile: &mut value::Table) {
let mut set_value_if_vacant = |key: &'static str, value: value::Value| {
if !profile.contains_key(key) {
profile.insert(key.into(), value);
fn set_value_if_vacant<T>(
key: &'static str,
value: Option<T>,
profile: &mut value::Table,
) where
T: Into<value::Value>,
{
if let Some(value) = value {
if !profile.contains_key(key) {
profile.insert(key.into(), value.into());
}
}
};
set_value_if_vacant("opt-level", self.opt_level.to_toml_value());
set_value_if_vacant("lto", self.lto.to_toml_value());
if let Some(codegen_units) = self.codegen_units {
set_value_if_vacant("codegen-units", codegen_units.into());
}
set_value_if_vacant("overflow-checks", self.overflow_checks.into());
set_value_if_vacant("panic", self.panic.to_toml_value());
set_value_if_vacant(
"opt-level",
self.opt_level.map(OptLevel::to_toml_value),
profile,
);
set_value_if_vacant("lto", self.lto.map(Lto::to_toml_value), profile);
set_value_if_vacant("codegen-units", self.codegen_units, profile);
set_value_if_vacant("overflow-checks", self.overflow_checks, profile);
set_value_if_vacant(
"panic",
self.panic.map(PanicStrategy::to_toml_value),
profile,
);
}
}

Expand Down

0 comments on commit 96092f5

Please sign in to comment.