Crafter.Build/tests/IncludeWalkOutputs/main.cpp

84 lines
4.1 KiB
C++
Raw Permalink Normal View History

// 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;
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
}
// GetCompileCommand adds every directory named include under a dependency's
// path as -I. bin/ and build/ hold what builds write for the configuration
// whose path holds them, a flatpak-builder /app tree among it, whose
// include/ holds the headers of whatever that build installed. Walked, they
// would have every compile built on a library at the project's root take
// those headers before the system's, and what compiled depend on which
// builds had run. A library's tree holds the outputs of configurations
// outside the graph of the compile, too: a program in a directory below it,
// and a project of its own beside it, as Crafter.Graphics' examples are. The
// walk leaves out every bin/ and build/, whichever configuration's they are,
// and still finds the library's own include/.
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-include-walk-outputs";
std::error_code ec;
fs::remove_all(scratch, ec);
const fs::path project = scratch / "project";
for (const fs::path& dir : {
project / "include",
project / "build" / "flatpak-apps" / "net.example.App" / "build-dir" / "files" / "include",
project / "bin" / "app-variant" / "files" / "share" / "include",
project / "tool" / "build" / "flatpak-apps" / "net.example.Tool" / "build-dir" / "files" / "include",
project / "examples" / "demo" / "build" / "flatpak-apps" / "net.example.Demo" / "build-dir" / "files" / "include",
}) {
fs::create_directories(dir);
}
// A project of its own beside the library, which loads it through
// LocalProject: in no graph here, and never loaded.
std::ofstream(project / "examples" / "demo" / "project.cpp") << "// the demo's own build description\n";
// A library at the project's root and a program beside it, as this
// repository's project.cpp has crafter.build-lib and crafter.build-exe,
// and one in a directory below it.
Configuration lib;
lib.path = project;
lib.name = "lib";
lib.target = HostTarget();
lib.type = ConfigurationType::LibraryStatic;
Configuration app;
app.path = project;
app.name = "app";
app.target = HostTarget();
app.type = ConfigurationType::Executable;
app.dependencies = { &lib };
Configuration tool;
tool.path = project / "tool";
tool.name = "tool";
tool.target = HostTarget();
tool.type = ConfigurationType::Executable;
tool.dependencies = { &lib };
const std::string appFlags = GetCompileCommand(app).includeFlags;
Check(appFlags.contains(std::format(" -I{}", (project / "include").string())), std::format("the library's include/ is found: {}", appFlags));
Check(!appFlags.contains("net.example.App") && !appFlags.contains("files/share"), std::format("nothing in the project's bin/ or build/ is: {}", appFlags));
Check(!appFlags.contains("net.example.Tool") && !appFlags.contains("net.example.Demo"), std::format("nor anything in the build/ of a configuration outside the graph, a program's below the library or a project's of its own: {}", appFlags));
const std::string toolFlags = GetCompileCommand(tool).includeFlags;
Check(toolFlags.contains(std::format(" -I{}", (project / "include").string())), std::format("for the program below it too: {}", toolFlags));
Check(!toolFlags.contains("net.example.App") && !toolFlags.contains("net.example.Tool") && !toolFlags.contains("net.example.Demo") && !toolFlags.contains("files/share"), std::format("and nothing in its own build/, or anyone else's, either: {}", toolFlags));
fs::remove_all(scratch, ec);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}