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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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::execute(argv, &output, global::verbose >= 2);
  118. parseWhitespaceSeparated(output, flags);
  119. }
  120. auto warnings = vars.find("warnings");
  121. if (warnings != vars.end()) {
  122. for (auto &w: warnings->second) {
  123. flags.push_back("-W" + w);
  124. }
  125. }
  126. auto includes = vars.find("includes");
  127. if (includes != vars.end()) {
  128. for (auto &i: includes->second) {
  129. flags.push_back("-I" + i);
  130. }
  131. }
  132. auto defines = vars.find("defines");
  133. if (defines != vars.end()) {
  134. for (auto &d: defines->second) {
  135. flags.push_back("-D" + d);
  136. }
  137. }
  138. auto sanitizers = vars.find("sanitizers");
  139. if (sanitizers != vars.end()) {
  140. for (auto &s: sanitizers->second) {
  141. flags.push_back("-fsanitize=" + s);
  142. }
  143. }
  144. auto optimize = vars.find("optimize");
  145. if (optimize != vars.end()) {
  146. flags.push_back("-O" + optimize->second[0]);
  147. }
  148. auto cflags = vars.find(
  149. type == FileType::C ? "cflags" :
  150. type == FileType::CXX ? "cxxflags" :
  151. nullptr);
  152. if (cflags != vars.end()) {
  153. for (auto &f: cflags->second) {
  154. flags.push_back(f);
  155. }
  156. }
  157. }
  158. void getLDLibs(const BXVariables &vars, std::vector<std::string> &flags) {
  159. auto pkgs = vars.find("pkgs");
  160. if (pkgs != vars.end()) {
  161. std::vector<std::string> argv;
  162. argv.push_back(getPkgConfig());
  163. argv.push_back("--libs");
  164. for (auto &pkg: pkgs->second) {
  165. argv.push_back(pkg);
  166. }
  167. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  168. std::string output;
  169. sys::execute(argv, &output, global::verbose >= 2);
  170. parseWhitespaceSeparated(output, flags);
  171. }
  172. auto ldlibs = vars.find("ldlibs");
  173. if (ldlibs != vars.end()) {
  174. for (auto &l: ldlibs->second) {
  175. flags.push_back("-l" + l);
  176. }
  177. }
  178. }
  179. void getLDFlags(const BXVariables &vars, std::vector<std::string> &flags) {
  180. auto sanitize = vars.find("sanitizers");
  181. if (sanitize != vars.end()) {
  182. for (auto &s: sanitize->second) {
  183. flags.push_back("-fsanitize=" + s);
  184. }
  185. }
  186. auto ldflags = vars.find("ldflags");
  187. if (ldflags != vars.end()) {
  188. for (auto &f: ldflags->second) {
  189. flags.push_back(f);
  190. }
  191. }
  192. }
  193. std::vector<std::string> getDependencies(
  194. const std::vector<std::string> &flags,
  195. FileType type,
  196. const std::string &path) {
  197. std::vector<std::string> argv;
  198. // $(compiler)
  199. argv.push_back(getCompilerFor(type));
  200. // $(flags)
  201. for (auto &flag: flags) {
  202. argv.push_back(flag);
  203. }
  204. // -MM $<
  205. argv.push_back("-MM");
  206. argv.push_back(path);
  207. // Execute $(compiler) $(flags) -MM $<
  208. std::string output;
  209. sys::execute(argv, &output, global::verbose >= 2);
  210. std::vector<std::string> deps;
  211. size_t idx = output.find(':');
  212. if (idx != std::string::npos) {
  213. parseWhitespaceSeparated(output, deps, idx + 1);
  214. }
  215. return deps;
  216. }
  217. std::vector<std::string> getCompileCommand(
  218. const std::vector<std::string> &flags,
  219. FileType type,
  220. const std::string &path,
  221. const std::string &outDir) {
  222. std::string objectPath = objectFilePath(path, outDir);
  223. std::vector<std::string> argv;
  224. // $(compiler)
  225. argv.push_back(getCompilerFor(type));
  226. // $(cflags)
  227. for (auto &flag: flags) {
  228. argv.push_back(flag.c_str());
  229. }
  230. // -o $@ -c $<
  231. argv.push_back("-o");
  232. argv.push_back(std::move(objectPath));
  233. argv.push_back("-c");
  234. argv.push_back(std::move(path));
  235. return argv;
  236. }
  237. std::vector<std::string> getLinkCommand(
  238. const std::vector<std::string> &ldFlags,
  239. const std::vector<std::string> &ldLibs,
  240. FileType type,
  241. TargetType targetType,
  242. const std::vector<std::string> &objs,
  243. const std::string &path,
  244. const std::string &outDir) {
  245. // TODO: Use ar to create STATIC_LIBRARY,
  246. // use GCC with -shared to make SHARED_LIBRARY
  247. std::vector<std::string> argv;
  248. // $(compiler)
  249. argv.push_back(getCompilerFor(type));
  250. // $(ldflags)
  251. for (auto &flag: ldFlags) {
  252. argv.push_back(flag);
  253. }
  254. // -o $@
  255. argv.push_back("-o");
  256. argv.push_back(targetFilePath(targetType, path, outDir));
  257. // $(objs)
  258. for (auto &obj: objs) {
  259. argv.push_back(obj);
  260. }
  261. // $(ldlibs)
  262. for (auto &flag: ldLibs) {
  263. argv.push_back(flag);
  264. }
  265. return argv;
  266. }
  267. }