From fbc13a98976b22c1a7a26507780497bc83088ce7 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 29 Dec 2024 00:51:02 +0100 Subject: [PATCH] improved CLI --- Crafter.Build-Project.cpp | 89 +++++++++++++++++++++++--------------- Crafter.Build-Project.cppm | 3 ++ README.md | 19 +++----- main.cpp | 43 +++++++++++------- project.json | 24 +++++++++- 5 files changed, 112 insertions(+), 66 deletions(-) diff --git a/Crafter.Build-Project.cpp b/Crafter.Build-Project.cpp index a33a583..397f091 100644 --- a/Crafter.Build-Project.cpp +++ b/Crafter.Build-Project.cpp @@ -79,47 +79,66 @@ ConfigData CollapseConfig(nlohmann::json& configs,nlohmann::json& config) { void Project::Build(std::string configuration) { for(const Configuration& config : configurations) { if(config.name == configuration){ - if (!std::filesystem::exists(config.buildDir)) { - std::filesystem::create_directory(config.buildDir); - } else { - for (const auto& entry : std::filesystem::directory_iterator(config.buildDir)){ - std::filesystem::remove_all(entry.path()); - } - } - if (!std::filesystem::exists(config.outputDir)) { - std::filesystem::create_directory(config.outputDir); - } else { - for (const auto& entry : std::filesystem::directory_iterator(config.outputDir)){ - std::filesystem::remove_all(entry.path()); - } - } - for(const std::string& moduleFile : config.moduleFiles){ - system(std::format("clang++ -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm", config.standard, moduleFile, config.buildDir, config.buildDir, moduleFile).c_str()); - } - std::vector threads = std::vector(config.moduleFiles.size() + config.sourceFiles.size()); - std::string files; - for(std::int_fast32_t i = 0; i < config.moduleFiles.size(); i++) { - files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]); - threads[i] = std::thread([i, config](){ - system(std::format("clang++ -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o", config.standard, config.buildDir, config.moduleFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i]).c_str()); - }); - } - for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) { - files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]); - threads[config.moduleFiles.size()+i] = std::thread([i, config](){ - system(std::format("clang++ -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o", config.standard, config.sourceFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i]).c_str()); - }); - } - for(std::thread& thread : threads){ - thread.join(); - } - system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, config.outputDir, name).c_str()); + Build(config, config.outputDir); return; } } throw std::runtime_error("Configuration: " + configuration + " not found."); } +void Project::Build(std::string configuration, std::string outputDir) { + for(const Configuration& config : configurations) { + if(config.name == configuration){ + Build(config, outputDir); + return; + } + } + throw std::runtime_error("Configuration: " + configuration + " not found."); +} + +void Project::Build(Configuration configuration) { + Build(configuration, configuration.outputDir); +} + +void Project::Build(Configuration config, std::string outputDir) { + if (!std::filesystem::exists(config.buildDir)) { + std::filesystem::create_directory(config.buildDir); + } else { + for (const auto& entry : std::filesystem::directory_iterator(config.buildDir)){ + std::filesystem::remove_all(entry.path()); + } + } + if (!std::filesystem::exists(outputDir)) { + std::filesystem::create_directory(outputDir); + } else { + for (const auto& entry : std::filesystem::directory_iterator(outputDir)){ + std::filesystem::remove_all(entry.path()); + } + } + for(const std::string& moduleFile : config.moduleFiles){ + system(std::format("clang++ -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm", config.standard, moduleFile, config.buildDir, config.buildDir, moduleFile).c_str()); + } + std::vector threads = std::vector(config.moduleFiles.size() + config.sourceFiles.size()); + std::string files; + for(std::int_fast32_t i = 0; i < config.moduleFiles.size(); i++) { + files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]); + threads[i] = std::thread([i, config](){ + system(std::format("clang++ -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o", config.standard, config.buildDir, config.moduleFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i]).c_str()); + }); + } + for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) { + files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]); + threads[config.moduleFiles.size()+i] = std::thread([i, config](){ + system(std::format("clang++ -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o", config.standard, config.sourceFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i]).c_str()); + }); + } + for(std::thread& thread : threads){ + thread.join(); + } + system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, outputDir, name).c_str()); + +} + Project Project::LoadFromJSON(std::string file) { if (!std::filesystem::exists(file)) { throw std::runtime_error("Project file: " + file + " not found."); diff --git a/Crafter.Build-Project.cppm b/Crafter.Build-Project.cppm index 79c04d1..b73f38a 100644 --- a/Crafter.Build-Project.cppm +++ b/Crafter.Build-Project.cppm @@ -11,6 +11,9 @@ export namespace Crafter::Build { std::vector configurations; Project(std::string name, std::vector configurations); void Build(std::string configuration); + void Build(std::string configuration, std::string outputDir); + void Build(Configuration configuration); + void Build(Configuration configuration, std::string outputDir); void SaveToJSON(std::string path); static Project LoadFromJSON(std::string file); }; diff --git a/README.md b/README.md index 0735db3..fc8563c 100644 --- a/README.md +++ b/README.md @@ -55,22 +55,15 @@ int main() { std::println("Hello World!"); } ``` -Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``build`` folder +Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``bin`` folder ## CLI arguments -```bash -crafter-build -``` -builds the project with the specified configuration, using the defualt project filename ``project.json`` -```bash -crafter-build -``` -builds the project with the specified configuration, using the specified project filename +``--help`` Displays this help message. -```bash -crafter-build --help -``` -displays this help message +``-c`` The name of the configuration to build. +``-p`` The name of the project file. + +``-o`` Overrides the output folder. diff --git a/main.cpp b/main.cpp index 68975dc..4be2c3e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,22 +6,32 @@ import Crafter.Build; using namespace Crafter::Build; int main(int argc, char* argv[]) { - if(argc == 1){ - std::println("No configuration provided, use --help for help"); - } else if(argc == 2) { - if(std::string(argv[1]) == "--help"){ - std::println(" to build this configuration with the defualt project filename \"project.json\"\n to build this configuration with the specified project filename\nhelp displays this help message"); - } else { - Project project = Project::LoadFromJSON("./project.json"); - project.Build(argv[1]); + if(argc == 1) { + std::println("No arguments provided, use --help for help"); + } + std::string filename = "project.json"; + std::string configuration; + std::string outputDir; + for (std::uint_fast32_t i = 1; i < argc; i++) { + std::string arg = std::string(argv[i]); + if(arg == "--help"){ + std::println("--help\tDisplays this help message.\n-c The name of the configuration to build.\n-p\nThe name of the project file.\n-o Overrides the output folder.\n"); + return 0; + } else if(arg == "-c"){ + configuration = argv[++i]; + } else if(arg == "-o"){ + outputDir = argv[++i]; + } else if(arg == "-p"){ + filename = argv[++i]; + } else{ + std::println("Unkown argument: {}", argv[i]); + return 1; } - return 0; - } else if(argc == 3) { - Project project = Project::LoadFromJSON(argv[2]); - project.Build(argv[1]); - return 0; - } else { - std::println("Too many arguments provided"); - return 1; + } + Project project = Project::LoadFromJSON(filename); + if(outputDir.empty()){ + project.Build(configuration); + } else{ + project.Build(configuration, outputDir); } } diff --git a/project.json b/project.json index 1f36113..c136bf6 100644 --- a/project.json +++ b/project.json @@ -10,13 +10,33 @@ "output_dir": "./bin" }, { - "name": "debug", + "name": "executable", "extends": ["base"], + "type":"executable" + }, + { + "name": "lib", + "extends": ["base"], + "type":"lib" + }, + { + "name": "debug", + "extends": ["executable"], "optimization_level": "0" }, { "name": "release", - "extends": ["base"], + "extends": ["executable"], + "optimization_level": "3" + }, + { + "name": "debug-lib", + "extends": ["lib"], + "optimization_level": "0" + }, + { + "name": "release-lib", + "extends": ["lib"], "optimization_level": "3" } ]