Skip to content
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
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ COPY .clang-tidy .
COPY cabin.toml .
COPY .git .
COPY Makefile .
COPY include ./include/
COPY lib ./lib/
COPY src ./src/
RUN make BUILD=release install

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DEFINES := -DCABIN_CABIN_PKG_VERSION='"$(VERSION)"' \
-DCABIN_CABIN_COMMIT_HASH='"$(COMMIT_HASH)"' \
-DCABIN_CABIN_COMMIT_SHORT_HASH='"$(COMMIT_SHORT_HASH)"' \
-DCABIN_CABIN_COMMIT_DATE='"$(COMMIT_DATE)"'
INCLUDES := -Isrc -isystem $(O)/DEPS/toml11/include \
INCLUDES := -Iinclude -Isrc -isystem $(O)/DEPS/toml11/include \
-isystem $(O)/DEPS/mitama-cpp-result/include

CXXFLAGS := -std=c++$(EDITION) -fdiagnostics-color $(CUSTOM_CXXFLAGS) \
Expand All @@ -62,8 +62,8 @@ endif
LDLIBS := $(PKG_LIBS)

# Source files
SRCS := $(shell find src -name '*.cc')
OBJS := $(SRCS:src/%.cc=$(O)/%.o)
SRCS := $(shell find src -name '*.cc') $(shell find lib -name '*.cc')
OBJS := $(SRCS:%.cc=$(O)/%.o)
DEPS := $(OBJS:.o=.d)

# Targets
Expand All @@ -79,7 +79,7 @@ $(PROJECT): $(OBJS)
@mkdir -p $(@D)
$(CXX) $(LDFLAGS) $^ -o $@ $(LDLIBS)

$(O)/%.o: src/%.cc $(GIT_DEPS)
$(O)/%.o: %.cc $(GIT_DEPS)
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c $< -o $@

Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/Builder/Compiler.hpp → include/Builder/Compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Compiler {
const std::string& sourceFile) const;
Command makePreprocessCmd(const CompilerOpts& opts,
const std::string& sourceFile) const;
std::string detectArchiver(bool useLTO) const;

private:
explicit Compiler(std::string cxx) noexcept : cxx(std::move(cxx)) {}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions src/Rustify/Tests.hpp → include/Rustify/Tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ constexpr std::string_view getModName(std::string_view file) noexcept {
return file;
}

const std::size_t start = file.find("src/");
std::size_t start = file.find("src/");
if (start == std::string_view::npos) {
start = file.find("lib/");
}
if (start == std::string_view::npos) {
return file;
}

