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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "toolchain.h"
  2. #include <stdexcept>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include "sys.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. void pkgFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  85. std::vector<const char *> argv;
  86. argv.push_back(getPkgConfig());
  87. argv.push_back("--cflags");
  88. for (auto &pkg: pkgs) {
  89. argv.push_back(pkg.c_str());
  90. }
  91. std::string output;
  92. sys::execute(argv, &output);
  93. parseWhitespaceSeparated(output, flags);
  94. }
  95. void pkgLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
  96. std::vector<const char *> argv;
  97. argv.push_back(getPkgConfig());
  98. argv.push_back("--libs");
  99. for (auto &pkg: pkgs) {
  100. argv.push_back(pkg.c_str());
  101. }
  102. std::string output;
  103. sys::execute(argv, &output);
  104. parseWhitespaceSeparated(output, flags);
  105. }
  106. void getDependencies(
  107. const std::vector<std::string> &flags,
  108. SourceFile::FileType type,
  109. const std::string &srcDir,
  110. const std::string &name,
  111. std::vector<std::string> &deps) {
  112. std::string sourcePath = srcDir + "/" + name;
  113. std::vector<const char *> argv;
  114. argv.push_back(getCompilerFor(type));
  115. for (auto &flag: flags) {
  116. argv.push_back(flag.c_str());
  117. }
  118. argv.push_back("-MM");
  119. argv.push_back("-c");
  120. argv.push_back(sourcePath.c_str());
  121. std::string output;
  122. sys::execute(argv, &output);
  123. size_t idx = output.find(':');
  124. if (idx != std::string::npos) {
  125. parseWhitespaceSeparated(output, deps, idx + 1);
  126. }
  127. }
  128. void compile(
  129. const std::vector<std::string> &flags,
  130. SourceFile::FileType type,
  131. const std::string &srcDir,
  132. const std::string &name,
  133. const std::string &outDir) {
  134. std::string sourcePath = srcDir + "/" + name;
  135. std::string destDir = outDir + "/" + srcDir;
  136. std::string destPath = destDir + "/" + name + ".o";
  137. sys::mkdirp(outDir);
  138. std::vector<const char *> argv;
  139. argv.push_back(getCompilerFor(type));
  140. for (auto &flag: flags) {
  141. argv.push_back(flag.c_str());
  142. }
  143. argv.push_back("-o");
  144. argv.push_back(destPath.c_str());
  145. argv.push_back("-c");
  146. argv.push_back(sourcePath.c_str());
  147. sys::execute(argv, nullptr);
  148. }
  149. }