add clean command

self-hosting
MoAlyousef 4 years ago
parent 77c328d51f
commit 606d942ca0

@ -90,6 +90,7 @@ arguments:
init [exe|shared|static] Starts a new project in the same directory. init [exe|shared|static] Starts a new project in the same directory.
gen Generates CMakeLists.txt file. gen Generates CMakeLists.txt file.
build <extra cmake args> Run cmake and build. build <extra cmake args> Run cmake and build.
clean Clean the build directory.
help Show help. help Show help.
version Current cmkr version. version Current cmkr version.
``` ```

@ -6,12 +6,16 @@ namespace cmkr::build {
int run(int argc, char **argv); int run(int argc, char **argv);
int clean();
} // namespace cmkr::build } // namespace cmkr::build
extern "C" { extern "C" {
#endif #endif
int cmkr_build_run(int argc, char **argv); int cmkr_build_run(int argc, char **argv);
int cmkr_build_clean(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -39,6 +39,11 @@ const char *handle_args(int argc, char **argv) {
if (ret) if (ret)
return "CMake build error!"; return "CMake build error!";
return "CMake run completed!"; return "CMake run completed!";
} else if (main_arg == "clean") {
auto ret = build::clean();
if (ret)
return "CMake clean error!";
return "CMake run completed!";
} else { } else {
return "Unknown argument!"; return "Unknown argument!";
} }

@ -10,6 +10,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <system_error> #include <system_error>
namespace fs = std::filesystem;
namespace cmkr::build { namespace cmkr::build {
int run(int argc, char **argv) { int run(int argc, char **argv) {
@ -21,7 +23,7 @@ int run(int argc, char **argv) {
} }
std::stringstream ss; std::stringstream ss;
if (!std::filesystem::exists("CMakeLists.txt")) if (!fs::exists("CMakeLists.txt"))
if (gen::generate_cmake(".")) if (gen::generate_cmake("."))
throw std::runtime_error("CMake generation failure!"); throw std::runtime_error("CMake generation failure!");
@ -45,6 +47,16 @@ int run(int argc, char **argv) {
return ::system(ss.str().c_str()); 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 } // namespace cmkr::build
int cmkr_build_run(int argc, char **argv) { int cmkr_build_run(int argc, char **argv) {
@ -56,3 +68,13 @@ int cmkr_build_run(int argc, char **argv) {
return cmkr::error::Status(cmkr::error::Status::Code::BuildError); 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);
}
}

@ -11,6 +11,7 @@ arguments:
init [exe|shared|static] Starts a new project in the same directory. init [exe|shared|static] Starts a new project in the same directory.
gen Generates CMakeLists.txt file. gen Generates CMakeLists.txt file.
build <extra cmake args> Run cmake and build. build <extra cmake args> Run cmake and build.
clean Clean the build directory.
help Show help. help Show help.
version Current cmkr version. version Current cmkr version.
)lit"; )lit";

Loading…
Cancel
Save