const std::size_t end = file.find_last_of('.');
if (end == std::string_view::npos) {
if (end == std::string_view::npos || end <= start) {
return file;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
190 changes: 189 additions & 1 deletion src/Builder/Compiler.cc → lib/Builder/Compiler.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "Compiler.hpp"
#include "Builder/Compiler.hpp"

#include "Algos.hpp"
#include "Command.hpp"
#include "Rustify/Result.hpp"

#include <array>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
Expand All @@ -15,6 +18,126 @@

namespace cabin {

static std::optional<std::string> getEnvVar(const char* name) {
if (const char* value = std::getenv(name);
value != nullptr && *value != '\0') {
return std::string(value);
}
return std::nullopt;
}

static std::optional<std::string>
findSiblingTool(const fs::path& base, const std::string& candidate) {
if (base.has_parent_path()) {
const fs::path sibling = base.parent_path() / candidate;
if (fs::exists(sibling)) {
return sibling.string();
}
}
return std::nullopt;
}

static std::optional<std::string>
makeToolNameForCompiler(const std::string& compilerName,
std::string_view suffix, std::string_view tool) {
const std::size_t pos = compilerName.rfind(suffix);
if (pos == std::string::npos) {
return std::nullopt;
}
if (pos + suffix.size() > compilerName.size()) {
return std::nullopt;
}
if (pos != 0) {
const auto prev = static_cast<unsigned char>(compilerName[pos - 1]);
if (std::isalnum(prev)) {
return std::nullopt;
}
}

const std::string prefix = compilerName.substr(0, pos);
const std::string postfix =
compilerName.substr(pos + static_cast<std::size_t>(suffix.size()));
return fmt::format("{}{}{}", prefix, tool, postfix);
}

static std::optional<std::string> resolveToolWithSuffix(const fs::path& cxxPath,
std::string_view suffix,
std::string_view tool) {
const std::string filename = cxxPath.filename().string();
const auto candidateName = makeToolNameForCompiler(filename, suffix, tool);
if (!candidateName.has_value()) {
return std::nullopt;
}
const std::string& candidate = candidateName.value();

if (auto sibling = findSiblingTool(cxxPath, candidate); sibling.has_value()) {
return sibling;
}
if (commandExists(candidate)) {
return candidate;
}
return std::nullopt;
}

static std::optional<std::string> resolveLlvmAr(const fs::path& cxxPath) {
if (auto resolved = resolveToolWithSuffix(cxxPath, "clang++", "llvm-ar");
resolved.has_value()) {
return resolved;
}
if (auto resolved = resolveToolWithSuffix(cxxPath, "clang", "llvm-ar");
resolved.has_value()) {
return resolved;
}
if (commandExists("llvm-ar")) {
return std::string("llvm-ar");
}
return std::nullopt;
}

static std::optional<std::string> resolveGccAr(const fs::path& cxxPath) {
if (auto resolved = resolveToolWithSuffix(cxxPath, "g++", "gcc-ar");
resolved.has_value()) {
return resolved;
}
if (auto resolved = resolveToolWithSuffix(cxxPath, "gcc", "gcc-ar");
resolved.has_value()) {
return resolved;
}
if (commandExists("gcc-ar")) {
return std::string("gcc-ar");
}
return std::nullopt;
}

enum class CompilerFlavor : std::uint8_t { Clang, Gcc, Other };

static CompilerFlavor detectCompilerFlavor(const fs::path& cxxPath) {
const std::string name = cxxPath.filename().string();
if (name.contains("clang")) {
return CompilerFlavor::Clang;
}
if (name.contains("g++") || name.contains("gcc")) {
return CompilerFlavor::Gcc;
}
return CompilerFlavor::Other;
}

static std::optional<std::string> envArchiverOverride() {
if (auto ar = getEnvVar("CABIN_AR"); ar.has_value()) {
return ar;
}
if (auto ar = getEnvVar("AR"); ar.has_value()) {
return ar;
}
if (auto ar = getEnvVar("LLVM_AR"); ar.has_value()) {
return ar;
}
if (auto ar = getEnvVar("GCC_AR"); ar.has_value()) {
return ar;
}
return std::nullopt;
}

// TODO: The parsing of pkg-config output might not be robust. It assumes
// that there wouldn't be backquotes or double quotes in the output, (should
// be treated as a single flag). The current code just splits the output by
Expand Down Expand Up @@ -215,4 +338,69 @@ Command Compiler::makePreprocessCmd(const CompilerOpts& opts,
.addArg(sourceFile);
}

std::string Compiler::detectArchiver(const bool useLTO) const {
if (auto override = envArchiverOverride(); override.has_value()) {
return override.value();
}
if (!useLTO) {
return "ar";
}

const fs::path cxxPath(cxx);
switch (detectCompilerFlavor(cxxPath)) {
case CompilerFlavor::Clang:
if (auto llvmAr = resolveLlvmAr(cxxPath); llvmAr.has_value()) {
return llvmAr.value();
}
break;
case CompilerFlavor::Gcc:
if (auto gccAr = resolveGccAr(cxxPath); gccAr.has_value()) {
return gccAr.value();
}
break;
case CompilerFlavor::Other:
break;
}

return "ar";
}

} // namespace cabin

#ifdef CABIN_TEST

# include "Rustify/Tests.hpp"

namespace tests {

using namespace cabin; // NOLINT(build/namespaces,google-build-using-namespace)

static void testMakeToolNameForCompiler() {
auto expectValue = [](const std::optional<std::string>& value,
const std::string& expected) {
assertTrue(value.has_value());
assertEq(*value, expected);
};

expectValue(makeToolNameForCompiler("clang++", "clang++", "llvm-ar"),
"llvm-ar");
expectValue(makeToolNameForCompiler("clang++-19", "clang++", "llvm-ar"),
"llvm-ar-19");
expectValue(makeToolNameForCompiler("aarch64-linux-gnu-clang++", "clang++",
"llvm-ar"),
"aarch64-linux-gnu-llvm-ar");
expectValue(
makeToolNameForCompiler("x86_64-w64-mingw32-g++-13", "g++", "gcc-ar"),
"x86_64-w64-mingw32-gcc-ar-13");

assertFalse(makeToolNameForCompiler("clang++", "g++", "gcc-ar").has_value());
assertFalse(makeToolNameForCompiler("foo", "clang++", "llvm-ar").has_value());

pass();
}

} // namespace tests

int main() { tests::testMakeToolNameForCompiler(); }

#endif
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/Git2/Commit.cc → lib/Git2/Commit.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "Commit.hpp"
#include "Git2/Commit.hpp"

#include "Exception.hpp"
#include "Oid.hpp"
#include "Repository.hpp"
#include "Time.hpp"
#include "Git2/Exception.hpp"
#include "Git2/Oid.hpp"
#include "Git2/Repository.hpp"
#include "Git2/Time.hpp"

#include <git2/commit.h>

Expand Down
4 changes: 2 additions & 2 deletions src/Git2/Config.cc → lib/Git2/Config.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Config.hpp"
#include "Git2/Config.hpp"

#include "Exception.hpp"
#include "Git2/Exception.hpp"

#include <git2/buffer.h>
#include <git2/config.h>
Expand Down
6 changes: 3 additions & 3 deletions src/Git2/Describe.cc → lib/Git2/Describe.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Describe.hpp"
#include "Git2/Describe.hpp"

#include "Exception.hpp"
#include "Repository.hpp"
#include "Git2/Exception.hpp"
#include "Git2/Repository.hpp"

#include <git2/buffer.h>
#include <git2/deprecated.h>
Expand Down
2 changes: 1 addition & 1 deletion src/Git2/Exception.cc → lib/Git2/Exception.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Exception.hpp"
#include "Git2/Exception.hpp"

#include <git2/deprecated.h>
#include <git2/errors.h>
Expand Down
4 changes: 2 additions & 2 deletions src/Git2/Global.cc → lib/Git2/Global.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Global.hpp"
#include "Git2/Global.hpp"

#include "Exception.hpp"
#include "Git2/Exception.hpp"

#include <git2/global.h>

Expand Down
4 changes: 2 additions & 2 deletions src/Git2/Object.cc → lib/Git2/Object.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Object.hpp"
#include "Git2/Object.hpp"

#include "Oid.hpp"
#include "Git2/Oid.hpp"

#include <git2/object.h>

Expand Down
4 changes: 2 additions & 2 deletions src/Git2/Oid.cc → lib/Git2/Oid.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Oid.hpp"
#include "Git2/Oid.hpp"

#include "Exception.hpp"
#include "Git2/Exception.hpp"

#include <git2/deprecated.h>
#include <git2/oid.h>
Expand Down
8 changes: 4 additions & 4 deletions src/Git2/Repository.cc → lib/Git2/Repository.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Repository.hpp"
#include "Git2/Repository.hpp"

#include "Config.hpp"
#include "Exception.hpp"
#include "Oid.hpp"
#include "Git2/Config.hpp"
#include "Git2/Exception.hpp"
#include "Git2/Oid.hpp"

#include <git2/checkout.h>
#include <git2/clone.h>
Expand Down
2 changes: 1 addition & 1 deletion src/Git2/Revparse.cc → lib/Git2/Revparse.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Revparse.hpp"
#include "Git2/Revparse.hpp"

#include <git2/object.h>

Expand Down
8 changes: 4 additions & 4 deletions src/Git2/Revwalk.cc → lib/Git2/Revwalk.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Revwalk.hpp"
#include "Git2/Revwalk.hpp"

#include "Exception.hpp"
#include "Oid.hpp"
#include "Repository.hpp"
#include "Git2/Exception.hpp"
#include "Git2/Oid.hpp"
#include "Git2/Repository.hpp"

#include <git2/revwalk.h>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion src/Git2/Time.cc → lib/Git2/Time.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Time.hpp"
#include "Git2/Time.hpp"

#include <ctime>
#include <string>
Expand Down
4 changes: 2 additions & 2 deletions src/Git2/Version.cc → lib/Git2/Version.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Version.hpp"
#include "Git2/Version.hpp"

#include "Exception.hpp"
#include "Git2/Exception.hpp"

#include <fmt/format.h>
#include <git2/common.h>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Loading