Build tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <getopt.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <utility>
  8. #include <stdexcept>
  9. #include "BXParser.h"
  10. #include "parallel.h"
  11. #include "toolchain.h"
  12. #include "globals.h"
  13. #include "logger.h"
  14. #include "sys.h"
  15. #include "build.h"
  16. #include "compdb.h"
  17. struct Conf {
  18. std::vector<std::string> args;
  19. std::vector<std::string> args2;
  20. std::vector<std::pair<std::string, std::string>> kvargs;
  21. bool exec = false;
  22. };
  23. static void run(Conf conf) {
  24. std::string op;
  25. std::string path;
  26. if (conf.args.size() == 0) {
  27. op = "build";
  28. path = "./bx-out";
  29. } else if (conf.args[0][0] == '.' || conf.args[0][0] == '/') {
  30. op = "build";
  31. path = conf.args[0];
  32. } else if (conf.args.size() == 1) {
  33. op = conf.args[0];
  34. path = "./bx-out";
  35. } else if (conf.args.size() == 2) {
  36. op = conf.args[0];
  37. path = conf.args[1];
  38. } else {
  39. // TODO: Print usage instead?
  40. throw std::runtime_error("Incorrect number of arguments");
  41. }
  42. sys::mkdirp(path);
  43. auto buildVariables = [&]() -> BXVariables {
  44. BXVariables variables;
  45. if (sys::fileExists(path + "/.config.bx")) {
  46. bufio::IFStream f(path + "/.config.bx");
  47. BXParser parser(f);
  48. parser.parse(variables);
  49. }
  50. for (auto &pair: conf.kvargs) {
  51. bufio::ISStream ss(pair.second);
  52. BXParser parser(ss);
  53. auto &list = variables[pair.first];
  54. list.clear();
  55. parser.parseList(variables, list);
  56. }
  57. if (conf.kvargs.size() > 0) {
  58. bufio::OFStream f(path + "/.config.bx");
  59. BXWriter w(f);
  60. w.write(variables);
  61. }
  62. return variables;
  63. };
  64. auto buildTree = [&](BXVariables vars) -> std::unique_ptr<DepNode> {
  65. return build::buildDepTree(path, std::move(vars));
  66. };
  67. auto buildCompileCommands = [&](DepNode &root) {
  68. bufio::OFStream f(path + "/compile_commands.json");
  69. compdb::Writer w(f);
  70. root.writeCompDB(path, w);
  71. sys::symlink(path + "/compile_commands.json", "compile_commands.json");
  72. };
  73. if (op == "build") {
  74. auto vars = buildVariables();
  75. std::string targetName = build::findTargetName(vars);
  76. auto root = buildTree(vars);
  77. buildCompileCommands(*root);
  78. if (root->hasChanged(path)) {
  79. root->startBuild(path);
  80. root->joinBuild();
  81. } else {
  82. logger::log("Nothing to do.");
  83. }
  84. if (conf.exec) {
  85. std::vector<std::string> argv;
  86. argv.reserve(conf.args2.size() + 1);
  87. argv.push_back(path + '/' + targetName);
  88. for (const auto &arg: conf.args2) {
  89. argv.push_back(std::move(arg));
  90. }
  91. try {
  92. sys::ProcConf conf;
  93. conf.forwardSignals = true;
  94. sys::execute(argv, conf);
  95. } catch (std::exception &ex) {
  96. logger::log(ex.what());
  97. }
  98. }
  99. } else if (op == "config") {
  100. BXVariables variables = buildVariables();
  101. printf("%s:\n", (path + "/.config.bx").c_str());
  102. if (variables.size() == 0) {
  103. printf("No config options set.\n");
  104. }
  105. for (auto &pair: variables) {
  106. printf("%s:", pair.first.c_str());
  107. for (auto &val: pair.second) {
  108. printf(" %s", val.c_str());
  109. }
  110. printf("\n");
  111. }
  112. } else if (op == "compile-commands") {
  113. buildCompileCommands(*buildTree(buildVariables()));
  114. } else if (op == "clean") {
  115. // TODO: Remove what's needed, instead of removing everything and
  116. // re-creating .config.bx
  117. BXVariables variables = buildVariables();
  118. sys::rmrf(path);
  119. sys::rmrf("compile_commands.json");
  120. sys::mkdirp(path);
  121. bufio::OFStream f(path + "/.config.bx");
  122. BXWriter w(f);
  123. w.write(variables);
  124. } else {
  125. throw std::runtime_error("Unknown operation '" + op + "'");
  126. }
  127. }
  128. int main(int argc, char **argv) {
  129. int jobs = parallel::coreCount() * 1.2 + 2;
  130. Conf conf;
  131. std::string workDir = "";
  132. std::string target = "";
  133. const char *shortopts = "hvj:C:";
  134. const struct option opts[] = {
  135. { "help", no_argument, NULL, 'h' },
  136. { "verbose", no_argument, NULL, 'v' },
  137. { "jobs", required_argument, NULL, 'j' },
  138. { "directory", required_argument, NULL, 'C' },
  139. { "run", no_argument, NULL, 'R' },
  140. {},
  141. };
  142. const char usage[] =
  143. "Usage: %s [options...] [build | config | compile-commands | clean] [path]\n"
  144. "\n"
  145. " -h, --help "
  146. "Show this help text.\n"
  147. " -v, --verbose "
  148. "Show every command as it's executing.\n"
  149. " -j, --jobs <count> "
  150. "Set the number of jobs run simultaneously. "
  151. "Default: the number of cores in the machine.\n"
  152. " -C, --directory <dir> "
  153. "Change directory before doing anything else.\n"
  154. " -R, --run <args...> "
  155. "Run executable after building.\n";
  156. // Parse options from argv
  157. bool parsingOpts = true;
  158. while (parsingOpts) {
  159. int optidx;
  160. int c = getopt_long(argc, argv, shortopts, opts, &optidx);
  161. if (c < 0) {
  162. break;
  163. }
  164. switch (c) {
  165. case 'h':
  166. printf(usage, argv[0]);
  167. return 0;
  168. case 'v':
  169. global::verbose += 1;
  170. break;
  171. case 'j':
  172. jobs = atoi(optarg);
  173. if (jobs <= 0) {
  174. fprintf(stderr, "Can't run %i jobs.\n", jobs);
  175. return 1;
  176. }
  177. break;
  178. case 'C':
  179. workDir = optarg;
  180. break;
  181. case 'R':
  182. conf.exec = true;
  183. parsingOpts = false;
  184. for (; optind < argc; ++optind) {
  185. conf.args2.push_back(argv[optind]);
  186. }
  187. break;
  188. default:
  189. printf("Unknown option: '%c'.\n", (char)c);
  190. printf(usage, argv[0]);
  191. return 1;
  192. }
  193. }
  194. parallel::ParallelContext par = parallel::init(jobs);
  195. // Change directory?
  196. if (workDir.size() > 0) {
  197. fprintf(stderr, "Entering directory '%s'\n", workDir.c_str());
  198. sys::chdir(workDir);
  199. }
  200. // Find args and keyword args
  201. for (; optind < argc; ++optind) {
  202. char *arg = argv[optind++];
  203. char *eq = strchr(arg, '=');
  204. if (eq == nullptr) {
  205. conf.args.push_back(arg);
  206. } else {
  207. conf.kvargs.push_back(std::make_pair(
  208. std::string(arg, eq - arg), std::string(eq + 1)));
  209. }
  210. }
  211. run(std::move(conf));
  212. }