[M3] add codegen::cmake_lists

This commit is contained in:
2026-05-08 12:44:49 +00:00
parent 384fbf6ac4
commit 4380c5181c
6 changed files with 581 additions and 0 deletions

289
src/codegen/cmake.cpp Normal file
View File

@@ -0,0 +1,289 @@
module cargoxx.codegen;
import std;
import cargoxx.manifest;
import cargoxx.layout;
import cargoxx.linkdb;
import cargoxx.lockfile;
namespace cargoxx::codegen {
namespace {
namespace fs = std::filesystem;
auto edition_to_int(manifest::Edition e) -> int {
switch (e) {
case manifest::Edition::Cpp20:
return 20;
case manifest::Edition::Cpp23:
return 23;
case manifest::Edition::Cpp26:
return 26;
}
return 23;
}
auto rel_to_build(const fs::path& p, const fs::path& project_root) -> std::string {
return (fs::path{".."} / p.lexically_relative(project_root)).generic_string();
}
auto collect_dep_targets(const std::vector<linkdb::Recipe>& recipes)
-> std::vector<std::string> {
std::vector<std::string> out;
for (const auto& r : recipes) {
for (const auto& t : r.targets) {
out.push_back(t);
}
}
return out;
}
auto link_block(std::string_view target_name, std::string_view visibility,
bool include_lib, std::string_view package_name,
const std::vector<std::string>& dep_targets) -> std::string {
if (!include_lib && dep_targets.empty()) {
return "";
}
std::string out = std::format("target_link_libraries({} {}\n", target_name, visibility);
if (include_lib) {
out += std::format(" {}\n", package_name);
}
for (const auto& t : dep_targets) {
out += std::format(" {}\n", t);
}
out += ")\n";
return out;
}
auto emit_header(const manifest::Manifest& m) -> std::string {
return std::format(
"cmake_minimum_required(VERSION 3.30)\n"
"project({} LANGUAGES CXX)\n"
"\n"
"# Generated by cargoxx — do not edit.\n"
"# Source of truth: ../Cargoxx.toml\n"
"\n"
"# ----- toolchain configuration -----\n"
"set(CMAKE_CXX_STANDARD {})\n"
"set(CMAKE_CXX_STANDARD_REQUIRED ON)\n"
"set(CMAKE_CXX_EXTENSIONS OFF)\n"
"set(CMAKE_CXX_SCAN_FOR_MODULES ON)\n"
"set(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n",
m.package.name, edition_to_int(m.package.edition));
}
auto emit_find_packages(const std::vector<linkdb::Recipe>& recipes) -> std::string {
if (recipes.empty()) {
return {};
}
std::string out = "\n# ----- dependencies -----\n";
for (const auto& r : recipes) {
out += std::format("find_package({})\n", r.find_package);
}
return out;
}
auto emit_library(const layout::Target& lib, const std::string& package_name,
const std::vector<linkdb::Recipe>& recipes,
const fs::path& project_root) -> std::string {
std::string out = "\n# ----- library target -----\n";
out += std::format("add_library({} STATIC)\n", package_name);
out += std::format("target_sources({}\n", package_name);
out += " PUBLIC\n";
out += " FILE_SET CXX_MODULES FILES\n";
for (const auto& m : lib.module_units) {
out += std::format(" {}\n", rel_to_build(m, project_root));
}
if (!lib.additional_sources.empty()) {
out += " PRIVATE\n";
for (const auto& s : lib.additional_sources) {
out += std::format(" {}\n", rel_to_build(s, project_root));
}
}
out += ")\n";
out += link_block(package_name, "PUBLIC", false, package_name,
collect_dep_targets(recipes));
return out;
}
auto emit_primary_binary(const layout::Target& bin, const std::string& package_name,
const std::vector<linkdb::Recipe>& recipes, bool has_lib,
const fs::path& project_root) -> std::string {
std::string out = "\n# ----- binary target -----\n";
out += std::format("add_executable({}_bin {})\n", package_name,
rel_to_build(bin.entry, project_root));
out += std::format("set_target_properties({}_bin PROPERTIES OUTPUT_NAME {})\n",
package_name, package_name);
out += link_block(std::format("{}_bin", package_name), "PRIVATE", has_lib, package_name,
collect_dep_targets(recipes));
return out;
}
auto emit_extra_binary(const layout::Target& bin, const std::string& package_name,
const std::vector<linkdb::Recipe>& recipes, bool has_lib,
const fs::path& project_root) -> std::string {
std::string out = std::format("\n# ----- binary target: {} -----\n", bin.name);
out += std::format("add_executable({} {})\n", bin.name,
rel_to_build(bin.entry, project_root));
out += link_block(bin.name, "PRIVATE", has_lib, package_name,
collect_dep_targets(recipes));
return out;
}
auto emit_test(const layout::Target& t, const std::string& package_name,
const std::vector<linkdb::Recipe>& recipes, bool has_lib,
const fs::path& project_root) -> std::string {
auto target = std::format("test_{}", t.name);
std::string out = std::format("add_executable({} {})\n", target,
rel_to_build(t.entry, project_root));
out += link_block(target, "PRIVATE", has_lib, package_name,
collect_dep_targets(recipes));
out += std::format("add_test(NAME {} COMMAND {})\n", t.name, target);
return out;
}
auto emit_example(const layout::Target& e, const std::string& package_name,
const std::vector<linkdb::Recipe>& recipes, bool has_lib,
const fs::path& project_root) -> std::string {
auto target = std::format("example_{}", e.name);
std::string out = std::format("add_executable({} {})\n", target,
rel_to_build(e.entry, project_root));
out += link_block(target, "PRIVATE", has_lib, package_name,
collect_dep_targets(recipes));
return out;
}
auto find_primary_bin(const layout::DiscoveredLayout& layout) -> const layout::Target* {
for (const auto& b : layout.binaries) {
if (b.entry.filename() == "main.cpp" &&
b.entry.parent_path().filename() == "src") {
return &b;
}
}
return nullptr;
}
auto collect_all_target_names(const layout::DiscoveredLayout& layout,
const std::string& package_name) -> std::vector<std::string> {
std::vector<std::string> out;
if (layout.library) {
out.push_back(package_name);
}
if (find_primary_bin(layout) != nullptr) {
out.push_back(std::format("{}_bin", package_name));
}
for (const auto& b : layout.binaries) {
if (b.entry.filename() == "main.cpp" &&
b.entry.parent_path().filename() == "src") {
continue;
}
out.push_back(b.name);
}
for (const auto& t : layout.tests) {
out.push_back(std::format("test_{}", t.name));
}
for (const auto& e : layout.examples) {
out.push_back(std::format("example_{}", e.name));
}
return out;
}
auto emit_build_flags(const manifest::BuildSettings& build,
const layout::DiscoveredLayout& layout,
const std::string& package_name) -> std::string {
if (!build.warnings_as_errors && build.sanitizers.empty()) {
return {};
}
auto targets = collect_all_target_names(layout, package_name);
if (targets.empty()) {
return {};
}
std::string targets_list;
for (std::size_t i = 0; i < targets.size(); ++i) {
if (i > 0) {
targets_list += ' ';
}
targets_list += targets[i];
}
std::string out = "\n# ----- build flags from [build] section -----\n";
if (build.warnings_as_errors) {
out += std::format("foreach(target_name IN ITEMS {})\n", targets_list);
out += " target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wpedantic "
"-Werror)\n";
out += "endforeach()\n";
}
if (!build.sanitizers.empty()) {
std::string san_list;
for (std::size_t i = 0; i < build.sanitizers.size(); ++i) {
if (i > 0) {
san_list += ',';
}
san_list += build.sanitizers[i];
}
out += std::format("foreach(target_name IN ITEMS {})\n", targets_list);
out += std::format(" target_compile_options(${{target_name}} PRIVATE "
"-fsanitize={})\n",
san_list);
out += std::format(" target_link_options(${{target_name}} PRIVATE -fsanitize={})\n",
san_list);
out += "endforeach()\n";
}
return out;
}
} // namespace
auto cmake_lists(const GenerateInputs& in) -> std::string {
const auto& pkg_name = in.manifest.package.name;
const bool has_lib = in.layout.library.has_value();
std::string out;
out += emit_header(in.manifest);
out += emit_find_packages(in.recipes);
if (in.layout.library) {
out += emit_library(*in.layout.library, pkg_name, in.recipes, in.project_root);
}
const auto* primary = find_primary_bin(in.layout);
if (primary) {
out += emit_primary_binary(*primary, pkg_name, in.recipes, has_lib, in.project_root);
}
bool has_extra_bin = false;
for (const auto& b : in.layout.binaries) {
if (&b == primary) {
continue;
}
if (!has_extra_bin) {
has_extra_bin = true;
}
out += emit_extra_binary(b, pkg_name, in.recipes, has_lib, in.project_root);
}
if (!in.layout.tests.empty()) {
out += "\n# ----- tests -----\nenable_testing()\n";
for (const auto& t : in.layout.tests) {
out += emit_test(t, pkg_name, in.recipes, has_lib, in.project_root);
}
}
if (!in.layout.examples.empty()) {
out += "\n# ----- examples -----\n";
for (const auto& e : in.layout.examples) {
out += emit_example(e, pkg_name, in.recipes, has_lib, in.project_root);
}
}
out += emit_build_flags(in.manifest.build, in.layout, pkg_name);
return out;
}
} // namespace cargoxx::codegen