94 lines
3.8 KiB
C++
94 lines
3.8 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Crafter.Build;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
using namespace Crafter;
|
||
|
|
|
||
|
|
// A project of three configurations: a library, a program built on it, and
|
||
|
|
// a tool that shares nothing with either. The library and the tool each
|
||
|
|
// declare a test and a script. --no-tool leaves the tool out, which leaves
|
||
|
|
// one executable for -r to run. --packaged packages both programs as
|
||
|
|
// Flatpaks, for install and bundle to choose between, and makes them wasm
|
||
|
|
// builds, which a build refuses to package wherever the test runs: nothing
|
||
|
|
// is ever exported, let alone installed. --twin-test has the library
|
||
|
|
// declare the tool's test as well, which a test run refuses to build.
|
||
|
|
// --twin adds a fourth configuration of the tool's name and path, which
|
||
|
|
// would be built into the tool's directory.
|
||
|
|
namespace {
|
||
|
|
// One new set per call, at addresses that never change: whoever loads
|
||
|
|
// this file keeps the pointers it returns.
|
||
|
|
std::deque<Configuration> Owned;
|
||
|
|
|
||
|
|
// Leaves `<script>.ran` in the project's directory, holding the name of
|
||
|
|
// the configuration the script was handed.
|
||
|
|
std::int32_t Stamp(const ScriptContext& ctx, std::string_view script) {
|
||
|
|
std::ofstream(ctx.projectDir / std::format("{}.ran", script)) << ctx.project.name;
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args) {
|
||
|
|
Configuration& greeter = Owned.emplace_back();
|
||
|
|
greeter.path = "./";
|
||
|
|
greeter.name = "greeter";
|
||
|
|
greeter.outputName = "greeter";
|
||
|
|
greeter.type = ConfigurationType::LibraryStatic;
|
||
|
|
{
|
||
|
|
std::array<fs::path, 1> interfaces = { "greeter/Greeter" };
|
||
|
|
std::array<fs::path, 0> implementations = {};
|
||
|
|
greeter.GetInterfacesAndImplementations(interfaces, implementations);
|
||
|
|
}
|
||
|
|
greeter.AddTest("GreeterTest").Dependencies({ &greeter });
|
||
|
|
if (std::ranges::contains(args, std::string_view("--twin-test"))) greeter.AddTest("ToolTest");
|
||
|
|
greeter.scripts.push_back({"greet", [](ScriptContext& ctx) { return Stamp(ctx, "greet"); }});
|
||
|
|
|
||
|
|
Configuration& hello = Owned.emplace_back();
|
||
|
|
hello.path = "./";
|
||
|
|
hello.name = "hello";
|
||
|
|
hello.outputName = "hello";
|
||
|
|
hello.type = ConfigurationType::Executable;
|
||
|
|
hello.dependencies = { &greeter };
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> interfaces = {};
|
||
|
|
std::array<fs::path, 1> implementations = { "hello/main" };
|
||
|
|
hello.GetInterfacesAndImplementations(interfaces, implementations);
|
||
|
|
}
|
||
|
|
|
||
|
|
const bool packaged = std::ranges::contains(args, std::string_view("--packaged"));
|
||
|
|
if (packaged) {
|
||
|
|
hello.packaging = Packaging::Flatpak;
|
||
|
|
hello.target = "wasm32-wasip1";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (std::ranges::contains(args, std::string_view("--no-tool"))) return { &greeter, &hello };
|
||
|
|
|
||
|
|
Configuration& tool = Owned.emplace_back();
|
||
|
|
tool.path = "./";
|
||
|
|
tool.name = "tool";
|
||
|
|
tool.outputName = "tool";
|
||
|
|
tool.type = ConfigurationType::Executable;
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> interfaces = {};
|
||
|
|
std::array<fs::path, 1> implementations = { "tool/main" };
|
||
|
|
tool.GetInterfacesAndImplementations(interfaces, implementations);
|
||
|
|
}
|
||
|
|
tool.AddTest("ToolTest");
|
||
|
|
tool.scripts.push_back({"stamp", [](ScriptContext& ctx) { return Stamp(ctx, "stamp"); }});
|
||
|
|
if (packaged) {
|
||
|
|
tool.packaging = Packaging::Flatpak;
|
||
|
|
tool.target = "wasm32-wasip1";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (std::ranges::contains(args, std::string_view("--twin"))) {
|
||
|
|
Configuration& twin = Owned.emplace_back();
|
||
|
|
twin.path = "./";
|
||
|
|
twin.name = "tool";
|
||
|
|
twin.outputName = "tool-twin";
|
||
|
|
twin.type = ConfigurationType::Executable;
|
||
|
|
return { &greeter, &hello, &tool, &twin };
|
||
|
|
}
|
||
|
|
|
||
|
|
return { &greeter, &hello, &tool };
|
||
|
|
}
|