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

Build: support always build compdb option #1105

Merged
merged 1 commit into from
Jan 19, 2025
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
3 changes: 3 additions & 0 deletions cabin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ tbb = {version = ">=2021.5.0 && <2023.0.0", system = true}
[profile]
cxxflags = ["-pedantic-errors", "-Wall", "-Wextra", "-Wpedantic"]

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

[profile.release]
lto = true

Expand Down
30 changes: 27 additions & 3 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -987,21 +987,45 @@ Result<BuildConfig>
emitMakefile(
const Manifest& manifest, const bool isDebug, const bool includeDevDeps
) {
const Profile& profile =
isDebug ? manifest.profiles.at("dev") : manifest.profiles.at("release");
auto config = Try(BuildConfig::init(manifest, isDebug));

// When emitting Makefile, we also build the project. So, we need to
// make sure the dependencies are installed.
Try(config.installDeps(includeDevDeps));

bool buildProj = false;
bool buildCompDb = false;
if (config.makefileIsUpToDate()) {
logger::debug("Makefile is up to date");
} else {
logger::debug("Makefile is NOT up to date");
buildProj = true;
}
if (profile.compDb) {
if (config.compdbIsUpToDate()) {
logger::debug("compile_commands.json is up to date");
} else {
logger::debug("compile_commands.json is NOT up to date");
buildCompDb = true;
}
}
if (!buildProj && !buildCompDb) {
return Ok(config);
}
logger::debug("Makefile is NOT up to date");

Try(config.configureBuild());
std::ofstream ofs(config.outBasePath / "Makefile");
Try(config.emitMakefile(ofs));

if (buildProj) {
std::ofstream makefile(config.outBasePath / "Makefile");
Try(config.emitMakefile(makefile));
}
if (buildCompDb) {
std::ofstream compDb(config.outBasePath / "compile_commands.json");
config.emitCompdb(compDb);
}

return Ok(config);
}

Expand Down
20 changes: 12 additions & 8 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ parseProfiles(const toml::value& val) noexcept {
"ldflags",
toml::find_or_default<std::vector<std::string>>(val, "profile", "ldflags")
));
const mitama::maybe lto = toml::try_find<bool>(val, "profile", "lto").ok();
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);
const mitama::maybe optLevel =
toml::try_find<std::uint8_t>(val, "profile", "opt_level").ok();

Expand All @@ -146,17 +148,18 @@ parseProfiles(const toml::value& val) noexcept {
val, "profile", "dev", "ldflags", ldflags
)
));
const auto devLto =
toml::find_or<bool>(val, "profile", "dev", "lto", lto.unwrap_or(false));
const auto devLto = toml::find_or<bool>(val, "profile", "dev", "lto", lto);
const auto devDebug = toml::find_or<bool>(
val, "profile", "dev", "debug", debug.unwrap_or(true)
);
const auto devCompDb =
toml::find_or<bool>(val, "profile", "dev", "comp_db", compDb);
const auto devOptLevel = Try(validateOptLevel(toml::find_or<std::uint8_t>(
val, "profile", "dev", "opt_level", optLevel.unwrap_or(0)
)));
profiles.insert({ "dev", Profile(
std::move(devCxxflags), std::move(devLdflags),
devLto, devDebug, devOptLevel
devLto, devDebug, devCompDb, devOptLevel
) });

// Release
Expand All @@ -170,19 +173,20 @@ parseProfiles(const toml::value& val) noexcept {
val, "profile", "release", "ldflags", ldflags
)
));
const auto relLto = toml::find_or<bool>(
val, "profile", "release", "lto", lto.unwrap_or(false)
);
const auto relLto =
toml::find_or<bool>(val, "profile", "release", "lto", lto);
const auto relDebug = toml::find_or<bool>(
val, "profile", "release", "debug", debug.unwrap_or(false)
);
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)
)));
profiles.insert({ "release",
Profile(
std::move(relCxxflags), std::move(relLdflags), relLto,
relDebug, relOptLevel
relDebug, relCompDb, relOptLevel
) });

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 @@ -75,14 +75,15 @@ struct Profile {
const std::vector<std::string> ldflags;
const bool lto;
const bool debug;
const bool compDb;
const std::uint8_t optLevel;

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

struct Cpplint {
Expand Down
Loading