From 6f42f40b47bcec1d4263767a03547d9ab203fc93 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 27 May 2022 09:34:50 +0200 Subject: Use built-in execute command for moc creation. And RAII for path changing. --- ctor.cc | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/ctor.cc b/ctor.cc index 2f4cb14..d4d4e28 100644 --- a/ctor.cc +++ b/ctor.cc @@ -3,8 +3,33 @@ #include #include +#include +#include + +int execute(const std::string& command, + const std::vector& args, + bool verbose = true); + namespace { +class PathRAII +{ +public: + PathRAII(const std::filesystem::path& path) + { + pwd = std::filesystem::current_path(); + std::filesystem::current_path(path); + } + + ~PathRAII() + { + std::filesystem::current_path(pwd); + } + +private: + std::filesystem::path pwd; +}; + std::vector eval_mocs(const std::string& path, const std::vector>& moc_list) { @@ -26,13 +51,11 @@ std::vector eval_mocs(const std::string& path, std::filesystem::last_write_time(path / input) > std::filesystem::last_write_time(path / output)) { - std::string cmd = "cd \"" + path + "\"; moc "; - cmd += "-o \"" + output.string() + "\" \"" + input.string() + "\""; - std::cerr << cmd << "\n"; - auto ret = system(cmd.data()); + PathRAII p(path); + auto ret = execute("/usr/bin/moc", {"-o", output.string(), input.string()}); if(ret != 0) { - std::cerr << "moc generation failed: " << cmd << '\n'; + std::cerr << "moc generation failed\n"; exit(ret); } } -- cgit v1.2.3