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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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(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 getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  101. std::vector<std::string> argv;
  102. argv.push_back(getPkgConfig());
  103. argv.push_back("--cflags");
  104. for (auto &pkg: pkgs) {
  105. argv.push_back(pkg);
  106. }
  107. // Execute $(PKG_CONFIG) --cflags $(PKGS)
  108. std::string output;
  109. sys::execute(argv, &output, global::verbose >= 2);
  110. parseWhitespaceSeparated(output, flags);
  111. }
  112. void getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  113. std::vector<std::string> argv;
  114. argv.push_back(getPkgConfig());
  115. argv.push_back("--libs");
  116. for (auto &pkg: pkgs) {
  117. argv.push_back(pkg);
  118. }
  119. // Execute $(PKG_CONFIG) --libs $(PKGS)
  120. std::string output;
  121. sys::execute(argv, &output, global::verbose >= 2);
  122. parseWhitespaceSeparated(output, flags);
  123. }
  124. std::vector<std::string> getDependencies(
  125. const std::vector<std::string> &flags,
  126. FileType type,
  127. const std::string &path) {
  128. std::vector<std::string> argv;
  129. // $(compiler)
  130. argv.push_back(getCompilerFor(type));
  131. // $(flags)
  132. for (auto &flag: flags) {
  133. argv.push_back(flag);
  134. }
  135. // -MM $<
  136. argv.push_back("-MM");
  137. argv.push_back(path);
  138. // Execute $(compiler) $(flags) -MM $<
  139. std::string output;
  140. sys::execute(argv, &output, global::verbose >= 2);
  141. std::vector<std::string> deps;
  142. size_t idx = output.find(':');
  143. if (idx != std::string::npos) {
  144. parseWhitespaceSeparated(output, deps, idx + 1);
  145. }
  146. return deps;
  147. }
  148. std::vector<std::string> getCompileCommand(
  149. const std::vector<std::string> &flags,
  150. FileType type,
  151. const std::string &path,
  152. const std::string &outDir) {
  153. std::string objectPath = objectFilePath(path, outDir);
  154. std::vector<std::string> argv;
  155. // $(compiler)
  156. argv.push_back(getCompilerFor(type));
  157. // $(cflags)
  158. for (auto &flag: flags) {
  159. argv.push_back(flag.c_str());
  160. }
  161. // -o $@ -c $<
  162. argv.push_back("-o");
  163. argv.push_back(std::move(objectPath));
  164. argv.push_back("-c");
  165. argv.push_back(std::move(path));
  166. return argv;
  167. }
  168. std::vector<std::string> getLinkCommand(
  169. const std::vector<std::string> &ldFlags,
  170. const std::vector<std::string> &ldLibs,
  171. FileType type,
  172. TargetType targetType,
  173. const std::vector<std::string> &objs,
  174. const std::string &path,
  175. const std::string &outDir) {
  176. // TODO: Use ar to create STATIC_LIBRARY,
  177. // use GCC with -shared to make SHARED_LIBRARY
  178. std::vector<std::string> argv;
  179. // $(compiler)
  180. argv.push_back(getCompilerFor(type));
  181. // $(ldflags)
  182. for (auto &flag: ldFlags) {
  183. argv.push_back(flag);
  184. }
  185. // -o $@
  186. argv.push_back("-o");
  187. argv.push_back(targetFilePath(targetType, path, outDir));
  188. // $(objs)
  189. for (auto &obj: objs) {
  190. argv.push_back(obj);
  191. }
  192. // $(ldlibs)
  193. for (auto &flag: ldLibs) {
  194. argv.push_back(flag);
  195. }
  196. return argv;
  197. }
  198. }