From 6035d3ae214611b8874b262889982b2d823964ca Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Thu, 10 Sep 2020 03:01:31 +0300 Subject: [PATCH] add cmake args --- CMakeLists.txt | 1 + README.md | 11 +++++++---- cmake.toml | 2 +- src/args.cpp | 12 ++---------- src/build.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/build.h | 17 +++++++++++++++++ src/gen.cpp | 8 ++++---- 7 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 src/build.cpp create mode 100644 src/build.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 23c18a4..cb5bc65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMKRLIB_SOURCES "src/args.cpp" "src/gen.cpp" "src/help.cpp" + "src/build.cpp" ) add_library(cmkrlib STATIC ${CMKRLIB_SOURCES}) diff --git a/README.md b/README.md index f86d136..2dcd494 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ version = "0.1.0" [[bin]] name = "cmkrlib" type = "static" -sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] +sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp", "src/build.cpp"] include-dirs = ["vendor"] features = ["cxx_std_17"] @@ -59,15 +59,18 @@ cpp-flags = [] # optional c-flags = [] # optional link-flags = [] # optional subdirs = [] # optional +bin-dir = "bin" # optional +generator = "Ninja" # optional +arguments = ["CMAKE_TOOLCHAIN_FILE=/path/to/toolchain"] # optional [project] # required per project name = "app" # required version = "0.1.0" # required -[find] # optional, runs find_package, use "*" to ignore version +[find-package] # optional, runs find_package, use "*" to ignore version boost = "1.74.0" # optional -[fetch] # optional, runs fetchContent +[fetch-content] # optional, runs fetchContent toml11 = { git = "https://github.com/ToruNiina/toml11", git-tag = "v3.5.0" } # optional [[bin]] # required, can define several binaries @@ -92,7 +95,7 @@ arguments: ``` The build command invokes cmake and the default build-system on your platform, it also accepts extra cmake arguments: ``` -cmkr build -GNinja -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain -DCMAKE_BUILD_TYPE=Release +cmkr build --config Release ``` ## Roadmap diff --git a/cmake.toml b/cmake.toml index 9ac386e..d6446c6 100644 --- a/cmake.toml +++ b/cmake.toml @@ -8,7 +8,7 @@ version = "0.1.0" [[bin]] name = "cmkrlib" type = "static" -sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] +sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp", "src/build.cpp"] include-dirs = ["vendor"] features = ["cxx_std_17"] diff --git a/src/args.cpp b/src/args.cpp index 56ea515..0337068 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -1,11 +1,10 @@ #include "args.h" +#include "build.h" #include "gen.h" #include "help.h" #include -#include #include -#include #include #include @@ -31,14 +30,7 @@ const char *handle_args(int argc, char **argv) { cmkr::gen::generate_project(args[2].c_str()); return "Directory initialized!"; } else if (main_arg == "build") { - std::string command = "cmake -S. -Bbin "; - if (args.size() > 2) { - for (size_t i = 2; i < args.size(); ++i) { - command += args[i] + " "; - } - } - command += "&& cmake --build bin"; - auto ret = system(command.c_str()); + auto ret = build::run(argc, argv); if (ret) return "Run faced an error!"; return "Run completed!"; diff --git a/src/build.cpp b/src/build.cpp new file mode 100644 index 0000000..3ecbf90 --- /dev/null +++ b/src/build.cpp @@ -0,0 +1,51 @@ +#include "build.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace cmkr::build { + +int run(int argc, char **argv) { + std::stringstream ss; + std::string bin_dir = "bin"; + if (!std::filesystem::exists("CMakeLists.txt")) + throw std::runtime_error("No valid CMakeLists.txt found!"); + + const auto toml = toml::parse("cmake.toml"); + if (toml.contains("cmake")) { + const auto &cmake = toml::find(toml, "cmake"); + ss << "cmake -S. -B"; + if (cmake.contains("bin-dir")) { + bin_dir = toml::find(cmake, "bin-dir").as_string(); + } + ss << bin_dir << " "; + if (cmake.contains("generator")) { + const auto gen = toml::find(cmake, "generator").as_string(); + ss << "-G " << gen << " "; + } + if (cmake.contains("arguments")) { + const auto args = toml::find(cmake, "arguments").as_array(); + for (const auto &arg : args) { + ss << "-D" << arg << " "; + } + } + ss << "&& cmake --build " << bin_dir; + if (argc > 2) { + for (size_t i = 2; i < argc; ++i) { + ss << " " << argv[i]; + } + } + } + return ::system(ss.str().c_str()); +} + +} // namespace cmkr::build + +int cmkr_build_run(int argc, char **argv) { + return cmkr::build::run(argc, argv); +} \ No newline at end of file diff --git a/src/build.h b/src/build.h new file mode 100644 index 0000000..ba0f22f --- /dev/null +++ b/src/build.h @@ -0,0 +1,17 @@ +#pragma once + +#ifdef __cplusplus + +namespace cmkr::build { + +int run(int argc, char **argv); + +} // namespace cmkr::build +extern "C" { +#endif + +int cmkr_build_run(int argc, char **argv); + +#ifdef __cplusplus +} +#endif diff --git a/src/gen.cpp b/src/gen.cpp index 11f2293..d6870b4 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -127,9 +127,9 @@ void generate_cmake(const char *path) { ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n"; } - if (toml.contains("find")) { + if (toml.contains("find-package")) { std::map deps = - toml::find>(toml, "find"); + toml::find>(toml, "find-package"); for (const auto &dep : deps) { ss << "find_package(" << dep.first; if (dep.second != "*") { @@ -140,9 +140,9 @@ void generate_cmake(const char *path) { } } - if (toml.contains("fetch")) { + if (toml.contains("fetch-content")) { std::map> deps = - toml::find>>(toml, "fetch"); + toml::find>>(toml, "fetch-content"); ss << "include(FetchContent)\n\n"; for (const auto &dep : deps) { ss << "FetchContent_Declare(\n\t" << dep.first << "\n";