Skip to content

Commit

Permalink
build: multithreaded individual object builds
Browse files Browse the repository at this point in the history
Signed-off-by: Dylan Turner <dylan.turner@tutanota.com>
  • Loading branch information
blueOkiris committed Aug 24, 2024
1 parent a4aa6f5 commit c49efbd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion include/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <variant>
#include <err.hpp>

#define VERSION 7
#define VERSION 8

namespace acbs {
namespace args {
Expand Down
2 changes: 1 addition & 1 deletion include/err.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace acbs {
case AcbsErrType::BadValue:
return "Bad value in ini file '" + err.extraInfo + "'";
case AcbsErrType::CompileCommand:
return "Error compiling file.";
return "Error compiling file '" + err.extraInfo + "'";
case AcbsErrType::LinkCommand:
return "Error linking files.";
}
Expand Down
9 changes: 4 additions & 5 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ pkgs ? import <nixpkgs> { } }:

let
unstable = import
{ pkgs ? import <nixpkgs> { } }:

let
unstable = import
(fetchTarball "https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz") {};
buildLibs = with pkgs; (with xorg; [
libX11
Expand All @@ -19,7 +19,6 @@ in with pkgs; with xorg; mkShell {
buildInputs = [
ccls
gcc
gnumake
pkg-config
] ++ buildLibs;
shellHook = ''
Expand Down
49 changes: 41 additions & 8 deletions src/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <optional>
#include <vector>
#include <filesystem>
#include <mutex>
#include <fstream>
#include <map>
#include <sstream>
#include <thread>
#include <err.hpp>
#include <ini.hpp>
#include <build.hpp>
Expand All @@ -22,6 +25,11 @@ static std::string buildFileCmd(
const std::filesystem::path &src, const std::filesystem::path &obj
);
static std::string linkObjsCmd(const ini::Project &proj);
static void parallelBuildFile(
const ini::Project &proj, const bool isDebug,
const std::filesystem::path &src, const std::filesystem::path &obj,
std::mutex &resultsMutex, std::map<std::string, bool> &results
);

std::optional<err::AcbsErr> acbs::build::build(const ini::Project &proj, const bool isDebug) {
// Get and show the users what files will be built
Expand All @@ -32,14 +40,24 @@ std::optional<err::AcbsErr> acbs::build::build(const ini::Project &proj, const b
}
std::cout << std::endl;

for (const auto &srcObj : srcObjZip) {
std::cout << "Building " << srcObj.second << ":" << std::endl;
const auto cmd = buildFileCmd(proj, isDebug, srcObj.first, srcObj.second);
std::cout << "- Command: '" << cmd << "'" << std::endl;
const auto result = std::system(cmd.c_str());
std::cout << "- Successful: " << (result == 0 ? "Yes" : "No") << std::endl;
if (result != 0) {
return err::AcbsErr { .type = err::AcbsErrType::CompileCommand, .extraInfo = "" };
std::map<std::string, bool> results;
std::mutex resultsMutex;
std::vector<std::thread> threads;
for (const auto& srcObj : srcObjZip) {
threads.push_back(std::thread(
parallelBuildFile,
proj, isDebug, srcObj.first, srcObj.second,
std::ref(resultsMutex), std::ref(results)
));
}
for (auto& t : threads) {
t.join();
}
for (const auto& result : results) {
if (!result.second) {
return err::AcbsErr {
.type = err::AcbsErrType::CompileCommand, .extraInfo = result.first
};
}
}
std::cout << std::endl;
Expand Down Expand Up @@ -219,3 +237,18 @@ static std::string linkObjsCmd(const ini::Project &proj) {
return cmd.str();
}

static void parallelBuildFile(
const ini::Project &proj, const bool isDebug,
const std::filesystem::path &src, const std::filesystem::path &obj,
std::mutex &resultsMutex, std::map<std::string, bool> &results) {
const auto cmd = buildFileCmd(proj, isDebug, src, obj);
const auto result = std::system(cmd.c_str());

std::lock_guard<std::mutex> lock(resultsMutex);
results[src.string()] = result == 0;
std::cout
<< "Building " << src << ":" << std::endl
<< "- Command: '" << cmd << "'" << std::endl
<< "- Successful: " << (result == 0 ? "Yes" : "No") << std::endl;
}

0 comments on commit c49efbd

Please sign in to comment.