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

98 lines
2.5 KiB

#include "build.hpp"
#include "cmake_generator.hpp"
#include "error.hpp"
#include "project_parser.hpp"
4 years ago
#include "fs.hpp"
3 years ago
#include <cstddef>
#include <cstdlib>
#include <sstream>
#include <stdexcept>
#include <system_error>
4 years ago
4 years ago
namespace cmkr {
namespace build {
4 years ago
int run(int argc, char **argv) {
parser::Project project(nullptr, ".", true);
4 years ago
if (argc > 2) {
for (int i = 2; i < argc; ++i) {
project.build_args.push_back(argv[i]);
4 years ago
}
}
4 years ago
std::stringstream ss;
if (gen::generate_cmake(fs::current_path().string().c_str()))
throw std::runtime_error("CMake generation failure!");
4 years ago
ss << "cmake -S. -DCMKR_BUILD_SKIP_GENERATION=ON -B" << project.build_dir << " ";
4 years ago
if (!project.generator.empty()) {
ss << "-G \"" << project.generator << "\" ";
4 years ago
}
if (!project.config.empty()) {
ss << "-DCMAKE_BUILD_TYPE=" << project.config << " ";
4 years ago
}
if (!project.gen_args.empty()) {
for (const auto &arg : project.gen_args) {
4 years ago
ss << "-D" << arg << " ";
4 years ago
}
4 years ago
}
ss << "&& cmake --build " << project.build_dir << " --parallel";
4 years ago
if (argc > 2) {
for (const auto &arg : project.build_args) {
4 years ago
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;
parser::Project project(nullptr, ".", true);
if (fs::exists(project.build_dir)) {
ret = fs::remove_all(project.build_dir);
fs::create_directory(project.build_dir);
}
4 years ago
return !ret;
}
4 years ago
int install() {
parser::Project project(nullptr, ".", false);
auto cmd = "cmake --install " + project.build_dir;
4 years ago
return ::system(cmd.c_str());
}
4 years ago
} // namespace build
} // namespace cmkr
4 years ago
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 (...) {
4 years ago
return cmkr::error::Status(cmkr::error::Status::Code::CleanError);
}
}
int cmkr_build_install(void) {
try {
return cmkr::build::install();
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::InstallError);
}
3 years ago
}