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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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("CC");
  37. if (cxxCompiler != nullptr) {
  38. return cxxCompiler;
  39. }
  40. return (cxxCompiler = "g++");
  41. }
  42. static const char *getCompilerFor(SourceFile::FileType type) {
  43. switch (type) {
  44. case SourceFile::FileType::C:
  45. return getCCompiler();
  46. case SourceFile::FileType::CXX:
  47. return getCXXCompiler();
  48. case SourceFile::FileType::UNKNOWN:
  49. abort();
  50. }
  51. abort();
  52. }
  53. static bool isWhitespace(char ch) {
  54. return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
  55. }
  56. static void parseWhitespaceSeparated(
  57. const std::string &input,
  58. std::vector<std::string> &output,
  59. size_t pos = 0) {
  60. size_t i = pos;
  61. while (true) {
  62. // Skip leading whitespace
  63. char ch = input[i];
  64. while (isWhitespace(ch) || (ch == '\\' && isWhitespace(input[i + 1]))) {
  65. ch = input[i += 1];
  66. }
  67. // Read non-whitespace
  68. std::string str;
  69. while (!isWhitespace(ch) && ch != '\0') {
  70. if (ch == '\\') {
  71. str += input[i + 1];
  72. ch = input[i += 2];
  73. } else {
  74. str += input[i];
  75. ch = input[i += 1];
  76. }
  77. }
  78. output.push_back(std::move(str));
  79. if (ch == '\0') {
  80. break;
  81. }
  82. }
  83. }
  84. std::string objectFilePath(
  85. const std::string &srcDir,
  86. const std::string &name,
  87. const std::string &outDir) {
  88. return outDir + '/' + srcDir + '/' + name + ".o";
  89. }
  90. std::string targetFilePath(
  91. TargetType type,
  92. const std::string &name,
  93. const std::string &outDir) {
  94. std::string base = outDir + '/' + name;
  95. switch (type) {
  96. case TargetType::BINARY:
  97. return base;
  98. case TargetType::SHARED_LIBRARY:
  99. return base + ".so";
  100. case TargetType::STATIC_LIBRARY:
  101. return base + ".a";
  102. }
  103. abort();
  104. }
  105. void getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  106. std::vector<std::string> argv;
  107. argv.push_back(getPkgConfig());
  108. argv.push_back("--cflags");
  109. for (auto &pkg: pkgs) {
  110. argv.push_back(pkg);
  111. }
  112. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  113. std::string output;
  114. sys::execute(argv, &output, global::verbose >= 2);
  115. parseWhitespaceSeparated(output, flags);
  116. }
  117. void getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  118. std::vector<std::string> argv;
  119. argv.push_back(getPkgConfig());
  120. argv.push_back("--libs");
  121. for (auto &pkg: pkgs) {
  122. argv.push_back(pkg);
  123. }
  124. // Execute $(PKG_CONFIG) --libs $(PKGS)
  125. std::string output;
  126. sys::execute(argv, &output, global::verbose >= 2);
  127. parseWhitespaceSeparated(output, flags);
  128. }
  129. void getDependencies(
  130. const std::vector<std::string> &flags,
  131. SourceFile::FileType type,
  132. const std::string &srcDir,
  133. const std::string &name,
  134. std::vector<std::string> &deps) {
  135. std::string sourcePath = srcDir + "/" + name;
  136. std::vector<std::string> argv;
  137. // $(compiler)
  138. argv.push_back(getCompilerFor(type));
  139. // $(flags)
  140. for (auto &flag: flags) {
  141. argv.push_back(flag);
  142. }
  143. // -MM $<
  144. argv.push_back("-MM");
  145. argv.push_back(sourcePath);
  146. // Execute $(compiler) $(flags) -MM $<
  147. std::string output;
  148. sys::execute(argv, &output, global::verbose >= 2);
  149. size_t idx = output.find(':');
  150. if (idx != std::string::npos) {
  151. parseWhitespaceSeparated(output, deps, idx + 1);
  152. }
  153. }
  154. std::vector<std::string> getCompileCommand(
  155. const std::vector<std::string> &flags,
  156. SourceFile::FileType type,
  157. const std::string &srcDir,
  158. const std::string &name,
  159. const std::string &outDir) {
  160. std::string sourcePath = srcDir + "/" + name;
  161. std::string objectPath = objectFilePath(srcDir, name, outDir);
  162. std::vector<std::string> argv;
  163. // $(compiler)
  164. argv.push_back(getCompilerFor(type));
  165. // $(cflags)
  166. for (auto &flag: flags) {
  167. argv.push_back(flag.c_str());
  168. }
  169. // -o $@ -c $<
  170. argv.push_back("-o");
  171. argv.push_back(std::move(objectPath));
  172. argv.push_back("-c");
  173. argv.push_back(std::move(sourcePath));
  174. return argv;
  175. }
  176. void compile(
  177. const std::vector<std::string> &flags,
  178. SourceFile::FileType type,
  179. const std::string &srcDir,
  180. const std::string &name,
  181. const std::string &outDir) {
  182. // Ensure the output directory actually exists
  183. std::string destDir = outDir + "/" + srcDir;
  184. sys::mkdirp(destDir);
  185. std::vector<std::string> argv = getCompileCommand(flags, type, srcDir, name, outDir);
  186. sys::execute(argv, nullptr, global::verbose >= 1);
  187. }
  188. void link(
  189. const std::string &name,
  190. const std::vector<std::string> &ldFlags,
  191. const std::vector<std::string> &ldLibs,
  192. SourceFile::FileType type,
  193. TargetType targetType,
  194. const std::vector<std::string> &objs,
  195. const std::string &outDir) {
  196. const std::string outPath = targetFilePath(targetType, name, outDir);
  197. // TODO: Use ar to create STATIC_LIBRARY,
  198. // use GCC with -shared to make SHARED_LIBRARY
  199. std::vector<std::string> argv;
  200. // $(compiler)
  201. argv.push_back(getCompilerFor(type));
  202. // $(ldflags)
  203. for (auto &flag: ldFlags) {
  204. argv.push_back(flag);
  205. }
  206. // -o $@
  207. argv.push_back("-o");
  208. argv.push_back(outPath);
  209. // $(objs)
  210. for (auto &obj: objs) {
  211. argv.push_back(obj);
  212. }
  213. // $(ldlibs)
  214. for (auto &flag: ldLibs) {
  215. argv.push_back(flag);
  216. }
  217. // Execute $(compiler) $(ldflags) -o $@ $(objs) $(ldlibs)
  218. sys::execute(argv, nullptr, global::verbose >= 1);
  219. }
  220. }