Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit a756ffc358e3d76b534d5b58ca5a673c137597d2 parent e260980016948431ec706d659cd97c866a78c409 Author: sentomk <sentomk@proton.me> Date: Wed, 18 Nov 2026 23:13:20 +0800 feat: add debug, release, and asan build profiles Diffstat:
| M | kumo.toml | | | 12 | +++++++++++- |
| M | kumo_builder.cpp | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- |
2 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/kumo.toml b/kumo.toml @@ -12,7 +12,7 @@ tests = "tests" # test programs: tests/**/*_test.cpp, run by `kumoko tes # Flags for every compile command. flags = [ - "-std=c++20", "-g", "-O0", "-Wall", "-Wextra", + "-std=c++20", "-Wall", "-Wextra", "-fno-exceptions", "-fno-rtti", "-Isrc", # allows includes relative to src/, e.g. #include "core/math.h" ] @@ -37,3 +37,13 @@ link = ["-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore" # otherwise its headers still contain `try` blocks. flags = ["-D_HAS_EXCEPTIONS=0", "-D_CRT_SECURE_NO_WARNINGS"] link = ["-luser32", "-lgdi32", "-ldwmapi"] + +[debug] +flags = ["-g", "-O0"] + +[release] +flags = ["-O2", "-DNDEBUG"] + +[asan] +flags = ["-g", "-O1", "-fsanitize=address,undefined", "-fno-omit-frame-pointer"] +link = ["-fsanitize=address,undefined"] diff --git a/kumo_builder.cpp b/kumo_builder.cpp @@ -322,28 +322,33 @@ static void print_command(const Cmd& cmd) { /// - `objcxx_flags`: extra compile flags for `.mm` files only; /// - `link`: link flags. /// -/// A platform table may hold `compiler`, `flags` and `link`. On that platform -/// `compiler` overrides the top-level setting, and `flags`/`link` are appended. +/// A platform or profile table may hold `compiler`, `flags` and `link`. On that +/// platform or profile `compiler` overrides the top-level setting, and +/// `flags`/`link` are appended. /// @{ /// @brief The settings of the project, as read from #CONFIG_FILE. /// -/// Output layout, for `src = "src"`, `out = "build"` and `name = "kumo"`: +/// Output layout, for `src = "src"`, `out = "build"`, `name = "kumo"` and the +/// default `debug` profile: +// clang-format off /// @code -/// build/obj/src/foo/bar.cpp.o object file (mirrors the source tree) -/// build/obj/src/foo/bar.cpp.d list of headers that file depends on -/// build/kumo final executable (kumo.exe on Windows) +/// build/debug/obj/src/foo/bar.cpp.o object file (mirrors the source tree) +/// build/debug/obj/src/foo/bar.cpp.d list of headers that file depends on +/// build/debug/kumo final executable (kumo.exe on Windows) /// @endcode +// clang-format on struct Config { std::string compiler; fs::path src_dir; - fs::path out_dir; + fs::path clean_dir; ///< Root output directory deleted by `clean`. + fs::path out_dir; ///< Active profile directory inside #clean_dir. fs::path entry; ///< The source file with `main`, inside #src_dir. fs::path tests_dir; ///< Directory of test programs. fs::path exe; ///< The final executable, inside #out_dir. - Cmd flags; ///< Compile flags, with those of this platform added. + Cmd flags; ///< Compile flags, with platform and profile added. Cmd objcxx_flags; ///< Extra compile flags for `.mm` files. - Cmd link_flags; ///< Link flags, with those of this platform added. + Cmd link_flags; ///< Link flags, with platform and profile added. }; /// @brief Reports a problem with #CONFIG_FILE and exits. @@ -359,6 +364,8 @@ struct Config { /// @brief The tables a platform can have, named like #HOST_NAME. static constexpr const char* PLATFORM_TABLES[] = {"mac", "linux", "windows"}; +/// @brief The profile tables supported in `kumo.toml`. +static constexpr const char* PROFILE_TABLES[] = {"debug", "release", "asan"}; /// @brief One `key = value` pair of the file. struct Entry { @@ -473,8 +480,13 @@ class ConfigParser { end_line(); // Checked here rather than with the keys, so that a misspelled table with // nothing in it is reported as well. - if (std::find(std::begin(PLATFORM_TABLES), std::end(PLATFORM_TABLES), - name) == std::end(PLATFORM_TABLES)) { + const bool known_platform = + std::find(std::begin(PLATFORM_TABLES), std::end(PLATFORM_TABLES), + name) != std::end(PLATFORM_TABLES); + const bool known_profile = + std::find(std::begin(PROFILE_TABLES), std::end(PROFILE_TABLES), name) != + std::end(PROFILE_TABLES); + if (!known_platform && !known_profile) { config_error(line_here(), "unknown table '[" + name + "]'"); } return name; @@ -586,7 +598,7 @@ static bool is_within(const fs::path& inner, const fs::path& outer) { /// the builder does not know. /// /// @return The settings, with the table of the current platform merged in. -static Config load_config() { +static Config load_config(const std::string& profile) { std::ifstream file(CONFIG_FILE); if (!file) { config_error(0, "cannot read it; run the builder from the project root"); @@ -625,7 +637,8 @@ static Config load_config() { take_string("name", fs::current_path().filename().string()); cfg.compiler = take_string("compiler", "clang++"); cfg.src_dir = take_string("src", "src"); - cfg.out_dir = take_string("out", "build"); + cfg.clean_dir = take_string("out", "build"); + cfg.out_dir = cfg.clean_dir / profile; cfg.entry = take_string("entry", (cfg.src_dir / "main.cpp").string()); cfg.tests_dir = take_string("tests", "tests"); cfg.flags = take_list("flags"); @@ -646,6 +659,18 @@ static Config load_config() { } } + for (const char* name_p : PROFILE_TABLES) { + const std::string section = name_p; + std::string compiler = take_string(section + ".compiler", ""); + Cmd flags = take_list(section + ".flags"); + Cmd link = take_list(section + ".link"); + if (section == profile) { + if (!compiler.empty()) cfg.compiler = compiler; + cfg.flags.insert(cfg.flags.end(), flags.begin(), flags.end()); + cfg.link_flags.insert(cfg.link_flags.end(), link.begin(), link.end()); + } + } + // Report the unknown key that was written first. const Entry* first = nullptr; std::string first_key; @@ -670,13 +695,13 @@ static Config load_config() { "'src' must be a relative path inside the project, and " "not the project directory itself"); } - if (!is_inside_project(cfg.out_dir)) { + if (!is_inside_project(cfg.clean_dir)) { config_error(0, "'out' must be a relative path inside the project, and " "not the project directory itself"); } - if (is_within(cfg.out_dir, cfg.src_dir) || - is_within(cfg.src_dir, cfg.out_dir)) { + if (is_within(cfg.clean_dir, cfg.src_dir) || + is_within(cfg.src_dir, cfg.clean_dir)) { config_error(0, "'out' and 'src' must not be the same directory or " "contain one another"); @@ -690,7 +715,7 @@ static Config load_config() { "'tests' must be a relative path inside the project, and " "not the project directory itself"); } - for (const fs::path* other : {&cfg.src_dir, &cfg.out_dir}) { + for (const fs::path* other : {&cfg.src_dir, &cfg.clean_dir}) { if (is_within(cfg.tests_dir, *other) || is_within(*other, cfg.tests_dir)) { config_error(0, "'tests' must not be the same directory as 'src' or 'out', " @@ -1472,6 +1497,9 @@ Commands: help Show this text Options: + --debug Build with debug symbols and no optimization (default) + --release Build with optimizations enabled + --asan Build with Address and UndefinedBehavior sanitizers -v, --verbose Print full commands -q, --quiet With test: one line per test program, not one per test case -h, --help Show this text @@ -1486,6 +1514,7 @@ on a terminal; set NO_COLOR to turn that off. struct Args { std::string command = "build"; std::string pattern; ///< For `test`: part of the name of the tests to run. + std::string profile = "debug"; ///< Build profile: debug, release, or asan. }; /// @brief Reads the command line. @@ -1500,12 +1529,19 @@ static Args parse_args(int argc, char** argv) { std::exit(2); }; + Args args; std::vector<std::string> words; for (int i = 1; i < argc; ++i) { const std::string arg = argv[i]; if (arg == "-h" || arg == "--help") { print_usage(stdout); std::exit(0); + } else if (arg == "--debug") { + args.profile = "debug"; + } else if (arg == "--release") { + args.profile = "release"; + } else if (arg == "--asan") { + args.profile = "asan"; } else if (arg == "-v" || arg == "--verbose") { verbose = true; } else if (arg == "-q" || arg == "--quiet") { @@ -1517,7 +1553,6 @@ static Args parse_args(int argc, char** argv) { } } - Args args; if (!words.empty()) args.command = words[0]; if (args.command == "help") { print_usage(stdout); @@ -1609,17 +1644,17 @@ int main(int argc, char** argv) { init_terminal(); rebuild_self(argc, argv); const Args args = parse_args(argc, argv); - const Config cfg = load_config(); + const Config cfg = load_config(args.profile); const Clock::time_point began = Clock::now(); if (args.command == "clean") { - const std::uintmax_t removed = fs::remove_all(cfg.out_dir); + const std::uintmax_t removed = fs::remove_all(cfg.clean_dir); if (removed > 0) { log_step(Style::Cyan, "clean", - "removed " + cfg.out_dir.generic_string() + "/"); + "removed " + cfg.clean_dir.generic_string() + "/"); } else { log_step(Style::Cyan, "clean", - "nothing to remove, " + cfg.out_dir.generic_string() + + "nothing to remove, " + cfg.clean_dir.generic_string() + "/ does not exist"); } return 0;
