diff options
author | Roland Reichwein <mail@reichwein.it> | 2024-05-01 16:42:30 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2024-05-01 16:42:30 +0200 |
commit | 9149c5fcd7dbb4417f089a4e225dd1463a86cc00 (patch) | |
tree | 08890c61982684df71f9f2a6e7ad8615ce4bb214 | |
parent | f60348f8b7513882b9a51019665614e787efa86e (diff) |
Implemented clean
-rw-r--r-- | xmake.cpp | 118 |
1 files changed, 80 insertions, 38 deletions
@@ -26,46 +26,34 @@ namespace { { std::cout << "Usage: xmake <target>" << std::endl; } -} - -int xmake(int argc, char* argv[]) -{ - try { - pt::ptree ptree; - pt::read_xml(XMakefile, ptree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); - - std::string action{"default"}; - - if (argc == 1) { // default action - } else if (argc == 2) { - action = argv[1]; - } else { - std::cerr << "Invalid arguments." << std::endl; - usage; - exit(1); - } - - if (action == "default"s || action == "build") { // build - } else if (action == "clean") { - } else if (action == "install") { - } else if (action == "rebuild") { - } else if (action == "run") { - } else { - std::cerr << "Invalid action: " << action << std::endl; - usage; - exit(1); - } - - // TODO: clearlist - // TODO: dependencies - // TODO: buildlist - std::string target{ptree.get<std::string>("xmake.build.name")}; + std::string get_target(pt::ptree& ptree) { + return ptree.get<std::string>("xmake.build.name"); + } + + std::vector<std::string> get_sources(pt::ptree& ptree) { std::vector<std::string> sources; for (pt::ptree::value_type &v: ptree.get_child("xmake.build")) { if (v.first == "source") sources.push_back(v.second.data()); } + return sources; + } + + std::vector<std::string> get_objects(pt::ptree& ptree) { + std::vector<std::string> objects{get_sources(ptree)}; + for (auto &i: objects) { + fs::path p{i}; + p.replace_extension("o"); + i = p.string(); + } + return objects; + } + + void build(pt::ptree& ptree) { + std::string target{get_target(ptree)}; + + std::vector<std::string> sources{get_sources(ptree)}; std::cout << "Target: " << target << std::endl; @@ -84,10 +72,9 @@ int xmake(int argc, char* argv[]) } // link std::string link_command{"g++"}; - for (auto &i: sources) { - fs::path p_obj{i}; - p_obj.replace_extension("o"); - link_command += fmt::format(" {}", p_obj.string()); + std::vector<std::string> objects{get_objects(ptree)}; + for (auto &i: objects) { + link_command += fmt::format(" {}", i); } link_command += " -lfmt"; link_command += fmt::format(" -o {}", target); @@ -106,6 +93,61 @@ int xmake(int argc, char* argv[]) throw std::runtime_error(fmt::format("Error {}", result)); } } + } + + void clean(pt::ptree& ptree) { + std::vector<std::string> cleanlist{get_objects(ptree)}; + cleanlist.push_back(get_target(ptree)); + + std::vector<std::string> commands; + for (auto &i: cleanlist) { + commands.push_back(fmt::format("rm -f {}", i)); + } + + std::cout << "Running commands: " << std::endl; + for (auto &i: commands) { + std::cout << i << std::endl; + int result{system(i.c_str())}; + if (result != 0) { + throw std::runtime_error(fmt::format("Error {}", result)); + } + } + } +} + +int xmake(int argc, char* argv[]) +{ + try { + pt::ptree ptree; + pt::read_xml(XMakefile, ptree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + + std::string action{"default"}; + + if (argc == 1) { // default action + } else if (argc == 2) { + action = argv[1]; + } else { + std::cerr << "Invalid arguments." << std::endl; + usage; + exit(1); + } + + if (action == "default"s || action == "build") { // build + build(ptree); + } else if (action == "clean") { + clean(ptree); + } else if (action == "install") { + throw std::runtime_error("unimplemented"); + } else if (action == "rebuild") { + throw std::runtime_error("unimplemented"); + } else if (action == "run") { + throw std::runtime_error("unimplemented"); + } else { + std::cerr << "Invalid action: " << action << std::endl; + usage; + exit(1); + } + } catch (const std::exception& ex) { std::cerr << "xmake: " << ex.what() << std::endl; |