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.

toolchain.cc 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "toolchain.h"
  2. #include <stdexcept>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "sys.h"
  7. #include "globals.h"
  8. namespace toolchain {
  9. static const char *getPkgConfig() {
  10. static const char *pkgConfig = nullptr;
  11. if (pkgConfig != nullptr) {
  12. return pkgConfig;
  13. }
  14. pkgConfig = getenv("PKG_CONFIG");
  15. if (pkgConfig != nullptr) {
  16. return pkgConfig;
  17. }
  18. return (pkgConfig = "pkg-config");
  19. }
  20. static const char *getCCompiler() {
  21. static const char *cCompiler = nullptr;
  22. if (cCompiler != nullptr) {
  23. return cCompiler;
  24. }
  25. cCompiler = getenv("CC");
  26. if (cCompiler != nullptr) {
  27. return cCompiler;
  28. }
  29. return (cCompiler = "cc");
  30. }
  31. static const char *getCXXCompiler() {
  32. static const char *cxxCompiler = nullptr;
  33. if (cxxCompiler != nullptr) {
  34. return cxxCompiler;
  35. }
  36. cxxCompiler = getenv("CXX");
  37. if (cxxCompiler != nullptr) {
  38. return cxxCompiler;
  39. }
  40. return (cxxCompiler = "g++");
  41. }
  42. static const char *getCompilerFor(FileType type) {
  43. switch (type) {
  44. case FileType::C:
  45. return getCCompiler();
  46. case FileType::CXX:
  47. return getCXXCompiler();
  48. }
  49. abort();
  50. }
  51. static bool isWhitespace(char ch) {
  52. return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
  53. }
  54. static void parseWhitespaceSeparated(
  55. const std::string &input,
  56. std::vector<std::string> &output,
  57. size_t pos = 0) {
  58. size_t i = pos;
  59. while (true) {
  60. // Skip leading whitespace
  61. char ch = input[i];
  62. while (isWhitespace(ch) || (ch == '\\' && isWhitespace(input[i + 1]))) {
  63. ch = input[i += 1];
  64. }
  65. // Read non-whitespace
  66. std::string str;
  67. while (!isWhitespace(ch) && ch != '\0') {
  68. if (ch == '\\') {
  69. str += input[i + 1];
  70. ch = input[i += 2];
  71. } else {
  72. str += input[i];
  73. ch = input[i += 1];
  74. }
  75. }
  76. if (str.size() != 0) {
  77. output.push_back(std::move(str));
  78. }
  79. if (ch == '\0') {
  80. break;
  81. }
  82. }
  83. }
  84. std::string objectFilePath(const std::string &path, const std::string &outDir) {
  85. return outDir + '/' + sys::sanitizePath(path) + ".o";
  86. }
  87. std::string targetFilePath(
  88. TargetType type,
  89. const std::string &path,
  90. const std::string &outDir) {
  91. std::string base = outDir + '/' + sys::sanitizePath(path);
  92. switch (type) {
  93. case TargetType::BINARY:
  94. return base;
  95. case TargetType::SHARED_LIBRARY:
  96. return base + ".so";
  97. case TargetType::STATIC_LIBRARY:
  98. return base + ".a";
  99. }
  100. abort();
  101. }
  102. void getFlags(const BXVariables &vars, FileType type, std::vector<std::string> &flags) {
  103. auto stdver = vars.find("std");
  104. if (stdver != vars.end() && stdver->second.size() > 0) {
  105. flags.push_back("-std=" + stdver->second[0]);
  106. }
  107. auto pkgs = vars.find("pkgs");
  108. if (pkgs != vars.end()) {
  109. std::vector<std::string> argv;
  110. argv.push_back(getPkgConfig());
  111. argv.push_back("--cflags");
  112. for (auto &pkg: pkgs->second) {
  113. argv.push_back(pkg);
  114. }
  115. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  116. std::string output;
  117. sys::ProcConf conf;
  118. conf.print = global::verbose >= 1;
  119. conf.prefix = "(PKGCONFIG)";
  120. conf.output = &output;
  121. sys::execute(argv, conf);
  122. parseWhitespaceSeparated(output, flags);
  123. }
  124. auto warnings = vars.find("warnings");
  125. if (warnings != vars.end()) {
  126. for (auto &w: warnings->second) {
  127. flags.push_back("-W" + w);
  128. }
  129. }
  130. auto includes = vars.find("includes");
  131. if (includes != vars.end()) {
  132. for (auto &i: includes->second) {
  133. flags.push_back("-I" + i);
  134. }
  135. }
  136. auto defines = vars.find("defines");
  137. if (defines != vars.end()) {
  138. for (auto &d: defines->second) {
  139. flags.push_back("-D" + d);
  140. }
  141. }
  142. auto sanitizers = vars.find("sanitizers");
  143. if (sanitizers != vars.end()) {
  144. for (auto &s: sanitizers->second) {
  145. flags.push_back("-fsanitize=" + s);
  146. }
  147. }
  148. auto optimize = vars.find("optimize");
  149. if (optimize != vars.end()) {
  150. flags.push_back("-O" + optimize->second[0]);
  151. }
  152. auto cflags = vars.find(
  153. type == FileType::C ? "cflags" :
  154. type == FileType::CXX ? "cxxflags" :
  155. nullptr);
  156. if (cflags != vars.end()) {
  157. for (auto &f: cflags->second) {
  158. flags.push_back(f);
  159. }
  160. }
  161. }
  162. void getLDLibs(const BXVariables &vars, std::vector<std::string> &flags) {
  163. auto pkgs = vars.find("pkgs");
  164. if (pkgs != vars.end()) {
  165. std::vector<std::string> argv;
  166. argv.push_back(getPkgConfig());
  167. argv.push_back("--libs");
  168. for (auto &pkg: pkgs->second) {
  169. argv.push_back(pkg);
  170. }
  171. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  172. std::string output;
  173. sys::ProcConf conf;
  174. conf.print = global::verbose >= 1;
  175. conf.prefix = "(PKGCONFIG)";
  176. conf.output = &output;
  177. sys::execute(argv, conf);
  178. parseWhitespaceSeparated(output, flags);
  179. }
  180. auto ldlibs = vars.find("ldlibs");
  181. if (ldlibs != vars.end()) {
  182. for (auto &l: ldlibs->second) {
  183. flags.push_back("-l" + l);
  184. }
  185. }
  186. }
  187. void getLDFlags(const BXVariables &vars, std::vector<std::string> &flags) {
  188. auto sanitize = vars.find("sanitizers");
  189. if (sanitize != vars.end()) {
  190. for (auto &s: sanitize->second) {
  191. flags.push_back("-fsanitize=" + s);
  192. }
  193. }
  194. auto ldflags = vars.find("ldflags");
  195. if (ldflags != vars.end()) {
  196. for (auto &f: ldflags->second) {
  197. flags.push_back(f);
  198. }
  199. }
  200. }
  201. std::vector<std::string> getDependencies(
  202. const std::vector<std::string> &flags,
  203. FileType type,
  204. const std::string &path) {
  205. std::vector<std::string> argv;
  206. // $(compiler)
  207. argv.push_back(getCompilerFor(type));
  208. // $(flags)
  209. for (auto &flag: flags) {
  210. argv.push_back(flag);
  211. }
  212. // -MM $<
  213. argv.push_back("-MM");
  214. argv.push_back(path);
  215. // Execute $(compiler) $(flags) -MM $<
  216. std::string output;
  217. sys::ProcConf conf;
  218. conf.print = global::verbose >= 1;
  219. conf.output = &output;
  220. conf.prefix = "(DEP)";
  221. sys::execute(argv, conf);
  222. std::vector<std::string> deps;
  223. size_t idx = output.find(':');
  224. if (idx != std::string::npos) {
  225. parseWhitespaceSeparated(output, deps, idx + 1);
  226. }
  227. return deps;
  228. }
  229. std::vector<std::string> getCompileCommand(
  230. const std::vector<std::string> &flags,
  231. FileType type,
  232. const std::string &path,
  233. const std::string &outDir) {
  234. std::string objectPath = objectFilePath(path, outDir);
  235. std::vector<std::string> argv;
  236. // $(compiler)
  237. argv.push_back(getCompilerFor(type));
  238. // $(cflags)
  239. for (auto &flag: flags) {
  240. argv.push_back(flag.c_str());
  241. }
  242. // -o $@ -c $<
  243. argv.push_back("-o");
  244. argv.push_back(std::move(objectPath));
  245. argv.push_back("-c");
  246. argv.push_back(std::move(path));
  247. return argv;
  248. }
  249. std::vector<std::string> getLinkCommand(
  250. const std::vector<std::string> &ldFlags,
  251. const std::vector<std::string> &ldLibs,
  252. FileType type,
  253. TargetType targetType,
  254. const std::vector<std::string> &objs,
  255. const std::string &path,
  256. const std::string &outDir) {
  257. // TODO: Use ar to create STATIC_LIBRARY,
  258. // use GCC with -shared to make SHARED_LIBRARY
  259. std::vector<std::string> argv;
  260. // $(compiler)
  261. argv.push_back(getCompilerFor(type));
  262. // $(ldflags)
  263. for (auto &flag: ldFlags) {
  264. argv.push_back(flag);
  265. }
  266. // -o $@
  267. argv.push_back("-o");
  268. argv.push_back(targetFilePath(targetType, path, outDir));
  269. // $(objs)
  270. for (auto &obj: objs) {
  271. argv.push_back(obj);
  272. }
  273. // $(ldlibs)
  274. for (auto &flag: ldLibs) {
  275. argv.push_back(flag);
  276. }
  277. return argv;
  278. }
  279. }