123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #include "toolchain.h"
-
- #include <stdexcept>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "sys.h"
- #include "globals.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(FileType type) {
- switch (type) {
- case FileType::C:
- return getCCompiler();
-
- case FileType::CXX:
- return getCXXCompiler();
- }
-
- 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;
- }
- }
- }
-
- std::string objectFilePath(const std::string &path, const std::string &outDir) {
- return outDir + '/' + path + ".o";
- }
-
- std::string targetFilePath(
- TargetType type,
- const std::string &path,
- const std::string &outDir) {
- std::string base = outDir + '/' + path;
-
- switch (type) {
- case TargetType::BINARY:
- return base;
- case TargetType::SHARED_LIBRARY:
- return base + ".so";
- case TargetType::STATIC_LIBRARY:
- return base + ".a";
- }
-
- abort();
- }
-
- void getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
- std::vector<std::string> argv;
- argv.push_back(getPkgConfig());
- argv.push_back("--cflags");
- for (auto &pkg: pkgs) {
- argv.push_back(pkg);
- }
-
- // Execute $(PKG_CONFIG) --cflags $(PKGS)
- std::string output;
- sys::execute(argv, &output, global::verbose >= 2);
- parseWhitespaceSeparated(output, flags);
- }
-
- void getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
- std::vector<std::string> argv;
- argv.push_back(getPkgConfig());
- argv.push_back("--libs");
- for (auto &pkg: pkgs) {
- argv.push_back(pkg);
- }
-
- // Execute $(PKG_CONFIG) --libs $(PKGS)
- std::string output;
- sys::execute(argv, &output, global::verbose >= 2);
- parseWhitespaceSeparated(output, flags);
- }
-
- std::vector<std::string> getDependencies(
- const std::vector<std::string> &flags,
- FileType type,
- const std::string &path) {
- std::vector<std::string> argv;
-
- // $(compiler)
- argv.push_back(getCompilerFor(type));
-
- // $(flags)
- for (auto &flag: flags) {
- argv.push_back(flag);
- }
-
- // -MM $<
- argv.push_back("-MM");
- argv.push_back(path);
-
- // Execute $(compiler) $(flags) -MM $<
- std::string output;
- sys::execute(argv, &output, global::verbose >= 2);
-
- std::vector<std::string> deps;
- size_t idx = output.find(':');
- if (idx != std::string::npos) {
- parseWhitespaceSeparated(output, deps, idx + 1);
- }
-
- return deps;
- }
-
- std::vector<std::string> getCompileCommand(
- const std::vector<std::string> &flags,
- FileType type,
- const std::string &path,
- const std::string &outDir) {
- std::string objectPath = objectFilePath(path, outDir);
-
- std::vector<std::string> argv;
-
- // $(compiler)
- argv.push_back(getCompilerFor(type));
-
- // $(cflags)
- for (auto &flag: flags) {
- argv.push_back(flag.c_str());
- }
-
- // -o $@ -c $<
- argv.push_back("-o");
- argv.push_back(std::move(objectPath));
- argv.push_back("-c");
- argv.push_back(std::move(path));
-
- return argv;
- }
-
- std::vector<std::string> getLinkCommand(
- const std::vector<std::string> &ldFlags,
- const std::vector<std::string> &ldLibs,
- FileType type,
- TargetType targetType,
- const std::vector<std::string> &objs,
- const std::string &path,
- const std::string &outDir) {
- // TODO: Use ar to create STATIC_LIBRARY,
- // use GCC with -shared to make SHARED_LIBRARY
-
- std::vector<std::string> argv;
-
- // $(compiler)
- argv.push_back(getCompilerFor(type));
-
- // $(ldflags)
- for (auto &flag: ldFlags) {
- argv.push_back(flag);
- }
-
- // -o $@
- argv.push_back("-o");
- argv.push_back(targetFilePath(targetType, path, outDir));
-
- // $(objs)
- for (auto &obj: objs) {
- argv.push_back(obj);
- }
-
- // $(ldlibs)
- for (auto &flag: ldLibs) {
- argv.push_back(flag);
- }
-
- return argv;
- }
-
- }
|