123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #include "toolchain.h"
-
- #include <stdexcept>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
-
- #include "sys.h"
-
- namespace toolchain {
-
- static const char *getPkgConfig() {
- static const char *pkgConfig = nullptr;
- if (pkgConfig != nullptr) {
- return pkgConfig;
- }
-
- pkgConfig = getenv("PKG_CONFIG");
- if (pkgConfig != nullptr) {
- return pkgConfig;
- }
-
- return (pkgConfig = "pkg-config");
- }
-
- static const char *getCCompiler() {
- static const char *cCompiler = nullptr;
- if (cCompiler != nullptr) {
- return cCompiler;
- }
-
- cCompiler = getenv("CC");
- if (cCompiler != nullptr) {
- return cCompiler;
- }
-
- return (cCompiler = "cc");
- }
-
- static const char *getCXXCompiler() {
- static const char *cxxCompiler = nullptr;
- if (cxxCompiler != nullptr) {
- return cxxCompiler;
- }
-
- cxxCompiler = getenv("CC");
- if (cxxCompiler != nullptr) {
- return cxxCompiler;
- }
-
- return (cxxCompiler = "g++");
- }
-
- static const char *getCompilerFor(SourceFile::FileType type) {
- switch (type) {
- case SourceFile::FileType::C:
- return getCCompiler();
-
- case SourceFile::FileType::CXX:
- return getCXXCompiler();
-
- case SourceFile::FileType::UNKNOWN:
- abort();
- }
-
- abort();
- }
-
- static bool isWhitespace(char ch) {
- return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
- }
-
- static void parseWhitespaceSeparated(
- const std::string &input,
- std::vector<std::string> &output,
- size_t pos = 0) {
- size_t i = pos;
- while (true) {
- // Skip leading whitespace
- char ch = input[i];
- while (isWhitespace(ch) || (ch == '\\' && isWhitespace(input[i + 1]))) {
- ch = input[i += 1];
- }
-
- // Read non-whitespace
- std::string str;
- while (!isWhitespace(ch) && ch != '\0') {
- if (ch == '\\') {
- str += input[i + 1];
- ch = input[i += 2];
- } else {
- str += input[i];
- ch = input[i += 1];
- }
- }
-
- output.push_back(std::move(str));
-
- if (ch == '\0') {
- break;
- }
- }
- }
-
- void pkgFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
- std::vector<const char *> argv;
- argv.push_back(getPkgConfig());
- argv.push_back("--cflags");
- for (auto &pkg: pkgs) {
- argv.push_back(pkg.c_str());
- }
-
- std::string output;
- sys::execute(argv, &output);
- parseWhitespaceSeparated(output, flags);
- }
-
- void pkgLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
- std::vector<const char *> argv;
- argv.push_back(getPkgConfig());
- argv.push_back("--libs");
- for (auto &pkg: pkgs) {
- argv.push_back(pkg.c_str());
- }
-
- std::string output;
- sys::execute(argv, &output);
- parseWhitespaceSeparated(output, flags);
- }
-
- void getDependencies(
- const std::vector<std::string> &flags,
- SourceFile::FileType type,
- const std::string &srcDir,
- const std::string &name,
- std::vector<std::string> &deps) {
- std::string sourcePath = srcDir + "/" + name;
-
- std::vector<const char *> argv;
- argv.push_back(getCompilerFor(type));
- for (auto &flag: flags) {
- argv.push_back(flag.c_str());
- }
-
- argv.push_back("-MM");
- argv.push_back("-c");
- argv.push_back(sourcePath.c_str());
- std::string output;
- sys::execute(argv, &output);
-
- size_t idx = output.find(':');
- if (idx != std::string::npos) {
- parseWhitespaceSeparated(output, deps, idx + 1);
- }
- }
-
- void compile(
- const std::vector<std::string> &flags,
- SourceFile::FileType type,
- const std::string &srcDir,
- const std::string &name,
- const std::string &outDir) {
- std::string sourcePath = srcDir + "/" + name;
- std::string destDir = outDir + "/" + srcDir;
- std::string destPath = destDir + "/" + name + ".o";
-
- sys::mkdirp(outDir);
-
- std::vector<const char *> argv;
- argv.push_back(getCompilerFor(type));
- for (auto &flag: flags) {
- argv.push_back(flag.c_str());
- }
-
- argv.push_back("-o");
- argv.push_back(destPath.c_str());
- argv.push_back("-c");
- argv.push_back(sourcePath.c_str());
- sys::execute(argv, nullptr);
- }
-
- }
|