123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- //#bb ldlibs := -lpthread
-
- #include <getopt.h>
- #include <stdexcept>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <unordered_set>
- #include <mutex>
-
- #include "SourceFile.h"
- #include "BBParser.h"
- #include "parallel.h"
- #include "toolchain.h"
- #include "globals.h"
- #include "logger.h"
-
- static void findSourcesInDir(std::string dir, std::vector<SourceFile> &sources,
- BBParser::Variables vars);
-
- static void findSources(std::string dir, std::string name,
- std::vector<SourceFile> &sources, BBParser::Variables vars) {
- std::string path = dir + "/" + name;
- sys::FileInfo finfo = sys::fileInfo(path);
-
- if (finfo.isDir) {
- // We don't want to send 'files'
- BBParser::Variables subvars = vars;
- subvars.erase("files");
- findSourcesInDir(path, sources, std::move(subvars));
- } else {
- SourceFile::FileType type = SourceFile::fileTypeFrom(name);
- if (type != SourceFile::FileType::UNKNOWN) {
- sources.emplace_back(dir, name, type, vars);
- }
- }
- }
-
- static void findSourcesInDir(std::string dir, std::vector<SourceFile> &sources,
- BBParser::Variables vars) {
- std::ifstream buildFile(dir + "/build.bb");
- std::vector<std::string> files;
- bool hasFiles = false;
-
- // Parse $dir/build.bb, see if it specifies a 'files' value.
- if (buildFile.good()) {
- BBParser parser(buildFile, BBParser::FLAG_NONE);
- parser.parse(vars);
-
- auto it = vars.find("files");
- if (it != vars.end()) {
- files = it->second;
- hasFiles = true;
- }
- }
-
- // If build.bb didn't specify 'files', we have to readdir
- if (!hasFiles) {
- sys::readDir(dir, files);
- }
-
- // Go through files
- for (auto &ent: files) {
- std::string path = dir + "/" + ent;
- BBParser::Variables subvars = vars;
- findSources(dir, ent, sources, vars);
- }
- }
-
- static void printState(const std::vector<SourceFile> &sources) {
- for (auto &source: sources) {
- std::cout << source.dir() << '/' << source.name() << ":\n";
- for (auto &kv: source.vars()) {
- std::cout << " " << kv.first << ":\n";
- for (auto &val: kv.second) {
- std::cout << " " << val << '\n';
- }
- }
- }
- }
-
- static bool compile(const std::vector<SourceFile> &sources, const std::string &outDir, int jobs) {
- bool compiled = false;
- std::mutex mut;
- parallel::parallel(jobs, sources, [&](const SourceFile &source) {
- if (source.needsRecompile(outDir)) {
- source.compile(outDir);
- mut.lock();
- compiled = true;
- mut.unlock();
- }
- });
-
- return compiled;
- }
-
- static void link(
- const std::vector<SourceFile> &sources,
- const std::string &outDir, const std::string &name,
- toolchain::TargetType targetType) {
- logger::log("Link " + outDir + '/' + name);
-
- std::vector<std::string> ldFlags;
- std::unordered_set<std::string> ldFlagsSet;
- std::vector<std::string> ldLibs;
- std::unordered_set<std::string> ldLibsSet;
- std::vector<std::string> objects;
-
- SourceFile::FileType type = SourceFile::FileType::C;
-
- // Gather ldflags, ldlibs and objects from sources
- std::vector<std::string> flags;
- for (auto &source: sources) {
- if (source.type() != SourceFile::FileType::C) {
- type = source.type();
- }
-
- flags = source.ldFlags();
- for (auto &flag: flags) {
- if (flag[0] == '-' && flag[1] == 'l') {
- std::cerr << "Warning: -l flags should go in ldlibs "
- << "(at " << source.path() << ": " << flag << ")\n";
- }
-
- auto it = ldFlagsSet.find(flag);
- if (it == ldFlagsSet.end()) {
- ldFlagsSet.emplace(flag);
- ldFlags.push_back(std::move(flag));
- }
- }
-
- flags = source.ldLibs();
- for (auto &flag: flags) {
- auto it = ldLibsSet.find(flag);
- if (it == ldLibsSet.end()) {
- ldLibsSet.emplace(flag);
- ldLibs.push_back(std::move(flag));
- }
- }
-
- objects.push_back(std::move(source.objectPath(outDir)));
- }
-
- flags.clear();
-
- toolchain::link(name, ldFlags, ldLibs, type, targetType, objects, outDir);
- }
-
- static bool compileAndLink(
- const std::vector<SourceFile> &sources, const std::string &outDir,
- int jobs, const std::string &target, toolchain::TargetType targetType) {
- std::string targetPath = toolchain::targetFilePath(targetType, target, outDir);
-
- if (compile(sources, outDir, jobs) || !sys::fileExists(targetPath)) {
- link(sources, outDir, target, targetType);
- return true;
- }
-
- return false;
- }
-
- static std::string findTargetName(
- const BBParser::Variables &vars,
- const std::vector<SourceFile> &sources) {
- auto it = vars.find("target");
- if (it != vars.end() && it->second.size() != 0) {
- return it->second[0];
- }
-
- for (auto &source: sources) {
- const std::vector<std::string> *ptr = source.variable("target");
- if (ptr != nullptr && ptr->size() != 0) {
- return (*ptr)[0];
- }
- }
-
- return "target";
- }
-
- int main(int argc, char **argv) {
- logger::LogContext logCtx = logger::init();
-
- std::string outDir = "bbbuild";
- int jobs = parallel::coreCount();
- std::string workDir = "";
- std::string target = "";
-
- enum class Action {
- BUILD, PRINT_STATE,
- };
- Action action = Action::BUILD;
-
- const char *shortopts = "hvo:j:C:t:p";
- const struct option opts[] = {
- { "help", no_argument, NULL, 'h' },
- { "verbose", no_argument, NULL, 'v' },
- { "output", required_argument, NULL, 'o' },
- { "jobs", required_argument, NULL, 'j' },
- { "directory", required_argument, NULL, 'C' },
- { "target", required_argument, NULL, 't' },
- { "print-state", no_argument, NULL, 'p' },
- {},
- };
-
- const char usage[] =
- "Usage: bbbuild [options...] [sources]\n"
- "\n"
- " -h, --help "
- "Show this help text.\n"
- " -v, --verbose "
- "Show every command as it's executing."
- " -o, --output <dir> "
- "Set output directory. Default: bbbuild\n"
- " -j, --jobs <count> "
- "Set the number of jobs run simultaneously. "
- "Default: the number of cores in the machine.\n"
- " -C, --directory <dir> "
- "Change directory before doing anything else.\n"
- " -t, --target <name> "
- "Set the name of the executable.\n"
- " -p, --print-state "
- "Print the state instead of building.\n";
-
- // Parse options from argv
- while (1) {
- int optidx;
- int c = getopt_long(argc, argv, shortopts, opts, &optidx);
-
- if (c < 0) {
- break;
- }
-
- switch (c) {
- case 'h':
- puts(usage);
- return 0;
-
- case 'v':
- global::verbose += 1;
- break;
-
- case 'o':
- outDir = optarg;
- break;
-
- case 'j':
- jobs = atoi(optarg);
- if (jobs <= 0) {
- fprintf(stderr, "Can't run %i jobs.\n", jobs);
- return 1;
- }
- break;
-
- case 'C':
- workDir = optarg;
- break;
-
- case 'p':
- action = Action::PRINT_STATE;
- break;
-
- default:
- printf("Unknown option: '%c'.\n%s", (char)c, usage);
- return 1;
- }
- }
-
- // Change directory?
- if (workDir.size() > 0) {
- fprintf(stderr, "Entering directory '%s'\n", workDir.c_str());
- sys::chdir(workDir);
- }
-
- // Read config from file
- BBParser::Variables variables;
- if (sys::fileExists("build.bb")) {
- std::ifstream stream("build.bb");
- BBParser parser(stream, BBParser::FLAG_NONE);
- parser.parse(variables);
- }
-
- // Read variables from argv
- while (optind < argc) {
- std::istringstream stream(argv[optind++]);
- BBParser parser(stream, BBParser::FLAG_ONE_LINE);
- parser.parse(variables);
- }
-
- // Find source dirs
- std::vector<std::string> sourceDirs;
- {
- auto it = variables.find("files");
- if (it == variables.end()) {
- sys::readDir(".", sourceDirs);
- } else {
- sourceDirs = it->second;
- }
- }
-
- // Read configs from source dirs
- std::vector<SourceFile> sources;
- for (std::string &dir: sourceDirs) {
- findSources(".", dir, sources, variables);
- }
-
- // Find target name
- if (target.size() == 0) {
- target = findTargetName(variables, sources);
- }
-
- switch (action) {
- case Action::BUILD:
- // TODO: Support more types than BINARY
- if (!compileAndLink(sources, outDir, jobs, target, toolchain::TargetType::BINARY)) {
- logger::log("Nothing to do.");
- }
- break;
-
- case Action::PRINT_STATE:
- printState(sources);
- break;
- }
- }
|