add cmake args

self-hosting
MoAlyousef 4 years ago
parent 11b3061d47
commit 6035d3ae21

@ -9,6 +9,7 @@ set(CMKRLIB_SOURCES
"src/args.cpp" "src/args.cpp"
"src/gen.cpp" "src/gen.cpp"
"src/help.cpp" "src/help.cpp"
"src/build.cpp"
) )
add_library(cmkrlib STATIC ${CMKRLIB_SOURCES}) add_library(cmkrlib STATIC ${CMKRLIB_SOURCES})

@ -40,7 +40,7 @@ version = "0.1.0"
[[bin]] [[bin]]
name = "cmkrlib" name = "cmkrlib"
type = "static" 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"] include-dirs = ["vendor"]
features = ["cxx_std_17"] features = ["cxx_std_17"]
@ -59,15 +59,18 @@ cpp-flags = [] # optional
c-flags = [] # optional c-flags = [] # optional
link-flags = [] # optional link-flags = [] # optional
subdirs = [] # optional subdirs = [] # optional
bin-dir = "bin" # optional
generator = "Ninja" # optional
arguments = ["CMAKE_TOOLCHAIN_FILE=/path/to/toolchain"] # optional
[project] # required per project [project] # required per project
name = "app" # required name = "app" # required
version = "0.1.0" # 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 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 toml11 = { git = "https://github.com/ToruNiina/toml11", git-tag = "v3.5.0" } # optional
[[bin]] # required, can define several binaries [[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: 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 ## Roadmap

@ -8,7 +8,7 @@ version = "0.1.0"
[[bin]] [[bin]]
name = "cmkrlib" name = "cmkrlib"
type = "static" 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"] include-dirs = ["vendor"]
features = ["cxx_std_17"] features = ["cxx_std_17"]

@ -1,11 +1,10 @@
#include "args.h" #include "args.h"
#include "build.h"
#include "gen.h" #include "gen.h"
#include "help.h" #include "help.h"
#include <filesystem> #include <filesystem>
#include <stddef.h>
#include <stdexcept> #include <stdexcept>
#include <stdlib.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -31,14 +30,7 @@ const char *handle_args(int argc, char **argv) {
cmkr::gen::generate_project(args[2].c_str()); cmkr::gen::generate_project(args[2].c_str());
return "Directory initialized!"; return "Directory initialized!";
} else if (main_arg == "build") { } else if (main_arg == "build") {
std::string command = "cmake -S. -Bbin "; auto ret = build::run(argc, argv);
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());
if (ret) if (ret)
return "Run faced an error!"; return "Run faced an error!";
return "Run completed!"; return "Run completed!";

@ -0,0 +1,51 @@
#include "build.h"
#include <filesystem>
#include <sstream>
#include <stddef.h>
#include <stdexcept>
#include <stdlib.h>
#include <string>
#include <toml.hpp>
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);
}

@ -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

@ -127,9 +127,9 @@ void generate_cmake(const char *path) {
ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n"; ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n";
} }
if (toml.contains("find")) { if (toml.contains("find-package")) {
std::map<std::string, std::string> deps = std::map<std::string, std::string> deps =
toml::find<std::map<std::string, std::string>>(toml, "find"); toml::find<std::map<std::string, std::string>>(toml, "find-package");
for (const auto &dep : deps) { for (const auto &dep : deps) {
ss << "find_package(" << dep.first; ss << "find_package(" << dep.first;
if (dep.second != "*") { if (dep.second != "*") {
@ -140,9 +140,9 @@ void generate_cmake(const char *path) {
} }
} }
if (toml.contains("fetch")) { if (toml.contains("fetch-content")) {
std::map<std::string, std::map<std::string, std::string>> deps = std::map<std::string, std::map<std::string, std::string>> deps =
toml::find<std::map<std::string, std::map<std::string, std::string>>>(toml, "fetch"); toml::find<std::map<std::string, std::map<std::string, std::string>>>(toml, "fetch-content");
ss << "include(FetchContent)\n\n"; ss << "include(FetchContent)\n\n";
for (const auto &dep : deps) { for (const auto &dep : deps) {
ss << "FetchContent_Declare(\n\t" << dep.first << "\n"; ss << "FetchContent_Declare(\n\t" << dep.first << "\n";

Loading…
Cancel
Save