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

Support parallel builds #816

Merged
merged 1 commit into from
Dec 30, 2023
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
21 changes: 18 additions & 3 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
#include <cstdlib>
#include <iostream>
#include <span>
#include <thread>

int buildImpl(const bool isDebug, String& outDir) {
int buildImpl(String& outDir, const bool isDebug, const bool isParallel) {
const auto start = std::chrono::steady_clock::now();

outDir = emitMakefile(isDebug);
const int status = std::system((getMakeCommand() + " -C " + outDir).c_str());
String makeCommand = getMakeCommand() + " -C " + outDir;
if (isParallel) {
const unsigned int numThreads = std::thread::hardware_concurrency();
if (numThreads > 1) {
makeCommand += " -j" + std::to_string(numThreads);
}
}

Logger::debug("Running `", makeCommand, '`');
const int status = std::system(makeCommand.c_str());
const int exitCode = status >> 8;

const auto end = std::chrono::steady_clock::now();
Expand All @@ -31,6 +41,7 @@ int buildMain(std::span<const StringRef> args) {
// Parse args
bool isDebug = true;
bool buildCompdb = false;
bool isParallel = true;
for (usize i = 0; i < args.size(); ++i) {
StringRef arg = args[i];
HANDLE_GLOBAL_OPTS({{"build"}}) // workaround for std::span until C++26
Expand All @@ -44,6 +55,9 @@ int buildMain(std::span<const StringRef> args) {
else if (arg == "--compdb") {
buildCompdb = true;
}
else if (arg == "--no-parallel") {
isParallel = false;
}
else {
Logger::error("Unknown argument: ", arg);
return EXIT_FAILURE;
Expand All @@ -52,7 +66,7 @@ int buildMain(std::span<const StringRef> args) {

if (!buildCompdb) {
String outDir;
return buildImpl(isDebug, outDir);
return buildImpl(outDir, isDebug, isParallel);
}

// Build compilation database
Expand All @@ -73,4 +87,5 @@ void buildHelp() noexcept {
printOption(
"--compdb", "", "Generate compilation database instead of building"
);
printOption("--no-parallel", "", "Disable parallel builds");
}
2 changes: 1 addition & 1 deletion src/Cmd/Build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
static inline constexpr StringRef buildDesc =
"Compile a local package and all of its dependencies";

int buildImpl(const bool, String&);
int buildImpl(String&, const bool, const bool);
int buildMain(std::span<const StringRef>);
void buildHelp() noexcept;
7 changes: 6 additions & 1 deletion src/Cmd/Run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
int runMain(std::span<const StringRef> args) {
// Parse args
bool isDebug = true;
bool isParallel = true;
String runArgs;
for (usize i = 0; i < args.size(); ++i) {
StringRef arg = args[i];
Expand All @@ -24,13 +25,16 @@ int runMain(std::span<const StringRef> args) {
else if (arg == "-r" || arg == "--release") {
isDebug = false;
}
else if (arg == "--no-parallel") {
isParallel = false;
}
else {
runArgs += " " + String(arg);
}
}

String outDir;
if (buildImpl(isDebug, outDir) != EXIT_SUCCESS) {
if (buildImpl(outDir, isDebug, isParallel) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}

Expand All @@ -51,6 +55,7 @@ void runHelp() noexcept {
printGlobalOpts();
printOption("--debug", "-d", "Build with debug information [default]");
printOption("--release", "-r", "Build with optimizations");
printOption("--no-parallel", "", "Disable parallel builds");
std::cout << '\n';
printHeader("Arguments:");
std::cout << " [args]...\tArguments passed to the program" << '\n';
Expand Down