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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. output.push_back(std::move(str));
  77. if (ch == '\0') {
  78. break;
  79. }
  80. }
  81. }
  82. std::string objectFilePath(const std::string &path, const std::string &outDir) {
  83. return outDir + '/' + path + ".o";
  84. }
  85. std::string targetFilePath(
  86. TargetType type,
  87. const std::string &path,
  88. const std::string &outDir) {
  89. std::string base = outDir + '/' + path;
  90. switch (type) {
  91. case TargetType::BINARY:
  92. return base;
  93. case TargetType::SHARED_LIBRARY:
  94. return base + ".so";
  95. case TargetType::STATIC_LIBRARY:
  96. return base + ".a";
  97. }
  98. abort();
  99. }
  100. void getFlags(const BXVariables &vars, FileType type, std::vector<std::string> &flags) {
  101. auto stdver = vars.find("std");
  102. if (stdver != vars.end() && stdver->second.size() > 0) {
  103. flags.push_back("-std=" + stdver->second[0]);
  104. }
  105. auto pkgs = vars.find("pkgs");
  106. if (pkgs != vars.end()) {
  107. std::vector<std::string> argv;
  108. argv.push_back(getPkgConfig());
  109. argv.push_back("--cflags");
  110. for (auto &pkg: pkgs->second) {
  111. argv.push_back(pkg);
  112. }
  113. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  114. std::string output;
  115. sys::execute(argv, &output, global::verbose >= 2);
  116. parseWhitespaceSeparated(output, flags);
  117. }
  118. auto warnings = vars.find("warnings");
  119. if (warnings != vars.end()) {
  120. for (auto &w: warnings->second) {
  121. flags.push_back("-W" + w);
  122. }
  123. }
  124. auto includes = vars.find("includes");
  125. if (includes != vars.end()) {
  126. for (auto &i: includes->second) {
  127. flags.push_back("-I" + i);
  128. }
  129. }
  130. auto sanitizers = vars.find("sanitizers");
  131. if (sanitizers != vars.end()) {
  132. for (auto &s: sanitizers->second) {
  133. flags.push_back("-fsanitize=" + s);
  134. }
  135. }
  136. auto cflags = vars.find(
  137. type == FileType::C ? "cflags" :
  138. type == FileType::CXX ? "cxxflags" :
  139. nullptr);
  140. if (cflags != vars.end()) {
  141. for (auto &f: cflags->second) {
  142. flags.push_back(f);
  143. }
  144. }
  145. }
  146. void getLDLibs(const BXVariables &vars, std::vector<std::string> &flags) {
  147. auto pkgs = vars.find("pkgs");
  148. if (pkgs != vars.end()) {
  149. std::vector<std::string> argv;
  150. argv.push_back(getPkgConfig());
  151. argv.push_back("--libs");
  152. for (auto &pkg: pkgs->second) {
  153. argv.push_back(pkg);
  154. }
  155. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  156. std::string output;
  157. sys::execute(argv, &output, global::verbose >= 2);
  158. parseWhitespaceSeparated(output, flags);
  159. }
  160. auto ldlibs = vars.find("ldlibs");
  161. if (ldlibs != vars.end()) {
  162. for (auto &l: ldlibs->second) {
  163. flags.push_back("-l" + l);
  164. }
  165. }
  166. }
  167. void getLDFlags(const BXVariables &vars, std::vector<std::string> &flags) {
  168. auto sanitize = vars.find("sanitizers");
  169. if (sanitize != vars.end()) {
  170. for (auto &s: sanitize->second) {
  171. flags.push_back("-fsanitizers=" + s);
  172. }
  173. }
  174. auto ldflags = vars.find("ldflags");
  175. if (ldflags != vars.end()) {
  176. for (auto &f: ldflags->second) {
  177. flags.push_back(f);
  178. }
  179. }
  180. }
  181. std::vector<std::string> getDependencies(
  182. const std::vector<std::string> &flags,
  183. FileType type,
  184. const std::string &path) {
  185. std::vector<std::string> argv;
  186. // $(compiler)
  187. argv.push_back(getCompilerFor(type));
  188. // $(flags)
  189. for (auto &flag: flags) {
  190. argv.push_back(flag);
  191. }
  192. // -MM $<
  193. argv.push_back("-MM");
  194. argv.push_back(path);
  195. // Execute $(compiler) $(flags) -MM $<
  196. std::string output;
  197. sys::execute(argv, &output, global::verbose >= 2);
  198. std::vector<std::string> deps;
  199. size_t idx = output.find(':');
  200. if (idx != std::string::npos) {
  201. parseWhitespaceSeparated(output, deps, idx + 1);
  202. }
  203. return deps;
  204. }
  205. std::vector<std::string> getCompileCommand(
  206. const std::vector<std::string> &flags,
  207. FileType type,
  208. const std::string &path,
  209. const std::string &outDir) {
  210. std::string objectPath = objectFilePath(path, outDir);
  211. std::vector<std::string> argv;
  212. // $(compiler)
  213. argv.push_back(getCompilerFor(type));
  214. // $(cflags)
  215. for (auto &flag: flags) {
  216. argv.push_back(flag.c_str());
  217. }
  218. // -o $@ -c $<
  219. argv.push_back("-o");
  220. argv.push_back(std::move(objectPath));
  221. argv.push_back("-c");
  222. argv.push_back(std::move(path));
  223. return argv;
  224. }
  225. std::vector<std::string> getLinkCommand(
  226. const std::vector<std::string> &ldFlags,
  227. const std::vector<std::string> &ldLibs,
  228. FileType type,
  229. TargetType targetType,
  230. const std::vector<std::string> &objs,
  231. const std::string &path,
  232. const std::string &outDir) {
  233. // TODO: Use ar to create STATIC_LIBRARY,
  234. // use GCC with -shared to make SHARED_LIBRARY
  235. std::vector<std::string> argv;
  236. // $(compiler)
  237. argv.push_back(getCompilerFor(type));
  238. // $(ldflags)
  239. for (auto &flag: ldFlags) {
  240. argv.push_back(flag);
  241. }
  242. // -o $@
  243. argv.push_back("-o");
  244. argv.push_back(targetFilePath(targetType, path, outDir));
  245. // $(objs)
  246. for (auto &obj: objs) {
  247. argv.push_back(obj);
  248. }
  249. // $(ldlibs)
  250. for (auto &flag: ldLibs) {
  251. argv.push_back(flag);
  252. }
  253. return argv;
  254. }
  255. }