Skip to content

Commit

Permalink
Manifest: refactor parseProfiles (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jan 20, 2025
1 parent 7b7ea5e commit aa832cd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cabin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tbb = {version = ">=2021.5.0 && <2023.0.0", system = true}
cxxflags = ["-pedantic-errors", "-Wall", "-Wextra", "-Wpedantic"]

[profile.dev]
comp_db = true # always build comp DB on dev
comp-db = true # always build comp DB on dev

[profile.release]
lto = true
Expand Down
107 changes: 72 additions & 35 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Package::tryFromToml(const toml::value& val) noexcept {
static Result<std::uint8_t>
validateOptLevel(const std::uint8_t optLevel) noexcept {
// TODO: use toml::format_error for better diagnostics.
Ensure(optLevel <= 3, "opt_level must be between 0 and 3");
Ensure(optLevel <= 3, "opt-level must be between 0 and 3");
return Ok(optLevel);
}

Expand Down Expand Up @@ -115,80 +115,117 @@ validateFlags(
return Ok(flags);
}

static Result<std::unordered_map<std::string, Profile>>
parseProfiles(const toml::value& val) noexcept {
std::unordered_map<std::string, Profile> profiles;
struct BaseProfile {
const std::vector<std::string> cxxflags;
const std::vector<std::string> ldflags;
const bool lto;
const mitama::maybe<bool> debug;
const bool compDb;
const mitama::maybe<std::uint8_t> optLevel;

BaseProfile(
std::vector<std::string> cxxflags, std::vector<std::string> ldflags,
const bool lto, const mitama::maybe<bool> debug, const bool compDb,
const mitama::maybe<std::uint8_t> optLevel
) noexcept
: cxxflags(std::move(cxxflags)), ldflags(std::move(ldflags)), lto(lto),
debug(debug), compDb(compDb), optLevel(optLevel) {}
};

// Base profile to propagate to other profiles
const auto cxxflags = Try(validateFlags(
static Result<BaseProfile>
parseBaseProfile(const toml::value& val) noexcept {
auto cxxflags = Try(validateFlags(
"cxxflags", toml::find_or_default<std::vector<std::string>>(
val, "profile", "cxxflags"
)
));
const auto ldflags = Try(validateFlags(
auto ldflags = Try(validateFlags(
"ldflags",
toml::find_or_default<std::vector<std::string>>(val, "profile", "ldflags")
));
const bool lto = toml::try_find<bool>(val, "profile", "lto").unwrap_or(false);
const mitama::maybe debug =
toml::try_find<bool>(val, "profile", "debug").ok();
const bool compDb =
toml::try_find<bool>(val, "profile", "comp_db").unwrap_or(false);
toml::try_find<bool>(val, "profile", "comp-db").unwrap_or(false);
const mitama::maybe optLevel =
toml::try_find<std::uint8_t>(val, "profile", "opt_level").ok();
toml::try_find<std::uint8_t>(val, "profile", "opt-level").ok();

// Dev
return Ok(BaseProfile(
std::move(cxxflags), std::move(ldflags), lto, debug, compDb, optLevel
));
}

static Result<Profile>
parseDevProfile(
const toml::value& val, const BaseProfile& baseProfile
) noexcept {
auto devCxxflags = Try(validateFlags(
"cxxflags", toml::find_or<std::vector<std::string>>(
val, "profile", "dev", "cxxflags", cxxflags
val, "profile", "dev", "cxxflags", baseProfile.cxxflags
)
));
auto devLdflags = Try(validateFlags(
"ldflags", toml::find_or<std::vector<std::string>>(
val, "profile", "dev", "ldflags", ldflags
val, "profile", "dev", "ldflags", baseProfile.ldflags
)
));
const auto devLto = toml::find_or<bool>(val, "profile", "dev", "lto", lto);
const auto devLto =
toml::find_or<bool>(val, "profile", "dev", "lto", baseProfile.lto);
const auto devDebug = toml::find_or<bool>(
val, "profile", "dev", "debug", debug.unwrap_or(true)
val, "profile", "dev", "debug", baseProfile.debug.unwrap_or(true)
);
const auto devCompDb =
toml::find_or<bool>(val, "profile", "dev", "comp_db", compDb);
toml::find_or<bool>(val, "profile", "dev", "comp-db", baseProfile.compDb);
const auto devOptLevel = Try(validateOptLevel(toml::find_or<std::uint8_t>(
val, "profile", "dev", "opt_level", optLevel.unwrap_or(0)
val, "profile", "dev", "opt-level", baseProfile.optLevel.unwrap_or(0)
)));
profiles.insert({ "dev", Profile(
std::move(devCxxflags), std::move(devLdflags),
devLto, devDebug, devCompDb, devOptLevel
) });

// Release
return Ok(Profile(
std::move(devCxxflags), std::move(devLdflags), devLto, devDebug,
devCompDb, devOptLevel
));
}

static Result<Profile>
parseReleaseProfile(
const toml::value& val, const BaseProfile& baseProfile
) noexcept {
auto relCxxflags = Try(validateFlags(
"cxxflags", toml::find_or<std::vector<std::string>>(
val, "profile", "release", "cxxflags", cxxflags
)
"cxxflags",
toml::find_or<std::vector<std::string>>(
val, "profile", "release", "cxxflags", baseProfile.cxxflags
)
));
auto relLdflags = Try(validateFlags(
"ldflags", toml::find_or<std::vector<std::string>>(
val, "profile", "release", "ldflags", ldflags
val, "profile", "release", "ldflags", baseProfile.ldflags
)
));
const auto relLto =
toml::find_or<bool>(val, "profile", "release", "lto", lto);
toml::find_or<bool>(val, "profile", "release", "lto", baseProfile.lto);
const auto relDebug = toml::find_or<bool>(
val, "profile", "release", "debug", debug.unwrap_or(false)
val, "profile", "release", "debug", baseProfile.debug.unwrap_or(false)
);
const auto relCompDb = toml::find_or<bool>(
val, "profile", "release", "comp-db", baseProfile.compDb
);
const auto relCompDb =
toml::find_or<bool>(val, "profile", "release", "comp_db", compDb);
const auto relOptLevel = Try(validateOptLevel(toml::find_or<std::uint8_t>(
val, "profile", "release", "opt_level", optLevel.unwrap_or(3)
val, "profile", "release", "opt-level", baseProfile.optLevel.unwrap_or(3)
)));
profiles.insert({ "release",
Profile(
std::move(relCxxflags), std::move(relLdflags), relLto,
relDebug, relCompDb, relOptLevel
) });

return Ok(Profile(
std::move(relCxxflags), std::move(relLdflags), relLto, relDebug,
relCompDb, relOptLevel
));
}

static Result<std::unordered_map<std::string, Profile>>
parseProfiles(const toml::value& val) noexcept {
std::unordered_map<std::string, Profile> profiles;
const BaseProfile baseProfile = Try(parseBaseProfile(val));
profiles.insert({ "dev", Try(parseDevProfile(val, baseProfile)) });
profiles.insert({ "release", Try(parseReleaseProfile(val, baseProfile)) });
return Ok(profiles);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Edition {
}

private:
Edition(Year year, std::string str) noexcept
Edition(const Year year, std::string str) noexcept
: edition(year), str(std::move(str)) {}
};

Expand All @@ -80,7 +80,8 @@ struct Profile {

Profile(
std::vector<std::string> cxxflags, std::vector<std::string> ldflags,
bool lto, bool debug, bool compDb, std::uint8_t optLevel
const bool lto, const bool debug, const bool compDb,
const std::uint8_t optLevel
) noexcept
: cxxflags(std::move(cxxflags)), ldflags(std::move(ldflags)), lto(lto),
debug(debug), compDb(compDb), optLevel(optLevel) {}
Expand Down

0 comments on commit aa832cd

Please sign in to comment.