Преглед изворни кода

linkings related stuff

feature/dependency-graph
Martin Dørum пре 5 година
родитељ
комит
c1017f4ccb
3 измењених фајлова са 68 додато и 13 уклоњено
  1. 33
    11
      src/main.cc
  2. 24
    2
      src/toolchain.cc
  3. 11
    0
      src/toolchain.h

+ 33
- 11
src/main.cc Прегледај датотеку

std::vector<std::string> files; std::vector<std::string> files;
bool hasFiles = false; bool hasFiles = false;


// Parse $dir/build.bbb, specifies a 'files' value
// Parse $dir/build.bbb, see if it specifies a 'files' value.
if (buildFile.good()) { if (buildFile.good()) {
BBBParser parser(buildFile, BBBParser::FLAG_NONE); BBBParser parser(buildFile, BBBParser::FLAG_NONE);
parser.parse(vars); parser.parse(vars);


static void link( static void link(
const std::vector<SourceFile> &sources, const std::vector<SourceFile> &sources,
const std::string &outDir, const std::string &name) {
const std::string &outDir, const std::string &name,
toolchain::TargetType targetType) {
std::cerr << "Link " << outDir << '/' << name << '\n'; std::cerr << "Link " << outDir << '/' << name << '\n';


std::vector<std::string> ldFlags; std::vector<std::string> ldFlags;


flags.clear(); flags.clear();


toolchain::link(name, ldFlags, ldLibs, type, objects, outDir);
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;
} }


int main(int argc, char **argv) { int main(int argc, char **argv) {
std::string outDir = "bbbuild"; std::string outDir = "bbbuild";
int jobs = parallel::coreCount(); int jobs = parallel::coreCount();
std::string workDir = ""; std::string workDir = "";
std::string target = "";


enum class Action { enum class Action {
BUILD, PRINT_STATE, BUILD, PRINT_STATE,
}; };
Action action = Action::BUILD; Action action = Action::BUILD;


const char *shortopts = "hvo:j:C:p";
const char *shortopts = "hvo:j:C:t:p";
const struct option opts[] = { const struct option opts[] = {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "verbose", no_argument, NULL, 'v' }, { "verbose", no_argument, NULL, 'v' },
{ "output", required_argument, NULL, 'o' }, { "output", required_argument, NULL, 'o' },
{ "jobs", required_argument, NULL, 'j' }, { "jobs", required_argument, NULL, 'j' },
{ "directory", required_argument, NULL, 'C' }, { "directory", required_argument, NULL, 'C' },
{ "target", required_argument, NULL, 't' },
{ "print-state", no_argument, NULL, 'p' }, { "print-state", no_argument, NULL, 'p' },
{}, {},
}; };
"Default: the number of cores in the machine.\n" "Default: the number of cores in the machine.\n"
" -C, --directory <dir> " " -C, --directory <dir> "
"Change directory before doing anything else.\n" "Change directory before doing anything else.\n"
" -t, --target <name> "
"Set the name of the executable.\n"
" -p, --print-state " " -p, --print-state "
"Print the state instead of building.\n"; "Print the state instead of building.\n";


workDir = optarg; workDir = optarg;
break; break;


case 't':
target = optarg;
break;

case 'p': case 'p':
action = Action::PRINT_STATE; action = Action::PRINT_STATE;
break; break;
} }
} }


// TODO: Read this from build.bbb
if (target.size() == 0) {
target = "target";
}

// Parse non-opt argv as source dirs // Parse non-opt argv as source dirs
if (optind < argc) { if (optind < argc) {
while (optind < argc) { while (optind < argc) {
readDir(dir, sources, {}); readDir(dir, sources, {});
} }


// TODO: Get this from somewhere
std::string target = "target";

switch (action) { switch (action) {
case Action::BUILD: case Action::BUILD:
if (compile(sources, outDir, jobs)) {
link(sources, outDir, target);
} else {
// TODO: Support more types than BINARY
if (!compileAndLink(sources, outDir, jobs, target, toolchain::TargetType::BINARY)) {
std::cerr << "Nothing to do.\n"; std::cerr << "Nothing to do.\n";
} }

break; break;


case Action::PRINT_STATE: case Action::PRINT_STATE:

+ 24
- 2
src/toolchain.cc Прегледај датотеку

#include "toolchain.h" #include "toolchain.h"


#include <stdexcept> #include <stdexcept>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
return outDir + '/' + srcDir + '/' + name + ".o"; return outDir + '/' + srcDir + '/' + name + ".o";
} }


std::string targetFilePath(
TargetType type,
const std::string &name,
const std::string &outDir) {
std::string base = outDir + '/' + name;

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) { void getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags) {
std::vector<const char *> argv; std::vector<const char *> argv;
argv.push_back(getPkgConfig()); argv.push_back(getPkgConfig());
const std::vector<std::string> &ldFlags, const std::vector<std::string> &ldFlags,
const std::vector<std::string> &ldLibs, const std::vector<std::string> &ldLibs,
SourceFile::FileType type, SourceFile::FileType type,
TargetType targetType,
const std::vector<std::string> &objs, const std::vector<std::string> &objs,
const std::string &outDir) { const std::string &outDir) {
const std::string outPath = outDir + '/' + name;
const std::string outPath = targetFilePath(targetType, name, outDir);

// TODO: Use ar to create STATIC_LIBRARY,
// use GCC with -shared to make SHARED_LIBRARY


std::vector<const char *> argv; std::vector<const char *> argv;


sys::execute(argv, nullptr); sys::execute(argv, nullptr);
} }



} }

+ 11
- 0
src/toolchain.h Прегледај датотеку



namespace toolchain { namespace toolchain {


enum TargetType {
BINARY,
SHARED_LIBRARY,
STATIC_LIBRARY,
};

std::string objectFilePath( std::string objectFilePath(
const std::string &srcDir, const std::string &srcDir,
const std::string &name, const std::string &name,
const std::string &outDir); const std::string &outDir);
std::string targetFilePath(
TargetType type,
const std::string &name,
const std::string &outDir);


void getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags); void getPkgConfigFlags(const std::vector<std::string> &pkgs, std::vector<std::string> &flags);
void getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags); void getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags);
const std::vector<std::string> &ldFlags, const std::vector<std::string> &ldFlags,
const std::vector<std::string> &ldLibs, const std::vector<std::string> &ldLibs,
SourceFile::FileType type, SourceFile::FileType type,
TargetType targetType,
const std::vector<std::string> &objs, const std::vector<std::string> &objs,
const std::string &outDir); const std::string &outDir);



Loading…
Откажи
Сачувај