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.0KB

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