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.

build.cc 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "build.h"
  2. #include <fstream>
  3. #include <sstream>
  4. #include <mutex>
  5. #include <unordered_set>
  6. #include "sys.h"
  7. #include "parallel.h"
  8. #include "toolchain.h"
  9. #include "logger.h"
  10. namespace build {
  11. static void findSourcesInDir(std::string dir, std::vector<SourceFile> &sources,
  12. BBParser::Variables vars);
  13. static void findSources(std::string dir, std::string name,
  14. std::vector<SourceFile> &sources, BBParser::Variables vars) {
  15. std::string path = dir + "/" + name;
  16. sys::FileInfo finfo = sys::fileInfo(path);
  17. if (finfo.isDir) {
  18. // We don't want to send 'files'
  19. BBParser::Variables subvars = vars;
  20. subvars.erase("files");
  21. findSourcesInDir(path, sources, std::move(subvars));
  22. } else {
  23. SourceFile::FileType type = SourceFile::fileTypeFrom(name);
  24. if (type != SourceFile::FileType::UNKNOWN) {
  25. sources.emplace_back(dir, name, type, vars);
  26. }
  27. }
  28. }
  29. static void findSourcesInDir(std::string dir, std::vector<SourceFile> &sources,
  30. BBParser::Variables vars) {
  31. std::ifstream buildFile(dir + "/build.bb");
  32. std::vector<std::string> files;
  33. bool hasFiles = false;
  34. // Parse $dir/build.bb, see if it specifies a 'files' value.
  35. if (buildFile.good()) {
  36. BBParser parser(buildFile, BBParser::FLAG_NONE);
  37. parser.parse(vars);
  38. auto it = vars.find("files");
  39. if (it != vars.end()) {
  40. files = it->second;
  41. hasFiles = true;
  42. }
  43. }
  44. // If build.bb didn't specify 'files', we have to readdir
  45. if (!hasFiles) {
  46. sys::readDir(dir, files);
  47. }
  48. // Go through files
  49. for (auto &ent: files) {
  50. std::string path = dir + "/" + ent;
  51. BBParser::Variables subvars = vars;
  52. findSources(dir, ent, sources, vars);
  53. }
  54. }
  55. static std::string findTargetName(const BuildConf &conf) {
  56. auto it = conf.variables.find("target");
  57. if (it != conf.variables.end() && it->second.size() != 0) {
  58. return it->second[0];
  59. }
  60. for (auto &source: conf.sources) {
  61. const std::vector<std::string> *ptr = source.variable("target");
  62. if (ptr != nullptr && ptr->size() != 0) {
  63. return (*ptr)[0];
  64. }
  65. }
  66. return "target";
  67. }
  68. BuildConf readBuildConf(std::string outDir, int numJobs, const std::vector<std::string> &args) {
  69. BuildConf conf;
  70. conf.outDir = outDir;
  71. conf.numJobs = numJobs;
  72. // Read config from file
  73. if (sys::fileExists("build.bb")) {
  74. std::ifstream stream("build.bb");
  75. BBParser parser(stream, BBParser::FLAG_NONE);
  76. parser.parse(conf.variables);
  77. }
  78. // Read variables from args
  79. for (auto &arg: args) {
  80. std::istringstream stream(arg);
  81. BBParser parser(stream, BBParser::FLAG_ONE_LINE);
  82. parser.parse(conf.variables);
  83. }
  84. // Find source dirs
  85. std::vector<std::string> sourceDirs;
  86. {
  87. auto it = conf.variables.find("files");
  88. if (it == conf.variables.end()) {
  89. sys::readDir(".", sourceDirs);
  90. } else {
  91. sourceDirs = it->second;
  92. }
  93. }
  94. // Read configs from source dirs
  95. for (std::string &dir: sourceDirs) {
  96. findSources(".", dir, conf.sources, conf.variables);
  97. }
  98. conf.targetName = findTargetName(conf);
  99. return conf;
  100. }
  101. bool compile(const BuildConf &conf) {
  102. bool compiled = false;
  103. std::mutex mut;
  104. parallel::parallel(conf.numJobs, conf.sources, [&](const SourceFile &source) {
  105. if (source.needsRecompile(conf.outDir)) {
  106. source.compile(conf.outDir);
  107. mut.lock();
  108. compiled = true;
  109. mut.unlock();
  110. }
  111. });
  112. return compiled;
  113. }
  114. void link(const BuildConf &conf, toolchain::TargetType targetType) {
  115. logger::log("Link " + conf.outDir + '/' + conf.targetName);
  116. std::vector<std::string> ldFlags;
  117. std::unordered_set<std::string> ldFlagsSet;
  118. std::vector<std::string> ldLibs;
  119. std::unordered_set<std::string> ldLibsSet;
  120. std::vector<std::string> objects;
  121. SourceFile::FileType type = SourceFile::FileType::C;
  122. // Gather ldflags, ldlibs and objects from sources
  123. std::vector<std::string> flags;
  124. for (auto &source: conf.sources) {
  125. if (source.type() != SourceFile::FileType::C) {
  126. type = source.type();
  127. }
  128. flags = source.ldFlags();
  129. for (auto &flag: flags) {
  130. if (flag[0] == '-' && flag[1] == 'l') {
  131. std::cerr << "Warning: -l flags should go in ldlibs "
  132. << "(at " << source.path() << ": " << flag << ")\n";
  133. }
  134. auto it = ldFlagsSet.find(flag);
  135. if (it == ldFlagsSet.end()) {
  136. ldFlagsSet.emplace(flag);
  137. ldFlags.push_back(std::move(flag));
  138. }
  139. }
  140. flags = source.ldLibs();
  141. for (auto &flag: flags) {
  142. auto it = ldLibsSet.find(flag);
  143. if (it == ldLibsSet.end()) {
  144. ldLibsSet.emplace(flag);
  145. ldLibs.push_back(std::move(flag));
  146. }
  147. }
  148. objects.push_back(std::move(source.objectPath(conf.outDir)));
  149. }
  150. flags.clear();
  151. toolchain::link(conf.targetName, ldFlags, ldLibs, type, targetType, objects, conf.outDir);
  152. }
  153. void writeCompileCommands(const BuildConf &conf) {
  154. sys::mkdirp(conf.outDir);
  155. std::string cwd = sys::cwd();
  156. std::ofstream os(conf.outDir + "/compile_commands.json");
  157. os << "[\n\t";
  158. bool first = true;
  159. for (auto &source: conf.sources) {
  160. std::string command;
  161. for (auto arg: source.compileCommand(conf.outDir)) {
  162. command += arg + ' ';
  163. }
  164. command.pop_back();
  165. if (!first) {
  166. os << ", ";
  167. }
  168. os << "{\n"
  169. << "\t\t\"directory\": \"" << cwd << "\"\n"
  170. << "\t\t\"command\": \"" << command << "\"\n"
  171. << "\t\t\"file\": \"" << source.path() << "\"\n"
  172. << "\t}";
  173. first = false;
  174. }
  175. os << "\n]\n";
  176. sys::symlink(conf.outDir + "/compile_commands.json", "compile_commands.json");
  177. }
  178. }