Skip to content

Commit 289f301

Browse files
Refactor forc_pkg::BuildConfig -> BuildProfile, fix CLI arg handling (#2094)
Previously, if any of the `print` args were set, the rest of the selected build profile was ignored. This changes the behaviour so that the command line arguments only override their associated build profile fields. Also renames `BuildConfig` to `BuildProfile` and moves it from `forc_pkg::pkg` to `forc_pkg::manifest` along with the rest of the serializable manifest types.
1 parent 07c4cca commit 289f301

File tree

4 files changed

+89
-83
lines changed

4 files changed

+89
-83
lines changed

forc-pkg/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ pub mod manifest;
99
mod pkg;
1010

1111
pub use lock::Lock;
12-
pub use manifest::{Manifest, ManifestFile};
12+
pub use manifest::{BuildProfile, Manifest, ManifestFile};
1313
#[doc(inline)]
1414
pub use pkg::*;

forc-pkg/src/manifest.rs

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
pkg::{manifest_file_missing, parsing_failed, wrong_program_type},
3-
BuildConfig,
4-
};
1+
use crate::pkg::{manifest_file_missing, parsing_failed, wrong_program_type};
52
use anyhow::{anyhow, bail, Result};
63
use forc_util::{find_manifest_dir, println_yellow_err, validate_name};
74
use serde::{Deserialize, Serialize};
@@ -30,7 +27,7 @@ pub struct Manifest {
3027
pub project: Project,
3128
pub network: Option<Network>,
3229
pub dependencies: Option<BTreeMap<String, Dependency>>,
33-
pub build_profile: Option<BTreeMap<String, BuildConfig>>,
30+
build_profile: Option<BTreeMap<String, BuildProfile>>,
3431
}
3532

3633
#[derive(Serialize, Deserialize, Debug)]
@@ -76,6 +73,16 @@ pub struct DependencyDetails {
7673
pub(crate) rev: Option<String>,
7774
}
7875

76+
/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
77+
#[derive(Serialize, Deserialize, Debug, Clone)]
78+
#[serde(rename_all = "kebab-case")]
79+
pub struct BuildProfile {
80+
pub print_ir: bool,
81+
pub print_finalized_asm: bool,
82+
pub print_intermediate_asm: bool,
83+
pub silent: bool,
84+
}
85+
7986
impl Dependency {
8087
/// The string of the `package` field if specified.
8188
pub fn package(&self) -> Option<&str> {
@@ -182,6 +189,13 @@ impl ManifestFile {
182189
Ok(())
183190
}
184191
}
192+
193+
/// Access the build profile associated with the given profile name.
194+
pub fn build_profile(&self, profile_name: &str) -> Option<&BuildProfile> {
195+
self.build_profile
196+
.as_ref()
197+
.and_then(|profiles| profiles.get(profile_name))
198+
}
185199
}
186200

