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

80 lines
1.9 KiB

4 years ago
#include "build.h"
4 years ago
#include "cmake.hpp"
4 years ago
#include "error.h"
#include "gen.h"
4 years ago
#include <filesystem>
#include <sstream>
#include <stddef.h>
#include <stdexcept>
#include <stdlib.h>
#include <system_error>
4 years ago
namespace fs = std::filesystem;
4 years ago
namespace cmkr::build {
int run(int argc, char **argv) {
4 years ago
cmake::CMake cmake(".", true);
if (argc > 2) {
for (size_t i = 2; i < argc; ++i) {
cmake.build_args.push_back(argv[i]);
}
}
4 years ago
std::stringstream ss;
if (!fs::exists("CMakeLists.txt"))
if (gen::generate_cmake("."))
throw std::runtime_error("CMake generation failure!");
4 years ago
4 years ago
ss << "cmake -S. -B" << cmake.bin_dir << " ";
if (!cmake.generator.empty()) {
ss << "-G \"" << cmake.generator << "\" ";
4 years ago
}
if (!cmake.gen_args.empty()) {
for (const auto &arg : cmake.gen_args) {
ss << "-D" << arg << " ";
4 years ago
}
4 years ago
}
ss << "&& cmake --build " << cmake.bin_dir;
if (argc > 2) {
for (const auto &arg : cmake.build_args) {
ss << " " << arg;
4 years ago
}
}
4 years ago
4 years ago
return ::system(ss.str().c_str());
}
int clean() {
4 years ago
bool ret = false;
cmake::CMake cmake(".", true);
if (fs::exists(cmake.bin_dir)) {
ret = fs::remove_all(cmake.bin_dir);
fs::create_directory(cmake.bin_dir);
}
4 years ago
return !ret;
}
4 years ago
} // namespace cmkr::build
int cmkr_build_run(int argc, char **argv) {
4 years ago
try {
return cmkr::build::run(argc, argv);
} catch (const std::system_error &e) {
return e.code().value();
4 years ago
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::BuildError);
}
}
int cmkr_build_clean(void) {
try {
return cmkr::build::clean();
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::BuildError);
}
4 years ago
}