Nostro/project.cpp
2026-11-01 18:35:23 +01:00

257 lines
10 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
#pragma crafter dependency "https://forgejo.catcrafts.net/Catcrafts/Crafter.LintRules.git"
import Crafter.LintRules;
namespace fs = std::filesystem;
using namespace Crafter;
// Nostro.Checkout: what a shop app links to have nostrod pay an order, as a library of its own that needs no bank code,
// OpenSSL, Crafter.Network or Crafter.DB, only libsystemd. `--product=checkout` builds it alone; nostrod takes its names
// from it.
static Configuration CheckoutLibrary(std::span<const std::string_view> args, const std::vector<std::string>& compileFlags) {
Configuration checkout;
checkout.path = "./";
checkout.name = "nostro-checkout";
checkout.outputName = "nostro-checkout";
checkout.type = ConfigurationType::LibraryStatic;
checkout.linkFlags.push_back("-lsystemd");
ApplyStandardArgs(checkout, args);
checkout.compileFlags.insert(checkout.compileFlags.end(), compileFlags.begin(), compileFlags.end());
std::array<fs::path, 1> interfaces = {"interfaces/Nostro.Checkout"};
std::array<fs::path, 1> implementations = {"implementations/Nostro.Checkout"};
checkout.GetInterfacesAndImplementations(interfaces, implementations);
LintRules::AddLiteRules(checkout);
return checkout;
}
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
constexpr std::string_view bankArgName = "--banks=";
bool lib = false;
bool dbusRequested = false;
bool bunqEnabled = false;
bool coverage = false;
std::string profileRuntime;
constexpr std::string_view profileRuntimeArgName = "--profile-runtime=";
// What a build makes: the CLI by default, "daemon" for nostrod, or "checkout" for the checkout client library.
std::string product;
constexpr std::string_view productArgName = "--product=";
std::vector<std::string> depArgs;
Configuration libcfg;
for(std::string_view arg : args) {
if(arg == "--lib") {
lib = true;
} else if(arg == "--dbus") {
dbusRequested = true;
} else if(arg.starts_with(productArgName)) {
product = arg.substr(productArgName.size());
} else if(arg == "--coverage") {
coverage = true;
} else if(arg.starts_with(profileRuntimeArgName)) {
profileRuntime = arg.substr(profileRuntimeArgName.size());
} else if(arg.starts_with(bankArgName)) {
for(std::ranges::subrange<std::string_view::iterator> part : std::views::split(arg.substr(bankArgName.size()), ',')) {
std::string_view name(part.begin(), part.end());
if(name == "bunq") {
bunqEnabled = true;
libcfg.defines.push_back({"NOSTRO_BUNQ_ENABLED"});
} else {
throw std::runtime_error(std::format("unknown connector '{}' in {}", name, bankArgName));
}
}
} else {
depArgs.emplace_back(arg);
}
}
// --coverage builds the library, the CLI and the tests with clang's source-based coverage, and every process writes
// its counts where LLVM_PROFILE_FILE says. The flags go to clang's front end so the driver does not link a profile
// runtime by itself: --profile-runtime= names compiler-rt's libclang_rt.profile archive for a toolchain without one.
std::vector<std::string> coverageCompileFlags;
std::vector<std::string> coverageLinkFlags;
if(coverage) {
coverageCompileFlags = {"-Xclang", "-fprofile-instrument=clang", "-Xclang", "-fcoverage-mapping"};
if(profileRuntime.empty()) {
coverageLinkFlags = {"-fprofile-instr-generate"};
} else {
coverageLinkFlags = {profileRuntime, "-Wl,-u,__llvm_profile_runtime"};
}
}
libcfg.compileFlags.insert(libcfg.compileFlags.end(), coverageCompileFlags.begin(), coverageCompileFlags.end());
if(product == "checkout") {
return CheckoutLibrary(args, coverageCompileFlags);
}
if(!product.empty() && product != "daemon") {
throw std::runtime_error(std::format("unknown product '{}' in {}", product, productArgName));
}
if(!bunqEnabled) {
throw std::runtime_error("must enable atleast one bank");
}
// The crypto partition is only needed by banks that sign their requests.
const bool opensslEnabled = bunqEnabled;
if(opensslEnabled) {
libcfg.defines.push_back({"NOSTRO_OPENSSL_ENABLED"});
}
// nostrod and the CLI talk over D-Bus (sd-bus, from libsystemd). A consumer building the library alone gets that part
// only when it asks with --dbus, so the library does not make every consumer depend on libsystemd. The wire carries
// bunq's listing types, so it needs bunq.
const bool dbusEnabled = (!lib || dbusRequested) && bunqEnabled;
if(dbusEnabled) {
libcfg.defines.push_back({"NOSTRO_DBUS_ENABLED"});
}
Configuration* checkout = new Configuration(CheckoutLibrary(args, coverageCompileFlags));
// nostro uses Crafter.Network's HTTP/1.1 client alone: --no-quic leaves out msquic, its CMake build, and the runpath
// into the building user's cache that a binary linked against it carries.
std::vector<std::string> networkArgs = depArgs;
networkArgs.emplace_back("--no-quic");
Configuration* network = GitProject({
.source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Network.git" },
.args = networkArgs,
});
Configuration* db = GitProject({
.source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.DB.git" },
.args = depArgs,
});
libcfg.path = "./";
libcfg.name = "nostro";
libcfg.outputName = "nostro";
libcfg.type = ConfigurationType::LibraryStatic;
libcfg.dependencies = {network, db};
if(dbusEnabled) {
libcfg.dependencies.push_back(checkout);
}
if(opensslEnabled) {
libcfg.linkFlags.push_back("-lssl");
libcfg.linkFlags.push_back("-lcrypto");
}
if(dbusEnabled) {
libcfg.linkFlags.push_back("-lsystemd");
}
ApplyStandardArgs(libcfg, args);
std::vector<fs::path> libInterfaces = {
"interfaces/Nostro",
"interfaces/Nostro-Connector",
"interfaces/Nostro-Transaction",
"interfaces/Nostro-Account",
"interfaces/Nostro-Banks",
"interfaces/Nostro-Bunq",
"interfaces/Nostro-Crypto",
"interfaces/Nostro-Currency",
"interfaces/Nostro-File",
"interfaces/Nostro-Utils",
"interfaces/Nostro-Bus",
"interfaces/Nostro-Client",
"interfaces/Nostro-Daemon",
};
std::vector<fs::path> libImplementations = {
"implementations/Nostro-Account",
"implementations/Nostro-File",
"implementations/Nostro-Connector"
};
if(bunqEnabled) {
libImplementations.push_back("implementations/Nostro-Bunq");
libImplementations.push_back("implementations/Nostro-Bunq-Payments");
}
if(opensslEnabled) {
libImplementations.push_back("implementations/Nostro-Crypto");
}
if(dbusEnabled) {
libImplementations.push_back("implementations/Nostro-Bus");
libImplementations.push_back("implementations/Nostro-Client");
libImplementations.push_back("implementations/Nostro-Daemon");
libImplementations.push_back("implementations/Nostro-Daemon-Worker");
}
libcfg.GetInterfacesAndImplementations(libInterfaces, libImplementations);
LintRules::AddLiteRules(libcfg);
if(lib) {
return libcfg;
}
// Both executables switch on the same bank and D-Bus flags as the library, and share the one library: two
// configurations exporting the same module in one build are an error.
const auto defines = libcfg.defines;
Configuration* library = new Configuration(std::move(libcfg));
// nostrod: the daemon that holds the connectors and answers the CLI over the system bus.
if(product == "daemon") {
Configuration daemon;
daemon.path = "./";
daemon.name = "nostrod";
daemon.outputName = "nostrod";
daemon.defines = defines;
ApplyStandardArgs(daemon, args);
daemon.compileFlags.insert(daemon.compileFlags.end(), coverageCompileFlags.begin(), coverageCompileFlags.end());
daemon.linkFlags.insert(daemon.linkFlags.end(), coverageLinkFlags.begin(), coverageLinkFlags.end());
daemon.dependencies = { library };
std::array<fs::path, 0> daemonInterfaces = {};
std::array<fs::path, 1> daemonImplementations = {"implementations/nostrod"};
daemon.GetInterfacesAndImplementations(daemonInterfaces, daemonImplementations);
LintRules::AddLiteRules(daemon);
return daemon;
}
Configuration execfg;
execfg.path = "./";
execfg.name = "nostro";
execfg.outputName = "nostro";
execfg.defines = defines;
ApplyStandardArgs(execfg, args);
std::array<fs::path, 0> exeInterfaces = {
};
std::array<fs::path, 1> exeImplementations = {
"implementations/main"
};
execfg.compileFlags.insert(execfg.compileFlags.end(), coverageCompileFlags.begin(), coverageCompileFlags.end());
execfg.linkFlags.insert(execfg.linkFlags.end(), coverageLinkFlags.begin(), coverageLinkFlags.end());
execfg.dependencies = { library };
execfg.GetInterfacesAndImplementations(exeInterfaces, exeImplementations);
// `crafter-build test --banks=bunq`: each test is tests/<Name>/main.cpp, linked against the library. The ones that
// talk to bunq talk to a fake of it on 127.0.0.1 (tests/Support/FakeBunq.inc), never to bunq itself.
constexpr std::array<std::string_view, 9> tests = {
"ShouldHandleMoneyAndText",
"ShouldSignAndVerify",
"ShouldStoreTransactions",
"ShouldKeepFilesPrivate",
"ShouldSaveConnectors",
"ShouldTalkToBunq",
"ShouldMoveMoney",
"ShouldRunCommands",
"ShouldServeEveryUser",
};
for(const std::string_view name : tests) {
TestBuilder test = execfg.AddTest(name).Dependencies({ library, network, db, checkout }).Timeout(std::chrono::seconds(180));
for(const std::string& flag : coverageCompileFlags) {
test.CompileFlag(flag);
}
for(const std::string& flag : coverageLinkFlags) {
test.LinkFlag(flag);
}
}
LintRules::AddLiteRules(execfg);
return execfg;
}