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

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