From 606d942ca01651686aa17a1f25f60a5a62384e11 Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Sat, 12 Sep 2020 18:08:36 +0300 Subject: [PATCH] add clean command --- README.md | 1 + include/build.h | 4 ++++ src/args.cpp | 5 +++++ src/build.cpp | 24 +++++++++++++++++++++++- src/help.cpp | 1 + 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d745bd8..8db3645 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ arguments: init [exe|shared|static] Starts a new project in the same directory. gen Generates CMakeLists.txt file. build Run cmake and build. + clean Clean the build directory. help Show help. version Current cmkr version. ``` diff --git a/include/build.h b/include/build.h index ba0f22f..40787b9 100644 --- a/include/build.h +++ b/include/build.h @@ -6,12 +6,16 @@ namespace cmkr::build { int run(int argc, char **argv); +int clean(); + } // namespace cmkr::build extern "C" { #endif int cmkr_build_run(int argc, char **argv); +int cmkr_build_clean(void); + #ifdef __cplusplus } #endif diff --git a/src/args.cpp b/src/args.cpp index ffb8002..a482eaf 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -39,6 +39,11 @@ const char *handle_args(int argc, char **argv) { if (ret) return "CMake build error!"; return "CMake run completed!"; + } else if (main_arg == "clean") { + auto ret = build::clean(); + if (ret) + return "CMake clean error!"; + return "CMake run completed!"; } else { return "Unknown argument!"; } diff --git a/src/build.cpp b/src/build.cpp index 89025e6..a8aaa81 100644 --- a/src/build.cpp +++ b/src/build.cpp @@ -10,6 +10,8 @@ #include #include +namespace fs = std::filesystem; + namespace cmkr::build { int run(int argc, char **argv) { @@ -21,7 +23,7 @@ int run(int argc, char **argv) { } std::stringstream ss; - if (!std::filesystem::exists("CMakeLists.txt")) + if (!fs::exists("CMakeLists.txt")) if (gen::generate_cmake(".")) throw std::runtime_error("CMake generation failure!"); @@ -45,6 +47,16 @@ int run(int argc, char **argv) { return ::system(ss.str().c_str()); } +int clean() { + int ret = 0; + cmake::CMake cmake(".", true); + if (fs::exists(cmake.bin_dir)) { + ret = fs::remove_all(cmake.bin_dir); + fs::create_directory(cmake.bin_dir); + } + return ret; +} + } // namespace cmkr::build int cmkr_build_run(int argc, char **argv) { @@ -55,4 +67,14 @@ int cmkr_build_run(int argc, char **argv) { } 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); + } } \ No newline at end of file diff --git a/src/help.cpp b/src/help.cpp index b7bc8e1..bbf744b 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -11,6 +11,7 @@ arguments: init [exe|shared|static] Starts a new project in the same directory. gen Generates CMakeLists.txt file. build Run cmake and build. + clean Clean the build directory. help Show help. version Current cmkr version. )lit";