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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //#bb ldlibs := -lpthread
  2. #include <getopt.h>
  3. #include <string.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include "BBParser.h"
  8. #include "parallel.h"
  9. #include "toolchain.h"
  10. #include "globals.h"
  11. #include "logger.h"
  12. #include "sys.h"
  13. #include "build.h"
  14. #include "compdb.h"
  15. int main(int argc, char **argv) {
  16. std::string outDir = "bbbuild";
  17. int jobs = parallel::coreCount() * 1.2 + 2;
  18. std::string workDir = "";
  19. std::string target = "";
  20. const char *shortopts = "hvo:j:C:cp";
  21. const struct option opts[] = {
  22. { "help", no_argument, NULL, 'h' },
  23. { "verbose", no_argument, NULL, 'v' },
  24. { "output", required_argument, NULL, 'o' },
  25. { "jobs", required_argument, NULL, 'j' },
  26. { "directory", required_argument, NULL, 'C' },
  27. { "target", required_argument, NULL, 't' },
  28. {},
  29. };
  30. const char usage[] =
  31. "Usage: bbbuild [options...] [sources]\n"
  32. "\n"
  33. " -h, --help "
  34. "Show this help text.\n"
  35. " -v, --verbose "
  36. "Show every command as it's executing.\n"
  37. " -o, --output <dir> "
  38. "Set output directory. Default: bbbuild\n"
  39. " -j, --jobs <count> "
  40. "Set the number of jobs run simultaneously. "
  41. "Default: the number of cores in the machine.\n"
  42. " -C, --directory <dir> "
  43. "Change directory before doing anything else.\n";
  44. // Parse options from argv
  45. while (1) {
  46. int optidx;
  47. int c = getopt_long(argc, argv, shortopts, opts, &optidx);
  48. if (c < 0) {
  49. break;
  50. }
  51. switch (c) {
  52. case 'h':
  53. puts(usage);
  54. return 0;
  55. case 'v':
  56. global::verbose += 1;
  57. break;
  58. case 'o':
  59. outDir = optarg;
  60. break;
  61. case 'j':
  62. jobs = atoi(optarg);
  63. if (jobs <= 0) {
  64. fprintf(stderr, "Can't run %i jobs.\n", jobs);
  65. return 1;
  66. }
  67. break;
  68. case 'C':
  69. workDir = optarg;
  70. break;
  71. default:
  72. printf("Unknown option: '%c'.\n%s", (char)c, usage);
  73. return 1;
  74. }
  75. }
  76. logger::LogContext logCtx = logger::init();
  77. parallel::ParallelContext par = parallel::init(jobs);
  78. // Change directory?
  79. if (workDir.size() > 0) {
  80. fprintf(stderr, "Entering directory '%s'\n", workDir.c_str());
  81. sys::chdir(workDir);
  82. }
  83. // Read in variables from conf if it exists
  84. BBVariables variables;
  85. if (sys::fileExists(outDir + "/.config.bb")) {
  86. std::ifstream f = sys::ifstream(outDir + "/.config.bb");
  87. BBParser parser(f, BBParser::FLAG_NONE);
  88. parser.parse(variables);
  89. }
  90. // Read non-option arguments (variable definitions and args)
  91. bool varsChanged = false;
  92. std::vector<std::string> args;
  93. while (optind < argc) {
  94. char *arg = argv[optind++];
  95. char *eq = strchr(arg, '=');
  96. if (eq == nullptr) {
  97. args.push_back(arg);
  98. } else {
  99. varsChanged = true;
  100. std::stringstream val(eq + 1);
  101. BBParser parser(val, BBParser::FLAG_NONE);
  102. std::string key(arg, eq - arg);
  103. std::vector<std::string> &list = variables[key];
  104. list.clear();
  105. parser.parseList(variables, list);
  106. }
  107. }
  108. // If our variables changed, write out the new ones
  109. if (varsChanged) {
  110. std::ofstream f = sys::ofstream(outDir + "/.config.bb");
  111. BBWriter w(f);
  112. w.write(variables);
  113. }
  114. // Build dependency graph
  115. std::unique_ptr<DepNode> root = buildDepTree(outDir, variables);
  116. if (root == nullptr) {
  117. logger::log("No source files.");
  118. return 0;
  119. }
  120. sys::mkdirp(outDir);
  121. // Build compile commands
  122. {
  123. std::ofstream f = sys::ofstream(outDir + "/compile_commands.json");
  124. compdb::Writer writer(f);
  125. root->writeCompDB(outDir, writer);
  126. sys::symlink(outDir + "/compile_commands.json", "compile_commands.json");
  127. }
  128. // Build the project
  129. if (root->hasChanged(outDir)) {
  130. root->startBuild(outDir);
  131. root->joinBuild();
  132. } else {
  133. logger::log("Nothing to do.");
  134. }
  135. }