Build tool
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.cc 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //#bb ldlibs := -lpthread
  2. #include <getopt.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include "BBParser.h"
  6. #include "parallel.h"
  7. #include "toolchain.h"
  8. #include "globals.h"
  9. #include "logger.h"
  10. #include "sys.h"
  11. #include "build.h"
  12. #include "compdb.h"
  13. int main(int argc, char **argv) {
  14. std::string outDir = "bbbuild";
  15. int jobs = parallel::coreCount();
  16. std::string workDir = "";
  17. std::string target = "";
  18. const char *shortopts = "hvo:j:C:cp";
  19. const struct option opts[] = {
  20. { "help", no_argument, NULL, 'h' },
  21. { "verbose", no_argument, NULL, 'v' },
  22. { "output", required_argument, NULL, 'o' },
  23. { "jobs", required_argument, NULL, 'j' },
  24. { "directory", required_argument, NULL, 'C' },
  25. { "target", required_argument, NULL, 't' },
  26. {},
  27. };
  28. const char usage[] =
  29. "Usage: bbbuild [options...] [sources]\n"
  30. "\n"
  31. " -h, --help "
  32. "Show this help text.\n"
  33. " -v, --verbose "
  34. "Show every command as it's executing.\n"
  35. " -o, --output <dir> "
  36. "Set output directory. Default: bbbuild\n"
  37. " -j, --jobs <count> "
  38. "Set the number of jobs run simultaneously. "
  39. "Default: the number of cores in the machine.\n"
  40. " -C, --directory <dir> "
  41. "Change directory before doing anything else.\n";
  42. // Parse options from argv
  43. while (1) {
  44. int optidx;
  45. int c = getopt_long(argc, argv, shortopts, opts, &optidx);
  46. if (c < 0) {
  47. break;
  48. }
  49. switch (c) {
  50. case 'h':
  51. puts(usage);
  52. return 0;
  53. case 'v':
  54. global::verbose += 1;
  55. break;
  56. case 'o':
  57. outDir = optarg;
  58. break;
  59. case 'j':
  60. jobs = atoi(optarg);
  61. if (jobs <= 0) {
  62. fprintf(stderr, "Can't run %i jobs.\n", jobs);
  63. return 1;
  64. }
  65. break;
  66. case 'C':
  67. workDir = optarg;
  68. break;
  69. default:
  70. printf("Unknown option: '%c'.\n%s", (char)c, usage);
  71. return 1;
  72. }
  73. }
  74. logger::LogContext logCtx = logger::init();
  75. parallel::ParallelContext par = parallel::init(jobs);
  76. // Change directory?
  77. if (workDir.size() > 0) {
  78. fprintf(stderr, "Entering directory '%s'\n", workDir.c_str());
  79. sys::chdir(workDir);
  80. }
  81. // Read everything related to the build configuration,
  82. // finding all source files and reading their configs
  83. std::vector<std::string> args;
  84. while (optind < argc) {
  85. args.push_back(argv[optind++]);
  86. }
  87. // TODO: Parse variables from CLI
  88. BBVariables vars;
  89. std::unique_ptr<DepNode> root = buildDepTree(outDir, vars);
  90. if (root == nullptr) {
  91. logger::log("No source files.");
  92. return 0;
  93. }
  94. {
  95. std::ofstream f(outDir + "/compile_commands.json");
  96. compdb::Writer writer(f);
  97. root->writeCompDB(outDir, writer);
  98. sys::symlink(outDir + "/compile_commands.json", "compile_commands.json");
  99. }
  100. if (root->hasChanged(outDir)) {
  101. root->build(outDir);
  102. } else {
  103. logger::log("Nothing to do.");
  104. }
  105. }