2017-04-30 17:37:22 +00:00
|
|
|
/* A build system for libostd. */
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
2017-05-01 15:28:54 +00:00
|
|
|
#include <vector>
|
2017-04-30 17:37:22 +00:00
|
|
|
#include <stdexcept>
|
2017-05-01 22:56:45 +00:00
|
|
|
#include <utility>
|
2017-05-01 15:28:54 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
#include <sys/wait.h>
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
#include "ostd/io.hh"
|
|
|
|
#include "ostd/range.hh"
|
|
|
|
#include "ostd/format.hh"
|
|
|
|
#include "ostd/string.hh"
|
|
|
|
#include "ostd/filesystem.hh"
|
2017-04-30 18:16:24 +00:00
|
|
|
#include "ostd/thread_pool.hh"
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
namespace fs = ostd::filesystem;
|
|
|
|
|
|
|
|
/* ugly, but do not explicitly compile */
|
|
|
|
#include "src/io.cc"
|
2017-04-30 17:37:22 +00:00
|
|
|
|
|
|
|
/* THESE VARIABLES CAN BE ALTERED */
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
static std::string EXAMPLES[] = {
|
2017-04-30 17:37:22 +00:00
|
|
|
"format", "listdir", "range", "range_pipe", "signal",
|
|
|
|
"stream1", "stream2", "coroutine1", "coroutine2", "concurrency"
|
|
|
|
};
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
static fs::path ASM_SOURCE_DIR = "src/asm";
|
|
|
|
static std::string ASM_SOURCES[] = {
|
2017-04-30 17:37:22 +00:00
|
|
|
"jump_all_gas", "make_all_gas", "ontop_all_gas"
|
|
|
|
};
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
static fs::path CXX_SOURCE_DIR = "src";
|
|
|
|
static std::string CXX_SOURCES[] = {
|
2017-05-01 22:03:42 +00:00
|
|
|
"context_stack", "environ", "io", "concurrency"
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
static fs::path TEST_DIR = "tests";
|
|
|
|
static std::string TEST_CASES[] = {
|
2017-04-30 17:37:22 +00:00
|
|
|
"algorithm", "range"
|
|
|
|
};
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
static std::string OSTD_SHARED_LIB = "libostd.so";
|
|
|
|
static std::string OSTD_STATIC_LIB = "libostd.a";
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 17:12:17 +00:00
|
|
|
static std::string DEFAULT_CXXFLAGS = "-std=c++1z -I. -O2 -Wall -Wextra "
|
|
|
|
"-Wshadow -Wold-style-cast";
|
|
|
|
static std::string DEFAULT_LDFLAGS = "-pthread";
|
|
|
|
static std::string DEFAULT_ASFLAGS = "";
|
|
|
|
|
|
|
|
static std::string DEBUG_CXXFLAGS = "-g";
|
|
|
|
|
|
|
|
static std::string SHARED_CXXFLAGS = "-fPIC";
|
|
|
|
static std::string SHARED_LDFLAGS = "-shared";
|
|
|
|
static std::string SHARED_ASFLAGS = "-fPIC";
|
|
|
|
|
2017-04-30 17:37:22 +00:00
|
|
|
/* DO NOT CHANGE PAST THIS POINT */
|
|
|
|
|
|
|
|
static std::string from_env_or(char const *evar, char const *defval) {
|
|
|
|
char const *varv = std::getenv(evar);
|
|
|
|
if (!varv || !varv[0]) {
|
|
|
|
return defval;
|
|
|
|
}
|
|
|
|
return varv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_cross(std::string &arg) {
|
|
|
|
char const *cross = std::getenv("CROSS");
|
|
|
|
if (!cross || !cross[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
arg.insert(0, cross);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_env(std::string &val, char const *evar) {
|
|
|
|
char const *varv = std::getenv(evar);
|
|
|
|
if (!varv || !varv[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
val += ' ';
|
|
|
|
val += varv;
|
|
|
|
}
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
static void print_help(ostd::string_range arg0) {
|
|
|
|
ostd::writef(
|
2017-04-30 17:37:22 +00:00
|
|
|
"%s [options]\n"
|
|
|
|
"Available options:\n"
|
|
|
|
" [no-]examples - (do not) build examples (default: yes)\n"
|
|
|
|
" [no-]test-suite - (do not) build test suite (default: yes)\n"
|
|
|
|
" [no-]static-lib - (do not) build static libostd (default: yes)\n"
|
|
|
|
" [no-]shared-lib - (do not) build shared libostd (default: no)\n"
|
|
|
|
" release - release build (strip, no -g)\n"
|
|
|
|
" debug - debug build (default, no strip, -g)\n"
|
|
|
|
" verbose - print entire commands\n"
|
|
|
|
" clean - remove generated files and exit\n"
|
|
|
|
" help - print this and exit\n",
|
|
|
|
arg0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
static void exec_command(
|
|
|
|
std::string const &cmd, std::vector<std::string> const &args
|
|
|
|
) {
|
|
|
|
auto argp = std::make_unique<char *[]>(args.size() + 2);
|
|
|
|
argp[0] = const_cast<char *>(cmd.data());
|
|
|
|
for (std::size_t i = 0; i < args.size(); ++i) {
|
|
|
|
argp[i + 1] = const_cast<char *>(args[i].data());
|
|
|
|
}
|
|
|
|
argp[args.size() + 1] = nullptr;
|
|
|
|
|
|
|
|
auto pid = fork();
|
|
|
|
if (pid == -1) {
|
2017-05-01 22:56:45 +00:00
|
|
|
throw std::runtime_error{"command failed"};
|
2017-05-01 15:28:54 +00:00
|
|
|
} else if (!pid) {
|
2017-05-01 23:08:01 +00:00
|
|
|
if (execvp(argp[0], argp.get())) {
|
2017-05-01 15:28:54 +00:00
|
|
|
argp.reset();
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (int retc = -1; (waitpid(pid, &retc, 0) == -1) || retc) {
|
2017-05-01 22:56:45 +00:00
|
|
|
auto app = ostd::appender<std::string>();
|
|
|
|
ostd::format(app, "command failed with code %d", retc);
|
|
|
|
throw std::runtime_error{app.get()};
|
2017-05-01 15:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_command(
|
|
|
|
std::string const &cmd, std::vector<std::string> const &args
|
|
|
|
) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::write(cmd);
|
2017-05-01 15:28:54 +00:00
|
|
|
for (auto &s: args) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writef(" %s", s);
|
2017-05-01 15:28:54 +00:00
|
|
|
}
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln();
|
2017-05-01 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_args(std::vector<std::string> &args, std::string const &cmdl) {
|
|
|
|
wordexp_t p;
|
|
|
|
if (wordexp(cmdl.data(), &p, 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (std::size_t i = 0; i < p.we_wordc; ++i) {
|
|
|
|
args.push_back(p.we_wordv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static fs::path path_with_ext(fs::path const &p, fs::path const &ext) {
|
|
|
|
fs::path rp = p;
|
|
|
|
rp.replace_extension(ext);
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:49:27 +00:00
|
|
|
static void try_remove(fs::path const &path, bool all = false) {
|
2017-05-01 15:28:54 +00:00
|
|
|
try {
|
2017-05-01 15:49:27 +00:00
|
|
|
if (all) {
|
|
|
|
fs::remove_all(path);
|
|
|
|
} else {
|
|
|
|
fs::remove(path);
|
|
|
|
}
|
2017-05-01 15:28:54 +00:00
|
|
|
} catch (fs::filesystem_error const &) {}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:49:27 +00:00
|
|
|
static bool verbose = false;
|
|
|
|
|
|
|
|
template<typename ...A>
|
2017-05-01 22:56:45 +00:00
|
|
|
static void echo_q(ostd::string_range fmt, A const &...args) {
|
2017-05-01 15:49:27 +00:00
|
|
|
if (!verbose) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writefln(fmt, args...);
|
2017-05-01 15:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exec_v(
|
|
|
|
std::string const &cmd, std::vector<std::string> const &args
|
|
|
|
) {
|
|
|
|
if (verbose) {
|
|
|
|
print_command(cmd, args);
|
|
|
|
}
|
|
|
|
exec_command(cmd, args);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:37:22 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
bool build_examples = true;
|
|
|
|
bool build_testsuite = true;
|
|
|
|
bool build_static = true;
|
|
|
|
bool build_shared = false;
|
2017-05-01 17:03:47 +00:00
|
|
|
std::string build_cfg = "debug";
|
2017-04-30 17:37:22 +00:00
|
|
|
bool clean = false;
|
|
|
|
|
2017-05-01 17:12:17 +00:00
|
|
|
std::string cxxflags = DEFAULT_CXXFLAGS;
|
|
|
|
std::string ldflags = DEFAULT_LDFLAGS;
|
|
|
|
std::string asflags = DEFAULT_ASFLAGS;
|
2017-04-30 17:37:22 +00:00
|
|
|
|
|
|
|
#define ARG_BOOL(arg, name, var) \
|
2017-05-01 17:03:47 +00:00
|
|
|
if ((arg == name) || (arg == "no-" name)) { \
|
|
|
|
var = (arg.substr(0, 3) != "-no"); \
|
2017-04-30 17:37:22 +00:00
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
2017-05-01 17:03:47 +00:00
|
|
|
std::string arg = argv[i];
|
2017-04-30 17:37:22 +00:00
|
|
|
ARG_BOOL(arg, "examples", build_examples);
|
|
|
|
ARG_BOOL(arg, "test-suite", build_testsuite);
|
|
|
|
ARG_BOOL(arg, "static-lib", build_static);
|
|
|
|
ARG_BOOL(arg, "shared-lib", build_shared);
|
2017-05-01 17:03:47 +00:00
|
|
|
if ((arg == "release") || (arg == "debug")) {
|
2017-04-30 17:37:22 +00:00
|
|
|
build_cfg = arg;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-01 17:03:47 +00:00
|
|
|
if (arg == "verbose") {
|
2017-04-30 17:37:22 +00:00
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-01 17:03:47 +00:00
|
|
|
if (arg == "clean") {
|
2017-04-30 17:37:22 +00:00
|
|
|
clean = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-01 17:03:47 +00:00
|
|
|
if (arg == "help") {
|
2017-04-30 17:37:22 +00:00
|
|
|
print_help(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writefln("unknown argument: %s", arg.data());
|
2017-04-30 17:37:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ARG_BOOL
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
std::string default_lib = OSTD_SHARED_LIB;
|
2017-04-30 17:37:22 +00:00
|
|
|
if (build_static) {
|
|
|
|
default_lib = OSTD_STATIC_LIB;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto strip = from_env_or("STRIP", "strip");
|
|
|
|
auto cxx = from_env_or("CXX", "c++");
|
2017-05-01 15:28:54 +00:00
|
|
|
auto as = from_env_or("AS", "c++");
|
2017-04-30 17:37:22 +00:00
|
|
|
auto ar = from_env_or("AR", "ar");
|
|
|
|
|
|
|
|
add_cross(strip);
|
|
|
|
add_cross(cxx);
|
|
|
|
add_cross(as);
|
|
|
|
add_cross(ar);
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
if (build_cfg == "debug") {
|
2017-05-01 17:12:17 +00:00
|
|
|
cxxflags += ' ';
|
|
|
|
cxxflags += DEBUG_CXXFLAGS;
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_env(cxxflags, "CXXFLAGS");
|
|
|
|
add_env(ldflags, "LDFLAGS");
|
|
|
|
add_env(asflags, "ASFLAGS");
|
|
|
|
|
|
|
|
if (clean) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln("Cleaning...");
|
2017-04-30 17:37:22 +00:00
|
|
|
|
|
|
|
for (auto ex: EXAMPLES) {
|
2017-05-01 15:49:27 +00:00
|
|
|
auto rp = fs::path{"examples"} / ex;
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-04-30 17:37:22 +00:00
|
|
|
rp.replace_extension(".o");
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
for (auto aso: ASM_SOURCES) {
|
2017-05-01 17:03:47 +00:00
|
|
|
auto rp = path_with_ext(ASM_SOURCE_DIR / aso, ".o");
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-05-01 15:49:27 +00:00
|
|
|
rp.replace_filename(std::string{aso} + "_dyn.o");
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
for (auto cso: CXX_SOURCES) {
|
2017-05-01 17:03:47 +00:00
|
|
|
auto rp = path_with_ext(CXX_SOURCE_DIR / cso, ".o");
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-05-01 15:49:27 +00:00
|
|
|
rp.replace_filename(std::string{cso} + "_dyn.o");
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(rp);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(OSTD_STATIC_LIB);
|
|
|
|
try_remove(OSTD_SHARED_LIB);
|
|
|
|
try_remove("test_runner.o");
|
|
|
|
try_remove("test_runner");
|
2017-05-01 15:49:27 +00:00
|
|
|
try_remove("tests", true);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
auto call_cxx = [&](
|
|
|
|
std::string const &input, std::string const &output, bool shared
|
|
|
|
) {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
add_args(args, cxxflags);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
|
|
|
if (shared) {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("CXX (shared): %s", input);
|
2017-05-01 17:12:17 +00:00
|
|
|
add_args(args, SHARED_CXXFLAGS);
|
2017-04-30 17:37:22 +00:00
|
|
|
} else {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("CXX: %s", input);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back("-o");
|
|
|
|
args.push_back(output);
|
|
|
|
args.push_back(input);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
exec_v(cxx, args);
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
/* mostly unnecessary to separately compile shared, but
|
|
|
|
* the files may check for __PIC__ (at least mips32 does)
|
|
|
|
*/
|
|
|
|
auto call_as = [&](
|
|
|
|
std::string const &input, std::string const &output, bool shared
|
|
|
|
) {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
add_args(args, asflags);
|
|
|
|
|
|
|
|
if (shared) {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("AS (shared): %s", input);
|
2017-05-01 17:12:17 +00:00
|
|
|
add_args(args, SHARED_ASFLAGS);
|
2017-05-01 15:28:54 +00:00
|
|
|
} else {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("AS: %s", input);
|
2017-05-01 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back("-o");
|
|
|
|
args.push_back(output);
|
|
|
|
args.push_back(input);
|
|
|
|
|
|
|
|
exec_v(as, args);
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
auto call_ld = [&](
|
|
|
|
std::string const &output,
|
|
|
|
std::vector<std::string> const &files,
|
|
|
|
std::vector<std::string> const &flags
|
|
|
|
) {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("LD: %s", output);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
std::vector<std::string> args;
|
|
|
|
add_args(args, cxxflags);
|
|
|
|
|
|
|
|
args.push_back("-o");
|
|
|
|
args.push_back(output);
|
|
|
|
args.insert(args.cend(), files.begin(), files.end());
|
|
|
|
args.insert(args.cend(), flags.begin(), flags.end());
|
|
|
|
|
|
|
|
add_args(args, ldflags);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
exec_v(cxx, args);
|
|
|
|
|
2017-05-01 17:03:47 +00:00
|
|
|
if (build_cfg == "release") {
|
2017-05-01 15:28:54 +00:00
|
|
|
args.clear();
|
|
|
|
args.push_back(output);
|
|
|
|
exec_v(strip, args);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
auto call_ldlib = [&](
|
|
|
|
std::string const &output, std::vector<std::string> const &files,
|
|
|
|
bool shared
|
|
|
|
) {
|
2017-05-01 17:12:17 +00:00
|
|
|
std::vector<std::string> args;
|
2017-04-30 17:37:22 +00:00
|
|
|
if (shared) {
|
2017-05-01 17:12:17 +00:00
|
|
|
add_args(args, SHARED_CXXFLAGS);
|
|
|
|
add_args(args, SHARED_LDFLAGS);
|
|
|
|
call_ld(output, files, args);
|
2017-04-30 17:37:22 +00:00
|
|
|
} else {
|
2017-05-01 22:56:45 +00:00
|
|
|
echo_q("AR: %s", output);
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
args.push_back("rcs");
|
|
|
|
args.push_back(output);
|
|
|
|
args.insert(args.cend(), files.begin(), files.end());
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
exec_v(ar, args);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
auto build_example = [&](std::string const &name) {
|
|
|
|
auto base = fs::path{"examples"} / name;
|
|
|
|
auto ccf = path_with_ext(base, ".cc");
|
|
|
|
auto obf = path_with_ext(base, ".o");
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
call_cxx(ccf.string(), obf.string(), false);
|
|
|
|
call_ld(base.string(), { obf.string() }, { default_lib });
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(obf);
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
auto build_test = [&](std::string const &name) {
|
2017-05-01 17:03:47 +00:00
|
|
|
auto base = TEST_DIR / name;
|
2017-05-01 15:28:54 +00:00
|
|
|
auto ccf = path_with_ext(base, ".cc");
|
|
|
|
auto obf = path_with_ext(base, ".o");
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
try_remove(ccf);
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::file_stream f{ccf.string(), ostd::stream_mode::WRITE};
|
|
|
|
if (!f.is_open()) {
|
|
|
|
auto app = ostd::appender<std::string>();
|
|
|
|
ostd::format(app, "cannot open '%s' for writing", ccf);
|
|
|
|
throw std::runtime_error{app.get()};
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
2017-05-01 22:56:45 +00:00
|
|
|
f.writef(
|
2017-04-30 17:37:22 +00:00
|
|
|
"#define OSTD_BUILD_TESTS libostd_%s\n"
|
|
|
|
"\n"
|
|
|
|
"#include <ostd/unit_test.hh>\n"
|
|
|
|
"#include <ostd/%s.hh>\n"
|
|
|
|
"#include <ostd/io.hh>\n"
|
|
|
|
"\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" auto [ succ, fail ] = ostd::test::run();\n"
|
|
|
|
" ostd::writeln(succ, \" \", fail);\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n",
|
2017-05-01 22:56:45 +00:00
|
|
|
name, name
|
2017-04-30 17:37:22 +00:00
|
|
|
);
|
2017-05-01 22:56:45 +00:00
|
|
|
f.close();
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
call_cxx(ccf.string(), obf.string(), false);
|
|
|
|
call_ld(base.string(), { obf.string() }, { default_lib });
|
|
|
|
try_remove(obf);
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto build_test_runner = [&]() {
|
|
|
|
call_cxx("test_runner.cc", "test_runner.o", false);
|
2017-05-01 15:28:54 +00:00
|
|
|
call_ld("test_runner", { "test_runner.o" }, { default_lib });
|
|
|
|
try_remove("test_runner.o");
|
2017-04-30 17:37:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
std::vector<std::string> asm_obj, cxx_obj, asm_dynobj, cxx_dynobj;
|
2017-04-30 17:37:22 +00:00
|
|
|
|
2017-04-30 18:16:24 +00:00
|
|
|
ostd::thread_pool tp;
|
|
|
|
tp.start();
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
std::queue<std::future<void>> futures;
|
|
|
|
|
2017-05-01 15:28:54 +00:00
|
|
|
/* build object file in static and shared (PIC) variants */
|
2017-05-01 22:56:45 +00:00
|
|
|
auto build_obj = [&tp, &futures, build_static, build_shared](
|
2017-05-01 15:28:54 +00:00
|
|
|
fs::path const &fpath, fs::path const &sext, auto &buildf,
|
|
|
|
auto &obj, auto &dynobj
|
|
|
|
) {
|
|
|
|
auto srcf = path_with_ext(fpath, sext);
|
2017-04-30 17:37:22 +00:00
|
|
|
if (build_static) {
|
2017-05-01 15:28:54 +00:00
|
|
|
auto srco = path_with_ext(srcf, ".o");
|
2017-05-01 22:56:45 +00:00
|
|
|
futures.push(tp.push([&, srcf, srco]() {
|
2017-05-01 15:28:54 +00:00
|
|
|
buildf(srcf.string(), srco.string(), false);
|
2017-05-01 22:56:45 +00:00
|
|
|
}));
|
2017-05-01 15:28:54 +00:00
|
|
|
obj.push_back(srco.string());
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
if (build_shared) {
|
2017-05-01 15:28:54 +00:00
|
|
|
auto srco = srcf;
|
|
|
|
srco.replace_extension();
|
|
|
|
srco += "_dyn.o";
|
2017-05-01 22:56:45 +00:00
|
|
|
futures.push(tp.push([&, srcf, srco]() {
|
2017-05-01 15:28:54 +00:00
|
|
|
buildf(srcf.string(), srco.string(), true);
|
2017-05-01 22:56:45 +00:00
|
|
|
}));
|
2017-05-01 15:28:54 +00:00
|
|
|
dynobj.push_back(srco.string());
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
2017-05-01 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln("Building the library...");
|
2017-05-01 15:28:54 +00:00
|
|
|
for (auto aso: ASM_SOURCES) {
|
2017-05-01 17:03:47 +00:00
|
|
|
build_obj(ASM_SOURCE_DIR / aso, ".S", call_as, asm_obj, asm_dynobj);
|
2017-05-01 15:28:54 +00:00
|
|
|
}
|
|
|
|
for (auto cso: CXX_SOURCES) {
|
2017-05-01 17:03:47 +00:00
|
|
|
build_obj(CXX_SOURCE_DIR / cso, ".cc", call_cxx, cxx_obj, cxx_dynobj);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
while (!futures.empty()) {
|
|
|
|
/* wait and propagate possible exception */
|
|
|
|
futures.front().get();
|
|
|
|
futures.pop();
|
|
|
|
}
|
2017-04-30 18:16:24 +00:00
|
|
|
|
2017-04-30 17:37:22 +00:00
|
|
|
if (build_static) {
|
2017-05-01 15:28:54 +00:00
|
|
|
std::vector<std::string> all_obj;
|
|
|
|
all_obj.insert(all_obj.cend(), asm_obj.begin(), asm_obj.end());
|
|
|
|
all_obj.insert(all_obj.cend(), cxx_obj.begin(), cxx_obj.end());
|
|
|
|
call_ldlib(OSTD_STATIC_LIB, all_obj, false);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
if (build_shared) {
|
2017-05-01 15:28:54 +00:00
|
|
|
std::vector<std::string> all_obj;
|
|
|
|
all_obj.insert(all_obj.cend(), asm_dynobj.begin(), asm_dynobj.end());
|
|
|
|
all_obj.insert(all_obj.cend(), cxx_dynobj.begin(), cxx_dynobj.end());
|
|
|
|
call_ldlib(OSTD_SHARED_LIB, all_obj, true);
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (build_examples) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln("Building examples...");
|
2017-04-30 17:37:22 +00:00
|
|
|
for (auto ex: EXAMPLES) {
|
2017-05-01 22:56:45 +00:00
|
|
|
futures.push(tp.push([&build_example, ex]() {
|
2017-04-30 18:16:24 +00:00
|
|
|
build_example(ex);
|
2017-05-01 22:56:45 +00:00
|
|
|
}));
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (build_testsuite) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln("Building tests...");
|
2017-04-30 17:37:22 +00:00
|
|
|
build_test_runner();
|
|
|
|
if (!fs::is_directory(TEST_DIR)) {
|
|
|
|
if (!fs::create_directory(TEST_DIR)) {
|
2017-05-01 22:56:45 +00:00
|
|
|
ostd::writeln("Failed creating test directory");
|
2017-04-30 17:37:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto test: TEST_CASES) {
|
2017-05-01 22:56:45 +00:00
|
|
|
futures.push(tp.push([&build_test, test]() {
|
2017-04-30 17:37:22 +00:00
|
|
|
build_test(test);
|
2017-05-01 22:56:45 +00:00
|
|
|
}));
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
2017-04-30 18:16:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 22:56:45 +00:00
|
|
|
while (!futures.empty()) {
|
|
|
|
/* wait and propagate possible exception */
|
|
|
|
futures.front().get();
|
|
|
|
futures.pop();
|
|
|
|
}
|
2017-04-30 18:16:24 +00:00
|
|
|
|
|
|
|
if (build_testsuite) {
|
2017-05-01 17:03:47 +00:00
|
|
|
exec_v("./test_runner", { TEST_DIR.string() });
|
2017-04-30 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|