You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cmkr/src/build.cpp

56 lines
1.5 KiB

4 years ago
#include "build.h"
4 years ago
#include "error.h"
4 years ago
#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) {
4 years ago
try {
return cmkr::build::run(argc, argv);
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::BuildError);
}
4 years ago
}