#include "xmake.h" #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; namespace bp = boost::process; namespace pt = boost::property_tree; using namespace std::string_literals; namespace { const fs::path XMakefile{"XMakefile"}; void usage() { std::cout << "Usage: xmake " << 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("xmake.build.name")}; std::vector sources; for (pt::ptree::value_type &v: ptree.get_child("xmake.build")) { if (v.first == "source") sources.push_back(v.second.data()); } std::cout << "Target: " << target << std::endl; std::cout << "Sources: " << std::endl; for (auto &i: sources) { std::cout << " " << i << std::endl; } std::vector commands; // compile for (auto &i: sources) { fs::path p{i}; commands.push_back(fmt::format("g++ -std=c++17 -c {} -o {}", p.string(), p.replace_extension("o").string())); } // link std::string link_command{"g++"}; for (auto &i: sources) { link_command += fmt::format(" {}", fs::path{i}.replace_extension("o").string()); } link_command += fmt::format(" -o {}", target); commands.push_back(link_command); std::cout << "Commands: " << std::endl; for (auto &i: commands) { std::cout << " " << i << std::endl; } 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)); } } } catch (const std::exception& ex) { std::cerr << "xmake: " << ex.what() << std::endl; return 1; } return 0; }