|
|
@@ -16,22 +16,29 @@ |
|
|
|
#include "build.h" |
|
|
|
#include "compdb.h" |
|
|
|
|
|
|
|
static void run(std::vector<std::string> args, std::vector<std::pair<std::string, std::string>> kwargs) { |
|
|
|
struct Conf { |
|
|
|
std::vector<std::string> args; |
|
|
|
std::vector<std::string> args2; |
|
|
|
std::vector<std::pair<std::string, std::string>> kwargs; |
|
|
|
bool exec = false; |
|
|
|
}; |
|
|
|
|
|
|
|
static void run(Conf conf) { |
|
|
|
std::string op; |
|
|
|
std::string path; |
|
|
|
|
|
|
|
if (args.size() == 0) { |
|
|
|
if (conf.args.size() == 0) { |
|
|
|
op = "build"; |
|
|
|
path = "./bx-out"; |
|
|
|
} else if (args[0][0] == '.' || args[0][0] == '/') { |
|
|
|
} else if (conf.args[0][0] == '.' || conf.args[0][0] == '/') { |
|
|
|
op = "build"; |
|
|
|
path = args[0]; |
|
|
|
} else if (args.size() == 1) { |
|
|
|
op = args[0]; |
|
|
|
path = conf.args[0]; |
|
|
|
} else if (conf.args.size() == 1) { |
|
|
|
op = conf.args[0]; |
|
|
|
path = "./bx-out"; |
|
|
|
} else if (args.size() == 2) { |
|
|
|
op = args[0]; |
|
|
|
path = args[1]; |
|
|
|
} else if (conf.args.size() == 2) { |
|
|
|
op = conf.args[0]; |
|
|
|
path = conf.args[1]; |
|
|
|
} else { |
|
|
|
// TODO: Print usage instead? |
|
|
|
throw std::runtime_error("Incorrect number of arguments"); |
|
|
@@ -47,7 +54,7 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
parser.parse(variables); |
|
|
|
} |
|
|
|
|
|
|
|
for (auto &pair: kwargs) { |
|
|
|
for (auto &pair: conf.kwargs) { |
|
|
|
bufio::ISStream ss(pair.second); |
|
|
|
BXParser parser(ss); |
|
|
|
auto &list = variables[pair.first]; |
|
|
@@ -55,7 +62,7 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
parser.parseList(variables, list); |
|
|
|
} |
|
|
|
|
|
|
|
if (kwargs.size() > 0) { |
|
|
|
if (conf.kwargs.size() > 0) { |
|
|
|
bufio::OFStream f(path + "/.config.bx"); |
|
|
|
BXWriter w(f); |
|
|
|
w.write(variables); |
|
|
@@ -65,7 +72,7 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
}; |
|
|
|
|
|
|
|
auto buildTree = [&](BXVariables vars) -> std::unique_ptr<DepNode> { |
|
|
|
return buildDepTree(path, std::move(vars)); |
|
|
|
return build::buildDepTree(path, std::move(vars)); |
|
|
|
}; |
|
|
|
|
|
|
|
auto buildCompileCommands = [&](DepNode &root) { |
|
|
@@ -76,7 +83,9 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
}; |
|
|
|
|
|
|
|
if (op == "build") { |
|
|
|
auto root = buildTree(buildVariables()); |
|
|
|
auto vars = buildVariables(); |
|
|
|
std::string targetName = build::findTargetName(vars); |
|
|
|
auto root = buildTree(vars); |
|
|
|
buildCompileCommands(*root); |
|
|
|
|
|
|
|
if (root->hasChanged(path)) { |
|
|
@@ -86,6 +95,20 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
logger::log("Nothing to do."); |
|
|
|
} |
|
|
|
|
|
|
|
if (conf.exec) { |
|
|
|
std::vector<std::string> argv; |
|
|
|
argv.reserve(conf.args2.size() + 1); |
|
|
|
argv.push_back(path + '/' + targetName); |
|
|
|
for (const auto &arg: conf.args2) { |
|
|
|
argv.push_back(std::move(arg)); |
|
|
|
} |
|
|
|
try { |
|
|
|
sys::execute(argv, true); |
|
|
|
} catch (std::exception &ex) { |
|
|
|
logger::log(ex.what()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else if (op == "config") { |
|
|
|
BXVariables variables = buildVariables(); |
|
|
|
|
|
|
@@ -122,6 +145,7 @@ static void run(std::vector<std::string> args, std::vector<std::pair<std::string |
|
|
|
|
|
|
|
int main(int argc, char **argv) { |
|
|
|
int jobs = parallel::coreCount() * 1.2 + 2; |
|
|
|
Conf conf; |
|
|
|
std::string workDir = ""; |
|
|
|
std::string target = ""; |
|
|
|
|
|
|
@@ -131,6 +155,7 @@ int main(int argc, char **argv) { |
|
|
|
{ "verbose", no_argument, NULL, 'v' }, |
|
|
|
{ "jobs", required_argument, NULL, 'j' }, |
|
|
|
{ "directory", required_argument, NULL, 'C' }, |
|
|
|
{ "run", no_argument, NULL, 'R' }, |
|
|
|
{}, |
|
|
|
}; |
|
|
|
|
|
|
@@ -145,10 +170,13 @@ int main(int argc, char **argv) { |
|
|
|
"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"; |
|
|
|
"Change directory before doing anything else.\n" |
|
|
|
" -R, --run <args...> " |
|
|
|
"Run executable after building.\n"; |
|
|
|
|
|
|
|
// Parse options from argv |
|
|
|
while (1) { |
|
|
|
bool parsingOpts = true; |
|
|
|
while (parsingOpts) { |
|
|
|
int optidx; |
|
|
|
int c = getopt_long(argc, argv, shortopts, opts, &optidx); |
|
|
|
|
|
|
@@ -177,6 +205,14 @@ int main(int argc, char **argv) { |
|
|
|
workDir = optarg; |
|
|
|
break; |
|
|
|
|
|
|
|
case 'R': |
|
|
|
conf.exec = true; |
|
|
|
parsingOpts = false; |
|
|
|
for (; optind < argc; ++optind) { |
|
|
|
conf.args2.push_back(argv[optind]); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
printf("Unknown option: '%c'.\n", (char)c); |
|
|
|
printf(usage, argv[0]); |
|
|
@@ -193,18 +229,16 @@ int main(int argc, char **argv) { |
|
|
|
} |
|
|
|
|
|
|
|
// Find args and keyword args |
|
|
|
std::vector<std::string> args; |
|
|
|
std::vector<std::pair<std::string, std::string>> kwargs; |
|
|
|
while (optind < argc) { |
|
|
|
for (; optind < argc; ++optind) { |
|
|
|
char *arg = argv[optind++]; |
|
|
|
char *eq = strchr(arg, '='); |
|
|
|
if (eq == nullptr) { |
|
|
|
args.push_back(arg); |
|
|
|
conf.args.push_back(arg); |
|
|
|
} else { |
|
|
|
kwargs.push_back(std::make_pair( |
|
|
|
conf.kwargs.push_back(std::make_pair( |
|
|
|
std::string(arg, eq - arg), std::string(eq + 1))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
run(std::move(args), std::move(kwargs)); |
|
|
|
run(std::move(conf)); |
|
|
|
} |