187201
impl Manifest {
@@ -241,7 +255,7 @@ impl Manifest {
241255
}
242256

243257
/// Produce an iterator yielding all listed build profiles.
244-
pub fn build_profiles(&self) -> impl Iterator<Item = (&String, &BuildConfig)> {
258+
pub fn build_profiles(&self) -> impl Iterator<Item = (&String, &BuildProfile)> {
245259
self.build_profile
246260
.as_ref()
247261
.into_iter()
@@ -291,32 +305,13 @@ impl Manifest {
291305
/// If they are provided, use the provided `debug` or `release` so that they override the default `debug`
292306
/// and `release`.
293307
fn implicitly_include_default_build_profiles_if_missing(&mut self) {
294-
const DEBUG: &str = "debug";
295-
const RELEASE: &str = "release";
296-
297308
let build_profiles = self.build_profile.get_or_insert_with(Default::default);
298309

299-
if build_profiles.get(DEBUG).is_none() {
300-
build_profiles.insert(
301-
DEBUG.to_string(),
302-
BuildConfig {
303-
print_ir: false,
304-
print_finalized_asm: false,
305-
print_intermediate_asm: false,
306-
silent: false,
307-
},
308-
);
310+
if build_profiles.get(BuildProfile::DEBUG).is_none() {
311+
build_profiles.insert(BuildProfile::DEBUG.into(), BuildProfile::debug());
309312
}
310-
if build_profiles.get(RELEASE).is_none() {
311-
build_profiles.insert(
312-
RELEASE.to_string(),
313-
BuildConfig {
314-
print_ir: false,
315-
print_finalized_asm: false,
316-
print_intermediate_asm: false,
317-
silent: false,
318-
},
319-
);
313+
if build_profiles.get(BuildProfile::RELEASE).is_none() {
314+
build_profiles.insert(BuildProfile::RELEASE.into(), BuildProfile::release());
320315
}
321316
}
322317

@@ -341,13 +336,43 @@ impl Manifest {
341336
}
342337
}
343338

339+
impl BuildProfile {
340+
pub const DEBUG: &'static str = "debug";
341+
pub const RELEASE: &'static str = "release";
342+
pub const DEFAULT: &'static str = Self::DEBUG;
343+
344+
pub fn debug() -> Self {
345+
Self {
346+
print_ir: false,
347+
print_finalized_asm: false,
348+
print_intermediate_asm: false,
349+
silent: false,
350+
}
351+
}
352+
353+
pub fn release() -> Self {
354+
Self {
355+
print_ir: false,
356+
print_finalized_asm: false,
357+
print_intermediate_asm: false,
358+
silent: false,
359+
}
360+
}
361+
}
362+
344363
impl std::ops::Deref for ManifestFile {
345364
type Target = Manifest;
346365
fn deref(&self) -> &Self::Target {
347366
&self.manifest
348367
}
349368
}
350369

370+
impl Default for BuildProfile {
371+
fn default() -> Self {
372+
Self::debug()
373+
}
374+
}
375+
351376
/// The definition for the implicit `std` dependency.
352377
fn implicit_std_dep(sway_git_tag: String) -> Dependency {
353378
const SWAY_GIT_REPO_URL: &str = "https://github.com/fuellabs/sway";

forc-pkg/src/pkg.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
lock::Lock,
3-
manifest::{Dependency, Manifest, ManifestFile},
3+
manifest::{BuildProfile, Dependency, Manifest, ManifestFile},
44
};
55
use anyhow::{anyhow, bail, Context, Error, Result};
66
use forc_util::{
@@ -166,16 +166,6 @@ pub struct BuildPlan {
166166
compilation_order: Vec<NodeIx>,
167167
}
168168

169-
/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
170-
#[derive(Serialize, Deserialize, Debug)]
171-
#[serde(rename_all = "kebab-case")]
172-
pub struct BuildConfig {
173-
pub print_ir: bool,
174-
pub print_finalized_asm: bool,
175-
pub print_intermediate_asm: bool,
176-
pub silent: bool,
177-
}
178-
179169
/// Error returned upon failed parsing of `PinnedId::from_str`.
180170
#[derive(Clone, Debug)]
181171
pub struct PinnedIdParseError;
@@ -1327,22 +1317,22 @@ fn dep_to_source(pkg_path: &Path, dep: &Dependency) -> Result<Source> {
13271317
Ok(source)
13281318
}
13291319

1330-
/// Given a `forc_pkg::BuildConfig`, produce the necessary `sway_core::BuildConfig` required for
1320+
/// Given a `forc_pkg::BuildProfile`, produce the necessary `sway_core::BuildConfig` required for
13311321
/// compilation.
13321322
pub fn sway_build_config(
13331323
manifest_dir: &Path,
13341324
entry_path: &Path,
1335-
build_conf: &BuildConfig,
1325+
build_profile: &BuildProfile,
13361326
) -> Result<sway_core::BuildConfig> {
13371327
// Prepare the build config to pass through to the compiler.
13381328
let file_name = find_file_name(manifest_dir, entry_path)?;
13391329
let build_config = sway_core::BuildConfig::root_from_file_name_and_manifest_path(
13401330
file_name.to_path_buf(),
13411331
manifest_dir.to_path_buf(),
13421332
)
1343-
.print_finalized_asm(build_conf.print_finalized_asm)
1344-
.print_intermediate_asm(build_conf.print_intermediate_asm)
1345-
.print_ir(build_conf.print_ir);
1333+
.print_finalized_asm(build_profile.print_finalized_asm)
1334+
.print_intermediate_asm(build_profile.print_intermediate_asm)
1335+
.print_ir(build_profile.print_ir);
13461336
Ok(build_config)
13471337
}
13481338

@@ -1384,12 +1374,12 @@ pub fn dependency_namespace(
13841374
/// Compiles the package to an AST.
13851375
pub fn compile_ast(
13861376
manifest: &ManifestFile,
1387-
build_config: &BuildConfig,
1377+
build_profile: &BuildProfile,
13881378
namespace: namespace::Module,
13891379
) -> Result<CompileAstResult> {
13901380
let source = manifest.entry_string()?;
13911381
let sway_build_config =
1392-
sway_build_config(manifest.dir(), &manifest.entry_path(), build_config)?;
1382+
sway_build_config(manifest.dir(), &manifest.entry_path(), build_profile)?;
13931383
let ast_res = sway_core::compile_to_ast(source, namespace, Some(&sway_build_config));
13941384
Ok(ast_res)
13951385
}
@@ -1415,16 +1405,16 @@ pub fn compile_ast(
14151405
pub fn compile(
14161406
pkg: &Pinned,
14171407
manifest: &ManifestFile,
1418-
build_config: &BuildConfig,
1408+
build_profile: &BuildProfile,
14191409
namespace: namespace::Module,
14201410
source_map: &mut SourceMap,
14211411
) -> Result<(Compiled, Option<namespace::Root>)> {
14221412
let entry_path = manifest.entry_path();
1423-
let sway_build_config = sway_build_config(manifest.dir(), &entry_path, build_config)?;
1424-
let silent_mode = build_config.silent;
1413+
let sway_build_config = sway_build_config(manifest.dir(), &entry_path, build_profile)?;
1414+
let silent_mode = build_profile.silent;
14251415

14261416
// First, compile to an AST. We'll update the namespace and check for JSON ABI output.
1427-
let ast_res = compile_ast(manifest, build_config, namespace)?;
1417+
let ast_res = compile_ast(manifest, build_profile, namespace)?;
14281418
match &ast_res {
14291419
CompileAstResult::Failure { warnings, errors } => {
14301420
print_on_failure(silent_mode, warnings, errors);
@@ -1487,7 +1477,7 @@ pub fn compile(
14871477
/// Also returns the resulting `sway_core::SourceMap` which may be useful for debugging purposes.
14881478
pub fn build(
14891479
plan: &BuildPlan,
1490-
conf: &BuildConfig,
1480+
profile: &BuildProfile,
14911481
sway_git_tag: &str,
14921482
) -> anyhow::Result<(Compiled, SourceMap)> {
14931483
let mut namespace_map = Default::default();
@@ -1501,7 +1491,7 @@ pub fn build(
15011491
let pkg = &plan.graph[node];
15021492
let path = &plan.path_map[&pkg.id()];
15031493
let manifest = ManifestFile::from_dir(path, sway_git_tag)?;
1504-
let res = compile(pkg, &manifest, conf, dep_namespace, &mut source_map)?;
1494+
let res = compile(pkg, &manifest, profile, dep_namespace, &mut source_map)?;
15051495
let (compiled, maybe_namespace) = res;
15061496
if let Some(namespace) = maybe_namespace {
15071497
namespace_map.insert(node, namespace.into());
@@ -1527,11 +1517,9 @@ pub fn check(
15271517
silent_mode: bool,
15281518
sway_git_tag: &str,
15291519
) -> anyhow::Result<CompileAstResult> {
1530-
let conf = &BuildConfig {
1531-
print_ir: false,
1532-
print_finalized_asm: false,
1533-
print_intermediate_asm: false,
1520+
let profile = BuildProfile {
15341521
silent: silent_mode,
1522+
..BuildProfile::debug()
15351523
};
15361524

15371525
let mut namespace_map = Default::default();
@@ -1542,7 +1530,7 @@ pub fn check(
15421530
let pkg = &plan.graph[node];
15431531
let path = &plan.path_map[&pkg.id()];
15441532
let manifest = ManifestFile::from_dir(path, sway_git_tag)?;
1545-
let ast_res = compile_ast(&manifest, conf, dep_namespace)?;
1533+
let ast_res = compile_ast(&manifest, &profile, dep_namespace)?;
15461534
if let CompileAstResult::Success { typed_program, .. } = &ast_res {
15471535
if let TreeType::Library { .. } = typed_program.kind.tree_type() {
15481536
namespace_map.insert(node, typed_program.root.namespace.clone());

forc/src/ops/forc_build.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,25 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
5858

5959
let plan = pkg::BuildPlan::load_from_manifest(&manifest, locked, offline, SWAY_GIT_TAG)?;
6060

61-
// If any cli parameter is passed by the user it overrides the selected build profile.
62-
let mut config = &pkg::BuildConfig {
63-
print_ir,
64-
print_finalized_asm,
65-
print_intermediate_asm,
66-
silent: silent_mode,
67-
};
68-
69-
// Check if any cli parameter is passed by the user if not fetch the build profile from manifest.
70-
if !print_ir && !print_intermediate_asm && !print_finalized_asm && !silent_mode {
71-
config = manifest
72-
.build_profile
73-
.as_ref()
74-
.and_then(|profiles| profiles.get(&selected_build_profile))
75-
.unwrap_or_else(|| {
76-
warn!(
77-
"provided profile option {} is not present in the manifest file. \
78-
Using default config.",
79-
selected_build_profile
80-
);
81-
config
82-
});
83-
}
61+
// Retrieve the specified build profile
62+
let mut profile = manifest
63+
.build_profile(&selected_build_profile)
64+
.cloned()
65+
.unwrap_or_else(|| {
66+
warn!(
67+
"provided profile option {} is not present in the manifest file. \
68+
Using default profile.",
69+
selected_build_profile
70+
);
71+
Default::default()
72+
});
73+
profile.print_ir |= print_ir;
74+
profile.print_finalized_asm |= print_finalized_asm;
75+
profile.print_intermediate_asm |= print_intermediate_asm;
76+
profile.silent |= silent_mode;
8477

8578
// Build it!
86-
let (compiled, source_map) = pkg::build(&plan, config, SWAY_GIT_TAG)?;
79+
let (compiled, source_map) = pkg::build(&plan, &profile, SWAY_GIT_TAG)?;
8780

8881
if let Some(outfile) = binary_outfile {
8982
fs::write(&outfile, &compiled.bytecode)?;

0 commit comments

Comments
 (0)