Browse Source

linkings related stuff

feature/dependency-graph
Martin Dørum 4 years ago
parent
commit
c1017f4ccb
3 changed files with 68 additions and 13 deletions
  1. 33
    11
      src/main.cc
  2. 24
    2
      src/toolchain.cc
  3. 11
    0
      src/toolchain.h

+ 33
- 11
src/main.cc View File

@@ -42,7 +42,7 @@ static void readDir(std::string dir, std::vector<SourceFile> &sources,
std::vector<std::string> files;
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()) {
BBBParser parser(buildFile, BBBParser::FLAG_NONE);
parser.parse(vars);
@@ -109,7 +109,8 @@ static bool compile(const std::vector<SourceFile> &sources, const std::string &o

static void link(
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::vector<std::string> ldFlags;
@@ -155,7 +156,20 @@ static void link(

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) {
@@ -163,19 +177,21 @@ int main(int argc, char **argv) {
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:p";
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' },
{},
};
@@ -194,6 +210,8 @@ int main(int argc, char **argv) {
"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";

@@ -231,6 +249,10 @@ int main(int argc, char **argv) {
workDir = optarg;
break;

case 't':
target = optarg;
break;

case 'p':
action = Action::PRINT_STATE;
break;
@@ -253,6 +275,11 @@ int main(int argc, char **argv) {
}
}

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

// Parse non-opt argv as source dirs
if (optind < argc) {
while (optind < argc) {
@@ -268,17 +295,12 @@ int main(int argc, char **argv) {
readDir(dir, sources, {});
}

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

switch (action) {
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";
}

break;

case Action::PRINT_STATE:

+ 24
- 2
src/toolchain.cc View File

@@ -1,6 +1,7 @@
#include "toolchain.h"

#include <stdexcept>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
@@ -110,6 +111,24 @@ std::string objectFilePath(
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) {
std::vector<const char *> argv;
argv.push_back(getPkgConfig());
@@ -208,9 +227,13 @@ void link(
const std::vector<std::string> &ldFlags,
const std::vector<std::string> &ldLibs,
SourceFile::FileType type,
TargetType targetType,
const std::vector<std::string> &objs,
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;

@@ -240,5 +263,4 @@ void link(
sys::execute(argv, nullptr);
}


}

+ 11
- 0
src/toolchain.h View File

@@ -7,10 +7,20 @@

namespace toolchain {

enum TargetType {
BINARY,
SHARED_LIBRARY,
STATIC_LIBRARY,
};

std::string objectFilePath(
const std::string &srcDir,
const std::string &name,
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 getPkgConfigLDLibs(const std::vector<std::string> &pkgs, std::vector<std::string> &flags);
@@ -34,6 +44,7 @@ void link(
const std::vector<std::string> &ldFlags,
const std::vector<std::string> &ldLibs,
SourceFile::FileType type,
TargetType targetType,
const std::vector<std::string> &objs,
const std::string &outDir);


Loading…
Cancel
Save