diff options
73 files changed, 7721 insertions, 1685 deletions
diff --git a/.gitmodules b/.gitmodules index c765825..c12c38f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "getoptpp"] - path = src/getoptpp - url = git://git.drumgizmo.org/getoptpp.git [submodule "test/uunit"] path = test/uunit url = git://git.drumgizmo.org/uunit.git diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..a558359 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,74 @@ +pipeline { + agent any + stages { + stage('all targets') + { + parallel { + //////////////////////////////////////////////////// + stage('MacOSX clang') { + agent { label 'macos' } + steps { + echo 'Cleaning workspace ...' + sh 'git clean -d -x -f' + echo 'Building (clang) ...' + sh 'CXX=/usr/local/opt/llvm/bin/clang++ LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind" ./bootstrap.sh' + echo 'Testing (clang) ...' + sh './ctor check' + echo 'Testing suite (clang) ...' + sh '(cd test/suite; CTORDIR=../../build CXX=/usr/local/opt/llvm/bin/clang++ LDFLAGS="-L/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/llvm/lib/unwind -lunwind" ./test.sh)' + } + post { + always { + xunit(thresholds: [ skipped(failureThreshold: '0'), + failed(failureThreshold: '0') ], + tools: [ CppUnit(pattern: 'build/test/*.xml') ]) + } + } + } + //////////////////////////////////////////////////// + stage('Linux gcc') { + agent { label 'linux && gcc && c++20' } + steps { + echo 'Cleaning workspace ...' + sh 'git clean -d -x -f' + echo 'Building (gcc) ...' + sh 'CXX=g++ ./bootstrap.sh' + echo 'Testing (gcc) ...' + sh './ctor check' + echo 'Testing suite (gcc) ...' + sh '(cd test/suite; CTORDIR=../../build CXX=g++ ./test.sh)' + } + post { + always { + xunit(thresholds: [ skipped(failureThreshold: '0'), + failed(failureThreshold: '0') ], + tools: [ CppUnit(pattern: 'build/test/*.xml') ]) + } + } + } + //////////////////////////////////////////////////// + stage('Linux clang') { + agent { label 'linux && clang && c++20' } + steps { + echo 'Cleaning workspace ...' + sh 'git clean -d -x -f' + echo 'Building (clang) ...' + sh 'CXX=clang++ ./bootstrap.sh' + echo 'Testing (clang) ...' + sh './ctor check' + echo 'Testing suite (clang) ...' + sh '(cd test/suite; CTORDIR=../../build CXX=clang++ ./test.sh)' + } + post { + always { + xunit(thresholds: [ skipped(failureThreshold: '0'), + failed(failureThreshold: '0') ], + tools: [ CppUnit(pattern: 'build/test/*.xml') ]) + } + } + } + //////////////////////////////////////////////////// + } + } + } +} @@ -16,7 +16,7 @@ Step 1: Create a build configuration, in C++ A really simple example ('hello_config.cc'): ```c++ -#include "libctor.h" +#include "ctor.h" namespace { diff --git a/bootstrap.sh b/bootstrap.sh index a5c11ac..4dd57ae 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,7 +1,12 @@ #!/bin/sh +set -e + +: ${CXX:=c++} +: ${BUILDDIR:=build} + echo "Bootstrapping..." -g++ -std=c++20 -Wall -O3 -Isrc -pthread src/bootstrap.cc ctor.cc test/ctor.cc -o ctor && \ -./ctor && \ -g++ -std=c++20 -Wall -O3 -Isrc -pthread ctor.cc test/ctor.cc -Lbuild -lctor -o ctor && \ -./ctor configure --ctor-includedir=src --ctor-libdir=build && \ +$CXX $LDFLAGS $CXXFLAGS -std=c++20 -Wall -O3 -Isrc -pthread src/bootstrap.cc ctor.cc -o ctor +./ctor +$CXX $LDFLAGS $CXXFLAGS -std=c++20 -Wall -O3 -Isrc -pthread ctor.cc test/ctor.cc -L$BUILDDIR -lctor -o ctor +./ctor configure --ctor-includedir=src --ctor-libdir=$BUILDDIR --build-dir=$BUILDDIR echo "Done. Now run ./ctor to (re)build." @@ -1,23 +1,25 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include <libctor.h> +#include <ctor.h> namespace { -BuildConfigurations ctorConfigs(const Settings& settings) +ctor::build_configurations ctorConfigs(const ctor::settings& settings) { return { { - .type = TargetType::StaticLibrary, + .system = ctor::output_system::build, .target = "libctor.a", .sources = { "src/build.cc", "src/configure.cc", + "src/deps.cc", "src/execute.cc", "src/externals_manual.cc", "src/libctor.cc", + "src/pointerlist.cc", "src/rebuild.cc", "src/task.cc", "src/task_ar.cc", @@ -26,6 +28,7 @@ BuildConfigurations ctorConfigs(const Settings& settings) "src/task_ld.cc", "src/task_so.cc", "src/tasks.cc", + "src/tools.cc", "src/util.cc", "src/unittest.cc", }, @@ -36,6 +39,10 @@ BuildConfigurations ctorConfigs(const Settings& settings) "-g", "-Wall", "-Werror", + "-Wextra", + "-Wshadow", + "-Wconversion", +// "-Wnrvo", "-Isrc", }, }, diff --git a/examples/cppbuild.cc b/examples/cppbuild.cc index eafac8d..9e1014f 100644 --- a/examples/cppbuild.cc +++ b/examples/cppbuild.cc @@ -1,4 +1,4 @@ -#include "libctor.h" +#include "ctor.h" namespace { diff --git a/examples/ctor.cc b/examples/ctor.cc index 1a02e90..6ed8c6c 100644 --- a/examples/ctor.cc +++ b/examples/ctor.cc @@ -1,7 +1,7 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include "libctor.h" +#include "ctor.h" namespace { diff --git a/examples/subdir/ctor.cc b/examples/subdir/ctor.cc index b5f5885..2e3de9f 100644 --- a/examples/subdir/ctor.cc +++ b/examples/subdir/ctor.cc @@ -1,7 +1,7 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include "../libctor.h" +#include "../ctor.h" namespace { diff --git a/src/argparser.h b/src/argparser.h new file mode 100644 index 0000000..c5337e0 --- /dev/null +++ b/src/argparser.h @@ -0,0 +1,477 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <functional> +#include <variant> +#include <optional> +#include <string> +#include <type_traits> +#include <stdexcept> +#include <iostream> +#include <limits> + +namespace arg +{ +struct noarg {}; +template<typename T> +struct Opt +{ + char shortopt; + std::string longopt; + std::function<int(T)> cb; + std::string help; + T t{}; +}; + +template<> +struct Opt<noarg> +{ + char shortopt; + std::string longopt; + std::function<int()> cb; + std::string help; + noarg t{}; +}; + +template<typename Callable, typename... Args> +auto call_if(Callable cb, Args... args) +{ + using Ret = std::invoke_result_t<decltype(cb), Args&&...>; + if constexpr (std::is_same_v<Ret, void>) + { + if(cb) + { + return cb(std::forward<Args>(args)...); + } + } + else + { + if(cb) + { + return cb(std::forward<Args>(args)...); + } + return Ret{}; + } +} + +enum class error +{ + missing_arg, + invalid_arg, + invalid_opt, +}; + +template<typename... Ts> +class Parser +{ +public: + struct missing_arg{}; + + Parser(int argc_, const char* const* argv_) + : argc(argc_) + , argv(argv_) + { + } + + int parse() const + { + bool demarcate{false}; + for(int i = 1; i < argc; ++i) // skip argv[0] which is program name + { + std::string_view arg{argv[i]}; + if(arg.size() == 0) + { + // Empty arg - This shouldn't happen + continue; + } + + if(arg[0] != '-' || demarcate) // positional arg + { + auto res = call_if(pos_cb, arg); + if(res != 0) + { + return res; + } + continue; + } + + if(arg == "--") + { + demarcate = true; + continue; + } + + bool was_handled{false}; + enum class state { handled, unhandled }; + + if(arg.size() > 1 && arg[0] == '-' && arg[1] == '-') // long + { + for(const auto& option : options) + { + auto ret = + std::visit([&](auto&& opt) -> std::pair<int, state> + { + if(opt.longopt != arg && + !arg.starts_with(opt.longopt+'=')) + { + return {0, state::unhandled}; + } + try + { + using T = std::decay_t<decltype(opt)>; + if constexpr (std::is_same_v<T, Opt<noarg>>) + { + return {opt.cb(), state::handled}; + } + else + { + return {opt.cb(convert(i, opt.t)), + state::handled}; + } + } + catch(std::invalid_argument&) + { + call_if(err_cb, error::invalid_arg, argv[i]); + return {1, state::handled}; + } + catch(missing_arg&) + { + call_if(err_cb, error::missing_arg, argv[i]); + return {1, state::handled}; + } + }, option); + if(ret.second == state::handled && ret.first != 0) + { + return ret.first; + } + was_handled |= ret.second == state::handled; + if(was_handled) + { + break; + } + } + } + else + if(arg.size() > 1 && arg[0] == '-') // short + { + for(auto index = 1u; index < arg.size(); ++index) + { + was_handled = false; + for(const auto& option : options) + { + auto ret = + std::visit([&](auto&& opt) -> std::pair<int, state> + { + char c = arg[index]; + if(opt.shortopt != c) + { + return {0, state::unhandled}; + } + try + { + using T = std::decay_t<decltype(opt)>; + if constexpr (std::is_same_v<T, Opt<noarg>>) + { + return {opt.cb(), state::handled}; + } + else + { + // Note: the rest of arg is converted to opt + auto idx = index; + // set index out of range all was eaten as arg + index = std::numeric_limits<int>::max(); + return {opt.cb(convert_short(&arg[idx], + i, opt.t)), + state::handled}; + } + } + catch(std::invalid_argument&) + { + call_if(err_cb, error::invalid_arg, argv[i]); + return {1, state::handled}; + } + catch(missing_arg&) + { + call_if(err_cb, error::missing_arg, argv[i]); + return {1, state::handled}; + } + }, option); + if(ret.second == state::handled && ret.first != 0) + { + return ret.first; + } + was_handled |= ret.second == state::handled; + if(was_handled) + { + break; + } + } + } + } + + if(!was_handled) + { + call_if(err_cb, error::invalid_opt, arg); + return 1; + } + } + return 0; + } + + template<typename T> + void add(char shortopt, + const std::string& longopt, + std::function<int(T)> cb, + const std::string& help) + { + options.emplace_back(Opt<T>{shortopt, longopt, cb, help}); + } + + void add(char shortopt, + const std::string& longopt, + std::function<int()> cb, + const std::string& help) + { + options.emplace_back(Opt<noarg>{shortopt, longopt, cb, help}); + } + + void set_pos_cb(std::function<int(std::string_view)> cb) + { + pos_cb = cb; + } + + void set_err_cb(std::function<void(error, std::string_view)> cb) + { + err_cb = cb; + } + + std::string prog_name() const + { + if(argc < 1) + { + return {}; + } + return argv[0]; + } + + void help() const + { + constexpr std::size_t width{26}; + constexpr std::size_t column_width{80}; + + for(const auto& option : options) + { + std::visit( + [&](auto&& opt) + { + std::string _args; + using T = std::decay_t<decltype(opt)>; + if constexpr (std::is_same_v<T, Opt<noarg>>) + { + } + else if constexpr (std::is_same_v<T, Opt<int>>) + { + _args = "<int>"; + } + else if constexpr (std::is_same_v<T, Opt<std::optional<int>>>) + { + _args = "[int]"; + } + else if constexpr (std::is_same_v<T, Opt<std::string>>) + { + _args = "<str>"; + } + else if constexpr (std::is_same_v<T, Opt<std::optional<std::string>>>) + { + _args = "[str]"; + } + else if constexpr (std::is_same_v<T, Opt<double>>) + { + _args = "<real>"; + } + else if constexpr (std::is_same_v<T, Opt<std::optional<double>>>) + { + _args = "[real]"; + } + else + { + static_assert(std::is_same_v<T, void>, "missing"); + } + + std::string option_str; + if(opt.shortopt != '\0' && !opt.longopt.empty()) + { + option_str = " -" + std::string(1, opt.shortopt) + ", " + + opt.longopt + " " + _args; + } + else if(opt.shortopt != '\0') + { + option_str = " -" + std::string(1, opt.shortopt) + _args; + } + else if(!opt.longopt.empty()) + { + option_str = " " + std::string(opt.longopt) + " " + _args; + } + + std::string padding; + if(option_str.size() < width) + { + padding.append(width - option_str.size(), ' '); + } + else + { + padding = "\n"; + padding.append(width, ' '); + } + + std::cout << option_str << padding; + + auto i = width; + for(auto c : opt.help) + { + if((c == '\n') || (i > column_width && (c == ' ' || c == '\t'))) + { + std::string _padding(width, ' '); + std::cout << '\n' << _padding; + i = width; + continue; + } + std::cout << c; + ++i; + } + std::cout << '\n'; + }, option); + } + } + +private: + template<typename T> + T convert(int& i, T) const + { + auto opt = convert(i, std::optional<T>{}); + if(!opt) + { + throw missing_arg{}; + } + return *opt; + } + + template<typename T> + std::optional<T> convert(int& i, std::optional<T>) const + { + std::string arg; + bool has_arg{false}; + std::string opt = argv[i]; + if(opt.starts_with("--")) + { + // long opt + auto equals_pos = opt.find('='); + if(equals_pos != std::string::npos) + { + arg = opt.substr(equals_pos + 1); + has_arg = true; + } + else if(i+1 < argc) + { + arg = argv[i+1]; + has_arg = !arg.starts_with("-"); + if(has_arg) + { + ++i; + } + } + } + + if(!has_arg) + { + return {}; + } + + if constexpr (std::is_same_v<T, int>) + { + return std::stoi(arg); + } + else if constexpr (std::is_same_v<T, double>) + { + return std::stod(arg); + } + else if constexpr (std::is_same_v<T, std::string>) + { + return arg; + } + else + { + static_assert(std::is_same_v<T, void>, "missing"); + } + return {}; + } + + template<typename T> + T convert_short(const char* arg_, int& i, T) const + { + auto opt = convert_short(arg_, i, std::optional<T>{}, false); + if(!opt) + { + throw missing_arg{}; + } + return *opt; + } + + template<typename T> + std::optional<T> convert_short(const char* arg_, int& i, + std::optional<T>, bool optional = true) const + { + std::string arg; + bool has_arg{false}; + std::string opt = arg_; + if(opt.length() > 1) + { + // arg in same token + arg = opt.substr(1); + has_arg = true; + } + else if(!optional && i+1 < argc) + { + arg = argv[i+1]; + has_arg = true;//!arg.starts_with("-"); + if(has_arg) + { + ++i; + } + } + + if(!has_arg) + { + return {}; + } + + if constexpr (std::is_same_v<T, int>) + { + return std::stoi(arg); + } + else if constexpr (std::is_same_v<T, double>) + { + return std::stod(arg); + } + else if constexpr (std::is_same_v<T, std::string>) + { + return arg; + } + else + { + static_assert(std::is_same_v<T, void>, "missing"); + } + return {}; + } + + using Opts = std::variant<Opt<noarg>, Opt<Ts>...>; + std::vector<Opts> options; + std::function<int(std::string_view)> pos_cb; + int argc; + const char* const* argv; + std::function<void(error, std::string_view)> err_cb; +}; + +} // arg:: diff --git a/src/bootstrap.cc b/src/bootstrap.cc index 6e4ef19..836504e 100644 --- a/src/bootstrap.cc +++ b/src/bootstrap.cc @@ -3,10 +3,12 @@ // See accompanying file LICENSE for details. #include <iostream> #include <array> +#include <cstdlib> +#include <span> #define BOOTSTRAP -#include "libctor.h" +#include "ctor.h" #include "util.cc" #include "rebuild.cc" @@ -16,39 +18,107 @@ #include "execute.cc" #include "tasks.cc" #include "build.cc" +#include "tools.cc" +#include "pointerlist.cc" -std::filesystem::path configurationFile("configuration.cc"); -std::filesystem::path configHeaderFile("config.h"); +const std::filesystem::path configurationFile("configuration.cc"); +const std::filesystem::path configHeaderFile("config.h"); -const Configuration default_configuration{}; -const Configuration& configuration() +const ctor::configuration& ctor::get_configuration() { - return default_configuration; + static ctor::configuration cfg; + static bool initialised{false}; + if(!initialised) + { + cfg.build_toolchain = getToolChain(cfg.get(ctor::cfg::build_cxx, "/usr/bin/g++")); + initialised = true; + } + + return cfg; } -bool hasConfiguration(const std::string& key) +bool ctor::configuration::has(const std::string& key) const { return false; } -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue) +std::string ctor::configuration::get(const std::string& key, const std::string& default_value) const +{ + static auto paths = get_paths(); + auto cxx_env = std::getenv("CXX"); + if(key == ctor::cfg::build_cxx && cxx_env) + { + static auto cxx_prog = locate(cxx_env, paths); + return cxx_prog; + } + + auto cc_env = std::getenv("CC"); + if(key == ctor::cfg::build_cc && cc_env) + { + static auto cc_prog = locate(cc_env, paths); + return cc_prog; + } + + auto ld_env = std::getenv("LD"); + if(key == ctor::cfg::build_ld && ld_env) + { + static auto ld_prog = locate(ld_env, paths); + return ld_prog; + } + + auto ar_env = std::getenv("AR"); + if(key == ctor::cfg::build_ar && ar_env) + { + static auto ar_prog = locate(ar_env, paths); + return ar_prog; + } + + auto builddir_env = std::getenv("BUILDDIR"); + if(key == ctor::cfg::builddir && builddir_env) + { + return builddir_env; + } + + return default_value; +} + +std::string ctor::configuration::getenv(const std::string& key) const +{ + auto envit = env.find(key); + if(envit != env.end()) + { + return envit->second; + } + + auto env = std::getenv(key.data()); + if(env) + { + return env; + } + + return {}; +} + +std::vector<std::string> readDeps(const std::string& depFile, + ctor::toolchain toolchain) { - return defaultValue; + return {}; } int main(int argc, char* argv[]) { - if(argc > 1) + auto args = std::span(argv, static_cast<std::size_t>(argc)); + if(args.size() > 1) { - std::cerr << "This is a minimal bootstrap version of " << argv[0] << + std::cerr << "This is a minimal bootstrap version of " << args[0] << " which doesn't support any arguments\n"; return 1; } - Settings settings{}; + ctor::settings settings{}; - settings.builddir = getConfiguration(cfg::builddir, "build"); + const auto& c = ctor::get_configuration(); + settings.builddir = c.get(ctor::cfg::builddir, settings.builddir); settings.parallel_processes = std::max(1u, std::thread::hardware_concurrency() * 2 - 1); settings.verbose = 0; @@ -65,8 +135,8 @@ int main(int argc, char* argv[]) auto& targets = getTargets(settings); for(const auto& target : targets) { - if(target.config.type != TargetType::UnitTest && - target.config.type != TargetType::UnitTestLib) + if(target.config.type != ctor::target_type::unit_test && + target.config.type != ctor::target_type::unit_test_library) { non_unittest_targets.push_back(target); } diff --git a/src/build.cc b/src/build.cc index 081c156..5995fb7 100644 --- a/src/build.cc +++ b/src/build.cc @@ -4,21 +4,20 @@ #include "build.h" #include <future> -#include <vector> #include <iostream> #include <chrono> -#include <set> #include <thread> #include <list> +#include <algorithm> -#include "libctor.h" +#include "ctor.h" using namespace std::chrono_literals; -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, - const std::set<std::shared_ptr<Task>>& tasks, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun) { if(settings.verbose > 1) @@ -26,19 +25,20 @@ int build(const Settings& settings, std::cout << "Building '" << name << "'\n"; } - std::set<std::shared_ptr<Task>> dirtyTasks; + std::vector<std::shared_ptr<Task>> dirtyTasks; for(auto task : tasks) { - if(task->dirty()) + if(task->dirty() && + std::find(dirtyTasks.begin(), dirtyTasks.end(), task) == dirtyTasks.end()) { - dirtyTasks.insert(task); + dirtyTasks.push_back(task); } } // Dry-run returns number of dirty tasks but otherwise does nothing. if(dryrun) { - return dirtyTasks.size(); + return static_cast<int>(dirtyTasks.size()); } if(dirtyTasks.empty()) @@ -65,7 +65,7 @@ int build(const Settings& settings, break; } - auto task = getNextTask(all_tasks, dirtyTasks); + auto task = getNextTask(settings, all_tasks, dirtyTasks); if(task == nullptr) { if(processes.empty() && !dirtyTasks.empty()) @@ -84,7 +84,9 @@ int build(const Settings& settings, return task->run(); })); started_one = true; - std::this_thread::sleep_for(2ms); + // Make sure we don't start tasks on top of each other to prevent + // straining the disk. + std::this_thread::sleep_for(50ms); } for(auto process = processes.begin(); @@ -106,27 +108,21 @@ int build(const Settings& settings, break; } - if(started_one) + if(!started_one) // prevent polling too fast if no task is yet ready { - std::this_thread::sleep_for(2ms); - } - else - { - std::this_thread::sleep_for(200ms); + std::this_thread::sleep_for(10ms); } } - for(auto process = processes.begin(); - process != processes.end(); - ++process) + for(auto& process : processes) { - if(process->valid() == false) + if(process.valid() == false) { continue; } - process->wait(); - auto ret = process->get(); - if(ret != 0) + process.wait(); + auto ret = process.get(); + if (ret != 0) { return ret; } @@ -135,30 +131,50 @@ int build(const Settings& settings, return 0; } -namespace +std::vector<std::shared_ptr<Task>> getDepTasks(std::shared_ptr<Task> task, + std::vector<std::shared_ptr<Task>> trace) { -std::set<std::shared_ptr<Task>> getDepTasks(std::shared_ptr<Task> task) -{ - std::set<std::shared_ptr<Task>> tasks; - tasks.insert(task); + std::vector<std::shared_ptr<Task>> tasks; + tasks.push_back(task); + trace.push_back(task); auto deps = task->getDependsTasks(); for(const auto& dep : deps) { - auto depSet = getDepTasks(dep); - for(const auto& dep : depSet) + if(std::find(trace.begin(), trace.end(), dep) != trace.end()) { - tasks.insert(dep); + trace.push_back(dep); + std::cerr << "Error: Cyclic dependency detected: "; + bool first{true}; + for(auto t : trace) + { + if(!first) + { + std::cerr << " -> "; + } + + first = false; + std::cerr << t->target(); + } + std::cerr << '\n'; + throw 1; + } + auto depSet = getDepTasks(dep, trace); + for(const auto& dep_inner : depSet) + { + if(std::find(tasks.begin(), tasks.end(), dep_inner) == tasks.end()) + { + tasks.push_back(dep_inner); + } } } return tasks; } -} -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun) { bool task_found{false}; @@ -168,17 +184,27 @@ int build(const Settings& settings, { task_found = true; - auto depSet = getDepTasks(task); - std::set<std::shared_ptr<Task>> ts; - for(const auto& task : depSet) + try { - ts.insert(task); - } + auto depSet = getDepTasks(task); + std::vector<std::shared_ptr<Task>> ts; + for(const auto& task_inner : depSet) + { + if(std::find(ts.begin(), ts.end(), task_inner) == ts.end()) + { + ts.push_back(task_inner); + } + } - auto ret = build(settings, name, ts, all_tasks, dryrun); - if(ret != 0) + auto ret = build(settings, name, ts, all_tasks, dryrun); + if(ret != 0) + { + return ret; + } + } + catch(...) { - return ret; + return 1; // cycle detected } break; @@ -194,14 +220,14 @@ int build(const Settings& settings, return 0; } -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, const std::vector<Target>& targets, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun) { bool task_found{false}; - std::set<std::shared_ptr<Task>> ts; + std::vector<std::shared_ptr<Task>> ts; for(const auto& target : targets) { @@ -212,10 +238,20 @@ int build(const Settings& settings, { task_found = true; - auto depSet = getDepTasks(task); - for(const auto& task : depSet) + try + { + auto depSet = getDepTasks(task); + for(const auto& task_inner : depSet) + { + if(std::find(ts.begin(), ts.end(), task_inner) == ts.end()) + { + ts.push_back(task_inner); + } + } + } + catch(...) { - ts.insert(task); + return 1; // cycle detected } } } diff --git a/src/build.h b/src/build.h index caf7a68..7296f76 100644 --- a/src/build.h +++ b/src/build.h @@ -4,30 +4,37 @@ #pragma once #include <string> -#include <set> +#include <vector> #include <memory> #include "task.h" #include "tasks.h" -struct Settings; +namespace ctor { +struct settings; +} // namespace ctor:: //! Dry-run returns number of dirty tasks but otherwise does nothing. -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, - const std::set<std::shared_ptr<Task>>& tasks, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun = false); //! Dry-run returns number of dirty tasks but otherwise does nothing. -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun = false); //! Dry-run returns number of dirty tasks but otherwise does nothing. -int build(const Settings& settings, +int build(const ctor::settings& settings, const std::string& name, const std::vector<Target>& targets, - const std::set<std::shared_ptr<Task>>& all_tasks, + const std::vector<std::shared_ptr<Task>>& all_tasks, bool dryrun = false); + +// Recursively build vector of dependency tasks from source task. +// Throws if a cycle is detected. +std::vector<std::shared_ptr<Task>> getDepTasks(std::shared_ptr<Task> task, + std::vector<std::shared_ptr<Task>> trace = {}); diff --git a/src/configure.cc b/src/configure.cc index bc7d3e8..910b878 100644 --- a/src/configure.cc +++ b/src/configure.cc @@ -7,119 +7,137 @@ #include <filesystem> #include <fstream> #include <optional> - -#include <getoptpp/getoptpp.hpp> +#include <span> +#include <cstring> #include "execute.h" -#include "libctor.h" +#include "ctor.h" #include "tasks.h" #include "rebuild.h" #include "externals.h" +#include "tools.h" +#include "util.h" +#include "argparser.h" -std::filesystem::path configurationFile("configuration.cc"); -std::filesystem::path configHeaderFile("config.h"); +const std::filesystem::path configurationFile("configuration.cc"); +const std::filesystem::path configHeaderFile("config.h"); std::map<std::string, std::string> external_includedir; std::map<std::string, std::string> external_libdir; -const Configuration default_configuration{}; -const Configuration& __attribute__((weak)) configuration() +const ctor::configuration& __attribute__((weak)) ctor::get_configuration() { - return default_configuration; + static ctor::configuration cfg; + static bool initialised{false}; + if(!initialised) + { + std::string cxx_prog{"c++"}; + get_env("CXX", cxx_prog); + + cfg.build_toolchain = getToolChain(cfg.get(ctor::cfg::build_cxx, cxx_prog)); + + initialised = true; + } + return cfg; } -namespace ctor -{ +namespace ctor { std::optional<std::string> includedir; std::optional<std::string> libdir; -} +std::optional<std::string> builddir; +std::map<std::string, std::string> conf_values; +} // ctor:: -bool hasConfiguration(const std::string& key) +bool ctor::configuration::has(const std::string& key) const { - if(key == cfg::ctor_includedir && ctor::includedir) + if(key == ctor::cfg::ctor_includedir && ctor::includedir) + { + return true; + } + + if(key == ctor::cfg::ctor_libdir && ctor::libdir) + { + return true; + } + + if(key == ctor::cfg::builddir && ctor::builddir) { return true; } - if(key == cfg::ctor_libdir && ctor::libdir) + if(ctor::conf_values.find(key) != ctor::conf_values.end()) { return true; } - const auto& c = configuration(); - return c.tools.find(key) != c.tools.end(); + return tools.find(key) != tools.end(); } -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue) +std::string ctor::configuration::get(const std::string& key, + const std::string& default_value) const { - if(key == cfg::ctor_includedir && ctor::includedir) + if(key == ctor::cfg::ctor_includedir && ctor::includedir) { return *ctor::includedir; } - if(key == cfg::ctor_libdir && ctor::libdir) + if(key == ctor::cfg::ctor_libdir && ctor::libdir) { return *ctor::libdir; } - const auto& c = configuration(); - if(hasConfiguration(key)) + if(key == ctor::cfg::builddir && ctor::builddir) { - return c.tools.at(key); + return *ctor::builddir; } - return defaultValue; -} + if(ctor::conf_values.find(key) != ctor::conf_values.end()) + { + return ctor::conf_values[key]; + } -std::string locate(const std::string& arch, const std::string& app) -{ - std::string path_env = std::getenv("PATH"); - //std::cout << path_env << "\n"; + if(tools.find(key) != tools.end()) + { + return tools.at(key); + } - std::string program = app; - if(!arch.empty()) + std::string value; + if(key == ctor::cfg::build_cxx && get_env("CXX", value)) { - program = arch + "-" + app; + return value; } - std::cout << "Looking for: " << program << "\n"; - std::vector<std::string> paths; + if(key == ctor::cfg::build_cc && get_env("CC", value)) { - std::stringstream ss(path_env); - std::string path; - while (std::getline(ss, path, ':')) - { - paths.push_back(path); - } + return value; } - for(const auto& path_str : paths) + + if(key == ctor::cfg::build_ld && get_env("LD", value)) { - std::filesystem::path path(path_str); - auto prog_path = path / program; - if(std::filesystem::exists(prog_path)) - { - std::cout << "Found file " << app << " in path: " << path << "\n"; - auto perms = std::filesystem::status(prog_path).permissions(); - if((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) - { - //std::cout << " - executable by owner\n"; - } - if((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) - { - //std::cout << " - executable by group\n"; - } - if((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none) - { - //std::cout << " - executable by others\n"; - } + return value; + } - return prog_path.string(); - } + if(key == ctor::cfg::build_ar && get_env("AR", value)) + { + return value; + } + + return default_value; +} + +std::string ctor::configuration::getenv(const std::string& key) const +{ + auto envit = env.find(key); + if(envit != env.end()) + { + return envit->second; + } + + if(std::string value; get_env(key.data(), value)) + { + return value; } - std::cerr << "Could not locate " << app << " for the " << arch << " architecture\n"; - exit(1); return {}; } @@ -130,38 +148,111 @@ public: Args(const std::vector<std::string>& args) { resize(args.size() + 1); - (*this)[0] = strdup("./ctor"); + owning_container.push_back("./ctor"); + (*this)[0] = owning_container.back().data(); for(std::size_t i = 0; i < size() - 1; ++i) { - (*this)[i + 1] = strdup(args[i].data()); + owning_container.push_back(args[i]); + (*this)[i + 1] = owning_container.back().data(); } } - ~Args() + std::deque<std::string> owning_container; +}; + +namespace { +std::ostream& operator<<(std::ostream& stream, const ctor::toolchain& toolchain) +{ + switch(toolchain) { - for(std::size_t i = 0; i < size(); ++i) - { - free((*this)[i]); - } + case ctor::toolchain::any: + stream << "ctor::toolchain::any"; + break; + case ctor::toolchain::none: + stream << "ctor::toolchain::none"; + break; + case ctor::toolchain::gcc: + stream << "ctor::toolchain::gcc"; + break; + case ctor::toolchain::clang: + stream << "ctor::toolchain::clang"; + break; } -}; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::arch& arch) +{ + switch(arch) + { + case ctor::arch::unix: + stream << "ctor::arch::unix"; + break; + case ctor::arch::apple: + stream << "ctor::arch::apple"; + break; + case ctor::arch::windows: + stream << "ctor::arch::windows"; + break; + case ctor::arch::unknown: + stream << "ctor::arch::unknown"; + break; + } + return stream; +} + +std::ostream& operator<<(std::ostream& ostr, const ctor::c_flag& flag) +{ + for(const auto& s : to_strings(ctor::toolchain::any, flag)) + { + ostr << s; + } + return ostr; +} + +std::ostream& operator<<(std::ostream& ostr, const ctor::cxx_flag& flag) +{ + for(const auto& s : to_strings(ctor::toolchain::any, flag)) + { + ostr << s; + } + return ostr; +} + +std::ostream& operator<<(std::ostream& ostr, const ctor::ld_flag& flag) +{ + for(const auto& s : to_strings(ctor::toolchain::any, flag)) + { + ostr << s; + } + return ostr; +} + +std::ostream& operator<<(std::ostream& ostr, const ctor::asm_flag& flag) +{ + for(const auto& s : to_strings(ctor::toolchain::any, flag)) + { + ostr << s; + } + return ostr; +} +} // helper constant for the visitor template<class> inline constexpr bool always_false_v = false; -int regenerateCache(const Settings& default_settings, +int regenerateCache(ctor::settings& settings, + const std::string& name, const std::vector<std::string>& args, const std::map<std::string, std::string>& env) { - Settings settings{default_settings}; Args vargs(args); - dg::Options opt; - int key{128}; + arg::Parser<std::string> opt(static_cast<int>(vargs.size()), vargs.data()); - std::string build_arch; + std::string build_arch_prefix; std::string build_path; - std::string host_arch; + std::string host_arch_prefix; std::string host_path; std::string cc_prog = "gcc"; std::string cxx_prog = "g++"; @@ -169,118 +260,135 @@ int regenerateCache(const Settings& default_settings, std::string ld_prog = "ld"; std::string ctor_includedir; std::string ctor_libdir; + std::string builddir; - opt.add("build-dir", required_argument, 'b', - "Set output directory for build files (default: '" + - settings.builddir + "').", - [&]() { - settings.builddir = optarg; + opt.add('b', "--build-dir", + std::function([&](std::string arg) + { + settings.builddir = arg; + builddir = arg; return 0; - }); + }), + "Set output directory for build files (default: '" + + settings.builddir + "')."); - opt.add("verbose", no_argument, 'v', - "Be verbose. Add multiple times for more verbosity.", - [&]() { + opt.add('v', "--verbose", + std::function([&]() + { settings.verbose++; return 0; - }); + }), + "Be verbose. Add multiple times for more verbosity."); - opt.add("cc", required_argument, key++, - "Use specified c-compiler instead of gcc.", - [&]() { - cc_prog = optarg; + opt.add({}, "--cc", + std::function([&](std::string arg) + { + cc_prog = arg; return 0; - }); + }), + "Use specified c-compiler instead of gcc."); - opt.add("cxx", required_argument, key++, - "Use specified c++-compiler instead of g++.", - [&]() { - cxx_prog = optarg; + opt.add({}, "--cxx", + std::function([&](std::string arg) + { + cxx_prog = arg; return 0; - }); + }), + "Use specified c++-compiler instead of g++."); - opt.add("ar", required_argument, key++, - "Use specified archiver instead of ar.", - [&]() { - ar_prog = optarg; + opt.add({}, "--ar", + std::function([&](std::string arg) + { + ar_prog = arg; return 0; - }); + }), + "Use specified archiver instead of ar."); - opt.add("ld", required_argument, key++, - "Use specified linker instead of ld.", - [&]() { - ld_prog = optarg; + opt.add({}, "--ld", + std::function([&](std::string arg) + { + ld_prog = arg; return 0; - }); + }), + "Use specified linker instead of ld."); - opt.add("build", required_argument, key++, - "Configure for building on specified architecture.", - [&]() { - build_arch = optarg; + opt.add({}, "--build", + std::function([&](std::string arg) + { + build_arch_prefix = arg; return 0; - }); + }), + "Configure for building on specified architecture."); - opt.add("build-path", required_argument, key++, - "Set path to build tool-chain.", - [&]() { - build_path = optarg; + opt.add({}, "--build-path", + std::function([&](std::string arg) + { + build_path = arg; return 0; - }); + }), + "Set path to build tool-chain."); - opt.add("host", required_argument, key++, - "Cross-compile to build programs to run on specified architecture.", - [&]() { - host_arch = optarg; + opt.add({}, "--host", + std::function([&](std::string arg) + { + host_arch_prefix = arg; return 0; - }); + }), + "Cross-compile to build programs to run on specified architecture."); - opt.add("host-path", required_argument, key++, - "Set path to cross-compile tool-chain.", - [&]() { - host_path = optarg; + opt.add({}, "--host-path", + std::function([&](std::string arg) + { + host_path = arg; return 0; - }); + }), + "Set path to cross-compile tool-chain."); - opt.add("ctor-includedir", required_argument, key++, - "Set path to ctor header file, used for re-compiling.", - [&]() { - ctor_includedir = optarg; + opt.add({}, "--ctor-includedir", + std::function([&](std::string arg) + { + ctor_includedir = arg; return 0; - }); + }), + "Set path to ctor header file, used for re-compiling."); - opt.add("ctor-libdir", required_argument, key++, - "Set path to ctor library file, used for re-compiling.", - [&]() { - ctor_libdir = optarg; + opt.add({}, "--ctor-libdir", + std::function([&](std::string arg) + { + ctor_libdir = arg; return 0; - }); + }), + "Set path to ctor library file, used for re-compiling."); // Resolv externals - ExternalConfigurations externalConfigs; - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + ctor::external_configurations externalConfigs; + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - auto newExternalConfigs = externalConfigFiles[i].cb(settings); + auto newExternalConfigs = externalConfigFile.cb(settings); externalConfigs.insert(externalConfigs.end(), newExternalConfigs.begin(), newExternalConfigs.end()); } auto add_path_args = - [&](const std::string& name) + [&](const std::string& arg_name) { - opt.add(name + "-includedir", required_argument, key++, - "Set path to " + name + " header file.", - [&]() { - external_includedir[name] = optarg; + opt.add({}, "--" + arg_name + "-includedir", + std::function([&](std::string arg) + { + external_includedir[arg_name] = arg; return 0; - }); + }), + "Set path to " + arg_name + " header file."); - opt.add(name + "-libdir", required_argument, key++, - "Set path to " + name + " libraries.", - [&]() { - external_libdir[name] = optarg; + opt.add({}, "--" + arg_name + "-libdir", + std::function([&](std::string arg) + { + external_libdir[arg_name] = arg; return 0; - }); + }), + "Set path to " + arg_name + " libraries."); }; for(const auto& ext : externalConfigs) @@ -288,7 +396,7 @@ int regenerateCache(const Settings& default_settings, std::visit([&](auto&& arg) { using T = std::decay_t<decltype(arg)>; - if constexpr (std::is_same_v<T, ExternalManual>) + if constexpr (std::is_same_v<T, ctor::external_manual>) { add_path_args(ext.name); } @@ -300,48 +408,159 @@ int regenerateCache(const Settings& default_settings, } - opt.add("help", no_argument, 'h', - "Print this help text.", - [&]() { - std::cout << "configure usage stuff\n"; + opt.add('h', "--help", + std::function([&]() -> int + { + std::cout << "Configure how to build with " << name << "\n"; + std::cout << "Usage: " << name << " configure [options]\n\n"; + std::cout << "Options:\n"; opt.help(); exit(0); - return 0; - }); + }), + "Print this help text."); - opt.process(vargs.size(), vargs.data()); + opt.set_err_cb( + [&](arg::error err, std::string_view arg) + { + switch(err) + { + case arg::error::invalid_arg: + std::cerr << opt.prog_name() << + ": invalid argument for option '" << arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + + case arg::error::missing_arg: + std::cerr << opt.prog_name() << ": option requires and argument '" << + arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + + case arg::error::invalid_opt: + std::cerr << opt.prog_name() << ": invalid option '" << arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + } + }); - if(host_arch.empty()) + opt.set_pos_cb( + [&](std::string_view) + { + std::cerr << + "The configure subcommand doesn't use positional arguments.\n"; + return 1; + }); + + auto res = opt.parse(); + if(res != 0) { - host_arch = build_arch; + return res; + } + + if(host_arch_prefix.empty()) + { + host_arch_prefix = build_arch_prefix; } auto tasks = getTasks(settings, {}, false); -/* - bool needs_cpp{false}; - bool needs_c{false}; - bool needs_ar{false}; - bool needs_asm{false}; + + bool needs_build{true}; // we always need to compile ctor itself + bool needs_build_c{false}; + bool needs_build_cxx{true}; // we always need to compile ctor itself + bool needs_build_ld{true}; // we always need to compile ctor itself + bool needs_build_ar{false}; + bool needs_build_asm{false}; + + bool needs_host_c{false}; + bool needs_host{false}; + bool needs_host_cxx{false}; + bool needs_host_ld{false}; + bool needs_host_ar{false}; + bool needs_host_asm{false}; + for(const auto& task :tasks) { - switch(task->sourceLanguage()) + switch(task->outputSystem()) { - case Language::Auto: - std::cerr << "TargetLanguage not deduced!\n"; - exit(1); - break; - case Language::C: - needs_cpp = false; - break; - case Language::Cpp: - needs_c = true; + case ctor::output_system::build: + needs_build = true; + switch(task->targetType()) + { + case ctor::target_type::executable: + case ctor::target_type::unit_test: + case ctor::target_type::dynamic_library: + needs_build_ld = true; + break; + case ctor::target_type::static_library: + case ctor::target_type::unit_test_library: + needs_build_ar = true; + break; + case ctor::target_type::object: + switch(task->sourceLanguage()) + { + case ctor::language::automatic: + std::cerr << "TargetLanguage not deduced!\n"; + exit(1); + break; + case ctor::language::c: + needs_build_c = true; + break; + case ctor::language::cpp: + needs_build_cxx = true; + break; + case ctor::language::assembler: + needs_build_asm = true; + break; + } + break; + case ctor::target_type::function: + case ctor::target_type::automatic: + case ctor::target_type::unknown: + break; + } break; - case Language::Asm: - needs_asm = true; + case ctor::output_system::host: + needs_host = true; + switch(task->targetType()) + { + case ctor::target_type::executable: + case ctor::target_type::unit_test: + case ctor::target_type::dynamic_library: + needs_host_ld = true; + break; + case ctor::target_type::static_library: + case ctor::target_type::unit_test_library: + needs_host_ar = true; + break; + case ctor::target_type::object: + switch(task->sourceLanguage()) + { + case ctor::language::automatic: + std::cerr << "TargetLanguage not deduced!\n"; + exit(1); + break; + case ctor::language::c: + needs_host_c = true; + break; + case ctor::language::cpp: + needs_host_cxx = true; + break; + case ctor::language::assembler: + needs_host_asm = true; + break; + } + break; + case ctor::target_type::function: + case ctor::target_type::automatic: + case ctor::target_type::unknown: + break; + } break; } } -*/ auto cc_env = env.find("CC"); if(cc_env != env.end()) @@ -367,54 +586,249 @@ int regenerateCache(const Settings& default_settings, ld_prog = ld_env->second; } - std::string host_cc = locate(host_arch, cc_prog); - std::string host_cxx = locate(host_arch, cxx_prog); - std::string host_ar = locate(host_arch, ar_prog); - std::string host_ld = locate(host_arch, ld_prog); - std::string build_cc = locate(build_arch, cc_prog); - std::string build_cxx = locate(build_arch, cxx_prog); - std::string build_ar = locate(build_arch, ar_prog); - std::string build_ld = locate(build_arch, ld_prog); + auto paths = get_paths(); + + auto path_env = env.find("PATH"); + if(path_env != env.end()) + { + paths = get_paths(path_env->second); + } + + std::string host_cc; + std::string host_cxx; + std::string host_ld; + std::string host_ar; + ctor::toolchain host_toolchain{ctor::toolchain::none}; + ctor::arch host_arch{ctor::arch::unknown}; + if(needs_host) + { + // Host detection + if(needs_host_c) + { + host_cc = locate(cc_prog, paths, host_arch_prefix); + if(host_cc.empty()) + { + std::cerr << "Could not locate host_cc prog" << std::endl; + return 1; + } + } + + if(needs_host_cxx) + { + host_cxx = locate(cxx_prog, paths, host_arch_prefix); + if(host_cxx.empty()) + { + std::cerr << "Could not locate host_cxx prog" << std::endl; + return 1; + } + } + + if(needs_host_ar) + { + host_ar = locate(ar_prog, paths, host_arch_prefix); + if(host_ar.empty()) + { + std::cerr << "Could not locate host_ar prog" << std::endl; + return 1; + } + } + + if(needs_host_ld) + { + host_ld = locate(ld_prog, paths, host_arch_prefix); + if(host_ld.empty()) + { + std::cerr << "Could not locate host_ld prog" << std::endl; + return 1; + } + } + + if(needs_host_asm) + { + // TODO + } + + host_toolchain = getToolChain(host_cxx); + auto host_arch_str = get_arch(ctor::output_system::host); + host_arch = get_arch(ctor::output_system::host, host_arch_str); + + std::cout << "** Host architecture '" << host_arch_str << "': " << host_arch << std::endl; + + if(host_arch == ctor::arch::unknown) + { + std::cerr << "Could not detect host architecture" << std::endl; + return 1; + } + } + + std::string build_cc; + std::string build_cxx; + std::string build_ld; + std::string build_ar; + ctor::toolchain build_toolchain{ctor::toolchain::none}; + ctor::arch build_arch{ctor::arch::unknown}; + if(needs_build) + { + // Build detection + if(needs_build_c) + { + build_cc = locate(cc_prog, paths, build_arch_prefix); + if(build_cc.empty()) + { + std::cerr << "Could not locate build_cc prog" << std::endl; + return 1; + } + } + + if(needs_build_cxx) + { + build_cxx = locate(cxx_prog, paths, build_arch_prefix); + if(build_cxx.empty()) + { + std::cerr << "Could not locate build_cxx prog" << std::endl; + return 1; + } + } + + if(needs_build_ar) + { + build_ar = locate(ar_prog, paths, build_arch_prefix); + if(build_ar.empty()) + { + std::cerr << "Could not locate build_ar prog" << std::endl; + return 1; + } + } + + if(needs_build_ld) + { + build_ld = locate(ld_prog, paths, build_arch_prefix); + if(build_ld.empty()) + { + std::cerr << "Could not locate build_ld prog" << std::endl; + return 1; + } + } + + if(needs_build_asm) + { + // TODO + } + + build_toolchain = getToolChain(build_cxx); + auto build_arch_str = get_arch(ctor::output_system::build); + build_arch = get_arch(ctor::output_system::build, build_arch_str); + + std::cout << "** Build architecture '" << build_arch_str << "': " << build_arch << std::endl; + + if(build_arch == ctor::arch::unknown) + { + std::cerr << "Could not detect build architecture" << std::endl; + return 1; + } + } + + + // Store current values for execution in this execution context. + if(!ctor_includedir.empty()) + { + ctor::conf_values[ctor::cfg::ctor_includedir] = ctor_includedir; + } + if(!ctor_libdir.empty()) + { + ctor::conf_values[ctor::cfg::ctor_libdir] = ctor_libdir; + } + if(!builddir.empty()) + { + ctor::conf_values[ctor::cfg::builddir] = builddir; + } + + ctor::conf_values[ctor::cfg::host_cxx] = host_cxx; + ctor::conf_values[ctor::cfg::build_cxx] = build_cxx; std::cout << "Writing results to: " << configurationFile.string() << "\n"; { std::ofstream istr(configurationFile); - istr << "#include <libctor.h>\n\n"; - istr << "const Configuration& configuration()\n"; + istr << "#include <ctor.h>\n\n"; + istr << "const ctor::configuration& ctor::get_configuration()\n"; istr << "{\n"; - istr << " static Configuration cfg =\n"; + istr << " static ctor::configuration cfg =\n"; istr << " {\n"; + if(needs_host) + { + istr << " .host_toolchain = " << host_toolchain << ",\n"; + istr << " .host_arch = " << host_arch << ",\n"; + } + if(needs_build) + { + istr << " .build_toolchain = " << build_toolchain << ",\n"; + istr << " .build_arch = " << build_arch << ",\n"; + } istr << " .args = {"; for(const auto& arg : args) { - istr << "\"" << arg << "\","; + istr << "\"" << esc(arg) << "\","; } istr << "},\n"; - istr << " .env = {"; + istr << " .env = {\n"; for(const auto& e : env) { - istr << "{\"" << e.first << "\", \"" << e.second << "\"}, "; + istr << " {\"" << esc(e.first) << "\", \"" << esc(e.second) << "\"},\n"; } - istr << "},\n"; + istr << " },\n"; istr << " .tools = {\n"; - istr << " { \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; - istr << " { \"" << cfg::host_cc << "\", \"" << host_cc << "\" },\n"; - istr << " { \"" << cfg::host_cxx << "\", \"" << host_cxx << "\" },\n"; - istr << " { \"" << cfg::host_ar << "\", \"" << host_ar << "\" },\n"; - istr << " { \"" << cfg::host_ld << "\", \"" << host_ld << "\" },\n"; - istr << " { \"" << cfg::build_cc << "\", \"" << build_cc << "\" },\n"; - istr << " { \"" << cfg::build_cxx << "\", \"" << build_cxx << "\" },\n"; - istr << " { \"" << cfg::build_ar << "\", \"" << build_ar << "\" },\n"; - istr << " { \"" << cfg::build_ld << "\", \"" << build_ld << "\" },\n"; + if(!builddir.empty()) + { + istr << " { \"" << ctor::cfg::builddir << "\", \"" << esc(builddir) << "\" },\n"; + ctor::builddir = builddir; + } + if(needs_host) + { + if(needs_host_c) + { + istr << " { \"" << ctor::cfg::host_cc << "\", \"" << esc(host_cc) << "\" },\n"; + } + if(needs_host_cxx) + { + istr << " { \"" << ctor::cfg::host_cxx << "\", \"" << esc(host_cxx) << "\" },\n"; + } + if(needs_host_ar) + { + istr << " { \"" << ctor::cfg::host_ar << "\", \"" << esc(host_ar) << "\" },\n"; + } + if(needs_host_ld) + { + istr << " { \"" << ctor::cfg::host_ld << "\", \"" << esc(host_ld) << "\" },\n"; + } + } + if(needs_build) + { + if(needs_build_c) + { + istr << " { \"" << ctor::cfg::build_cc << "\", \"" << esc(build_cc) << "\" },\n"; + } + if(needs_build_cxx) + { + istr << " { \"" << ctor::cfg::build_cxx << "\", \"" << esc(build_cxx) << "\" },\n"; + } + if(needs_build_ar) + { + istr << " { \"" << ctor::cfg::build_ar << "\", \"" << esc(build_ar) << "\" },\n"; + } + if(needs_build_ld) + { + istr << " { \"" << ctor::cfg::build_ld << "\", \"" << esc(build_ld) << "\" },\n"; + } + } if(!ctor_includedir.empty()) { - istr << " { \"" << cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n"; + istr << " { \"" << ctor::cfg::ctor_includedir << "\", \"" << esc(ctor_includedir) << "\" },\n"; ctor::includedir = ctor_includedir; } if(!ctor_libdir.empty()) { - istr << " { \"" << cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n"; + istr << " { \"" << ctor::cfg::ctor_libdir << "\", \"" << esc(ctor_libdir) << "\" },\n"; ctor::libdir = ctor_libdir; } @@ -423,12 +837,12 @@ int regenerateCache(const Settings& default_settings, for(const auto& ext : externalConfigs) { - istr << " { \"" << ext.name << "\", {\n"; - Flags resolved_flags; - if(std::holds_alternative<ExternalManual>(ext.external)) + istr << " { \"" << esc(ext.name) << "\", {\n"; + ctor::flags resolved_flags; + if(std::holds_alternative<ctor::external_manual>(ext.external)) { - if(auto ret = resolv(settings, ext.name, - std::get<ExternalManual>(ext.external), + if(auto ret = resolv(settings, ext, + std::get<ctor::external_manual>(ext.external), resolved_flags)) { return ret; @@ -440,22 +854,22 @@ int regenerateCache(const Settings& default_settings, return 1; } - if(!resolved_flags.cxxflags.empty()) + if(!resolved_flags.cflags.empty()) { - istr << " .cxxflags = {"; - for(const auto& flag : resolved_flags.cxxflags) + istr << " .cflags = {"; + for(const auto& flag : resolved_flags.cflags) { - istr << "\"" << flag << "\","; + istr << flag << ","; } istr << "},\n"; } - if(!resolved_flags.cflags.empty()) + if(!resolved_flags.cxxflags.empty()) { - istr << " .cflags = {"; - for(const auto& flag : resolved_flags.cflags) + istr << " .cxxflags = {"; + for(const auto& flag : resolved_flags.cxxflags) { - istr << "\"" << flag << "\","; + istr << flag << ","; } istr << "},\n"; } @@ -465,7 +879,7 @@ int regenerateCache(const Settings& default_settings, istr << " .ldflags = {"; for(const auto& flag : resolved_flags.ldflags) { - istr << "\"" << flag << "\","; + istr << flag << ","; } istr << "},\n"; } @@ -475,7 +889,7 @@ int regenerateCache(const Settings& default_settings, istr << " .asmflags = {"; for(const auto& flag : resolved_flags.asmflags) { - istr << "\"" << flag << "\","; + istr << flag << ","; } istr << "},\n"; } @@ -485,7 +899,7 @@ int regenerateCache(const Settings& default_settings, istr << " },\n"; istr << " };\n"; istr << " return cfg;\n"; - istr << "}\n\n"; + istr << "}\n"; } { @@ -498,42 +912,61 @@ int regenerateCache(const Settings& default_settings, return 0; } -int configure(const Settings& global_settings, int argc, char* argv[]) +int configure(const ctor::settings& global_settings, int argc, char* argv[]) { - Settings settings{global_settings}; + auto args_span = std::span(argv, static_cast<std::size_t>(argc)); + + ctor::settings settings{global_settings}; std::vector<std::string> args; - for(int i = 2; i < argc; ++i) // skip command and the first 'configure' arg + for(std::size_t i = 2; i < args_span.size(); ++i) // skip command and the first 'configure' arg { - args.push_back(argv[i]); + args.emplace_back(args_span[i]); } std::map<std::string, std::string> env; - auto cc_env = getenv("CC"); - if(cc_env) + std::string value; + if(get_env("CC", value)) { - env["CC"] = cc_env; + env["CC"] = value; } - auto cxx_env = getenv("CXX"); - if(cxx_env) + if(get_env("CFLAGS", value)) { - env["CXX"] = cxx_env; + env["CFLAGS"] = value; } - auto ar_env = getenv("AR"); - if(ar_env) + if(get_env("CXX", value)) { - env["AR"] = ar_env; + env["CXX"] = value; } - auto ld_env = getenv("LD"); - if(ld_env) + if(get_env("CXXFLAGS", value)) { - env["LD"] = ld_env; + env["CXXFLAGS"] = value; } - auto ret = regenerateCache(settings, args, env); + if(get_env("AR", value)) + { + env["AR"] = value; + } + + if(get_env("LD", value)) + { + env["LD"] = value; + } + + if(get_env("LDFLAGS", value)) + { + env["LDFLAGS"] = value; + } + + if(get_env("PATH", value)) + { + env["PATH"] = value; + } + + auto ret = regenerateCache(settings, args_span[0], args, env); if(ret != 0) { return ret; @@ -544,36 +977,40 @@ int configure(const Settings& global_settings, int argc, char* argv[]) return 0; } -int reconfigure(const Settings& settings, int argc, char* argv[]) +int reconfigure(const ctor::settings& global_settings, int argc, char* argv[]) { + auto args_span = std::span(argv, static_cast<std::size_t>(argc)); + + ctor::settings settings{global_settings}; + bool no_rerun{false}; std::vector<std::string> args; - for(int i = 2; i < argc; ++i) // skip executable name and 'reconfigure' arg + for(std::size_t i = 2; i < args_span.size(); ++i) // skip executable name and 'reconfigure' arg { - if(i == 2 && std::string(argv[i]) == "--no-rerun") + if(i == 2 && std::string(args_span[i]) == "--no-rerun") { no_rerun = true; continue; } - args.push_back(argv[i]); + args.emplace_back(args_span[i]); } - const auto& cfg = configuration(); + const auto& cfg = ctor::get_configuration(); std::cout << "Re-running configure:\n"; for(const auto& e : cfg.env) { std::cout << e.first << "=\"" << e.second << "\" "; } - std::cout << argv[0] << " configure "; + std::cout << args_span[0] << " configure "; for(const auto& arg : cfg.args) { std::cout << arg << " "; } std::cout << "\n"; - auto ret = regenerateCache(settings, cfg.args, cfg.env); + auto ret = regenerateCache(settings, args_span[0], cfg.args, cfg.env); if(ret != 0) { return ret; @@ -586,5 +1023,5 @@ int reconfigure(const Settings& settings, int argc, char* argv[]) return 0; // this was originally invoked by configure, don't loop } - return execute(argv[0], args); + return execute(settings, args_span[0], args); } diff --git a/src/configure.cc.bak b/src/configure.cc.bak deleted file mode 100644 index bcbeea9..0000000 --- a/src/configure.cc.bak +++ /dev/null @@ -1,387 +0,0 @@ -// -*- c++ -*- -// Distributed under the BSD 2-Clause License. -// See accompanying file LICENSE for details. -#include "configure.h" - -#include <iostream> -#include <filesystem> -#include <fstream> - -#include <getoptpp/getoptpp.hpp> - -#include "settings.h" -#include "execute.h" -#include "libcppbuild.h" -#include "tasks.h" - -std::filesystem::path configurationFile("configuration.cc"); -std::filesystem::path configHeaderFile("config.h"); - -const std::map<std::string, std::string> default_configuration{}; -const std::map<std::string, std::string>& __attribute__((weak)) configuration() -{ - return default_configuration; -} - -bool hasConfiguration(const std::string& key) -{ - const auto& c = configuration(); - return c.find(key) != c.end(); -} - -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue) -{ - const auto& c = configuration(); - if(hasConfiguration(key)) - { - return c.at(key); - } - - return defaultValue; -} - - -/* -Configuration: - -h, --help display this help and exit - --help=short display options specific to this package - --help=recursive display the short help of all the included packages - -V, --version display version information and exit - -q, --quiet, --silent do not print `checking ...' messages - --cache-file=FILE cache test results in FILE [disabled] - -C, --config-cache alias for `--cache-file=config.cache' - -n, --no-create do not create output files - --srcdir=DIR find the sources in DIR [configure dir or `..'] - -Installation directories: - --prefix=PREFIX install architecture-independent files in PREFIX - [/usr/local] - --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX - [PREFIX] - -By default, `make install' will install all the files in -`/usr/local/bin', `/usr/local/lib' etc. You can specify -an installation prefix other than `/usr/local' using `--prefix', -for instance `--prefix=$HOME'. - -For better control, use the options below. - -Fine tuning of the installation directories: - --bindir=DIR user executables [EPREFIX/bin] - --sbindir=DIR system admin executables [EPREFIX/sbin] - --libexecdir=DIR program executables [EPREFIX/libexec] - --sysconfdir=DIR read-only single-machine data [PREFIX/etc] - --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] - --localstatedir=DIR modifiable single-machine data [PREFIX/var] - --libdir=DIR object code libraries [EPREFIX/lib] - --includedir=DIR C header files [PREFIX/include] - --oldincludedir=DIR C header files for non-gcc [/usr/include] - --datarootdir=DIR read-only arch.-independent data root [PREFIX/share] - --datadir=DIR read-only architecture-independent data [DATAROOTDIR] - --infodir=DIR info documentation [DATAROOTDIR/info] - --localedir=DIR locale-dependent data [DATAROOTDIR/locale] - --mandir=DIR man documentation [DATAROOTDIR/man] - --docdir=DIR documentation root [DATAROOTDIR/doc/drumgizmo] - --htmldir=DIR html documentation [DOCDIR] - --dvidir=DIR dvi documentation [DOCDIR] - --pdfdir=DIR pdf documentation [DOCDIR] - --psdir=DIR ps documentation [DOCDIR] - -Program names: - --program-prefix=PREFIX prepend PREFIX to installed program names - --program-suffix=SUFFIX append SUFFIX to installed program names - --program-transform-name=PROGRAM run sed PROGRAM on installed program names - -System types: - --build=BUILD configure for building on BUILD [guessed] - --host=HOST cross-compile to build programs to run on HOST [BUILD] - -Optional Features: - --disable-option-checking ignore unrecognized --enable/--with options - --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) - --enable-FEATURE[=ARG] include FEATURE [ARG=yes] - --enable-silent-rules less verbose build output (undo: "make V=1") - --disable-silent-rules verbose build output (undo: "make V=0") - --enable-dependency-tracking - do not reject slow dependency extractors - --disable-dependency-tracking - speeds up one-time build - --enable-shared[=PKGS] build shared libraries [default=yes] - --enable-static[=PKGS] build static libraries [default=yes] - --enable-fast-install[=PKGS] - optimize for fast installation [default=yes] - --disable-libtool-lock avoid locking (might break parallel builds) - --disable-largefile omit support for large files - --enable-gui=backend Use specified gui backend. Can be x11, win32, cocoa, - pugl-x11, pugl-win32, pugl-cocoa or auto - [default=auto] - --enable-custom-channel-count=count - Compile with specified number of output channels - [default=16] - --enable-lv2 Compile the LV2 plugin [default=no] - --enable-vst Compile the VST plugin [default=no] - --enable-cli Compile the command line interface [default=yes] - --disable-input-dummy Disable input dummy plugin [default=enabled] - --disable-input-test Disable input test plugin [default=enabled] - --disable-input-jackmidi - Disable input jackmidi plugin [default=enabled] - --disable-input-alsamidi - Disable input alsamidi plugin [default=enabled] - --disable-input-midifile - Disable input midifile plugin [default=enabled] - --disable-input-oss Disable input oss plugin [enabled by default on - FreeBSD, disabled otherwise] - --disable-output-dummy Disable output dummy plugin [default=enabled] - --disable-output-jackaudio - Disable output jack plugin [default=enabled] - --disable-output-alsa Disable output alsa plugin [default=enabled] - --disable-output-wavfile - Disable output wavfile plugin [default=enabled] - --disable-output-oss Disable output oss plugin [enabled by default on - FreeBSD, disabled otherwise] - --enable-sse=level Enable SSE Level 1, 2, 3 or auto [default=auto] - -Optional Packages: - --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] - --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) - --with-pic[=PKGS] try to use only PIC/non-PIC objects [default=use - both] - --with-aix-soname=aix|svr4|both - shared library versioning (aka "SONAME") variant to - provide on AIX, [default=aix]. - --with-gnu-ld assume the C compiler uses GNU ld [default=no] - --with-sysroot[=DIR] Search for dependent libraries within DIR (or the - compiler's sysroot if not specified). - --with-debug Build with debug support - --with-nls Build with nls support (default nls enabled) - --with-test Build unit tests - --with-lv2dir=DIR Use DIR as the lv2 plugin directory - [default=LIBDIR/lv2] - --with-vst-sources Point this to the vstsdk24 directory - -Some influential environment variables: - CXX C++ compiler command - CXXFLAGS C++ compiler flags - LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a - nonstandard directory <lib dir> - LIBS libraries to pass to the linker, e.g. -l<library> - CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if - you have headers in a nonstandard directory <include dir> - OBJC Objective C compiler command - OBJCFLAGS Objective C compiler flags - OBJCXX Objective C++ compiler command - OBJCXXFLAGS Objective C++ compiler flags - CC C compiler command - CFLAGS C compiler flags - LT_SYS_LIBRARY_PATH - User-defined run-time library search path. - CPP C preprocessor - CXXCPP C++ preprocessor - PKG_CONFIG path to pkg-config utility - PKG_CONFIG_PATH - directories to add to pkg-config's search path - PKG_CONFIG_LIBDIR - path overriding pkg-config's built-in search path - X11_CFLAGS C compiler flags for X11, overriding pkg-config - X11_LIBS linker flags for X11, overriding pkg-config - XEXT_CFLAGS C compiler flags for XEXT, overriding pkg-config - XEXT_LIBS linker flags for XEXT, overriding pkg-config - LV2_CFLAGS C compiler flags for LV2, overriding pkg-config - LV2_LIBS linker flags for LV2, overriding pkg-config - SMF_CFLAGS C compiler flags for SMF, overriding pkg-config - SMF_LIBS linker flags for SMF, overriding pkg-config - SNDFILE_CFLAGS - C compiler flags for SNDFILE, overriding pkg-config - SNDFILE_LIBS - linker flags for SNDFILE, overriding pkg-config - JACK_CFLAGS C compiler flags for JACK, overriding pkg-config - JACK_LIBS linker flags for JACK, overriding pkg-config - ALSA_CFLAGS C compiler flags for ALSA, overriding pkg-config - ALSA_LIBS linker flags for ALSA, overriding pkg-config - -Use these variables to override the choices made by `configure' or to help -it to find libraries and programs with nonstandard names/locations. - -Report bugs to the package provider. -*/ -int configure(int argc, char* argv[]) -{ - Settings settings; - - settings.builddir = "build"; - - std::string cmd_str; - for(int i = 0; i < argc; ++i) - { - if(i > 0) - { - cmd_str += " "; - } - cmd_str += argv[i]; - } - - dg::Options opt; - int key{256}; - - std::string build_arch; - std::string build_path; - std::string host_arch; - std::string host_path; - - opt.add("build-dir", required_argument, 'b', - "Set output directory for build files (default: '" + - settings.builddir + "').", - [&]() { - settings.builddir = optarg; - return 0; - }); - - opt.add("verbose", no_argument, 'v', - "Be verbose. Add multiple times for more verbosity.", - [&]() { - settings.verbose++; - return 0; - }); - - opt.add("build", required_argument, key++, - "Configure for building on specified architecture.", - [&]() { - build_arch = optarg; - return 0; - }); - - opt.add("build-path", required_argument, key++, - "Set path to build tool-chain.", - [&]() { - build_path = optarg; - return 0; - }); - - opt.add("host", required_argument, key++, - "Cross-compile to build programs to run on specified architecture.", - [&]() { - host_arch = optarg; - return 0; - }); - - opt.add("host-path", required_argument, key++, - "Set path to cross-compile tool-chain.", - [&]() { - host_path = optarg; - return 0; - }); - - opt.add("help", no_argument, 'h', - "Print this help text.", - [&]() { - std::cout << "configure usage stuff\n"; - opt.help(); - exit(0); - return 0; - }); - - opt.process(argc, argv); - - if(host_arch.empty()) - { - host_arch = build_arch; - } - - auto tasks = getTasks(settings); - - bool needs_cpp{false}; - bool needs_c{false}; - bool needs_ar{false}; - bool needs_asm{false}; - for(const auto& task :tasks) - { - switch(task->sourceLanguage()) - { - case Language::Auto: - std::cerr << "TargetLanguage not deduced!\n"; - exit(1); - break; - case Language::C: - needs_cpp = false; - break; - case Language::Cpp: - needs_c = true; - break; - case Language::Asm: - needs_asm = true; - break; - } - } - - // CC=clang - // CXX=clang++ - - std::string path_env = std::getenv("PATH"); - std::cout << path_env << "\n"; - - std::vector<std::string> paths; - - { - std::stringstream ss(path_env); - std::string path; - while (std::getline(ss, path, ':')) - { - paths.push_back(path); - } - } - for(const auto& path_str : paths) - { - std::filesystem::path path(path_str); - auto gcc = path / "gcc"; - if(std::filesystem::exists(gcc)) - { - std::cout << "Found file gcc in path: " << path << "\n"; - auto perms = std::filesystem::status(gcc).permissions(); - if((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) - { - std::cout << " - executable by owner\n"; - } - if((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) - { - std::cout << " - executable by group\n"; - } - if((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none) - { - std::cout << " - executable by others\n"; - } - } - } - exit(0); - - { - std::ofstream istr(configurationFile); - istr << "#include \"libcppbuild.h\"\n\n"; - istr << "const std::map<std::string, std::string>& configuration()\n"; - istr << "{\n"; - istr << " static std::map<std::string, std::string> c =\n"; - istr << " {\n"; - istr << " { \"cmd\", \"" << cmd_str << "\" },\n"; - istr << " { \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; - istr << " { \"" << cfg::target_cc << "\", \"/usr/bin/gcc\" },\n"; - istr << " { \"" << cfg::target_cpp << "\", \"/usr/bin/g++\" },\n"; - istr << " { \"" << cfg::target_ar << "\", \"/usr/bin/ar\" },\n"; - istr << " { \"" << cfg::target_ld << "\", \"/usr/bin/ld\" },\n"; - istr << " { \"" << cfg::host_cc << "\", \"/usr/bin/gcc\" },\n"; - istr << " { \"" << cfg::host_cpp << "\", \"/usr/bin/g++\" },\n"; - istr << " { \"" << cfg::host_ar << "\", \"/usr/bin/ar\" },\n"; - istr << " { \"" << cfg::host_ld << "\", \"/usr/bin/ld\" },\n"; - istr << " };\n"; - istr << " return c;\n"; - istr << "}\n"; - } - - { - std::ofstream istr(configHeaderFile); - istr << "#pragma once\n\n"; - istr << "#define HAS_FOO 1\n"; - istr << "//#define HAS_BAR 1\n"; - } - - return 0; -} diff --git a/src/configure.h b/src/configure.h index 16499d6..5344646 100644 --- a/src/configure.h +++ b/src/configure.h @@ -8,10 +8,12 @@ #include <map> #include <vector> -struct Settings; +namespace ctor { +struct settings; +} // namespace ctor:: -extern std::filesystem::path configurationFile;; -extern std::filesystem::path configHeaderFile; +extern const std::filesystem::path configurationFile; +extern const std::filesystem::path configHeaderFile; -int configure(const Settings& settings, int argc, char* argv[]); -int reconfigure(const Settings& settings, int argc, char* argv[]); +int configure(const ctor::settings& settings, int argc, char* argv[]); +int reconfigure(const ctor::settings& settings, int argc, char* argv[]); diff --git a/src/ctor.h b/src/ctor.h new file mode 100644 index 0000000..27b30af --- /dev/null +++ b/src/ctor.h @@ -0,0 +1,305 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <source_location> +#include <string> +#include <vector> +#include <map> +#include <variant> +#include <cstddef> +#include <functional> +#include <string_view> + +namespace ctor { + +enum class target_type +{ + automatic, // Default - deduce from target name and sources extensions + + executable, + static_library, + dynamic_library, + object, + unit_test, + unit_test_library, + function, + + unknown, +}; + +enum class language +{ + automatic, // Default - deduce language from source extensions + + c, + cpp, + assembler, +}; + +enum class output_system +{ + host, // Output for the target system + build, // Internal tool during cross-compilation +}; + +enum class arch +{ + unix, //!< Target platform architecture is unix-based (ie. linux, bsd, etc) + apple, //!< Target platform architecture is macos + windows, //!< Target platform architecture is windows + + unknown, //!< Target platform architecture has not yet detected or was not possible to detect +}; + +enum class toolchain +{ + any, + none, + gcc, + clang, +}; + +struct source +{ + source(const char* file_) : file(file_) {} // convenience ctor + + source(std::string_view file_) : source(file_, ctor::language::automatic) {} + source(std::string_view file_, ctor::language lang_) : file(file_), language(lang_) {} + + source(std::string_view file_, std::string_view output_) : file(file_), output(output_) {} + source(std::string_view file_, ctor::language lang_, std::string_view output_) : file(file_), language(lang_), output(output_) {} + + source(ctor::toolchain toolchain_, std::string_view file_) : file(file_), toolchain(toolchain_) {} + source(ctor::toolchain toolchain_, std::string_view file_, ctor::language lang_) : file(file_), toolchain(toolchain_), language(lang_) {} + + source(ctor::toolchain toolchain_, std::string_view file_, std::string_view output_) : file(file_), toolchain(toolchain_), output(output_) {} + + source(ctor::toolchain toolchain_, std::string_view file_, ctor::language lang_, std::string_view output_) : file(file_), toolchain(toolchain_), language(lang_), output(output_) {} + + std::string file; + ctor::toolchain toolchain{ctor::toolchain::any}; + ctor::language language{ctor::language::automatic}; + std::string output{}; +}; + +enum class cxx_opt +{ + // gcc/clang + output, // -o + debug, // -g + warn_all, // -Wall + warn_conversion, // -Wconversion + warn_shadow, // -Wshadow + warn_extra, // -Wextra + warnings_as_errors, // -Werror + generate_dep_tree, // -MMD + no_link, // -c + include_path, // -I<arg> + cpp_std, // -std=<arg> + optimization, // -O<arg> + position_independent_code, // -fPIC + position_independent_executable, // -fPIE + define, // -D<arg>[=<arg2>] + custom, // entire option taken verbatim from <arg> +}; + +enum class c_opt +{ + // gcc/clang + output, // -o + debug, // -g + warn_all, // -Wall + warn_conversion, // -Wconversion + warn_shadow, // -Wshadow + warn_extra, // -Wextra + warnings_as_errors, // -Werror + generate_dep_tree, // -MMD + no_link, // -c + include_path, // -I<arg> + c_std, // -std=<arg> + optimization, // -O<arg> + position_independent_code, // -fPIC + position_independent_executable, // -fPIE + define, // -D<arg>[=<arg2>] + custom, // entire option taken verbatim from <arg> +}; + +enum class ld_opt +{ + // gcc/clang + output, // -o + warn_all, // -Wall + warnings_as_errors, // -Werror + library_path, // -L<arg> + link, // -l<arg> + cpp_std, // -std=<arg> + build_shared, // -shared + threads, // -pthread + position_independent_code, // -fPIC + position_independent_executable, // -fPIE + custom, // entire option taken verbatim from <arg> +}; + +enum class ar_opt +{ + // gcc/clang + replace, // -r + add_index, // -s + create, // -c + output, // <arg> + + custom, // entire option taken verbatim from <arg> +}; + +enum class asm_opt +{ + // gcc/clang + custom, // entire option taken verbatim from <arg> +}; + +template<typename T> +class flag +{ +public: + flag(std::string_view str); + flag(const char* str); + flag(T opt_) : opt(opt_) {} + flag(T opt_, std::string_view arg_, std::string_view arg2_ = "") + : opt(opt_), arg(arg_), arg2(arg2_) {} + flag(T opt_, const char* arg_, const char* arg2_ = "") + : opt(opt_), arg(arg_), arg2(arg2_) {} + flag(ctor::toolchain toolchain_, T opt_) + : toolchain(toolchain_), opt(opt_) {} + flag(ctor::toolchain toolchain_, T opt_, const char* arg_, const char* arg2_ = "") + : toolchain(toolchain_), opt(opt_), arg(arg_), arg2(arg2_) {} + flag(ctor::toolchain toolchain_, T opt_, std::string_view arg_, std::string_view arg2_ = "") + : toolchain(toolchain_), opt(opt_), arg(arg_), arg2(arg2_) {} + + ctor::toolchain toolchain{ctor::toolchain::any}; + T opt{}; + std::string arg; + std::string arg2; +}; + +using c_flag = ctor::flag<ctor::c_opt>; +using cxx_flag = ctor::flag<ctor::cxx_opt>; +using ld_flag = ctor::flag<ctor::ld_opt>; +using ar_flag = ctor::flag<ctor::ar_opt>; +using asm_flag = ctor::flag<ctor::asm_opt>; + +using c_flags = std::vector<ctor::c_flag>; +using cxx_flags = std::vector<ctor::cxx_flag>; +using ld_flags = std::vector<ctor::ld_flag>; +using ar_flags = std::vector<ctor::ar_flag>; +using asm_flags = std::vector<ctor::asm_flag>; + +struct flags +{ + ctor::c_flags cflags; // flags for c compiler + ctor::cxx_flags cxxflags; // flags for c++ compiler + ctor::ld_flags ldflags; // flags for linker + ctor::ar_flags arflags; // flags for archiver + ctor::asm_flags asmflags; // flags for asm translator +}; + +struct settings +{ + std::string builddir{"build"}; + std::size_t parallel_processes{1}; + int verbose{0}; // -1: completely silent, 0: normal, 1: verbose, ... + bool dry_run{false}; +}; + +struct build_configuration; +using GeneratorCb = std::function<int(const std::string& input, + const std::string& output, + const build_configuration& config, + const ctor::settings& settings)>; + +struct build_configuration +{ + std::string name; // Name - used for referring in other configurations. + ctor::target_type type{ctor::target_type::automatic}; + ctor::output_system system{ctor::output_system::build}; + std::string target; // Output target file for this configuration + std::vector<ctor::source> sources; // source list + std::vector<std::string> depends; // internal target dependencies + ctor::flags flags; + std::vector<std::string> externals; // externals used by this configuration + GeneratorCb function; +}; + +using build_configurations = std::vector<build_configuration>; + +int reg(std::function<ctor::build_configurations (const ctor::settings&)> cb, + const std::source_location location = std::source_location::current()); + +// This type will use flags verbatim +struct external_manual +{ + ctor::flags flags; +}; + + +struct external_configuration +{ + std::string name; // Name for configuration + ctor::output_system system{ctor::output_system::build}; + std::variant<ctor::external_manual> external; +}; + +using external_configurations = std::vector<ctor::external_configuration>; + +int reg(std::function<ctor::external_configurations (const ctor::settings&)> cb, + const std::source_location location = std::source_location::current()); + +// Convenience macro - ugly but keeps things simple(r) +#define CTOR_CONCAT(a, b) CTOR_CONCAT_INNER(a, b) +#define CTOR_CONCAT_INNER(a, b) a ## b +#define CTOR_UNIQUE_NAME(base) CTOR_CONCAT(base, __LINE__) +#define REG(cb) namespace { int CTOR_UNIQUE_NAME(unique) = reg(cb); } + +// Predefined configuration keys +namespace cfg +{ +constexpr auto builddir = "builddir"; + +constexpr auto host_cc = "host-cc"; +constexpr auto host_cxx = "host-cxx"; +constexpr auto host_ar = "host-ar"; +constexpr auto host_ld = "host-ld"; + +constexpr auto build_cc = "build-cc"; +constexpr auto build_cxx = "build-cxx"; +constexpr auto build_ar = "build-ar"; +constexpr auto build_ld = "build-ld"; + +constexpr auto ctor_includedir = "ctor-includedir"; +constexpr auto ctor_libdir = "ctor-libdir"; +} + +struct configuration +{ + bool has(const std::string& key) const; + std::string get(const std::string& key, const std::string& default_value = {}) const; + + ctor::toolchain host_toolchain{ctor::toolchain::none}; + ctor::arch host_arch{ctor::arch::unknown}; + + ctor::toolchain build_toolchain{ctor::toolchain::none}; + ctor::arch build_arch{ctor::arch::unknown}; + + std::vector<std::string> args; // vector of arguments used when last calling configure + + std::string getenv(const std::string& key) const; + std::map<std::string, std::string> env; // env used when last calling configure + + std::map<std::string, std::string> tools; // tools + std::map<std::string, ctor::flags> externals; +}; + +const ctor::configuration& get_configuration(); + +} // ctor:: diff --git a/src/deps.cc b/src/deps.cc new file mode 100644 index 0000000..9400b35 --- /dev/null +++ b/src/deps.cc @@ -0,0 +1,120 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "deps.h" + +#include "util.h" + +#include <fstream> + +namespace { +/* Format example: +build/src/libctor_a-libctor_cc.o: src/libctor.cc \ + src/getoptpp/getoptpp.hpp src/ctor.h src/configure.h src/rebuild.h \ + src/A\ B\ C.h src/task.h src/build.h src/unittest.h + */ +std::vector<std::string> readDepsMake(const std::string& dep_file) +{ + auto str = readFile(dep_file); + + std::vector<std::string> output; + std::string tmp; + + enum class State + { + pre_colon, + post_colon, + in_escape, + } state{State::pre_colon}; + + auto old_state{state}; + for(const auto& c : str) + { + if(c == '\r') + { + // just always ignore windows extra newline char + continue; + } + + switch(state) + { + case State::pre_colon: + if(c == ':') // ignore everything until the colon + { + state = State::post_colon; + continue; + } + continue; + + case State::post_colon: + if(c == '\n') + { + // done + if(!tmp.empty()) + { + output.emplace_back(tmp); + } + return output; + } + if(c == '\\') + { + old_state = state; + state = State::in_escape; + continue; + } + if(c == '\t' || c == ' ') // new token + { + if(!tmp.empty()) + { + output.emplace_back(tmp); + } + tmp.clear(); + continue; + } + break; + + case State::in_escape: + if(c == '\n') + { + // linewrap + state = old_state; + continue; + } + state = old_state; + break; + } + + tmp += c; + } + + if(!tmp.empty()) + { + output.emplace_back(tmp); + } + + return output; +} +} + +std::vector<std::string> readDeps(const std::string& dep_file, + ctor::toolchain toolchain) +{ + if(!std::filesystem::exists(dep_file)) + { + return {}; + } + + switch(toolchain) + { + case ctor::toolchain::any: + case ctor::toolchain::none: + return {}; + + // makefile .d file based: + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return readDepsMake(dep_file); + } + + return {}; +} diff --git a/src/deps.h b/src/deps.h new file mode 100644 index 0000000..be0dfb2 --- /dev/null +++ b/src/deps.h @@ -0,0 +1,12 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include "ctor.h" + +#include <vector> +#include <string> + +std::vector<std::string> readDeps(const std::string& dep_file, + ctor::toolchain toolchain); diff --git a/src/execute.cc b/src/execute.cc index 610ccdd..c050732 100644 --- a/src/execute.cc +++ b/src/execute.cc @@ -3,6 +3,9 @@ // See accompanying file LICENSE for details. #include "execute.h" +#include "ctor.h" +#include "pointerlist.h" + #include <unistd.h> #include <cstring> #include <sys/types.h> @@ -20,22 +23,53 @@ https://stackoverflow.com/questions/4259629/what-is-the-difference-between-fork- namespace { + int parent_waitpid(pid_t pid) { - int status; + int status{}; + + auto rc_pid = waitpid(pid, &status, 0); - if(waitpid(pid, &status, 0) != pid) + if(rc_pid > 0) { - return 1; + if(WIFEXITED(status)) + { + // Child exited with normally + return WEXITSTATUS(status); + } + if(WIFSIGNALED(status)) + { + // Child exited via signal (segfault, abort, ...) + std::cerr << strsignal(status) << '\n'; + return WTERMSIG(status); + } + } + else + { // No PID returned, this is an error + if(errno == ECHILD) + { + // No children exist. + return 1; + } + else + { + // Unexpected error. + abort(); + } } - return WEXITSTATUS(status); + // Should never happen... + return 1; } } // namespace :: -int execute(const std::string& command, +extern char **environ; // see 'man environ' + +int execute(const ctor::settings& settings, + const std::string& command, const std::vector<std::string>& args, - bool verbose) + const std::map<std::string, std::string>& env, + [[maybe_unused]] bool terminate) { std::vector<const char*> argv; argv.push_back(command.data()); @@ -45,46 +79,64 @@ int execute(const std::string& command, } argv.push_back(nullptr); - if(verbose) + std::string cmd; + for(const auto& arg : argv) { - std::string cmd; - for(const auto& arg : argv) + if(arg == nullptr) + { + break; + } + if(!cmd.empty()) { - if(arg == nullptr) - { - break; - } - if(!cmd.empty()) - { - cmd += " "; - } - cmd += arg; + cmd += " "; } + cmd += arg; + } - std::cout << cmd << "\n"; + if(settings.verbose > 0) + { + std::cout << cmd << std::endl; } #if 1 auto pid = vfork(); if(pid == 0) { - execv(command.data(), (char**)argv.data()); + EnvMap envmap((const char**)environ); + for(const auto& [key, value] : env) + { + envmap.insert(key + "=" + value); + } + if(settings.dry_run) + { + _exit(0); + } + auto [_, envv] = envmap.get(); + execve(command.data(), const_cast<char* const *>(argv.data()), + const_cast<char* const *>(envv)); std::cout << "Could not execute " << command << ": " << strerror(errno) << "\n"; - _exit(1); // execv only returns if an error occurred + _exit(1); // execve only returns if an error occurred } - auto ret = parent_waitpid(pid); + return parent_waitpid(pid); #elif 0 pid_t pid; + std::vector<std::string> venv; + for(const auto& [key, value] : env) + { + venv.push_back(key + "=" + value); + } + Env penv(venv); if(posix_spawn(&pid, command.data(), nullptr, nullptr, - (char**)argv.data(), nullptr)) + (char**)argv.data(), penv.data())) { return 1; } - auto ret = parent_waitpid(pid); + return parent_waitpid(pid); #else - auto ret = system(cmd.data()); + (void)parent_waitpid; + return system(cmd.data()); #endif - return ret; + return 1; } diff --git a/src/execute.h b/src/execute.h index c750a83..4288bb7 100644 --- a/src/execute.h +++ b/src/execute.h @@ -5,7 +5,14 @@ #include <string> #include <vector> +#include <map> -int execute(const std::string& command, - const std::vector<std::string>& args, - bool verbose = true); +namespace ctor { +struct settings; +}//ctor:: + +int execute(const ctor::settings& settings, + const std::string& command, + const std::vector<std::string>& args = {}, + const std::map<std::string, std::string>& env = {}, + bool terminate = false); diff --git a/src/externals_manual.cc b/src/externals_manual.cc index 79c9be3..0563a5e 100644 --- a/src/externals_manual.cc +++ b/src/externals_manual.cc @@ -5,27 +5,30 @@ #include <map> -#include "libctor.h" +#include "ctor.h" + +#include "util.h" +#include "tools.h" extern std::map<std::string, std::string> external_includedir; extern std::map<std::string, std::string> external_libdir; -int resolv(const Settings& settings, const std::string& name, - const ExternalManual& ext, Flags& flags) +int resolv([[maybe_unused]]const ctor::settings& settings, const ctor::external_configuration& config, + const ctor::external_manual& ext, ctor::flags& flags) { flags = ext.flags; - auto inc = external_includedir.find(name); + auto inc = external_includedir.find(config.name); if(inc != external_includedir.end()) { - flags.cflags.push_back("-I" + inc->second); - flags.cxxflags.push_back("-I" + inc->second); + flags.cflags.emplace_back(ctor::c_opt::include_path, inc->second); + flags.cxxflags.emplace_back(ctor::cxx_opt::include_path, inc->second); } - auto lib = external_libdir.find(name); + auto lib = external_libdir.find(config.name); if(lib != external_libdir.end()) { - flags.ldflags.push_back("-L" + lib->second); + flags.ldflags.emplace_back(ctor::ld_opt::library_path, lib->second); } return 0; diff --git a/src/externals_manual.h b/src/externals_manual.h index c7e3b9a..a906ab7 100644 --- a/src/externals_manual.h +++ b/src/externals_manual.h @@ -3,11 +3,13 @@ // See accompanying file LICENSE for details. #pragma once -#include <string> +namespace ctor { +struct settings; +struct external_configuration; +struct external_manual; +struct flags; +} // namespace ctor:: -struct Settings; -struct ExternalManual; -struct Flags; - -int resolv(const Settings& settings, const std::string& name, - const ExternalManual& ext, Flags& flags); +int resolv(const ctor::settings& settings, + const ctor::external_configuration& name, + const ctor::external_manual& ext, ctor::flags& flags); diff --git a/src/getoptpp b/src/getoptpp deleted file mode 160000 -Subproject 9ff20ef857429619267e3f156a4f81ad9e1eb8c diff --git a/src/libctor.cc b/src/libctor.cc index d188771..2685ec0 100644 --- a/src/libctor.cc +++ b/src/libctor.cc @@ -15,31 +15,34 @@ #include <deque> #include <fstream> #include <cstdlib> -#include <set> +#include <span> -#include <getoptpp/getoptpp.hpp> - -#include "libctor.h" +#include "ctor.h" #include "configure.h" #include "rebuild.h" #include "tasks.h" #include "build.h" #include "unittest.h" +#include "argparser.h" +#include "util.h" int main(int argc, char* argv[]) { - Settings settings{}; + auto args = std::span(argv, static_cast<std::size_t>(argc)); + + ctor::settings settings{}; + const auto& c = ctor::get_configuration(); - settings.builddir = getConfiguration(cfg::builddir, settings.builddir); + settings.builddir = c.get(ctor::cfg::builddir, settings.builddir); settings.parallel_processes = std::max(1u, std::thread::hardware_concurrency()) * 2 - 1; - if(argc > 1 && std::string(argv[1]) == "configure") + if(args.size() > 1 && std::string(args[1]) == "configure") { return configure(settings, argc, argv); } - if(argc > 1 && std::string(argv[1]) == "reconfigure") + if(args.size() > 1 && std::string(args[1]) == "reconfigure") { return reconfigure(settings, argc, argv); } @@ -55,108 +58,121 @@ int main(int argc, char* argv[]) bool list_targets{false}; bool no_relaunch{false}; // true means no re-launch after rebuild. bool run_unittests{false}; - - dg::Options opt; - int key{128}; - - opt.add("jobs", required_argument, 'j', - "Number of parallel jobs. (default: cpucount * 2 - 1)", - [&]() { - try - { - settings.parallel_processes = std::stoi(optarg); - } - catch(...) - { - std::cerr << "Not a number\n"; - return 1; - } + std::vector<std::string> arguments; + arg::Parser<int, std::string> opt(argc, argv); + opt.set_pos_cb( + [&](std::string_view arg) + { + arguments.emplace_back(std::string(arg)); + return 0; + }); + + opt.add('j', "--jobs", + std::function([&](int jobs) + { + settings.parallel_processes = static_cast<std::size_t>(jobs); return 0; - }); + }), + "Number of parallel jobs. (default: cpucount * 2 - 1)"); - opt.add("build-dir", required_argument, 'b', - "Overload output directory for build files (default: '" + - settings.builddir + "').", - [&]() { - settings.builddir = optarg; + opt.add('b', "--build-dir", + std::function([&](std::string builddir) { + settings.builddir = builddir; return 0; - }); + }), + "Overload output directory for build files (default: '" + + settings.builddir + "')."); - opt.add("verbose", no_argument, 'v', - "Be verbose. Add multiple times for more verbosity.", - [&]() { + opt.add('v', "--verbose", + std::function([&]() + { settings.verbose++; return 0; - }); + }), + "Be verbose. Add multiple times for more verbosity."); - opt.add("quiet", no_argument, 'q', - "Be completely silent.", - [&]() { + opt.add('q', "--quiet", + std::function([&]() { settings.verbose = -1; return 0; - }); + }), + "Be completely silent."); - opt.add("add", required_argument, 'a', - "Add specified file to the build configurations.", - [&]() { + opt.add('a', "--add", + std::function([&](std::string filename) { no_relaunch = true; - add_files.push_back(optarg); + add_files.emplace_back(filename); return 0; - }); + }), + "Add specified file to the build configurations."); - opt.add("remove", required_argument, 'r', - "Remove specified file from the build configurations.", - [&]() { + opt.add('r', "--remove", + std::function([&](std::string filename) + { no_relaunch = true; - remove_files.push_back(optarg); + remove_files.emplace_back(filename); return 0; - }); + }), + "Remove specified file from the build configurations."); - opt.add("list-files", no_argument, 'L', - "List files in the build configurations.", - [&]() { + opt.add('L', "--list-files", + std::function([&]() + { no_relaunch = true; list_files = true; return 0; - }); + }), + "List files in the build configurations."); - opt.add("list-targets", no_argument, 'l', - "List targets.", - [&]() { + opt.add('l', "--list-targets", + std::function([&]() + { no_relaunch = true; list_targets = true; return 0; - }); + }), + "List targets."); + + opt.add('n', "--dry-run", + std::function([&]() + { + settings.dry_run = true; + return 0; + }), + "Print the commands to be executed, but do not execute them."); - opt.add("configure-cmd", no_argument, key++, - "Print commandline for last configure.", - [&]() { + opt.add({}, "--configure-cmd", + std::function([&]() + { no_relaunch = true; print_configure_cmd = true; return 0; - }); + }), + "Print commandline for last configure."); - opt.add("configure-db", no_argument, key++, - "Print entire configure parameter database.", - [&]() { + opt.add({}, "--configure-db", + std::function([&]() + { no_relaunch = true; print_configure_db = true; return 0; - }); + }), + "Print entire configure parameter database."); - opt.add("database", required_argument, 'd', - "Write compilation database json file.", - [&]() { + opt.add('d', "--database", + std::function([&](std::string database) + { no_relaunch = true; write_compilation_database = true; - compilation_database = optarg; + compilation_database = database; return 0; - }); + }), + "Write compilation database json file."); - opt.add("help", no_argument, 'h', - "Print this help text.", - [&]() { - std::cout << "Usage: " << argv[0] << " [options] [target] ...\n"; + opt.add('h', "--help", + std::function([&]() -> int + { + std::cout << "Usage: " << args[0] << " [options] [target] ...\n"; std::cout << R"_( where target can be either: configure - run configuration step (cannot be used with other targets). @@ -170,31 +186,70 @@ Options: )_"; opt.help(); exit(0); - return 0; - }); + }), + "Print this help text."); - opt.process(argc, argv); + opt.set_err_cb( + [&](arg::error err, std::string_view arg) + { + switch(err) + { + case arg::error::invalid_arg: + std::cerr << opt.prog_name() << + ": invalid argument for option '" << arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + + case arg::error::missing_arg: + std::cerr << opt.prog_name() << ": option requires and argument '" << + arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + + case arg::error::invalid_opt: + std::cerr << opt.prog_name() << ": invalid option '" << arg << "'\n"; + std::cerr << "Type '" << opt.prog_name() << + " -h' for more information.\n"; + break; + } + }); + auto res = opt.parse(); + if(res != 0) + { + return res; + } - auto verbose_env = std::getenv("V"); - if(verbose_env) + if(std::string value; get_env("V", value)) { - settings.verbose = std::atoi(verbose_env); + try + { + settings.verbose = std::stoi(value); + } + catch(...) + { + // not an integer + } } if(list_files) { no_default_build = true; - std::set<std::string> files; - for(std::size_t i = 0; i < numConfigFiles; ++i) + std::vector<std::string> files; + const auto& configFiles = getConfigFileList(); + for(const auto& configFile : configFiles) { - files.insert(configFiles[i].file); + files.emplace_back(configFile.file); } - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - files.insert(externalConfigFiles[i].file); + files.emplace_back(externalConfigFile.file); } + std::sort(files.begin(), files.end()); for(const auto& file : files) { std::cout << file << "\n"; @@ -263,13 +318,12 @@ Options: if(print_configure_cmd) { no_default_build = true; - std::cout << getConfiguration("cmd") << "\n"; + std::cout << c.get("cmd") << "\n"; } if(print_configure_db) { no_default_build = true; - const auto& c = configuration(); for(const auto& config : c.tools) { std::cout << config.first << ": " << config.second << "\n"; @@ -285,7 +339,7 @@ Options: } bool build_all{!no_default_build}; - for(const auto& arg : opt.arguments()) + for(const auto& arg : arguments) { if(arg == "configure") { @@ -315,8 +369,8 @@ Options: auto& targets = getTargets(settings); for(const auto& target : targets) { - if(target.config.type == TargetType::UnitTest || - target.config.type == TargetType::UnitTestLib) + if(target.config.type == ctor::target_type::unit_test || + target.config.type == ctor::target_type::unit_test_library) { unittest_targets.push_back(target); } @@ -338,8 +392,8 @@ Options: auto& targets = getTargets(settings); for(const auto& target : targets) { - if(target.config.type != TargetType::UnitTest && - target.config.type != TargetType::UnitTestLib) + if(target.config.type != ctor::target_type::unit_test && + target.config.type != ctor::target_type::unit_test_library) { non_unittest_targets.push_back(target); } @@ -368,8 +422,8 @@ Options: auto& targets = getTargets(settings); for(const auto& target : targets) { - if(target.config.type != TargetType::UnitTest && - target.config.type != TargetType::UnitTestLib) + if(target.config.type != ctor::target_type::unit_test && + target.config.type != ctor::target_type::unit_test_library) { non_unittest_targets.push_back(target); } diff --git a/src/libctor.h b/src/libctor.h deleted file mode 100644 index 14fdf1d..0000000 --- a/src/libctor.h +++ /dev/null @@ -1,153 +0,0 @@ -// -*- c++ -*- -// Distributed under the BSD 2-Clause License. -// See accompanying file LICENSE for details. -#pragma once - -#include <source_location> -#include <string> -#include <vector> -#include <map> -#include <variant> -#include <cstddef> -#include <functional> - -enum class TargetType -{ - Auto, // Default - deduce from target name and sources extensions - - Executable, - StaticLibrary, - DynamicLibrary, - Object, - UnitTest, - UnitTestLib, - Function, -}; - -enum class Language -{ - Auto, // Default - deduce language from source extensions - - C, - Cpp, - Asm, -}; - -enum class OutputSystem -{ - Host, // Output for the target system - Build, // Internal tool during cross-compilation -}; - -struct Source -{ - Source(const char* file) : file(file) {} - Source(const std::string& file) : file(file) {} - Source(const char* file, Language lang) : file(file), language(lang) {} - Source(const std::string& file, Language lang) : file(file), language(lang) {} - - Source(const char* file, const char* output) : file(file), output(output) {} - Source(const std::string& file, const std::string& output) : file(file), output(output) {} - Source(const char* file, Language lang, const char* output) : file(file), language(lang), output(output) {} - Source(const std::string& file, Language lang, const std::string& output) : file(file), language(lang), output(output) {} - - std::string file; - Language language{Language::Auto}; - std::string output{}; -}; - -struct Flags -{ - std::vector<std::string> cxxflags; // flags for c++ compiler - std::vector<std::string> cflags; // flags for c compiler - std::vector<std::string> ldflags; // flags for linker - std::vector<std::string> asmflags; // flags for asm translator -}; - -struct Settings -{ - std::string builddir{"build"}; - std::size_t parallel_processes{1}; - int verbose{0}; // -1: completely silent, 0: normal, 1: verbose, ... -}; - -struct BuildConfiguration; -using GeneratorCb = std::function<int(const std::string& input, - const std::string& output, - const BuildConfiguration& config, - const Settings& settings)>; - -struct BuildConfiguration -{ - std::string name; // Name - used for referring in other configurations. - TargetType type{TargetType::Auto}; - OutputSystem system{OutputSystem::Host}; - std::string target; // Output target file for this configuration - std::vector<Source> sources; // source list - std::vector<std::string> depends; // internal target dependencies - Flags flags; - std::vector<std::string> externals; // externals used by this configuration - GeneratorCb function; -}; - -using BuildConfigurations = std::vector<BuildConfiguration>; - -int reg(BuildConfigurations (*cb)(const Settings&), - const std::source_location location = std::source_location::current()); - -// This type will use flags verbatim -struct ExternalManual -{ - Flags flags; -}; - - -struct ExternalConfiguration -{ - std::string name; // Name for configuration - std::variant<ExternalManual> external; -}; - -using ExternalConfigurations = std::vector<ExternalConfiguration>; - -int reg(ExternalConfigurations (*cb)(const Settings&), - const std::source_location location = std::source_location::current()); - -// Convenience macro - ugly but keeps things simple(r) -#define CONCAT(a, b) CONCAT_INNER(a, b) -#define CONCAT_INNER(a, b) a ## b -#define UNIQUE_NAME(base) CONCAT(base, __LINE__) -#define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(cb); } - -// Predefined configuration keys -namespace cfg -{ -constexpr auto builddir = "builddir"; - -constexpr auto host_cc = "host-cc"; -constexpr auto host_cxx = "host-cpp"; -constexpr auto host_ar = "host-ar"; -constexpr auto host_ld = "host-ld"; - -constexpr auto build_cc = "build-cc"; -constexpr auto build_cxx = "build-cpp"; -constexpr auto build_ar = "build-ar"; -constexpr auto build_ld = "build-ld"; - -constexpr auto ctor_includedir = "ctor-includedir"; -constexpr auto ctor_libdir = "ctor-libdir"; -} - -struct Configuration -{ - std::vector<std::string> args; // vector of arguments used when last calling configure - std::map<std::string, std::string> env; // env used when last calling configure - - std::map<std::string, std::string> tools; // tools - std::map<std::string, Flags> externals; -}; - -const Configuration& configuration(); -bool hasConfiguration(const std::string& key); -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue = {}); diff --git a/src/pointerlist.cc b/src/pointerlist.cc new file mode 100644 index 0000000..c0242f7 --- /dev/null +++ b/src/pointerlist.cc @@ -0,0 +1,123 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "pointerlist.h" + +#include <cstring> + +PointerList::PointerList(int argc, const char* const argv[]) +{ + for(int i = 0; i < argc; ++i) + { + push_back(argv[i]); + } +} + +std::pair<int, const char* const*> PointerList::get() +{ + argptrs.clear(); + for(const auto& arg : *this) + { + argptrs.push_back(arg.data()); + } + argptrs.push_back(nullptr); + + return {argptrs.size() - 1, // size not counting the nullptr at the end + argptrs.data()}; +} + +EnvMap::EnvMap(const char* const env[]) +{ + if(env == nullptr) + { + return; + } + + auto ptr = env; + while(*ptr) + { + insert(std::string_view(*ptr)); + ++ptr; + } +} + +EnvMap::EnvMap(const char* env) +{ + if(env == nullptr) + { + return; + } + + auto ptr = env; + while(*ptr) + { + insert(ptr); + ptr += strlen(ptr) + 1; + } +} + +std::string EnvMap::operator[](const std::string& key) const +{ + try + { + return data.at(key); + } + catch(...) + { + return {}; + } +} + +void EnvMap::clear() +{ + data.clear(); +} + +void EnvMap::insert(std::string_view key_value) +{ + auto equals_sign = key_value.find('='); + if(equals_sign == std::string::npos) + { + insert({key_value, ""}); + return; + } + std::string key{key_value.substr(0, equals_sign)}; + std::string value{key_value.substr(equals_sign + 1)}; // skip '=' + insert({key, value}); +} + +void EnvMap::insert(const std::pair<std::string_view, std::string_view>& item) +{ + data[std::string(item.first)] = item.second; +} + +bool EnvMap::contains(std::string_view key) const +{ + return data.contains(std::string{key}); +} + +std::size_t EnvMap::size() const +{ + return data.size(); +} + +std::string EnvMap::stringify() const +{ + std::string str; + for(const auto& [key, value] : data) + { + str += key + "=" + value + '\0'; + } + str += '\0'; + return str; +} + +std::pair<int, const char* const*> EnvMap::get() +{ + pointerlist.clear(); + for(const auto& [key, value] : data) + { + pointerlist.push_back(key + "=" + value + '\0'); + } + return pointerlist.get(); +} diff --git a/src/pointerlist.h b/src/pointerlist.h new file mode 100644 index 0000000..988bb26 --- /dev/null +++ b/src/pointerlist.h @@ -0,0 +1,74 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <string> +#include <vector> +#include <deque> +#include <utility> +#include <map> + +//! Maintains an (owning) list of string args and converts them to argc/argv +//! compatible arguments on request. +//! The returned pointers are guaranteed to be valid as long as the PointerList +//! object lifetime is not exceeded. +class PointerList + : public std::deque<std::string> +{ +public: + PointerList() = default; + PointerList(int argc, const char* const argv[]); + + //! Returns argc/argv pair from the current list of args + //! The argv entry after the last is a nullptr (not included in the argc) + std::pair<int, const char* const*> get(); + +private: + std::vector<const char*> argptrs; +}; + + +//! Maintains an owning map of strings representing the env. +class EnvMap +{ +public: + EnvMap() = default; + + //! Initialize from an array of pointers to key=value\0 strings terminated + //! by \0 + EnvMap(const char* const env[]); + + //! Initialize from a string of the format + //! key1=val\0key2=val\0...keyN=val\0\0 + EnvMap(const char* env); + + std::string operator[](const std::string& key) const; + + //! Clear all items in the map + void clear(); + + //! Insert string from format: key=value + void insert(std::string_view key_value); + + //! Regular map insert + void insert(const std::pair<std::string_view, std::string_view>& item); + + //! Checks if the container contains element with specific key + bool contains(std::string_view key) const; + + std::size_t size() const; + + //! Return string with the following format: + //! key1=val\0key2=val\0...keyN=val\0\0 + std::string stringify() const; + + //! Returns the map as argc/argv pair where each pointer points to a string + //! of the format key=value\0 and is terminated with a nullptr + std::pair<int, const char* const*> get(); + +private: + std::map<std::string, std::string> data{}; + + PointerList pointerlist; +}; diff --git a/src/rebuild.cc b/src/rebuild.cc index 3e0e78d..d62e998 100644 --- a/src/rebuild.cc +++ b/src/rebuild.cc @@ -7,116 +7,109 @@ #include <filesystem> #include <algorithm> #include <source_location> +#include <cstring> +#include <span> +#include <vector> #include "configure.h" -#include "libctor.h" +#include "ctor.h" #include "tasks.h" #include "build.h" #include "execute.h" +#include "tools.h" +#include "util.h" -std::array<BuildConfigurationEntry, 1024> configFiles; -std::size_t numConfigFiles{0}; +std::vector<BuildConfigurationEntry>& getConfigFileList() +{ + static std::vector<BuildConfigurationEntry> configFiles; + return configFiles; +} + +std::vector<ExternalConfigurationEntry>& getExternalConfigFileList() +{ + static std::vector<ExternalConfigurationEntry> externalConfigFiles; + return externalConfigFiles; +} -int reg(BuildConfigurations (*cb)(const Settings&), +namespace ctor { +int reg(std::function<ctor::build_configurations (const ctor::settings&)> cb, const std::source_location location) { - // NOTE: std::cout cannot be used here - if(numConfigFiles >= configFiles.size()) + BuildConfigurationEntry entry; + + auto loc = std::filesystem::path(location.file_name()); + if(loc.is_absolute()) { - fprintf(stderr, "Max %d build configurations currently supported.\n", - (int)configFiles.size()); - exit(1); + auto pwd = std::filesystem::current_path(); + auto rel = std::filesystem::relative(loc, pwd); + entry.file = rel.string(); } - - configFiles[numConfigFiles].file = location.file_name(); - configFiles[numConfigFiles].cb = cb; - ++numConfigFiles; + else + { + entry.file = location.file_name(); + } + entry.cb = cb; + auto& configFiles = getConfigFileList(); + configFiles.push_back(entry); return 0; } +} // ctor:: int reg(const char* location) { - // NOTE: std::cout cannot be used here - if(numConfigFiles >= configFiles.size()) - { - fprintf(stderr, "Max %d build configurations currently supported.\n", - (int)configFiles.size()); - exit(1); - } + BuildConfigurationEntry entry; - configFiles[numConfigFiles].file = location; - configFiles[numConfigFiles].cb = - [](const Settings&){ return std::vector<BuildConfiguration>{}; }; - ++numConfigFiles; + entry.file = location; + entry.cb = + [](const ctor::settings&) + { + return std::vector<ctor::build_configuration>{}; + }; + auto& configFiles = getConfigFileList(); + configFiles.push_back(entry); return 0; } int unreg(const char* location) { - std::size_t found{0}; - for(std::size_t i = 0; i < numConfigFiles;) - { - if(std::string(location) == configFiles[i].file) - { - ++found; - for(std::size_t j = i; j < numConfigFiles; ++j) - { - configFiles[j] = configFiles[j + 1]; - } - --numConfigFiles; - } - else - { - ++i; - } - } - - for(std::size_t i = 0; i < numExternalConfigFiles;) - { - if(std::string(location) == externalConfigFiles[i].file) - { - ++found; - for(std::size_t j = i; j < numExternalConfigFiles; ++j) - { - externalConfigFiles[j] = externalConfigFiles[j + 1]; - } - --numExternalConfigFiles; - } - else - { - ++i; - } - } - - return found; + auto& configFiles = getConfigFileList(); + auto erasedConfigs = + std::erase_if(configFiles, + [&](const BuildConfigurationEntry& entry) + { + return entry.file == location; + }); + + auto& externalConfigFiles = getExternalConfigFileList(); + auto erasedExternals = + std::erase_if(externalConfigFiles, + [&](const ExternalConfigurationEntry& entry) + { + return entry.file == location; + }); + + return static_cast<int>(erasedConfigs) + static_cast<int>(erasedExternals); } -std::array<ExternalConfigurationEntry, 1024> externalConfigFiles; -std::size_t numExternalConfigFiles{0}; - -int reg(ExternalConfigurations (*cb)(const Settings&), +namespace ctor { +int reg(std::function<ctor::external_configurations (const ctor::settings&)> cb, const std::source_location location) { - // NOTE: std::cout cannot be used here - if(numExternalConfigFiles >= externalConfigFiles.size()) - { - fprintf(stderr, "Max %d external configurations currently supported.\n", - (int)externalConfigFiles.size()); - exit(1); - } - - externalConfigFiles[numExternalConfigFiles].file = location.file_name(); - externalConfigFiles[numExternalConfigFiles].cb = cb; - ++numExternalConfigFiles; + ExternalConfigurationEntry entry; + entry.file = location.file_name(); + entry.cb = cb; + auto& externalConfigFiles = getExternalConfigFileList(); + externalConfigFiles.push_back(entry); return 0; } +} // namespace ctor:: namespace { -bool contains(const std::vector<Source>& sources, const std::string& file) +bool contains(const std::vector<ctor::source>& sources, const std::string& file) { for(const auto& source : sources) { @@ -130,51 +123,63 @@ bool contains(const std::vector<Source>& sources, const std::string& file) } } -bool recompileCheck(const Settings& global_settings, int argc, char* argv[], +bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[], bool relaunch_allowed) { + auto args_span = std::span(argv, static_cast<std::size_t>(argc)); + using namespace std::string_literals; + const auto& configFiles = getConfigFileList(); if(global_settings.verbose > 1) { - std::cout << "Recompile check (" << numConfigFiles << "):\n"; + std::cout << "Recompile check (" << configFiles.size() << "):\n"; } - BuildConfiguration config; + ctor::build_configuration config; + config.type = ctor::target_type::executable; config.name = "ctor"; - config.flags.cxxflags = - std::vector<std::string>({ "-s", "-O3", "-std=c++20" }); - if(hasConfiguration(cfg::ctor_includedir)) + config.system = ctor::output_system::build; + + config.flags.cxxflags.emplace_back(ctor::cxx_opt::optimization, "3"); + config.flags.cxxflags.emplace_back(ctor::cxx_opt::cpp_std, "c++20"); + + const auto& c = ctor::get_configuration(); + if(c.has(ctor::cfg::ctor_includedir)) { - config.flags.cxxflags.push_back("-I"s + getConfiguration(cfg::ctor_includedir)); + config.flags.cxxflags.emplace_back(ctor::cxx_opt::include_path, + c.get(ctor::cfg::ctor_includedir)); } - if(hasConfiguration(cfg::ctor_libdir)) + if(c.has(ctor::cfg::ctor_libdir)) { - config.flags.ldflags.push_back("-L"s + getConfiguration(cfg::ctor_libdir)); + config.flags.ldflags.emplace_back(ctor::ld_opt::library_path, + c.get(ctor::cfg::ctor_libdir)); } - config.flags.ldflags.push_back("-lctor"); - config.flags.ldflags.push_back("-pthread"); - + config.flags.ldflags.emplace_back(ctor::ld_opt::link, "ctor"); + config.flags.ldflags.emplace_back(ctor::ld_opt::threads); - Settings settings{global_settings}; + ctor::settings settings{global_settings}; settings.verbose = -1; // Make check completely silent. - settings.builddir += "/ctor"; // override builddir to use ctor subdir + + // override builddir to use ctor subdir + auto ctor_builddir = std::filesystem::path(settings.builddir) / "ctor"; + settings.builddir = ctor_builddir.string(); { std::filesystem::path buildfile = settings.builddir; - std::filesystem::path currentfile = argv[0]; + std::filesystem::path currentfile = args_span[0]; config.target = std::filesystem::relative(currentfile, buildfile).string(); } if(std::filesystem::exists(configurationFile)) { - config.sources.push_back(configurationFile.string()); + config.sources.emplace_back(configurationFile.string()); } - for(std::size_t i = 0; i < numConfigFiles; ++i) + for(const auto& configFile : configFiles) { - std::string location = configFiles[i].file; + std::string location = configFile.file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; @@ -183,13 +188,14 @@ bool recompileCheck(const Settings& global_settings, int argc, char* argv[], // Ensure that files containing multiple configurations are only added once. if(!contains(config.sources, location)) { - config.sources.push_back(location); + config.sources.emplace_back(location); } } - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - std::string location = externalConfigFiles[i].file; + std::string location = externalConfigFile.file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; @@ -198,11 +204,11 @@ bool recompileCheck(const Settings& global_settings, int argc, char* argv[], // Ensure that files containing multiple configurations are only added once. if(!contains(config.sources, location)) { - config.sources.push_back(location); + config.sources.emplace_back(location); } } - auto tasks = taskFactory({config}, settings, {}); + auto tasks = taskFactory({config}, settings, {}, true); for(auto task : tasks) { @@ -238,16 +244,17 @@ bool recompileCheck(const Settings& global_settings, int argc, char* argv[], if(reconfigure) { std::vector<std::string> args; - args.push_back("reconfigure"); + args.emplace_back("reconfigure"); if(!relaunch_allowed) { - args.push_back("--no-rerun"); + args.emplace_back("--no-rerun"); } - for(int i = 1; i < argc; ++i) + for(std::size_t i = 1; i < args_span.size(); ++i) { - args.push_back(argv[i]); + args.emplace_back(args_span[i]); } - auto ret = execute(argv[0], args); + + auto ret = execute(settings, args_span[0], args); //if(ret != 0) { exit(ret); diff --git a/src/rebuild.h b/src/rebuild.h index f1255c6..8e0c78a 100644 --- a/src/rebuild.h +++ b/src/rebuild.h @@ -5,32 +5,30 @@ #include <vector> #include <array> +#include <string> +#include <functional> -#include "libctor.h" - -class Settings; +#include "ctor.h" struct BuildConfigurationEntry { - const char* file; - BuildConfigurations (*cb)(const Settings&); + std::string file; + std::function<ctor::build_configurations (const ctor::settings&)> cb; }; +std::vector<BuildConfigurationEntry>& getConfigFileList(); + struct ExternalConfigurationEntry { - const char* file; - ExternalConfigurations (*cb)(const Settings&); + std::string file; + std::function<ctor::external_configurations (const ctor::settings&)> cb; }; -extern std::array<BuildConfigurationEntry, 1024> configFiles; -extern std::size_t numConfigFiles; - -extern std::array<ExternalConfigurationEntry, 1024> externalConfigFiles; -extern std::size_t numExternalConfigFiles; +std::vector<ExternalConfigurationEntry>& getExternalConfigFileList(); int reg(const char* location); int unreg(const char* location); //! Returns true of recompilation was needed. -bool recompileCheck(const Settings& settings, int argc, char* argv[], +bool recompileCheck(const ctor::settings& settings, int argc, char* argv[], bool relaunch_allowed = true); diff --git a/src/task.cc b/src/task.cc index fb50765..ef7731b 100644 --- a/src/task.cc +++ b/src/task.cc @@ -3,19 +3,20 @@ // See accompanying file LICENSE for details. #include "task.h" -#include <unistd.h> #include <iostream> +#include <algorithm> +#include <utility> -Task::Task(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir) - : config(config) +Task::Task(const ctor::build_configuration& config_, const ctor::settings& settings_, + std::string sourceDir_) + : config(config_) , output_system(config.system) - , settings(settings) - , sourceDir(sourceDir) + , settings(settings_) + , sourceDir(std::move(sourceDir_)) { } -int Task::registerDepTasks(const std::set<std::shared_ptr<Task>>& tasks) +int Task::registerDepTasks(const std::vector<std::shared_ptr<Task>>& tasks) { for(const auto& depStr : depends()) { @@ -24,7 +25,10 @@ int Task::registerDepTasks(const std::set<std::shared_ptr<Task>>& tasks) { if(*task == depStr) { - dependsTasks.insert(task); + if(std::find(dependsTasks.begin(), dependsTasks.end(), task) == dependsTasks.end()) + { + dependsTasks.push_back(task); + } found = true; } } @@ -41,11 +45,14 @@ int Task::registerDepTasks(const std::set<std::shared_ptr<Task>>& tasks) bool Task::operator==(const std::string& depStr) { + std::filesystem::path generated_output = sourceDir; + generated_output /= target(); return - name() == depStr || - target() == depStr || - sourceDir + "/" + target() == depStr || - targetFile().string() == depStr + (!derived() && name() == depStr) || // compare to name + (!derived() && config.target == depStr) || // compare to stated target (ex. foo.a) + target() == depStr || // compare to derived (derived to foo.lib on msvc) + generated_output == depStr || // not sure what this is for?! + targetFile().string() == depStr // compare to target output file ; } @@ -107,54 +114,60 @@ State Task::state() const return task_state.load(); } -const BuildConfiguration& Task::buildConfig() const +const ctor::build_configuration& Task::buildConfig() const { return config; } -TargetType Task::targetType() const +ctor::target_type Task::targetType() const { return target_type; } -Language Task::sourceLanguage() const +ctor::language Task::sourceLanguage() const { return source_language; } -OutputSystem Task::outputSystem() const +ctor::output_system Task::outputSystem() const { return output_system; } std::string Task::compiler() const { + const auto& c = ctor::get_configuration(); switch(sourceLanguage()) { - case Language::C: + case ctor::language::c: switch(outputSystem()) { - case OutputSystem::Host: - return getConfiguration(cfg::host_cc, "/usr/bin/gcc"); - case OutputSystem::Build: - return getConfiguration(cfg::build_cc, "/usr/bin/gcc"); + case ctor::output_system::host: + return c.get(ctor::cfg::host_cc, "/usr/bin/gcc"); + case ctor::output_system::build: + return c.get(ctor::cfg::build_cc, "/usr/bin/gcc"); } - case Language::Cpp: + break; + case ctor::language::cpp: switch(outputSystem()) { - case OutputSystem::Host: - return getConfiguration(cfg::host_cxx, "/usr/bin/g++"); - case OutputSystem::Build: - return getConfiguration(cfg::build_cxx, "/usr/bin/g++"); + case ctor::output_system::host: + return c.get(ctor::cfg::host_cxx, "/usr/bin/g++"); + case ctor::output_system::build: + return c.get(ctor::cfg::build_cxx, "/usr/bin/g++"); } + break; default: std::cerr << "Unknown CC target type\n"; exit(1); break; } + + std::cerr << "Unhandled compiler!\n"; + exit(1); } -std::set<std::shared_ptr<Task>> Task::getDependsTasks() +std::vector<std::shared_ptr<Task>> Task::getDependsTasks() { return dependsTasks; } @@ -6,11 +6,10 @@ #include <vector> #include <string> #include <atomic> -#include <set> #include <memory> #include <filesystem> -#include "libctor.h" +#include "ctor.h" enum class State { @@ -21,20 +20,19 @@ enum class State Error, }; -struct Settings; - class Task { public: - Task(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir); + Task(const ctor::build_configuration& config, const ctor::settings& settings, + std::string sourceDir); + virtual ~Task() = default; - int registerDepTasks(const std::set<std::shared_ptr<Task>>& tasks); - virtual int registerDepTasksInner(const std::set<std::shared_ptr<Task>>& tasks) { return 0; } + int registerDepTasks(const std::vector<std::shared_ptr<Task>>& tasks); + virtual int registerDepTasksInner([[maybe_unused]]const std::vector<std::shared_ptr<Task>>& tasks) { return 0; } bool operator==(const std::string& dep); - virtual std::string name() const; + std::string name() const; bool dirty(); bool ready(); int run(); @@ -58,14 +56,14 @@ public: //! Returns a reference to the originating build config. //! Note: the build config of a derived task will be that of its parent //! (target) task. - const BuildConfiguration& buildConfig() const; + const ctor::build_configuration& buildConfig() const; - TargetType targetType() const; - Language sourceLanguage() const; - OutputSystem outputSystem() const; + ctor::target_type targetType() const; + ctor::language sourceLanguage() const; + ctor::output_system outputSystem() const; std::string compiler() const; - std::set<std::shared_ptr<Task>> getDependsTasks(); + std::vector<std::shared_ptr<Task>> getDependsTasks(); virtual std::string source() const { return {}; } @@ -75,11 +73,11 @@ protected: virtual bool dirtyInner() { return false; } std::vector<std::string> dependsStr; - std::set<std::shared_ptr<Task>> dependsTasks; - const BuildConfiguration& config; - TargetType target_type{TargetType::Auto}; - Language source_language{Language::Auto}; - OutputSystem output_system{OutputSystem::Host}; - const Settings& settings; + std::vector<std::shared_ptr<Task>> dependsTasks; + const ctor::build_configuration& config; + ctor::target_type target_type{ctor::target_type::automatic}; + ctor::language source_language{ctor::language::automatic}; + ctor::output_system output_system{ctor::output_system::host}; + const ctor::settings& settings; std::string sourceDir; }; diff --git a/src/task_ar.cc b/src/task_ar.cc index 3e1746c..3b45cc2 100644 --- a/src/task_ar.cc +++ b/src/task_ar.cc @@ -6,23 +6,27 @@ #include <iostream> #include <fstream> -#include "libctor.h" +#include "ctor.h" #include "execute.h" #include "util.h" +#include "tools.h" -TaskAR::TaskAR(const BuildConfiguration& config, - const Settings& settings, +TaskAR::TaskAR(const ctor::build_configuration& config_, + const ctor::settings& settings_, const std::string& target, const std::vector<std::string>& objects, - const std::string& sourceDir) - : Task(config, settings, sourceDir) - , config(config) - , settings(settings) - , sourceDir(sourceDir) + const std::string& sourceDir_) + : Task(config_, settings_, sourceDir_) + , _targetFile(target) + , config(config_) + , settings(settings_) + , sourceDir(sourceDir_) { - std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir); + target_type = ctor::target_type::static_library; + output_system = config.system; - _targetFile = target; + auto toolchain = getToolChain(config.system); + _targetFile = extension(toolchain, target_type, config.system, _targetFile); for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -32,23 +36,24 @@ TaskAR::TaskAR(const BuildConfiguration& config, for(const auto& dep : config.depends) { - depFiles.push_back(dep); + depFiles.emplace_back(dep); } flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; - target_type = TargetType::StaticLibrary; - source_language = Language::C; + source_language = ctor::language::c; for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); // TODO: Use task languages instead if(sourceFile.extension().string() != ".c") { - source_language = Language::Cpp; + source_language = ctor::language::cpp; } } + + std::filesystem::create_directories(targetFile().parent_path()); } bool TaskAR::dirtyInner() @@ -77,9 +82,17 @@ bool TaskAR::dirtyInner() int TaskAR::runInner() { + auto toolchain = getToolChain(config.system); + std::vector<std::string> args; - args.push_back("rcs"); - args.push_back(targetFile().string()); + for(const auto& flag : config.flags.arflags) + { + append(args, to_strings(toolchain, flag)); + } + append(args, ar_option(toolchain, ctor::ar_opt::replace)); + append(args, ar_option(toolchain, ctor::ar_opt::add_index)); + append(args, ar_option(toolchain, ctor::ar_opt::create)); + append(args, ar_option(toolchain, ctor::ar_opt::output, targetFile().string())); for(const auto& task : getDependsTasks()) { args.push_back(task->targetFile().string()); @@ -92,21 +105,29 @@ int TaskAR::runInner() if(settings.verbose == 0) { - std::cout << "AR => " << targetFile().string() << "\n"; + std::string output = "AR => " + targetFile().string() + '\n'; + std::cout << output << std::flush; } + const auto& c = ctor::get_configuration(); std::string tool; switch(outputSystem()) { - case OutputSystem::Host: - tool = getConfiguration(cfg::host_ar, "/usr/bin/ar"); + case ctor::output_system::host: + tool = c.get(ctor::cfg::host_ar, "/usr/bin/ar"); break; - case OutputSystem::Build: - tool = getConfiguration(cfg::build_ar, "/usr/bin/ar"); + case ctor::output_system::build: + tool = c.get(ctor::cfg::build_ar, "/usr/bin/ar"); break; } - return execute(tool, args, settings.verbose > 0); + auto res = execute(settings, tool, args, c.env); + if(res != 0) + { + std::filesystem::remove(targetFile()); + } + + return res; } int TaskAR::clean() @@ -159,16 +180,21 @@ bool TaskAR::derived() const std::string TaskAR::flagsString() const { + auto toolchain = getToolChain(config.system); std::string flagsStr; + bool first{true}; for(const auto& flag : config.flags.ldflags) { - if(flag != config.flags.ldflags[0]) + for(const auto& str : to_strings(toolchain, flag)) { - flagsStr += " "; + if(first) + { + flagsStr += " "; + first = false; + } + flagsStr += str; } - flagsStr += flag; } - flagsStr += "\n"; for(const auto& dep : config.depends) { diff --git a/src/task_ar.h b/src/task_ar.h index c76a852..601afeb 100644 --- a/src/task_ar.h +++ b/src/task_ar.h @@ -10,15 +10,12 @@ #include <future> #include <filesystem> -struct BuildConfiguration; -struct Settings; - class TaskAR : public Task { public: - TaskAR(const BuildConfiguration& config, - const Settings& settings, + TaskAR(const ctor::build_configuration& config, + const ctor::settings& settings, const std::string& target, const std::vector<std::string>& objects, const std::string& sourceDir); @@ -44,7 +41,7 @@ private: std::filesystem::path _targetFile; std::filesystem::path flagsFile; - const BuildConfiguration& config; - const Settings& settings; + const ctor::build_configuration& config; + const ctor::settings& settings; std::string sourceDir; }; diff --git a/src/task_cc.cc b/src/task_cc.cc index a55a619..9628455 100644 --- a/src/task_cc.cc +++ b/src/task_cc.cc @@ -6,48 +6,51 @@ #include <iostream> #include <fstream> #include <cassert> +#include <algorithm> -#include "libctor.h" +#include "ctor.h" #include "execute.h" #include "util.h" - -TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir, const Source& source) - : Task(config, settings, sourceDir) - , config(config) - , settings(settings) - , sourceDir(sourceDir) +#include "tools.h" +#include "deps.h" + +TaskCC::TaskCC(const ctor::build_configuration& config_, const ctor::settings& settings_, + const std::string& sourceDir_, const ctor::source& source) + : Task(config_, settings_, sourceDir_) + , sourceFile(sourceDir_) + , config(config_) + , settings(settings_) + , sourceDir(sourceDir_) , _source(source) { - sourceFile = sourceDir; sourceFile /= source.file; std::filesystem::path base = sourceFile.parent_path(); - std::filesystem::create_directories(std::filesystem::path(settings.builddir) / base); base /= cleanUp(config.target); base += "-"; base += sourceFile.stem(); - target_type = TargetType::Object; + target_type = ctor::target_type::object; + output_system = config.system; source_language = source.language; - if(source_language == Language::Auto) + if(source_language == ctor::language::automatic) { source_language = languageFromExtension(sourceFile); } switch(source_language) { - case Language::C: + case ctor::language::c: base += "_c"; break; - case Language::Cpp: + case ctor::language::cpp: base += "_cc"; break; - case Language::Asm: + case ctor::language::assembler: base += "_asm"; break; - case Language::Auto: + case ctor::language::automatic: assert(0 && "This should never happen"); break; } @@ -55,36 +58,38 @@ TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, if(source.output.empty()) { _targetFile = base; - _targetFile += ".o"; } else { _targetFile = source.output; } + auto toolchain = getToolChain(config.system); + _targetFile = extension(toolchain, target_type, config.system, _targetFile); + depsFile = targetFile().parent_path() / targetFile().stem(); depsFile += ".d"; flagsFile = targetFile().parent_path() / targetFile().stem(); flagsFile += ".flags"; + + std::filesystem::create_directories(targetFile().parent_path()); } -int TaskCC::registerDepTasksInner(const std::set<std::shared_ptr<Task>>& tasks) +int TaskCC::registerDepTasksInner(const std::vector<std::shared_ptr<Task>>& tasks) { for(const auto& task : tasks) { if(*task == _source.file) { - dependsTasks.insert(task); + if(std::find(dependsTasks.begin(), dependsTasks.end(), task) == dependsTasks.end()) + { + dependsTasks.push_back(task); + } } } return 0; } -std::string TaskCC::name() const -{ - return {}; -} - bool TaskCC::dirtyInner() { if(!std::filesystem::exists(sourceFile)) @@ -127,7 +132,8 @@ bool TaskCC::dirtyInner() } } - auto depList = readDeps(depsFile.string()); + auto toolchain = getToolChain(config.system); + auto depList = readDeps(depsFile.string(), toolchain); for(const auto& dep : depList) { if(!std::filesystem::exists(dep) || @@ -166,12 +172,59 @@ int TaskCC::runInner() if(settings.verbose == 0) { - std::cout << compiler() << " " << - sourceFile.lexically_normal().string() << " => " << - targetFile().lexically_normal().string() << "\n"; + std::string output; + switch(sourceLanguage()) + { + case ctor::language::c: + output = "CC "; + break; + case ctor::language::cpp: + output = "CXX "; + break; + case ctor::language::automatic: + case ctor::language::assembler: + // Only c/c++ handled by this task type. + break; + } + output += sourceFile.lexically_normal().string() + " => " + + targetFile().lexically_normal().string() + '\n'; + std::cout << output << std::flush; } - return execute(compiler(), args, settings.verbose > 0); + auto toolchain = getToolChain(config.system); + const auto& cfg = ctor::get_configuration(); + switch(sourceLanguage()) + { + case ctor::language::c: + { + auto cflags = cfg.getenv("CFLAGS"); + if(!cflags.empty()) + { + append(args, c_option(toolchain, ctor::c_opt::custom, cflags)); + } + } + break; + case ctor::language::cpp: + { + auto cxxflags = cfg.getenv("CXXFLAGS"); + if(!cxxflags.empty()) + { + append(args, cxx_option(toolchain, ctor::cxx_opt::custom, cxxflags)); + } + } + break; + case ctor::language::automatic: + case ctor::language::assembler: + // Only c/c++ handled by this task type. + break; + } + auto res = execute(settings, compiler(), args, cfg.env); + if(res != 0) + { + std::filesystem::remove(targetFile()); + } + + return res; } int TaskCC::clean() @@ -242,17 +295,29 @@ std::string TaskCC::source() const std::vector<std::string> TaskCC::flags() const { + std::vector<std::string> flags; + auto toolchain = getToolChain(config.system); + switch(sourceLanguage()) { - case Language::C: - return config.flags.cflags; - case Language::Cpp: - return config.flags.cxxflags; + case ctor::language::c: + for(const auto& flag : config.flags.cflags) + { + append(flags, to_strings(toolchain, flag)); + } + return flags; + case ctor::language::cpp: + for(const auto& flag : config.flags.cxxflags) + { + append(flags, to_strings(toolchain, flag)); + } + return flags; default: std::cerr << "Unknown CC target type\n"; exit(1); break; } + } std::string TaskCC::flagsString() const @@ -267,42 +332,102 @@ std::string TaskCC::flagsString() const std::vector<std::string> TaskCC::getCompilerArgs() const { + auto toolchain = getToolChain(config.system); auto compiler_flags = flags(); std::vector<std::string> args; - args.push_back("-MMD"); - if(std::filesystem::path(config.target).extension() == ".so") + switch(sourceLanguage()) { - // Add -fPIC arg to all contained object files - args.push_back("-fPIC"); - } + case ctor::language::c: + { + append(args, c_option(toolchain, ctor::c_opt::generate_dep_tree, depsFile.string())); - args.push_back("-c"); - args.push_back(sourceFile.string()); - args.push_back("-o"); - args.push_back(targetFile().string()); + if(std::filesystem::path(config.target).extension() == ".so") + { + // Add -fPIC arg to all contained object files + append(args, c_option(toolchain, + ctor::c_opt::position_independent_code)); + } - for(const auto& flag : compiler_flags) - { - // Is arg an added include path? - if(flag.substr(0, 2) == "-I") + append(args, c_option(toolchain, ctor::c_opt::no_link)); + args.push_back(sourceFile.string()); + append(args, c_option(toolchain, + ctor::c_opt::output, targetFile().string())); + + // Relative include paths has to be altered to be relative to sourceDir + for(const auto& flag : compiler_flags) + { + auto option = c_option(flag, toolchain); + switch(option.opt) + { + case ctor::c_opt::include_path: + { + std::filesystem::path path(option.arg); + if(path.is_relative()) + { + path = (sourceDir / path).lexically_normal(); + append(args, c_option(toolchain, + ctor::c_opt::include_path, path.string())); + continue; + } + } + break; + default: + break; + } + + args.push_back(flag); + } + } + break; + + case ctor::language::cpp: { - std::string include_path = flag.substr(2); - include_path.erase(0, include_path.find_first_not_of(' ')); - std::filesystem::path path(include_path); + append(args, cxx_option(toolchain, ctor::cxx_opt::generate_dep_tree, depsFile.string())); - // Is it relative? - if(path.is_relative()) + if(std::filesystem::path(config.target).extension() == ".so") { - path = (sourceDir / path).lexically_normal(); - std::string new_include_path = "-I" + path.string(); - args.push_back(new_include_path); - continue; + // Add -fPIC arg to all contained object files + append(args, cxx_option(toolchain, + ctor::cxx_opt::position_independent_code)); } + + append(args, cxx_option(toolchain, ctor::cxx_opt::no_link)); + args.push_back(sourceFile.string()); + append(args, cxx_option(toolchain, + ctor::cxx_opt::output, targetFile().string())); + + // Relative include paths has to be altered to be relative to sourceDir + for(const auto& flag : compiler_flags) + { + auto option = cxx_option(flag, toolchain); + switch(option.opt) + { + case ctor::cxx_opt::include_path: + { + std::filesystem::path path(option.arg); + if(path.is_relative()) + { + path = (sourceDir / path).lexically_normal(); + append(args, cxx_option(toolchain, + ctor::cxx_opt::include_path, path.string())); + continue; + } + } + break; + default: + break; + } + + args.push_back(flag); + } + } + break; - args.push_back(flag); + default: + break; } return args; diff --git a/src/task_cc.h b/src/task_cc.h index 0a0d96a..2299fcd 100644 --- a/src/task_cc.h +++ b/src/task_cc.h @@ -10,21 +10,17 @@ #include <future> #include <filesystem> -struct BuildConfiguration; -struct Settings; - class TaskCC : public Task { public: - TaskCC(const BuildConfiguration& config, - const Settings& settings, - const std::string& sourceDir, const Source& source); + TaskCC(const ctor::build_configuration& config, + const ctor::settings& settings, + const std::string& sourceDir, const ctor::source& source); virtual ~TaskCC() = default; - int registerDepTasksInner(const std::set<std::shared_ptr<Task>>& tasks) override; + int registerDepTasksInner(const std::vector<std::shared_ptr<Task>>& tasks) override; - std::string name() const override; bool dirtyInner() override; int runInner() override; @@ -51,8 +47,8 @@ protected: std::filesystem::path depsFile; std::filesystem::path flagsFile; - const BuildConfiguration& config; - const Settings& settings; + const ctor::build_configuration& config; + const ctor::settings& settings; std::filesystem::path sourceDir; - const Source& _source; + const ctor::source& _source; }; diff --git a/src/task_fn.cc b/src/task_fn.cc index ab00fae..b6b50ea 100644 --- a/src/task_fn.cc +++ b/src/task_fn.cc @@ -7,17 +7,17 @@ #include <fstream> #include <cassert> -#include "libctor.h" +#include "ctor.h" #include "execute.h" #include "util.h" -TaskFn::TaskFn(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir, const Source& source) - : Task(config, settings, sourceDir) - , config(config) - , settings(settings) +TaskFn::TaskFn(const ctor::build_configuration& config_, const ctor::settings& settings_, + const std::string& sourceDir_, const ctor::source& source) + : Task(config_, settings_, sourceDir_) + , sourceFile(sourceDir_) + , config(config_) + , settings(settings_) { - sourceFile = sourceDir; sourceFile /= source.file; std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceFile.parent_path()); @@ -68,15 +68,22 @@ int TaskFn::runInner() if(settings.verbose >= 0) { - std::cout << "Fn" << " " << - sourceFile.lexically_normal().string() << " => " << - targetFile().lexically_normal().string() << "\n"; + std::string output = "Fn " + + sourceFile.lexically_normal().string() + " => " + + targetFile().lexically_normal().string() + '\n'; + std::cout << output << std::flush; } - return config.function(sourceFile.string(), - targetFile().string(), - config, - settings); + auto res = config.function(sourceFile.string(), + targetFile().string(), + config, + settings); + if(res != 0) + { + std::filesystem::remove(targetFile()); + } + + return res; } int TaskFn::clean() @@ -97,7 +104,7 @@ std::vector<std::string> TaskFn::depends() const std::string TaskFn::target() const { - return _targetFile; + return _targetFile.string(); } std::filesystem::path TaskFn::targetFile() const diff --git a/src/task_fn.h b/src/task_fn.h index e350395..1bad32c 100644 --- a/src/task_fn.h +++ b/src/task_fn.h @@ -10,16 +10,13 @@ #include <future> #include <filesystem> -struct BuildConfiguration; -struct Settings; - class TaskFn : public Task { public: - TaskFn(const BuildConfiguration& config, - const Settings& settings, - const std::string& sourceDir, const Source& source); + TaskFn(const ctor::build_configuration& config, + const ctor::settings& settings, + const std::string& sourceDir, const ctor::source& source); virtual ~TaskFn() = default; bool dirtyInner() override; @@ -41,7 +38,7 @@ protected: std::filesystem::path sourceFile; std::filesystem::path _targetFile; - const BuildConfiguration& config; - const Settings& settings; + const ctor::build_configuration& config; + const ctor::settings& settings; std::filesystem::path sourceDir; }; diff --git a/src/task_ld.cc b/src/task_ld.cc index 3e05fb1..03745be 100644 --- a/src/task_ld.cc +++ b/src/task_ld.cc @@ -6,29 +6,34 @@ #include <iostream> #include <fstream> -#include "libctor.h" +#include "ctor.h" #include "execute.h" #include "util.h" +#include "tools.h" -TaskLD::TaskLD(const BuildConfiguration& config, - const Settings& settings, +TaskLD::TaskLD(const ctor::build_configuration& config_, + const ctor::settings& settings_, const std::string& target, const std::vector<std::string>& objects, - const std::string& sourceDir) - : Task(config, settings, sourceDir) - , config(config) - , settings(settings) - , sourceDir(sourceDir) + const std::string& sourceDir_, + bool is_self_) + : Task(config_, settings_, sourceDir_) + , config(config_) + , settings(settings_) + , sourceDir(sourceDir_) + , is_self(is_self_) { target_type = config.type; - if(target_type == TargetType::Auto) + output_system = config.system; + + if(target_type == ctor::target_type::automatic) { - target_type = TargetType::Executable; + target_type = ctor::target_type::executable; } - std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir); - _targetFile = target; + auto toolchain = getToolChain(config.system); + _targetFile = extension(toolchain, target_type, config.system, _targetFile); for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -38,21 +43,23 @@ TaskLD::TaskLD(const BuildConfiguration& config, for(const auto& dep : config.depends) { - depFiles.push_back(dep); + depFiles.emplace_back(dep); } flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; - source_language = Language::C; + source_language = ctor::language::c; for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); if(sourceFile.extension().string() != ".c") { - source_language = Language::Cpp; + source_language = ctor::language::cpp; } } + + std::filesystem::create_directories(targetFile().parent_path()); } bool TaskLD::dirtyInner() @@ -81,17 +88,22 @@ bool TaskLD::dirtyInner() int TaskLD::runInner() { + auto toolchain = getToolChain(config.system); + std::vector<std::string> args; for(const auto& dep : getDependsTasks()) { auto depFile = dep->targetFile(); - if(depFile.extension() == ".so") + auto dep_type = target_type_from_extension(toolchain, depFile); + if(dep_type == ctor::target_type::dynamic_library) { - args.push_back(std::string("-L") + settings.builddir); + append(args, ld_option(toolchain, ctor::ld_opt::library_path, + targetFile().parent_path().string())); auto lib = depFile.stem().string().substr(3); // strip 'lib' prefix - args.push_back(std::string("-l") + lib); + append(args, ld_option(toolchain, ctor::ld_opt::link, lib)); } - else if(depFile.extension() == ".a" || depFile.extension() == ".o") + else if(dep_type == ctor::target_type::static_library || + dep_type == ctor::target_type::object) { args.push_back(depFile.string()); } @@ -99,10 +111,10 @@ int TaskLD::runInner() for(const auto& flag : config.flags.ldflags) { - args.push_back(flag); + append(args, to_strings(toolchain, flag)); } - args.push_back("-o"); - args.push_back(targetFile().string()); + + append(args, ld_option(toolchain, ctor::ld_opt::output, targetFile().string())); { // Write flags to file. std::ofstream flagsStream(flagsFile); @@ -111,11 +123,24 @@ int TaskLD::runInner() if(settings.verbose == 0) { - std::cout << "LD => " << targetFile().string() << "\n"; + std::string output = "LD => " + targetFile().string() + '\n'; + std::cout << output << std::flush; } auto tool = compiler(); - return execute(tool, args, settings.verbose > 0); + const auto& cfg = ctor::get_configuration(); + auto ldflags = cfg.getenv("LDFLAGS"); + if(!ldflags.empty()) + { + append(args, ld_option(toolchain, ctor::ld_opt::custom, ldflags)); + } + auto res = execute(settings, tool, args, cfg.env, is_self); + if(res != 0) + { + std::filesystem::remove(targetFile()); + } + + return res; } int TaskLD::clean() @@ -168,16 +193,21 @@ bool TaskLD::derived() const std::string TaskLD::flagsString() const { + auto toolchain = getToolChain(config.system); std::string flagsStr; + bool first{true}; for(const auto& flag : config.flags.ldflags) { - if(flag != config.flags.ldflags[0]) + for(const auto& str : to_strings(toolchain, flag)) { - flagsStr += " "; + if(first) + { + flagsStr += " "; + first = false; + } + flagsStr += str; } - flagsStr += flag; } - flagsStr += "\n"; for(const auto& dep : config.depends) { diff --git a/src/task_ld.h b/src/task_ld.h index 8625075..c0e3ebb 100644 --- a/src/task_ld.h +++ b/src/task_ld.h @@ -10,18 +10,16 @@ #include <future> #include <filesystem> -struct BuildConfiguration; -struct Settings; - class TaskLD : public Task { public: - TaskLD(const BuildConfiguration& config, - const Settings& settings, + TaskLD(const ctor::build_configuration& config, + const ctor::settings& settings, const std::string& target, const std::vector<std::string>& objects, - const std::string& _sourceDir); + const std::string& _sourceDir, + bool is_self); virtual ~TaskLD() = default; bool dirtyInner() override; @@ -44,7 +42,8 @@ private: std::filesystem::path _targetFile; std::filesystem::path flagsFile; - const BuildConfiguration& config; - const Settings& settings; + const ctor::build_configuration& config; + const ctor::settings& settings; std::string sourceDir; + bool is_self; }; diff --git a/src/task_so.cc b/src/task_so.cc index 1af14b5..92aeefe 100644 --- a/src/task_so.cc +++ b/src/task_so.cc @@ -6,24 +6,29 @@ #include <iostream> #include <fstream> -#include "libctor.h" +#include "ctor.h" #include "execute.h" #include "util.h" +#include "tools.h" -TaskSO::TaskSO(const BuildConfiguration& config, - const Settings& settings, +TaskSO::TaskSO(const ctor::build_configuration& config_, + const ctor::settings& settings_, const std::string& target, const std::vector<std::string>& objects, - const std::string& sourceDir) - : Task(config, settings, sourceDir) - , config(config) - , settings(settings) - , sourceDir(sourceDir) + const std::string& sourceDir_) + : Task(config_, settings_, sourceDir_) + , config(config_) + , settings(settings_) + , sourceDir(sourceDir_) { std::filesystem::path base = sourceDir; - std::filesystem::create_directories(std::filesystem::path(settings.builddir) / base); + + target_type = ctor::target_type::dynamic_library; + output_system = config.system; _targetFile = base / target; + auto toolchain = getToolChain(config.system); + _targetFile = extension(toolchain, target_type, config.system, _targetFile); for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -33,25 +38,24 @@ TaskSO::TaskSO(const BuildConfiguration& config, for(const auto& dep : config.depends) { - std::filesystem::path depFile = settings.builddir; - depFile /= dep; - depFiles.push_back(depFile); + depFiles.emplace_back(dep); } flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; - target_type = TargetType::DynamicLibrary; - source_language = Language::C; + source_language = ctor::language::c; for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); // TODO: Use task languages instead if(sourceFile.extension().string() != ".c") { - source_language = Language::Cpp; + source_language = ctor::language::cpp; } } + + std::filesystem::create_directories(targetFile().parent_path()); } bool TaskSO::dirtyInner() @@ -80,27 +84,23 @@ bool TaskSO::dirtyInner() int TaskSO::runInner() { + auto toolchain = getToolChain(config.system); + std::vector<std::string> args; - args.push_back("-fPIC"); - args.push_back("-shared"); + append(args, ld_option(toolchain, ctor::ld_opt::position_independent_code)); + append(args, ld_option(toolchain, ctor::ld_opt::build_shared)); - args.push_back("-o"); - args.push_back(targetFile().string()); + append(args, ld_option(toolchain, ctor::ld_opt::output, targetFile().string())); for(const auto& task : getDependsTasks()) { - args.push_back(task->target()); - } - - for(const auto& depFile : depFiles) - { - args.push_back(depFile.string()); + args.push_back(task->targetFile().string()); } for(const auto& flag : config.flags.ldflags) { - args.push_back(flag); + append(args, to_strings(toolchain, flag)); } { // Write flags to file. @@ -110,11 +110,24 @@ int TaskSO::runInner() if(settings.verbose == 0) { - std::cout << "LD => " << targetFile().string() << "\n"; + std::string output = "LD => " + targetFile().string() + '\n'; + std::cout << output << std::flush; } auto tool = compiler(); - return execute(tool, args, settings.verbose > 0); + const auto& cfg = ctor::get_configuration(); + auto ldflags = cfg.getenv("LDFLAGS"); + if(!ldflags.empty()) + { + append(args, ld_option(toolchain, ctor::ld_opt::custom, ldflags)); + } + auto res = execute(settings, tool, args, cfg.env); + if(res != 0) + { + std::filesystem::remove(targetFile()); + } + + return res; } int TaskSO::clean() @@ -167,12 +180,21 @@ bool TaskSO::derived() const std::string TaskSO::flagsString() const { - std::string flagsStr = compiler(); + auto toolchain = getToolChain(config.system); + std::string flagsStr; + bool first{true}; for(const auto& flag : config.flags.ldflags) { - flagsStr += " " + flag; + for(const auto& str : to_strings(toolchain, flag)) + { + if(first) + { + flagsStr += " "; + first = false; + } + flagsStr += str; + } } - flagsStr += "\n"; for(const auto& dep : config.depends) { diff --git a/src/task_so.h b/src/task_so.h index fe5d2fd..60af225 100644 --- a/src/task_so.h +++ b/src/task_so.h @@ -10,15 +10,12 @@ #include <future> #include <filesystem> -struct BuildConfiguration; -struct Settings; - class TaskSO : public Task { public: - TaskSO(const BuildConfiguration& config, - const Settings& settings, + TaskSO(const ctor::build_configuration& config, + const ctor::settings& settings, const std::string& target, const std::vector<std::string>& objects, const std::string& sourceDir); @@ -44,7 +41,7 @@ private: std::filesystem::path _targetFile; std::filesystem::path flagsFile; - const BuildConfiguration& config; - const Settings& settings; + const ctor::build_configuration& config; + const ctor::settings& settings; std::string sourceDir; }; diff --git a/src/tasks.cc b/src/tasks.cc index 68b2476..94fe269 100644 --- a/src/tasks.cc +++ b/src/tasks.cc @@ -8,8 +8,9 @@ #include <list> #include <iostream> #include <algorithm> +#include <span> -#include "libctor.h" +#include "ctor.h" #include "task.h" #include "task_cc.h" #include "task_ld.h" @@ -18,24 +19,28 @@ #include "task_fn.h" #include "rebuild.h" #include "configure.h" +#include "util.h" +#include "tools.h" -const std::deque<Target>& getTargets(const Settings& settings, +const std::deque<Target>& getTargets(const ctor::settings& settings, bool resolve_externals) { + auto& config_files = getConfigFileList(); + static bool initialised{false}; static std::deque<Target> targets; if(!initialised) { - const auto& externals = configuration().externals; - for(std::size_t i = 0; i < numConfigFiles; ++i) + const auto& externals = ctor::get_configuration().externals; + for(const auto& config_file : config_files) { std::string path = - std::filesystem::path(configFiles[i].file).parent_path().string(); + std::filesystem::path(config_file.file).parent_path().string(); if(settings.verbose > 1) { - std::cout << configFiles[i].file << " in path " << path << "\n"; + std::cout << config_file.file << " in path " << path << "\n"; } - auto configs = configFiles[i].cb(settings); + auto configs = config_file.cb(settings); for(auto& config : configs) { if(resolve_externals) @@ -50,21 +55,12 @@ const std::deque<Target>& getTargets(const Settings& settings, exit(1); } const auto& flags = externals.at(external); - config.flags.cflags.insert(config.flags.cflags.end(), - flags.cflags.begin(), - flags.cflags.end()); - config.flags.cxxflags.insert(config.flags.cxxflags.end(), - flags.cxxflags.begin(), - flags.cxxflags.end()); - config.flags.ldflags.insert(config.flags.ldflags.end(), - flags.ldflags.begin(), - flags.ldflags.end()); - config.flags.asmflags.insert(config.flags.asmflags.end(), - flags.asmflags.begin(), - flags.asmflags.end()); - //config.libs.insert(config.libs.end(), - // libs.begin(), - // libs.end()); + append(config.flags.cflags, flags.cflags); + append(config.flags.cxxflags, flags.cxxflags); + append(config.flags.ldflags, flags.ldflags); + append(config.flags.arflags, flags.arflags); + append(config.flags.asmflags, flags.asmflags); + //append(config.libs.insert(config.libs libs); } } @@ -77,49 +73,42 @@ const std::deque<Target>& getTargets(const Settings& settings, return targets; } -std::set<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config, - const Settings& settings, - const std::string& sourceDir) +std::vector<std::shared_ptr<Task>> taskFactory(const ctor::build_configuration& config, + const ctor::settings& settings, + const std::string& sourceDir, + bool is_self) { - std::set<std::shared_ptr<Task>> tasks; + std::vector<std::shared_ptr<Task>> tasks; std::filesystem::path targetFile(config.target); - TargetType target_type{config.type}; - if(target_type == TargetType::Auto) + ctor::target_type target_type{config.type}; + if(target_type == ctor::target_type::automatic) { if(config.function != nullptr) { - target_type = TargetType::Function; - } - else if(targetFile.extension() == ".a") - { - target_type = TargetType::StaticLibrary; - } - else if(targetFile.extension() == ".so") - { - target_type = TargetType::DynamicLibrary; - } - else if(targetFile.extension() == "") - { - target_type = TargetType::Executable; + target_type = ctor::target_type::function; } else { - std::cerr << "Could not deduce target type from target " << - targetFile.string() << " please specify.\n"; - exit(1); + target_type = target_type_from_extension(ctor::toolchain::any, targetFile); } } + const auto& c = ctor::get_configuration(); std::vector<std::string> objects; - if(target_type != TargetType::Function) + if(target_type != ctor::target_type::function) { - for(const auto& file : config.sources) + for(const auto& source : config.sources) { - auto task = std::make_shared<TaskCC>(config, settings, sourceDir, file); - tasks.insert(task); - objects.push_back(task->targetFile().string()); + if(source.toolchain == ctor::toolchain::any || + (config.system == ctor::output_system::build && source.toolchain == c.build_toolchain) || + (config.system == ctor::output_system::host && source.toolchain == c.host_toolchain)) + { + auto task = std::make_shared<TaskCC>(config, settings, sourceDir, source); + tasks.push_back(task); + objects.push_back(task->targetFile().string()); + } } } #ifndef BOOTSTRAP @@ -128,7 +117,7 @@ std::set<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config, for(const auto& file : config.sources) { auto task = std::make_shared<TaskFn>(config, settings, sourceDir, file); - tasks.insert(task); + tasks.push_back(task); objects.push_back(task->target()); } } @@ -136,35 +125,41 @@ std::set<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config, switch(target_type) { - case TargetType::Auto: + case ctor::target_type::automatic: // The target_type cannot be Auto break; - case TargetType::StaticLibrary: - case TargetType::UnitTestLib: - tasks.insert(std::make_shared<TaskAR>(config, settings, config.target, - objects, sourceDir)); + case ctor::target_type::unknown: + std::cerr << "Could not deduce target type from target " << + targetFile.string() << " please specify.\n"; + exit(1); + break; + + case ctor::target_type::static_library: + case ctor::target_type::unit_test_library: + tasks.push_back(std::make_shared<TaskAR>(config, settings, config.target, + objects, sourceDir)); break; #ifndef BOOTSTRAP - case TargetType::DynamicLibrary: + case ctor::target_type::dynamic_library: // TODO: Use C++20 starts_with if(targetFile.stem().string().substr(0, 3) != "lib") { std::cerr << "Dynamic library target must have 'lib' prefix\n"; exit(1); } - tasks.insert(std::make_shared<TaskSO>(config, settings, config.target, - objects, sourceDir)); + tasks.push_back(std::make_shared<TaskSO>(config, settings, config.target, + objects, sourceDir)); break; - case TargetType::Executable: - case TargetType::UnitTest: - tasks.insert(std::make_shared<TaskLD>(config, settings, config.target, - objects, sourceDir)); + case ctor::target_type::executable: + case ctor::target_type::unit_test: + tasks.push_back(std::make_shared<TaskLD>(config, settings, config.target, + objects, sourceDir, is_self)); break; - case TargetType::Object: - case TargetType::Function: + case ctor::target_type::object: + case ctor::target_type::function: break; #else default: @@ -175,18 +170,20 @@ std::set<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config, return tasks; } -std::shared_ptr<Task> getNextTask(const std::set<std::shared_ptr<Task>>& allTasks, - std::set<std::shared_ptr<Task>>& dirtyTasks) +std::shared_ptr<Task> getNextTask([[maybe_unused]]const ctor::settings& settings, + [[maybe_unused]]const std::vector<std::shared_ptr<Task>>& allTasks, + std::vector<std::shared_ptr<Task>>& dirtyTasks) { for(auto dirtyTask = dirtyTasks.begin(); dirtyTask != dirtyTasks.end(); ++dirtyTask) { + auto task = *dirtyTask; //std::cout << "Examining target " << (*dirtyTask)->target() << "\n"; - if((*dirtyTask)->ready()) + if(task->ready() || settings.dry_run) { dirtyTasks.erase(dirtyTask); - return *dirtyTask; + return task; } } @@ -194,20 +191,20 @@ std::shared_ptr<Task> getNextTask(const std::set<std::shared_ptr<Task>>& allTask return nullptr; } -std::set<std::shared_ptr<Task>> getTasks(const Settings& settings, - const std::vector<std::string> names, - bool resolve_externals) +std::vector<std::shared_ptr<Task>> getTasks(const ctor::settings& settings, + const std::vector<std::string> names, + bool resolve_externals) { auto& targets = getTargets(settings, resolve_externals); - std::set<std::shared_ptr<Task>> tasks; + std::vector<std::shared_ptr<Task>> tasks; for(const auto& target : targets) { if(names.empty() || std::find(std::begin(names), std::end(names), target.config.target) != std::end(names)) { std::vector<std::string> objects; - auto t = taskFactory(target.config, settings, target.path); - tasks.insert(t.begin(), t.end()); + auto t = taskFactory(target.config, settings, target.path, false); + tasks.insert(tasks.end(), t.begin(), t.end()); } } diff --git a/src/tasks.h b/src/tasks.h index c547432..97fc84d 100644 --- a/src/tasks.h +++ b/src/tasks.h @@ -4,40 +4,38 @@ #pragma once #include <string> -#include <set> #include <memory> #include <deque> #include "task.h" -class BuildConfiguration; -class Settings; - struct Target { - BuildConfiguration config; + ctor::build_configuration config; std::string path; }; //! Get list of all registered targets -const std::deque<Target>& getTargets(const Settings& settings, +const std::deque<Target>& getTargets(const ctor::settings& settings, bool resolve_externals = true); //! Returns next dirty task from the dirtyTasks list that has all its dependencies //! fulfilled. //! The returned task is removed from the dirty list. //! Return nullptr if no dirty task is ready. -std::shared_ptr<Task> getNextTask(const std::set<std::shared_ptr<Task>>& allTasks, - std::set<std::shared_ptr<Task>>& dirtyTasks); +std::shared_ptr<Task> getNextTask(const ctor::settings& settings, + const std::vector<std::shared_ptr<Task>>& allTasks, + std::vector<std::shared_ptr<Task>>& dirtyTasks); //! Get list of tasks filtered by name including each of their direct //! dependency tasks (ie. objects tasks from their sources). -std::set<std::shared_ptr<Task>> getTasks(const Settings& settings, - const std::vector<std::string> names = {}, - bool resolve_externals = true); +std::vector<std::shared_ptr<Task>> getTasks(const ctor::settings& settings, + const std::vector<std::string> names = {}, + bool resolve_externals = true); //! Generate list of targets from a single configuration, including the final //! link target and all its objects files (if any). -std::set<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config, - const Settings& settings, - const std::string& sourceDir); +std::vector<std::shared_ptr<Task>> taskFactory(const ctor::build_configuration& config, + const ctor::settings& settings, + const std::string& sourceDir, + bool is_self); diff --git a/src/tools.cc b/src/tools.cc new file mode 100644 index 0000000..dfabdff --- /dev/null +++ b/src/tools.cc @@ -0,0 +1,1120 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "tools.h" + +#include <filesystem> +#include <iostream> +#include <sstream> +#include <array> + +#include <cassert> +#include <cstdio> + +#include "util.h" + +std::ostream& operator<<(std::ostream& stream, const ctor::c_opt& opt) +{ + // Adding to this enum should also imply adding to the unit-tests below + switch(opt) + { + case ctor::c_opt::output: stream << "ctor::c_opt::output"; break; + case ctor::c_opt::debug: stream << "ctor::c_opt::debug"; break; + case ctor::c_opt::warn_all: stream << "ctor::c_opt::warn_all"; break; + case ctor::c_opt::warn_conversion: stream << "ctor::c_opt::warn_conversion"; break; + case ctor::c_opt::warn_shadow: stream << "ctor::c_opt::warn_shadow"; break; + case ctor::c_opt::warn_extra: stream << "ctor::c_opt::warn_extra"; break; + case ctor::c_opt::warnings_as_errors: stream << "ctor::c_opt::warnings_as_errors"; break; + case ctor::c_opt::generate_dep_tree: stream << "ctor::c_opt::generate_dep_tree"; break; + case ctor::c_opt::no_link: stream << "ctor::c_opt::no_link"; break; + case ctor::c_opt::include_path: stream << "ctor::c_opt::include_path"; break; + case ctor::c_opt::c_std: stream << "ctor::c_opt::c_std"; break; + case ctor::c_opt::optimization: stream << "ctor::c_opt::optimization"; break; + case ctor::c_opt::position_independent_code: stream << "ctor::c_opt::position_independent_code"; break; + case ctor::c_opt::position_independent_executable: stream << "ctor::c_opt::position_independent_executable"; break; + case ctor::c_opt::define: stream << "ctor::c_opt::define"; break; + case ctor::c_opt::custom: stream << "ctor::c_opt::custom"; break; + } + + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::cxx_opt& opt) +{ + // Adding to this enum should also imply adding to the unit-tests below + switch(opt) + { + case ctor::cxx_opt::output: stream << "ctor::cxx_opt::output"; break; + case ctor::cxx_opt::debug: stream << "ctor::cxx_opt::debug"; break; + case ctor::cxx_opt::warn_all: stream << "ctor::cxx_opt::warn_all"; break; + case ctor::cxx_opt::warn_conversion: stream << "ctor::cxx_opt::warn_conversion"; break; + case ctor::cxx_opt::warn_shadow: stream << "ctor::cxx_opt::warn_shadow"; break; + case ctor::cxx_opt::warn_extra: stream << "ctor::cxx_opt::warn_extra"; break; + case ctor::cxx_opt::warnings_as_errors: stream << "ctor::cxx_opt::warnings_as_errors"; break; + case ctor::cxx_opt::generate_dep_tree: stream << "ctor::cxx_opt::generate_dep_tree"; break; + case ctor::cxx_opt::no_link: stream << "ctor::cxx_opt::no_link"; break; + case ctor::cxx_opt::include_path: stream << "ctor::cxx_opt::include_path"; break; + case ctor::cxx_opt::cpp_std: stream << "ctor::cxx_opt::cpp_std"; break; + case ctor::cxx_opt::optimization: stream << "ctor::cxx_opt::optimization"; break; + case ctor::cxx_opt::position_independent_code: stream << "ctor::cxx_opt::position_independent_code"; break; + case ctor::cxx_opt::position_independent_executable: stream << "ctor::cxx_opt::position_independent_executable"; break; + case ctor::cxx_opt::define: stream << "ctor::cxx_opt::define"; break; + case ctor::cxx_opt::custom: stream << "ctor::cxx_opt::custom"; break; + } + + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::ld_opt& opt) +{ + // Adding to this enum should also imply adding to the unit-tests below + switch(opt) + { + case ctor::ld_opt::output: stream << "ctor::ld_opt::output"; break; + case ctor::ld_opt::warn_all: stream << "ctor::ld_opt::warn_all"; break; + case ctor::ld_opt::warnings_as_errors: stream << "ctor::ld_opt::warnings_as_errors"; break; + case ctor::ld_opt::library_path: stream << "ctor::ld_opt::library_path"; break; + case ctor::ld_opt::link: stream << "ctor::ld_opt::link"; break; + case ctor::ld_opt::cpp_std: stream << "ctor::ld_opt::cpp_std"; break; + case ctor::ld_opt::build_shared: stream << "ctor::ld_opt::build_shared"; break; + case ctor::ld_opt::threads: stream << "ctor::ld_opt::threads"; break; + case ctor::ld_opt::position_independent_code: stream << "ctor::ld_opt::position_independent_code"; break; + case ctor::ld_opt::position_independent_executable: stream << "ctor::ld_opt::position_independent_executable"; break; + case ctor::ld_opt::custom: stream << "ctor::ld_opt::custom"; break; + } + + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::ar_opt& opt) +{ + // Adding to this enum should also imply adding to the unit-tests below + switch(opt) + { + case ctor::ar_opt::replace: stream << "ctor::ar_opt::replace"; break; + case ctor::ar_opt::add_index: stream << "ctor::ar_opt::add_index"; break; + case ctor::ar_opt::create: stream << "ctor::ar_opt::create"; break; + case ctor::ar_opt::output: stream << "ctor::ar_opt::output"; break; + case ctor::ar_opt::custom: stream << "ctor::ar_opt::custom"; break; + } + + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::asm_opt& opt) +{ + // Adding to this enum should also imply adding to the unit-tests below + switch(opt) + { + case ctor::asm_opt::custom: stream << "ctor::asm_opt::custom"; break; + } + + return stream; +} + +ctor::toolchain getToolChain(const std::string& compiler) +{ + std::filesystem::path cc(compiler); + auto cc_cmd = cc.stem().string(); + + // Note: "g++" is a substring of "clang++" so "clang++" must be tested first. + if(cc_cmd.find("clang++") != std::string::npos || + cc_cmd.find("clang") != std::string::npos) + { + return ctor::toolchain::clang; + } + else if(cc_cmd.find("g++") != std::string::npos || + cc_cmd.find("gcc") != std::string::npos || + cc_cmd.find("mingw") != std::string::npos) + { + return ctor::toolchain::gcc; + } + + std::cerr << "Unsupported output system.\n"; + return ctor::toolchain::gcc; +} + +ctor::toolchain getToolChain(ctor::output_system system) +{ + const auto& cfg = ctor::get_configuration(); + if(system == ctor::output_system::host) + { + if(cfg.host_toolchain != ctor::toolchain::none) + { + return cfg.host_toolchain; + } + return getToolChain(cfg.get(ctor::cfg::host_cxx, "g++")); + } + else + { + if(cfg.build_toolchain != ctor::toolchain::none) + { + return cfg.build_toolchain; + } + return getToolChain(cfg.get(ctor::cfg::build_cxx, "g++")); + } +} + +namespace gcc { +std::string get_arch(ctor::output_system system) +{ + std::string cmd; + + const auto& c = ctor::get_configuration(); + switch(system) + { + case ctor::output_system::host: + cmd = c.get(ctor::cfg::host_cxx, "/usr/bin/g++"); + break; + case ctor::output_system::build: + cmd = c.get(ctor::cfg::build_cxx, "/usr/bin/g++"); + break; + } + + cmd += " -v"; + cmd += " 2>&1"; + auto pipe = popen(cmd.data(), "r"); + if(!pipe) + { + std::cerr << "Could not run compiler " << cmd << "\n"; + return {};//ctor::arch::unknown; + } + + std::string arch; + while(!feof(pipe)) + { + constexpr auto buffer_size{1024}; + std::array<char, buffer_size> buf{}; + if(fgets(buf.data(), buf.size(), pipe) != nullptr) + { + arch = buf.data(); + if(arch.starts_with("Target:")) + { + break; + } + } + } + + pclose(pipe); + + // Remove trailing newline + if(arch.ends_with("\n")) + { + arch = arch.substr(0, arch.length() - 1); + } + + // Remove 'Target: ' prefix + constexpr auto prefix_length{8}; + arch = arch.substr(prefix_length); + return arch; +} + +ctor::arch get_arch(const std::string& str) +{ + // gcc -v 2>&1 | grep Target + // Target: x86_64-apple-darwin19.6.0 + // Target: x86_64-pc-linux-gnu + // Target: arm-linux-gnueabihf + // Target: x86_64-linux-gnu + // Target: x86_64-unknown-freebsd13.1 + // # x86_64-w64-mingw32-c++-win32 -v 2>&1 | grep Target + // Target: x86_64-w64-mingw32 + // # i686-w64-mingw32-c++-win32 -v 2>&1 | grep Target + // Target: i686-w64-mingw32 + + if(str.find("apple") != std::string::npos || + str.find("darwin") != std::string::npos) + { + return ctor::arch::apple; + } + + if(str.find("linux") != std::string::npos || + str.find("bsd") != std::string::npos) + { + return ctor::arch::unix; + } + + if(str.find("mingw") != std::string::npos) + { + return ctor::arch::windows; + } + + std::cerr << "Could not deduce gcc arch from '" << str << "'" << std::endl; + + return ctor::arch::unknown; +} + +ctor::c_flag c_option(const std::string& flag) +{ + if(flag.starts_with("-I")) + { + std::string path = flag.substr(2); + path.erase(0, path.find_first_not_of(' ')); + return { ctor::c_opt::include_path, path }; + } + + if(flag.starts_with("-std=")) + { + std::string std = flag.substr(5); + return { ctor::c_opt::c_std, std }; + } + + if(flag.starts_with("-O")) + { + std::string opt = flag.substr(2, 1); + return { ctor::c_opt::optimization, opt }; + } + + if(flag.starts_with("-Wall")) + { + return { ctor::c_opt::warn_all }; + } + + if(flag.starts_with("-Wconversion")) + { + return { ctor::c_opt::warn_conversion}; + } + + if(flag.starts_with("-Wshadow")) + { + return { ctor::c_opt::warn_shadow}; + } + + if(flag.starts_with("-Wextra")) + { + return { ctor::c_opt::warn_extra}; + } + + if(flag.starts_with("-Werror")) + { + return { ctor::c_opt::warnings_as_errors }; + } + + if(flag.starts_with("-g")) + { + return { ctor::c_opt::debug }; + } + + if(flag.starts_with("-D")) + { + std::string def = flag.substr(2); + auto pos = def.find('='); + if(pos != def.npos) + { + return { ctor::c_opt::define, def.substr(0, pos), def.substr(pos + 1) }; + } + else + { + return { ctor::c_opt::define, def }; + } + } + return { ctor::c_opt::custom, flag }; +} + +ctor::cxx_flag cxx_option(const std::string& flag) +{ + if(flag.starts_with("-I")) + { + std::string path = flag.substr(2); + path.erase(0, path.find_first_not_of(' ')); + return { ctor::cxx_opt::include_path, path }; + } + + if(flag.starts_with("-std=")) + { + std::string std = flag.substr(5); + return { ctor::cxx_opt::cpp_std, std }; + } + + if(flag.starts_with("-O")) + { + std::string opt = flag.substr(2, 1); + return { ctor::cxx_opt::optimization, opt }; + } + + if(flag.starts_with("-Wall")) + { + return { ctor::cxx_opt::warn_all }; + } + + if(flag.starts_with("-Werror")) + { + return { ctor::cxx_opt::warnings_as_errors }; + } + + if(flag.starts_with("-Wconversion")) + { + return { ctor::cxx_opt::warn_conversion}; + } + + if(flag.starts_with("-Wshadow")) + { + return { ctor::cxx_opt::warn_shadow}; + } + + if(flag.starts_with("-Wextra")) + { + return { ctor::cxx_opt::warn_extra}; + } + + if(flag.starts_with("-g")) + { + return { ctor::cxx_opt::debug }; + } + + if(flag.starts_with("-D")) + { + std::string def = flag.substr(2); + auto pos = def.find('='); + if(pos != def.npos) + { + return { ctor::cxx_opt::define, def.substr(0, pos), def.substr(pos + 1) }; + } + else + { + return { ctor::cxx_opt::define, def }; + } + } + + return { ctor::cxx_opt::custom, flag }; +} + +ctor::ld_flag ld_option(const std::string& flag) +{ + if(flag.starts_with("-L")) + { + std::string path = flag.substr(2); + path.erase(0, path.find_first_not_of(' ')); + return { ctor::ld_opt::library_path, path }; + } + + if(flag.starts_with("-pthread")) + { + return { ctor::ld_opt::threads }; + } + + return { ctor::ld_opt::custom, flag }; +} + +ctor::ar_flag ar_option(const std::string& flag) +{ + return { ctor::ar_opt::custom, flag }; +} + +std::vector<std::string> cxx_option(ctor::cxx_opt opt, const std::string& arg, + const std::string& arg2) +{ + switch(opt) + { + case ctor::cxx_opt::output: + return {"-o", arg}; + case ctor::cxx_opt::debug: + return {"-g"}; + case ctor::cxx_opt::warn_all: + return {"-Wall"}; + case ctor::cxx_opt::warn_conversion: + return {"-Wconversion"}; + case ctor::cxx_opt::warn_shadow: + return {"-Wshadow"}; + case ctor::cxx_opt::warn_extra: + return {"-Wextra"}; + case ctor::cxx_opt::warnings_as_errors: + return {"-Werror"}; + case ctor::cxx_opt::generate_dep_tree: + return {"-MMD"}; + case ctor::cxx_opt::no_link: + return {"-c"}; + case ctor::cxx_opt::include_path: + return {"-I" + arg}; + case ctor::cxx_opt::cpp_std: + return {"-std=" + arg}; + case ctor::cxx_opt::optimization: + return {"-O" + arg}; + case ctor::cxx_opt::position_independent_code: + return {"-fPIC"}; + case ctor::cxx_opt::position_independent_executable: + return {"-fPIE"}; + case ctor::cxx_opt::define: + if(!arg2.empty()) + { + return {"-D" + arg + "=" + arg2}; + } + return {"-D" + arg}; + case ctor::cxx_opt::custom: + return argsplit(arg); + } + + std::cerr << "Unsupported compiler option.\n"; + return {}; +} + +std::vector<std::string> c_option(ctor::c_opt opt, const std::string& arg, + const std::string& arg2) +{ + switch(opt) + { + case ctor::c_opt::output: + return {"-o", arg}; + case ctor::c_opt::debug: + return {"-g"}; + case ctor::c_opt::warn_all: + return {"-Wall"}; + case ctor::c_opt::warn_conversion: + return {"-Wconversion"}; + case ctor::c_opt::warn_shadow: + return {"-Wshadow"}; + case ctor::c_opt::warn_extra: + return {"-Wextra"}; + case ctor::c_opt::warnings_as_errors: + return {"-Werror"}; + case ctor::c_opt::generate_dep_tree: + return {"-MMD"}; + case ctor::c_opt::no_link: + return {"-c"}; + case ctor::c_opt::include_path: + return {"-I" + arg}; + case ctor::c_opt::c_std: + return {"-std=" + arg}; + case ctor::c_opt::optimization: + return {"-O" + arg}; + case ctor::c_opt::position_independent_code: + return {"-fPIC"}; + case ctor::c_opt::position_independent_executable: + return {"-fPIE"}; + case ctor::c_opt::define: + if(!arg2.empty()) + { + return {"-D" + arg + "=" + arg2}; + } + return {"-D" + arg}; + case ctor::c_opt::custom: + return argsplit(arg); + } + + std::cerr << "Unsupported compiler option.\n"; + return {}; +} + +std::vector<std::string> ld_option(ctor::ld_opt opt, const std::string& arg, + [[maybe_unused]]const std::string& arg2) +{ + switch(opt) + { + case ctor::ld_opt::output: + return {"-o", arg}; + case ctor::ld_opt::warn_all: + return {"-Wall"}; + case ctor::ld_opt::warnings_as_errors: + return {"-Werror"}; + case ctor::ld_opt::library_path: + return {"-L" + arg}; + case ctor::ld_opt::link: + return {"-l" + arg}; + case ctor::ld_opt::cpp_std: + return {"-std=" + arg}; + case ctor::ld_opt::build_shared: + return {"-shared"}; + case ctor::ld_opt::threads: + return {"-pthread"}; + case ctor::ld_opt::position_independent_code: + return {"-fPIC"}; + case ctor::ld_opt::position_independent_executable: + return {"-fPIE"}; + case ctor::ld_opt::custom: + return argsplit(arg); + } + + std::cerr << "Unsupported compiler option.\n"; + return {}; +} + +std::vector<std::string> ar_option(ctor::ar_opt opt, const std::string& arg, + [[maybe_unused]]const std::string& arg2) +{ + switch(opt) + { + case ctor::ar_opt::replace: + return {"-r"}; + case ctor::ar_opt::add_index: + return {"-s"}; + case ctor::ar_opt::create: + return {"-c"}; + case ctor::ar_opt::output: + return {arg}; + case ctor::ar_opt::custom: + return argsplit(arg); + } + + std::cerr << "Unsupported compiler option.\n"; + return {}; +} + +std::vector<std::string> asm_option(ctor::asm_opt opt, const std::string& arg, + [[maybe_unused]]const std::string& arg2) +{ + switch(opt) + { + case ctor::asm_opt::custom: + return argsplit(arg); + } + + std::cerr << "Unsupported compiler option.\n"; + return {}; +} +} // gcc:: + +std::string get_arch(ctor::output_system system) +{ + auto toolchain = getToolChain(system); + switch(toolchain) + { + case ctor::toolchain::clang: + case ctor::toolchain::gcc: + return gcc::get_arch(system); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + return {}; +} + +ctor::arch get_arch(ctor::output_system system, const std::string& str) +{ + auto toolchain = getToolChain(system); + switch(toolchain) + { + case ctor::toolchain::clang: + case ctor::toolchain::gcc: + return gcc::get_arch(str); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + return ctor::arch::unknown; +} + +std::vector<std::string> c_option(ctor::toolchain toolchain, + ctor::c_opt opt, + const std::string& arg, + const std::string& arg2) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::c_option(opt, arg, arg2); + case ctor::toolchain::any: + { + std::ostringstream ss; + ss << "{" << opt; + if(!arg.empty()) + { + ss << ", \"" << arg << "\""; + } + if(!arg2.empty()) + { + ss << ", \"" << arg2 << "\""; + } + ss << "}"; + return { ss.str() }; + } + case ctor::toolchain::none: + break; + } + + std::cerr << "Unsupported tool-chain.\n"; + return {}; +} + +std::vector<std::string> cxx_option(ctor::toolchain toolchain, + ctor::cxx_opt opt, + const std::string& arg, + const std::string& arg2) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::cxx_option(opt, arg, arg2); + case ctor::toolchain::any: + { + std::ostringstream ss; + ss << "{" << opt; + if(!arg.empty()) + { + ss << ", \"" << arg << "\""; + } + if(!arg2.empty()) + { + ss << ", \"" << arg2 << "\""; + } + ss << "}"; + return { ss.str() }; + } + case ctor::toolchain::none: + break; + } + + std::cerr << "Unsupported tool-chain.\n"; + return {}; +} + +std::vector<std::string> ld_option(ctor::toolchain toolchain, + ctor::ld_opt opt, + const std::string& arg, + const std::string& arg2) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::ld_option(opt, arg, arg2); + case ctor::toolchain::any: + { + std::ostringstream ss; + ss << "{" << opt; + if(!arg.empty()) + { + ss << ", \"" << arg << "\""; + } + if(!arg2.empty()) + { + ss << ", \"" << arg2 << "\""; + } + ss << "}"; + return { ss.str() }; + } + case ctor::toolchain::none: + break; + } + + std::cerr << "Unsupported tool-chain.\n"; + return {}; +} + +std::vector<std::string> ar_option(ctor::toolchain toolchain, + ctor::ar_opt opt, + const std::string& arg, + const std::string& arg2) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::ar_option(opt, arg, arg2); + case ctor::toolchain::any: + { + std::ostringstream ss; + ss << "{" << opt; + if(!arg.empty()) + { + ss << ", \"" << arg << "\""; + } + if(!arg2.empty()) + { + ss << ", \"" << arg2 << "\""; + } + ss << "}"; + return { ss.str() }; + } + case ctor::toolchain::none: + break; + } + + std::cerr << "Unsupported tool-chain.\n"; + return {}; +} + +std::vector<std::string> asm_option(ctor::toolchain toolchain, + ctor::asm_opt opt, + const std::string& arg, + const std::string& arg2) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::asm_option(opt, arg, arg2); + case ctor::toolchain::any: + { + std::ostringstream ss; + ss << "{" << opt; + if(!arg.empty()) + { + ss << ", \"" << arg << "\""; + } + if(!arg2.empty()) + { + ss << ", \"" << arg2 << "\""; + } + ss << "}"; + return { ss.str() }; + } + case ctor::toolchain::none: + break; + } + + std::cerr << "Unsupported tool-chain.\n"; + return {}; +} + + +ctor::c_flag c_option(const std::string& flag, ctor::toolchain toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::c_option(flag); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + + return { ctor::c_opt::custom, flag }; +} + +ctor::cxx_flag cxx_option(const std::string& flag, ctor::toolchain toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::cxx_option(flag); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + + return { ctor::cxx_opt::custom, flag }; +} + +ctor::ld_flag ld_option(const std::string& flag, ctor::toolchain toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::ld_option(flag); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + + return { ctor::ld_opt::custom, flag }; +} + +ctor::ar_flag ar_option(const std::string& flag, ctor::toolchain toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + return gcc::ar_option(flag); + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + + return { ctor::ar_opt::custom, flag }; +} + +ctor::asm_flag asm_option(const std::string& flag, ctor::toolchain toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::gcc: + case ctor::toolchain::clang: + case ctor::toolchain::any: + case ctor::toolchain::none: + break; + } + + return { ctor::asm_opt::custom, flag }; +} + +// Flag to string coversions + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::c_flag& flag) +{ + if(flag.toolchain == ctor::toolchain::any || + flag.toolchain == toolchain) + { + return c_option(toolchain, flag.opt, flag.arg, flag.arg2); + } + + return {}; +} + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::cxx_flag& flag) +{ + if(flag.toolchain == ctor::toolchain::any || + flag.toolchain == toolchain) + { + return cxx_option(toolchain, flag.opt, flag.arg, flag.arg2); + } + + return {}; +} + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::ld_flag& flag) +{ + if(flag.toolchain == ctor::toolchain::any || + flag.toolchain == toolchain) + { + return ld_option(toolchain, flag.opt, flag.arg, flag.arg2); + } + + return {}; +} + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::ar_flag& flag) +{ + if(flag.toolchain == ctor::toolchain::any || + flag.toolchain == toolchain) + { + return ar_option(toolchain, flag.opt, flag.arg, flag.arg2); + } + + return {}; +} + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::asm_flag& flag) +{ + if(flag.toolchain == ctor::toolchain::any || + flag.toolchain == toolchain) + { + return asm_option(toolchain, flag.opt, flag.arg, flag.arg2); + } + + return {}; +} + +namespace { +ctor::toolchain guess_toolchain(const std::string& opt) +{ + if(opt.empty()) + { + return ctor::toolchain::any; + } + + if(opt[0] == '-') + { + return ctor::toolchain::gcc; + } + + //if(opt[0] == '/') + //{ + // return ctor::toolchain::msvc; + //} + return ctor::toolchain::any; +} +} + +template<> +ctor::flag<ctor::c_opt>::flag(const char* str) +{ + *this = c_option(str, guess_toolchain(str)); +} + +template<> +ctor::flag<ctor::cxx_opt>::flag(const char* str) +{ + *this = cxx_option(str, guess_toolchain(str)); +} + +template<> +ctor::flag<ctor::ld_opt>::flag(const char* str) +{ + *this = ld_option(str, guess_toolchain(str)); +} + +template<> +ctor::flag<ctor::ar_opt>::flag(const char* str) +{ + *this = ar_option(str, guess_toolchain(str)); +} + +template<> +ctor::flag<ctor::asm_opt>::flag(const char* str) +{ + *this = asm_option(str, guess_toolchain(str)); +} + + +ctor::target_type target_type_from_extension(ctor::toolchain toolchain, + const std::filesystem::path& file) +{ + auto ext = to_lower(file.extension().string()); + // Loosely based on: + // https://en.wikipedia.org/wiki/List_of_file_formats#Object_code,_executable_files,_shared_and_dynamically_linked_libraries + if(toolchain == ctor::toolchain::any || + toolchain == ctor::toolchain::gcc || + toolchain == ctor::toolchain::clang) + { + if(ext == ".a") + { + return ctor::target_type::static_library; + } + + if(ext == ".so" || + ext == ".dylib") + { + return ctor::target_type::dynamic_library; + } + + if(ext == ".o") + { + return ctor::target_type::object; + } + + if(ext == "" || + ext == ".bin" || + ext == ".run" || + ext == ".out") + { + return ctor::target_type::executable; + } + } + + if(toolchain == ctor::toolchain::any// || + //toolchain == ctor::toolchain::msvc || + //toolchain == ctor::toolchain::mingw || + ) + { + if(ext == ".lib") + { + return ctor::target_type::static_library; + } + + if(ext == ".dll") + { + return ctor::target_type::dynamic_library; + } + + if(ext == ".obj") + { + return ctor::target_type::object; + } + + if(ext == ".exe" || + ext == ".com") + { + return ctor::target_type::executable; + } + } + + return ctor::target_type::unknown; +} + + +std::filesystem::path extension(ctor::toolchain toolchain, + ctor::target_type target_type, + ctor::output_system system, + const std::filesystem::path& file) +{ + auto type = target_type_from_extension(toolchain, file); + if(type == target_type) + { + // File already has the correct extension + return file; + } + + const auto& c = ctor::get_configuration(); + ctor::arch arch{}; + switch(system) + { + case ctor::output_system::host: + arch = c.host_arch; + break; + case ctor::output_system::build: + arch = c.build_arch; + break; + } + + // This might be before configure - so detection is needed for boostrap + if(arch == ctor::arch::unknown) + { + arch = get_arch(system, get_arch(system)); + } + + std::string ext{file.extension().string()}; + switch(target_type) + { + case ctor::target_type::automatic: + break; + case ctor::target_type::executable: + case ctor::target_type::unit_test: + switch(arch) + { + case ctor::arch::unix: + case ctor::arch::apple: + ext = ""; + break; + case ctor::arch::windows: + ext = ".exe"; + break; + case ctor::arch::unknown: + break; + } + break; + case ctor::target_type::static_library: + case ctor::target_type::unit_test_library: + switch(arch) + { + case ctor::arch::unix: + case ctor::arch::apple: + ext = ".a"; + break; + case ctor::arch::windows: + ext = ".lib"; + break; + case ctor::arch::unknown: + break; + } + break; + case ctor::target_type::dynamic_library: + switch(arch) + { + case ctor::arch::unix: + ext = ".so"; + break; + case ctor::arch::apple: + ext = ".dylib"; + break; + case ctor::arch::windows: + ext = ".dll"; + break; + case ctor::arch::unknown: + break; + } + break; + case ctor::target_type::object: + switch(arch) + { + case ctor::arch::unix: + case ctor::arch::apple: + ext = ".o"; + break; + case ctor::arch::windows: + ext = ".obj"; + break; + case ctor::arch::unknown: + break; + } + break; + case ctor::target_type::function: + break; + case ctor::target_type::unknown: + break; + } + + auto output{file}; + output.replace_extension(ext); + return output; +} diff --git a/src/tools.h b/src/tools.h new file mode 100644 index 0000000..0e7fc15 --- /dev/null +++ b/src/tools.h @@ -0,0 +1,129 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <vector> +#include <string> +#include <ostream> +#include <filesystem> + +#include "ctor.h" + + +std::ostream& operator<<(std::ostream& stream, const ctor::c_opt& opt); +std::ostream& operator<<(std::ostream& stream, const ctor::cxx_opt& opt); +std::ostream& operator<<(std::ostream& stream, const ctor::ld_opt& opt); +std::ostream& operator<<(std::ostream& stream, const ctor::ar_opt& opt); +std::ostream& operator<<(std::ostream& stream, const ctor::asm_opt& opt); + +std::string get_arch(ctor::output_system system); +ctor::arch get_arch(ctor::output_system system, const std::string& str); + +//! Get tool-chain type from compiler path string +ctor::toolchain getToolChain(const std::string& compiler); + +//! Get tool-chain type from output system (via configuration) +ctor::toolchain getToolChain(ctor::output_system system); + + + +//! Get tool argument(s) for specific option type matching the supplied +//! tool-chain +std::vector<std::string> c_option(ctor::toolchain toolchain, + ctor::c_opt option, + const std::string& arg = {}, + const std::string& arg2 = {}); + +//! Get tool argument(s) for specific option type matching the supplied +//! tool-chain +std::vector<std::string> cxx_option(ctor::toolchain toolchain, + ctor::cxx_opt option, + const std::string& arg = {}, + const std::string& arg2 = {}); + +//! Get tool argument(s) for specific option type matching the supplied +//! tool-chain +std::vector<std::string> ld_option(ctor::toolchain toolchain, + ctor::ld_opt option, + const std::string& arg = {}, + const std::string& arg2 = {}); + +//! Get tool argument(s) for specific option type matching the supplied +//! tool-chain +std::vector<std::string> ar_option(ctor::toolchain toolchain, + ctor::ar_opt option, + const std::string& arg = {}, + const std::string& arg2 = {}); + +//! Get tool argument(s) for specific option type matching the supplied +//! tool-chain +std::vector<std::string> asm_option(ctor::toolchain toolchain, + ctor::asm_opt option, + const std::string& arg = {}, + const std::string& arg2 = {}); + + + +//! Get ctor::c_opt enum value and argument from string, +//! ie. { ctor::c_opt::inlude_path, "foo/bar" } from "-Ifoo/bar" +//! Returns { ctor::c_opt::custom, flag } if unknown. +ctor::c_flag c_option(const std::string& flag, ctor::toolchain toolchain); + +//! Get ctor::cxx_opt enum value and argument from string, +//! ie. { ctor::cxx_opt::inlude_path, "foo/bar" } from "-Ifoo/bar" +//! Returns { ctor::cxx_opt::custom, flag } if unknown. +ctor::cxx_flag cxx_option(const std::string& flag, ctor::toolchain toolchain); + +//! Get ctor::ld_opt enum value and argument from string, +//! ie. { ctor::ld_opt::inlude_path, "foo/bar" } from "-Ifoo/bar" +//! Returns { ctor::ld_opt::custom, flag } if unknown. +ctor::ld_flag ld_option(const std::string& flag, ctor::toolchain toolchain); + +//! Get ctor::ar_opt enum value and argument from string, +//! ie. { ctor::ar_opt::inlude_path, "foo/bar" } from "-Ifoo/bar" +//! Returns { ctor::ar_opt::custom, flag } if unknown. +ctor::ar_flag ar_option(const std::string& flag, ctor::toolchain toolchain); + +//! Get ctor::asm_opt enum value and argument from string, +//! ie. { ctor::asm_opt::inlude_path, "foo/bar" } from "-Ifoo/bar" +//! Returns { ctor::asm_opt::custom, flag } if unknown. +ctor::asm_flag asm_option(const std::string& flag, ctor::toolchain toolchain); + + + +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::cxx_flag& flag); +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::c_flag& flag); +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::ld_flag& flag); +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::ar_flag& flag); +std::vector<std::string> to_strings(ctor::toolchain toolchain, + const ctor::asm_flag& flag); + + +// Get target type from file extension +// If toolchain is not ::any only extensions for that toolchain will be accepted +// If no match is found ::unknown will be returned. +ctor::target_type target_type_from_extension(ctor::toolchain toolchain, + const std::filesystem::path& file); + + +// Get appropriate extension from original extension and target type using +// the toolchain. +// ie. { gcc, static_lib, ".lib" } will return ".a" +// { msvc, dynamic_lib, ".dylib" } will return ".dll" +// ... +// If the supplied extension is normal for the supplied type and toolchain, then this is used, otherwise a conversion to the default extension to the given toolchain and type is given. +// The defaults for the toolchains are as follows: +// toolchain executable static-lib dynamic-lib +// gcc (none) .a .so(.dylib on macos) +// clang (none) .a .so(.dylib on macos) +// msvc .exe .lib .dll +// mingw .exe .lib .dll +std::filesystem::path extension(ctor::toolchain toolchain, + ctor::target_type target_type, + ctor::output_system system, + const std::filesystem::path& file); diff --git a/src/unittest.cc b/src/unittest.cc index f18de47..b95a931 100644 --- a/src/unittest.cc +++ b/src/unittest.cc @@ -8,15 +8,15 @@ #include "execute.h" #include "task.h" -int runUnitTests(std::set<std::shared_ptr<Task>>& tasks, - const Settings& settings) +int runUnitTests(std::vector<std::shared_ptr<Task>>& tasks, + const ctor::settings& settings) { bool ok{true}; - std::cout << "Running unit-tests:\n"; + std::cout << "Running unit-tests:" << std::endl; // Run unit-tests for(const auto& task : tasks) { - if(task->targetType() == TargetType::UnitTest) + if(task->targetType() == ctor::target_type::unit_test) { auto name = task->name(); if(name.empty()) @@ -24,15 +24,15 @@ int runUnitTests(std::set<std::shared_ptr<Task>>& tasks, name = task->target(); } std::cout << name << ": " << std::flush; - auto ret = execute(task->targetFile(), {}, settings.verbose > 0); + auto ret = execute(settings, task->targetFile().string(), {}, {}); ok &= ret == 0; if(ret == 0) { - std::cout << " OK\n"; + std::cout << " OK" << std::endl; } else { - std::cout << " FAILED\n"; + std::cout << " FAILED" << std::endl; } } } diff --git a/src/unittest.h b/src/unittest.h index 8dee33c..6d1385e 100644 --- a/src/unittest.h +++ b/src/unittest.h @@ -3,11 +3,14 @@ // See accompanying file LICENSE for details. #pragma once -#include <set> +#include <vector> #include <memory> class Task; -class Settings; -int runUnitTests(std::set<std::shared_ptr<Task>>& tasks, - const Settings& settings); +namespace ctor { +struct settings; +} // namespace ctor:: + +int runUnitTests(std::vector<std::shared_ptr<Task>>& tasks, + const ctor::settings& settings); diff --git a/src/util.cc b/src/util.cc index 92560b6..a4abd23 100644 --- a/src/util.cc +++ b/src/util.cc @@ -5,105 +5,70 @@ #include <iostream> #include <fstream> +#include <algorithm> +#include <sstream> +#include <cctype> +#include <cstdlib> + +std::string to_lower(std::string str) +{ + std::transform(str.begin(), str.end(), str.begin(), + [](unsigned char c) + { + return std::tolower(c); + }); + return str; +} std::string readFile(const std::string& fileName) { std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); - std::ifstream::pos_type fileSize = ifs.tellg(); + auto size = ifs.tellg(); + if(size < 0) + { + return {}; + } ifs.seekg(0, std::ios::beg); - std::vector<char> bytes(fileSize); - ifs.read(bytes.data(), fileSize); + std::string bytes(static_cast<std::size_t>(size), '\0'); + ifs.read(bytes.data(), static_cast<std::streamsize>(bytes.size())); - return std::string(bytes.data(), fileSize); + return bytes; } -std::vector<std::string> readDeps(const std::string& depFile) +ctor::language languageFromExtension(const std::filesystem::path& file) { - if(!std::filesystem::exists(depFile)) - { - return {}; - } - - auto str = readFile(depFile); + auto ext = file.extension().string(); - std::vector<std::string> output; - std::string tmp; - bool start{false}; - bool in_whitespace{false}; - for(const auto& c : str) + // First a few case sensitive comparisons + if(ext == ".c") { - if(c == '\\' || c == '\n') - { - continue; - } - - if(c == ':') - { - start = true; - continue; - } - - if(!start) - { - continue; - } - - if(c == ' ' || c == '\t') - { - if(in_whitespace) - { - continue; - } - - if(!tmp.empty()) - { - output.push_back(tmp); - } - tmp.clear(); - in_whitespace = true; - } - else - { - in_whitespace = false; - tmp += c; - } + return ctor::language::c; } - if(!tmp.empty()) + if(ext == ".C") { - output.push_back(tmp); + return ctor::language::cpp; } - return output; -} - -Language languageFromExtension(const std::filesystem::path& file) -{ - auto ext = file.extension().string(); - if(ext == ".c") - { - return Language::C; - } + // The rest are compared in lowercase + ext = to_lower(ext); - if(ext == ".C" || - ext == ".cc" || + if(ext == ".cc" || ext == ".cpp" || - ext == ".CPP" || ext == ".c++" || ext == ".cp" || ext == ".cxx") { - return Language::Cpp; + return ctor::language::cpp; } if(ext == ".s" || - ext == ".S" || ext == ".asm") { - return Language::Asm; + return ctor::language::assembler; } std::cerr << "Could not deduce language from " << file.string() << "\n"; @@ -135,3 +100,210 @@ std::string cleanUp(const std::string& path) } return cleaned; } + +std::string esc(const std::string& in) +{ + std::string out; + for(auto c : in) + { + switch(c) + { + case '\\': out += "\\\\"; break; + case '"': out += "\\\""; break; + default: + out += c; + break; + } + } + return out; +} + +std::vector<std::string> get_paths(const std::string& path_env_) +{ + std::string path_env; + if(!path_env_.empty()) + { + path_env = path_env_; + } + else + { + get_env("PATH", path_env); + } + + std::vector<std::string> paths; + +#ifdef _WIN32 + const char sep{';'}; +#else + const char sep{':'}; +#endif + + std::stringstream ss(path_env); + std::string path; + while (std::getline(ss, path, sep)) + { + paths.push_back(path); + } + + return paths; +} + +bool check_executable(const std::filesystem::path& prog) +{ + auto perms = std::filesystem::status(prog).permissions(); + + if((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) + { + return true; + } + + if((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) + { + return true; + } + + if((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none) + { + return true; + } + + return false; +} + +std::string locate(const std::string& prog, + const std::vector<std::string>& paths, + const std::string& arch) +{ + std::string program = prog; + if(!arch.empty()) + { + program = arch + "-" + prog; + } + + // first check if arch contains an absolute path to prog + if(std::filesystem::exists(program)) + { + if(check_executable(program)) + { + return program; + } + } + + for(const auto& path_str : paths) + { + std::filesystem::path path(path_str); + auto prog_path = path / program; + if(std::filesystem::exists(prog_path)) + { + if(check_executable(prog_path)) + { + return prog_path.string(); + } + } + } + + return {}; +} + +std::vector<std::string> argsplit(const std::string& str) +{ + enum class state + { + normal, + in_quot, + in_apotrophe, + } state{state::normal}; + bool esc{false}; + + std::string token; + std::vector<std::string> tokens; + for(auto c : str) + { + switch(state) + { + case state::normal: + if(esc) + { + esc = false; + } + else + { + if(c == ' ') + { + tokens.push_back(token); + token.clear(); + continue; + } + if(c == '\\') + { + esc = true; + } + if(c == '"') + { + state = state::in_quot; + } + if(c == '\'') + { + state = state::in_apotrophe; + } + } + + token += c; + break; + case state::in_quot: + if(esc) + { + esc = false; + } + else + { + if(c == '\\') + { + esc = true; + } + if(c == '"') + { + state = state::normal; + } + } + + token += c; + break; + case state::in_apotrophe: + if(esc) + { + esc = false; + } + else + { + if(c == '\\') + { + esc = true; + } + if(c == '\'') + { + state = state::normal; + } + } + + token += c; + break; + } + } + if(!token.empty()) + { + tokens.push_back(token); + } + return tokens; +} + +bool get_env(std::string_view name, std::string& value) +{ + auto var = getenv(name.data()); + if(var) + { + value = var; + return true; + } + return false; +} @@ -3,12 +3,39 @@ // See accompanying file LICENSE for details. #pragma once -#include "libctor.h" +#include "ctor.h" #include <string> #include <filesystem> +#include <cstdlib> + +std::string to_lower(std::string str); std::string readFile(const std::string& fileName); -std::vector<std::string> readDeps(const std::string& depFile); -Language languageFromExtension(const std::filesystem::path& file); +ctor::language languageFromExtension(const std::filesystem::path& file); std::string cleanUp(const std::string& path); + +template<typename T> +void append(T& a, const T& b) +{ + a.insert(a.end(), b.begin(), b.end()); +} + +std::string esc(const std::string& in); + +//! Get system paths (ie. env var PATH). +//! If path_env is provided, this search string will be used, other the PATH +//! env variable is used. +//! \returns a vector of the individual toknized paths. +std::vector<std::string> get_paths(const std::string& path_env = {}); + +std::string locate(const std::string& app, + const std::vector<std::string>& paths, + const std::string& arch = {}); + +//! Splits string into tokens adhering to quotations " and ' +std::vector<std::string> argsplit(const std::string& str); + +//! Calls the system getenv and sets the string if the env name it exists. +//! \returns true if the env name existed, false otherwise. +bool get_env(std::string_view name, std::string& value); diff --git a/test/argparser_test.cc b/test/argparser_test.cc new file mode 100644 index 0000000..b649e8c --- /dev/null +++ b/test/argparser_test.cc @@ -0,0 +1,1019 @@ +#include <argparser.h> + +#include <iostream> +#include <string> + +std::ostream& operator<<(std::ostream& ostr, const arg::error& err) +{ + switch(err) + { + case arg::error::invalid_arg: + ostr << "arg::error::invalid_arg"; + break; + case arg::error::invalid_opt: + ostr << "arg::error::invalid_opt"; + break; + case arg::error::missing_arg: + ostr << "arg::error::missing_arg"; + break; + } + return ostr; +} + +#include <uunit.h> + +class ArgParserTest + : public uUnit +{ +public: + ArgParserTest() + { + uTEST(ArgParserTest::test_zero); + uTEST(ArgParserTest::test_one); + uTEST(ArgParserTest::test_many); + uTEST(ArgParserTest::test_exceptional); + uTEST(ArgParserTest::test_err_callback); + uTEST(ArgParserTest::test_pos_callback); + uTEST(ArgParserTest::test_grouped); + uTEST(ArgParserTest::test_nullprogram); + } + + void test_zero() + { + const char* const argv[] = { "app-name" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int> args(argc, argv); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + } + + void test_one() + { + const char* argv[] = { "app-name", "-x", "42" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int> args(argc, argv); + + int x{}; + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(42, x); + } + + void test_many() + { + const char* argv[] = { "app-name", "-x", "42", "-y17", "-z", + "--long-X=12", "--long-Y", "18" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int> args(argc, argv); + + int x{}; + int y{}; + bool z{false}; + int X{}; + int Y{}; + + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](int i){ y = i; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](){ z = true; return 0;}), "Help z"); + + args.add('X', "--long-X", + std::function([&](int i){ X = i; return 0;}), "Help X"); + + args.add('Y', "--long-Y", + std::function([&](int i){ Y = i; return 0;}), "Help Y"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(42, x); + uASSERT_EQUAL(17, y); + uASSERT_EQUAL(true, z); + uASSERT_EQUAL(12, X); + uASSERT_EQUAL(18, Y); + } + + void test_exceptional() + { + + { // Missing arg at trailing opt + const char* argv[] = { "app-name", "-x" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + int y{}; + + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](int i){ y = i; return 0;}), "Help y"); + + auto res = args.parse(); + uASSERT_EQUAL(1, res); + } + + { // Missing arg before other opt + const char* argv[] = { "app-name", "-x", "-y" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + int y{}; + + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](int i){ y = i; return 0;}), "Help y"); + + auto res = args.parse(); + uASSERT_EQUAL(1, res); + } + + { // Unknown arg + const char* argv[] = { "app-name", "-T" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + int y{}; + + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](int i){ y = i; return 0;}), "Help y"); + + auto res = args.parse(); + uASSERT_EQUAL(1, res); + } + } + + void test_err_callback() + { + using namespace std::string_literals; + + { // Missing arg at trailing opt + const char* argv[] = { "app-name", "-x" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + bool called{false}; + args.set_err_cb( + [&](arg::error err, std::string_view opt) + { + called = true; + uASSERT_EQUAL(arg::error::missing_arg, err); + uASSERT_EQUAL("-x"s, opt); + }); + auto res = args.parse(); + uASSERT_EQUAL(1, res); + uASSERT(called); + } + + { // Invalid arg format + const char* argv[] = { "app-name", "-x", "abc" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + bool called{false}; + args.set_err_cb( + [&](arg::error err, std::string_view opt) + { + called = true; + uASSERT_EQUAL(arg::error::invalid_arg, err); + uASSERT_EQUAL("abc"s, opt); + }); + auto res = args.parse(); + uASSERT_EQUAL(1, res); + uASSERT(called); + } + + { // Invalid opt + const char* argv[] = { "app-name", "-y" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int> args(argc, argv); + + int x{}; + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + bool called{false}; + args.set_err_cb( + [&](arg::error err, std::string_view opt) + { + called = true; + uASSERT_EQUAL(arg::error::invalid_opt, err); + uASSERT_EQUAL("-y"s, opt); + }); + auto res = args.parse(); + uASSERT_EQUAL(1, res); + uASSERT(called); + } + } + + void test_pos_callback() + { + using namespace std::string_literals; + const char* argv[] = + { "app-name", "foo", "-x", "42", "bar", "-X43", "-Y", "42" }; + int argc = sizeof(argv)/sizeof(*argv); + arg::Parser<int, std::optional<int>> args(argc, argv); + + int x{}; + int X{}; + int Y{}; + args.add('x', "--long-x", + std::function([&](int i){ x = i; return 0;}), "Help x"); + + args.add('X', "--opt-x", + std::function([&](std::optional<int> i) + { + uASSERT(i.has_value()); + X = *i; + return 0; + }), + "Help X"); + + args.add('Y', "--opt-y", + std::function([&](std::optional<int> i) + { + uASSERT(!i.has_value()); + Y = 1; + return 0; + }), + "Help X"); + + std::vector<std::string> pos; + args.set_pos_cb( + [&](std::string_view sv) + { + pos.push_back(std::string(sv)); + return 0; + }); + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(3u, pos.size()); + uASSERT_EQUAL("foo"s, pos[0]); + uASSERT_EQUAL("bar"s, pos[1]); + uASSERT_EQUAL("42"s, pos[2]); + uASSERT_EQUAL(42, x); + uASSERT_EQUAL(43, X); + uASSERT_EQUAL(1, Y); + } + + void test_grouped() + { + { + const char* argv[] = { "app-name", "-xyz", "42" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int> args(argc, argv); + + bool x{false}; + bool y{false}; + int z{}; + args.add('x', "--long-x", + std::function([&](){ x = true; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](){ y = true; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](int i){ z = i; return 0;}), "Help z"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT(x); + uASSERT(y); + uASSERT_EQUAL(42, z); + } + + { + const char* argv[] = { "app-name", "-xyz42" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int> args(argc, argv); + + bool x{false}; + bool y{false}; + int z{}; + args.add('x', "--long-x", + std::function([&](){ x = true; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](){ y = true; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](int i){ z = i; return 0;}), "Help z"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT(x); + uASSERT(y); + uASSERT_EQUAL(42, z); + } + + + { + const char* argv[] = { "app-name", "-xyz42" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int, std::optional<int>> args(argc, argv); + + bool x{false}; + bool y{false}; + int z{}; + args.add('x', "--long-x", + std::function([&](){ x = true; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](){ y = true; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](std::optional<int> i) + { + uASSERT(i.has_value()); + z = *i; return 0; + }), "Help z"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT(x); + uASSERT(y); + uASSERT_EQUAL(42, z); + } + + { + const char* argv[] = { "app-name", "-xyz" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int, std::optional<int>> args(argc, argv); + + bool x{false}; + bool y{false}; + int z{}; + args.add('x', "--long-x", + std::function([&](){ x = true; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](){ y = true; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](std::optional<int> i) + { + uASSERT(!i.has_value()); + z = 1; return 0; + }), "Help z"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT(x); + uASSERT(y); + uASSERT_EQUAL(1, z); + } + + { + const char* argv[] = { "app-name", "-xyz", "42" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<int, std::optional<int>> args(argc, argv); + + bool x{false}; + bool y{false}; + int z{}; + args.add('x', "--long-x", + std::function([&](){ x = true; return 0;}), "Help x"); + + args.add('y', "--long-y", + std::function([&](){ y = true; return 0;}), "Help y"); + + args.add('z', "--long-z", + std::function([&](std::optional<int> i) + { + uASSERT(!i.has_value()); + z = 1; return 0; + }), "Help z"); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT(x); + uASSERT(y); + uASSERT_EQUAL(1, z); + } + } + + void test_nullprogram() + { + using namespace std::string_literals; + // Inspired by https://nullprogram.com/blog/2020/08/01/ + + // + // Short options + // + { + const char* argv[] = { "program", "-a", "-b", "-c" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<char> r; + args.add('a', {}, std::function([&](){ r.push_back('a'); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back('b'); return 0;}), {}); + args.add('c', {}, std::function([&](){ r.push_back('c'); return 0;}), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(3u, r.size()); + uASSERT_EQUAL('a', r[0]); + uASSERT_EQUAL('b', r[1]); + uASSERT_EQUAL('c', r[2]); + } + + { + const char* argv[] = { "program", "-abc" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<char> r; + args.add('a', {}, std::function([&](){ r.push_back('a'); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back('b'); return 0;}), {}); + args.add('c', {}, std::function([&](){ r.push_back('c'); return 0;}), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(3u, r.size()); + uASSERT_EQUAL('a', r[0]); + uASSERT_EQUAL('b', r[1]); + uASSERT_EQUAL('c', r[2]); + } + + { + const char* argv[] = { "program", "-acb" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<char> r; + args.add('a', {}, std::function([&](){ r.push_back('a'); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back('b'); return 0;}), {}); + args.add('c', {}, std::function([&](){ r.push_back('c'); return 0;}), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(3u, r.size()); + uASSERT_EQUAL('a', r[0]); + uASSERT_EQUAL('c', r[1]); + uASSERT_EQUAL('b', r[2]); + } + + { + const char* argv[] = { "program", "-i", "input.txt", "-o", "output.txt" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string> args(argc, argv); + + std::vector<std::string> r; + args.add('i', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + + args.add('o', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("input.txt"s, r[0]); + uASSERT_EQUAL("output.txt"s, r[1]); + } + + { + const char* argv[] = { "program", "-iinput.txt", "-ooutput.txt" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string> args(argc, argv); + + std::vector<std::string> r; + args.add('i', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + args.add('o', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("input.txt"s, r[0]); + uASSERT_EQUAL("output.txt"s, r[1]); + } + + { + const char* argv[] = { "program", "-abco", "output.txt" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.add('c', {}, std::function([&](){ r.push_back("c"); return 0;}), {}); + args.add('o', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("a"s, r[0]); + uASSERT_EQUAL("b"s, r[1]); + uASSERT_EQUAL("c"s, r[2]); + uASSERT_EQUAL("output.txt"s, r[3]); + } + + { + const char* argv[] = { "program", "-abcooutput.txt" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.add('c', {}, std::function([&](){ r.push_back("c"); return 0;}), {}); + args.add('o', {}, + std::function([&](std::string input) + { + r.push_back(input); + return 0; + }), {}); + + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("a"s, r[0]); + uASSERT_EQUAL("b"s, r[1]); + uASSERT_EQUAL("c"s, r[2]); + uASSERT_EQUAL("output.txt"s, r[3]); + } + + { + // Optional omitted + const char* argv[] = { "program", "-c" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add('c', {}, + std::function([&](std::optional<std::string> c) + { + uASSERT(!c.has_value()); + r.push_back("c"); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(1u, r.size()); + uASSERT_EQUAL("c"s, r[0]); + } + + { + // Optional provided + const char* argv[] = { "program", "-cblue" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add('c', {}, + std::function([&](std::optional<std::string> c) + { + uASSERT(c.has_value()); + r.push_back(*c); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(1u, r.size()); + uASSERT_EQUAL("blue"s, r[0]); + } + + { + // Optional omitted (blue is a new argument) + const char* argv[] = { "program", "-c", "blue" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add('c', {}, + std::function([&](std::optional<std::string> c) + { + uASSERT(!c.has_value()); + r.push_back("c"); + return 0; + }), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("c"s, r[0]); + uASSERT_EQUAL("blue"s, r[1]); + } + + { + // Two seperate flags + const char* argv[] = { "program", "-c", "-x" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add('x', {}, std::function([&](){ r.push_back("x"); return 0;}), {}); + args.add('c', {}, + std::function([&](std::optional<std::string> c) + { + uASSERT(!c.has_value()); + r.push_back("c"); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("c"s, r[0]); + uASSERT_EQUAL("x"s, r[1]); + } + + { + // -c with argument "-x" + const char* argv[] = { "program", "-c-x" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add('x', {}, std::function([&](){ r.push_back("x"); return 0;}), {}); + args.add('c', {}, + std::function([&](std::optional<std::string> c) + { + uASSERT(c.has_value()); + r.push_back(*c); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(1u, r.size()); + uASSERT_EQUAL("-x"s, r[0]); + } + + { + const char* argv[] = { "program", "-a", "-b", "foo", "bar" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("a"s, r[0]); + uASSERT_EQUAL("b"s, r[1]); + uASSERT_EQUAL("foo"s, r[2]); + uASSERT_EQUAL("bar"s, r[3]); + } + + { + const char* argv[] = { "program", "-b", "-a", "foo", "bar" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("b"s, r[0]); + uASSERT_EQUAL("a"s, r[1]); + uASSERT_EQUAL("foo"s, r[2]); + uASSERT_EQUAL("bar"s, r[3]); + } + + { + const char* argv[] = { "program", "-a", "foo", "-b", "bar" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("a"s, r[0]); + uASSERT_EQUAL("foo"s, r[1]); + uASSERT_EQUAL("b"s, r[2]); + uASSERT_EQUAL("bar"s, r[3]); + } + + { + const char* argv[] = { "program", "foo", "-a", "-b", "bar" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("foo"s, r[0]); + uASSERT_EQUAL("a"s, r[1]); + uASSERT_EQUAL("b"s, r[2]); + uASSERT_EQUAL("bar"s, r[3]); + } + + { + const char* argv[] = { "program", "foo", "bar", "-a", "-b" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(4u, r.size()); + uASSERT_EQUAL("foo"s, r[0]); + uASSERT_EQUAL("bar"s, r[1]); + uASSERT_EQUAL("a"s, r[2]); + uASSERT_EQUAL("b"s, r[3]); + } + + { + const char* argv[] = { "program", "-a", "-b", "--", "-x", "foo", "bar" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add('a', {}, std::function([&](){ r.push_back("a"); return 0;}), {}); + args.add('b', {}, std::function([&](){ r.push_back("b"); return 0;}), {}); + args.set_pos_cb(std::function([&](std::string_view pos) + { + r.push_back(std::string(pos)); + return 0; + })); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(5u, r.size()); + uASSERT_EQUAL("a"s, r[0]); + uASSERT_EQUAL("b"s, r[1]); + uASSERT_EQUAL("-x"s, r[2]); + uASSERT_EQUAL("foo"s, r[3]); + uASSERT_EQUAL("bar"s, r[4]); + } + + // + // Long options + // + { + const char* argv[] = { "program", "--sort" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--sort", + std::function([&](){ r.push_back("sort"); return 0;}), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(1u, r.size()); + uASSERT_EQUAL("sort"s, r[0]); + } + + { + const char* argv[] = { "program", "--no-sort" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--no-sort", + std::function([&](){ r.push_back("no-sort"); return 0;}), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(1u, r.size()); + uASSERT_EQUAL("no-sort"s, r[0]); + } + + { + const char* argv[] = + { "program", "--output", "output.txt", "--block-size", "1024" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string, int> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--output", + std::function([&](std::string output) + { + r.push_back(output); + return 0; + }), {}); + args.add({}, "--block-size", + std::function([&](int block_size) + { + r.push_back("["s + std::to_string(block_size) + "]"s); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("output.txt"s, r[0]); + uASSERT_EQUAL("[1024]"s, r[1]); + } + + { + const char* argv[] = + { "program", "--output=output.txt", "--block-size=1024" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::string, int> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--output", + std::function([&](std::string output) + { + r.push_back(output); + return 0; + }), {}); + args.add({}, "--block-size", + std::function([&](int block_size) + { + r.push_back("["s + std::to_string(block_size) + "]"s); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("output.txt"s, r[0]); + uASSERT_EQUAL("[1024]"s, r[1]); + } + + { + const char* argv[] = + { "program", "--color", "--reverse" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--color", + std::function([&](std::optional<std::string> color) + { + uASSERT(!color.has_value()); + r.push_back("color"); + return 0; + }), {}); + args.add({}, "--reverse", + std::function([&]() + { + r.push_back("reverse"); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("color"s, r[0]); + uASSERT_EQUAL("reverse"s, r[1]); + } + + { + const char* argv[] = + { "program", "--color=never", "--reverse" }; + int argc = sizeof(argv)/sizeof(*argv); + + arg::Parser<std::optional<std::string>> args(argc, argv); + + std::vector<std::string> r; + args.add({}, "--color", + std::function([&](std::optional<std::string> color) + { + uASSERT(color.has_value()); + r.push_back(*color); + return 0; + }), {}); + args.add({}, "--reverse", + std::function([&]() + { + r.push_back("reverse"); + return 0; + }), {}); + + auto res = args.parse(); + uASSERT_EQUAL(0, res); + uASSERT_EQUAL(2u, r.size()); + uASSERT_EQUAL("never"s, r[0]); + uASSERT_EQUAL("reverse"s, r[1]); + } + + } +}; + +// Registers the fixture into the 'registry' +static ArgParserTest test; diff --git a/test/argsplit_test.cc b/test/argsplit_test.cc new file mode 100644 index 0000000..7dce561 --- /dev/null +++ b/test/argsplit_test.cc @@ -0,0 +1,203 @@ +#include <uunit.h> + +#include <util.h> + +class ArgSplitTest + : public uUnit +{ +public: + using fs = std::filesystem::path; + + ArgSplitTest() + { + uTEST(ArgSplitTest::plain_test); + uTEST(ArgSplitTest::quot_test); + uTEST(ArgSplitTest::apotrophe_test); + uTEST(ArgSplitTest::mixed_test); + uTEST(ArgSplitTest::escape_test); + } + + void plain_test() + { + using namespace std::string_literals; + + { // zero + auto res = argsplit({}); + uASSERT_EQUAL(0u, res.size()); + } + + { // one + auto res = argsplit("hello"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("hello"s, res[0]); + } + + { // many + auto res = argsplit("hello world"s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("hello"s, res[0]); + uASSERT_EQUAL("world"s, res[1]); + } + } + + void quot_test() + { + using namespace std::string_literals; + + { // zero + auto res = argsplit("\"\""); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\"\""s, res[0]); + } + + { // one + auto res = argsplit("\"hello\""s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\"hello\""s, res[0]); + } + + { // one with space + auto res = argsplit("\"hel lo\""s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\"hel lo\""s, res[0]); + } + + { // many + auto res = argsplit("\"hello\" \"world\""s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\"hello\""s, res[0]); + uASSERT_EQUAL("\"world\""s, res[1]); + } + + { // many with spaces + auto res = argsplit("\"hel lo\" \"wor ld\""s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\"hel lo\""s, res[0]); + uASSERT_EQUAL("\"wor ld\""s, res[1]); + } + } + + void apotrophe_test() + { + using namespace std::string_literals; + + { // zero + auto res = argsplit("\'\'"); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'\'"s, res[0]); + } + + { // one + auto res = argsplit("\'hello\'"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'hello\'"s, res[0]); + } + + { // one with space + auto res = argsplit("\'hel lo\'"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'hel lo\'"s, res[0]); + } + + { // many + auto res = argsplit("\'hello\' \'world\'"s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\'hello\'"s, res[0]); + uASSERT_EQUAL("\'world\'"s, res[1]); + } + + { // many with spaces + auto res = argsplit("\'hel lo\' \'wor ld\'"s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\'hel lo\'"s, res[0]); + uASSERT_EQUAL("\'wor ld\'"s, res[1]); + } + } + + void mixed_test() + { + using namespace std::string_literals; + + { // zero + auto res = argsplit("\'\'"); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'\'"s, res[0]); + } + + { // one + auto res = argsplit("\'he\"llo\'"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'he\"llo\'"s, res[0]); + } + + { // one + auto res = argsplit("\"he\'llo\""s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\"he\'llo\""s, res[0]); + } + + { // one with space + auto res = argsplit("\'he\"l lo\'\""s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\'he\"l lo\'\""s, res[0]); + } + + { // one with space + auto res = argsplit("\"he\'l lo\"\'"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\"he\'l lo\"\'"s, res[0]); + } + + { // many + auto res = argsplit("\"he\'llo\" \"wor\'ld\""s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\"he\'llo\""s, res[0]); + uASSERT_EQUAL("\"wor\'ld\""s, res[1]); + } + + { // many with spaces + auto res = argsplit("\'hel\"lo\' \'wor\"ld\'"s); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("\'hel\"lo\'"s, res[0]); + uASSERT_EQUAL("\'wor\"ld\'"s, res[1]); + } + } + + void escape_test() + { + using namespace std::string_literals; + + { // zero + auto res = argsplit("\\"); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\\"s, res[0]); + } + + { // one + auto res = argsplit("\\\'"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\\\'"s, res[0]); + } + + { // one + auto res = argsplit("\\\""s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\\\""s, res[0]); + } + + { // one + auto res = argsplit("\\\\"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("\\\\"s, res[0]); + } + + { // one + auto res = argsplit("a\\ b"s); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("a\\ b"s, res[0]); + } + } +}; + +// Registers the fixture into the 'registry' +static ArgSplitTest test; diff --git a/test/ctor.cc b/test/ctor.cc index c24ded7..0d77a3e 100644 --- a/test/ctor.cc +++ b/test/ctor.cc @@ -1,25 +1,111 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include <libctor.h> +#include <ctor.h> namespace { -BuildConfigurations ctorTestConfigs(const Settings& settings) +ctor::build_configurations ctorTestConfigs(const ctor::settings& settings) { return { { - .type = TargetType::UnitTest, + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "argparser_test", + .sources = { + "argparser_test.cc", + "testmain.cc", + }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"argparser\"", + }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "argsplit_test", + .sources = { + "argsplit_test.cc", + "testmain.cc", + "../src/util.cc", + }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"argsplit\"", + }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "pointerlist_test", + .sources = { + "pointerlist_test.cc", + "testmain.cc", + "../src/pointerlist.cc", + }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"pointerlist\"", + }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "deps_test", + .sources = { + "deps_test.cc", + "testmain.cc", + "../src/deps.cc", + "../src/util.cc", + }, + .depends = { "testprog", }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"deps\"", + }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "testprog", + .sources = { + "testprog.cc", + }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, .target = "execute_test", .sources = { "execute_test.cc", "testmain.cc", "../src/execute.cc", + "../src/pointerlist.cc", + "../src/util.cc", }, + .depends = { "testprog", }, .flags = { .cxxflags = { - "-std=c++20", "-O3", "-s", "-Wall", "-Werror", + "-std=c++20", "-O3", "-Wall", "-Werror", "-I../src", "-Iuunit", "-DOUTPUT=\"execute\"", }, @@ -27,7 +113,8 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) }, }, { - .type = TargetType::UnitTest, + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, .target = "tasks_test", .sources = { "tasks_test.cc", @@ -36,7 +123,7 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) .depends = { "libctor_nomain.a" }, .flags = { .cxxflags = { - "-std=c++20", "-O3", "-s", "-Wall", "-Werror", + "-std=c++20", "-O3", "-Wall", "-Werror", "-I../src", "-Iuunit", "-DOUTPUT=\"tasks\"", }, @@ -44,7 +131,26 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) }, }, { - .type = TargetType::UnitTest, + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "cycle_test", + .sources = { + "cycle_test.cc", + "testmain.cc", + }, + .depends = { "libctor_nomain.a" }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"cycle\"", + }, + .ldflags = { "-pthread" }, + }, + }, + { + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, .target = "source_type_test", .sources = { "source_type_test.cc", @@ -53,7 +159,7 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) .depends = { "libctor_nomain.a" }, .flags = { .cxxflags = { - "-std=c++20", "-O3", "-s", "-Wall", "-Werror", + "-std=c++20", "-O3", "-Wall", "-Werror", "-I../src", "-Iuunit", "-DOUTPUT=\"source_type\"", }, @@ -61,12 +167,34 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) }, }, { - .type = TargetType::UnitTestLib, + .type = ctor::target_type::unit_test, + .system = ctor::output_system::build, + .target = "tools_test", + .sources = { + "tools_test.cc", + "testmain.cc", + "../src/util.cc", + "../src/tools.cc", + }, + //.depends = { "libctor_nomain.a" }, + .flags = { + .cxxflags = { + "-std=c++20", "-O3", "-Wall", "-Werror", + "-I../src", "-Iuunit", + "-DOUTPUT=\"tools\"", + }, + }, + }, + { + .type = ctor::target_type::unit_test_library, + .system = ctor::output_system::build, .target = "libctor_nomain.a", .sources = { "../src/build.cc", "../src/configure.cc", + "../src/deps.cc", "../src/execute.cc", + "../src/pointerlist.cc", "../src/rebuild.cc", "../src/tasks.cc", "../src/task.cc", @@ -75,12 +203,13 @@ BuildConfigurations ctorTestConfigs(const Settings& settings) "../src/task_fn.cc", "../src/task_ld.cc", "../src/task_so.cc", + "../src/tools.cc", "../src/util.cc", "../src/externals_manual.cc", }, .flags = { .cxxflags = { - "-std=c++20", "-O3", "-s", "-Wall", "-Werror", + "-std=c++20", "-O3", "-Wall", "-Werror", "-I../src", }, .ldflags = { "-pthread" }, diff --git a/test/cycle_test.cc b/test/cycle_test.cc new file mode 100644 index 0000000..3b45632 --- /dev/null +++ b/test/cycle_test.cc @@ -0,0 +1,87 @@ +#include <uunit.h> + +#include <ctor.h> +#include <build.h> + +namespace +{ +ctor::build_configurations ctorTestConfigsCyclic(const ctor::settings&) +{ + return + { + // No dependency + { + .target = "target0", + }, + + // Direct (self-depends) + { + .target = "target1", + .depends = { "target1" }, + }, + + // Indirect cyclic depends + { + .target = "target2", + .depends = { "target3" }, + }, + { + .target = "target3", + .depends = { "target2" }, + }, + }; +} +} + +REG(ctorTestConfigsCyclic); + +class CycleTest + : public uUnit +{ +public: + CycleTest() + { + uTEST(CycleTest::getTargets_test); + } + + void getTargets_test() + { + using namespace std::string_literals; + ctor::settings settings{}; + const auto& tasks = getTasks(settings); + uASSERT_EQUAL(4u, tasks.size()); + + uASSERT_EQUAL("target0"s, tasks[0]->target()); + uASSERT_EQUAL("target1"s, tasks[1]->target()); + uASSERT_EQUAL("target2"s, tasks[2]->target()); + uASSERT_EQUAL("target3"s, tasks[3]->target()); + + for(auto task : tasks) + { + if(task->registerDepTasks(tasks)) + { + uASSERT(false); + } + } + + bool exc; + exc = false; + try { getDepTasks(tasks[0]); } catch(...) { exc = true; } + uASSERT(!exc); + + exc = false; + try { getDepTasks(tasks[1]); } catch(...) { exc = true; } + uASSERT(exc); + + exc = false; + try { getDepTasks(tasks[2]); } catch(...) { exc = true; } + uASSERT(exc); + + exc = false; + try { getDepTasks(tasks[3]); } catch(...) { exc = true; } + uASSERT(exc); + } +}; + +// Registers the fixture into the 'registry' +static CycleTest test; diff --git a/test/deps_test.cc b/test/deps_test.cc new file mode 100644 index 0000000..762b0e5 --- /dev/null +++ b/test/deps_test.cc @@ -0,0 +1,97 @@ +#include <uunit.h> + +#include <fstream> +#include <map> +#include <vector> + +#include <deps.h> +#include <algorithm> + +#include "paths.h" +#include "tmpfile.h" + +class DepsTest + : public uUnit +{ +public: + DepsTest() + { + uTEST(DepsTest::parser_test); + } + + void parser_test() + { + using namespace std::string_literals; + + auto test_data = paths::top_srcdir / "test" / "deps_test_data"; + + auto empty = test_data / "empty.d"; + auto trivial = test_data / "trivial.d"; + auto no_newline = test_data / "no_newline.d"; + auto no_deps = test_data / "no_deps.d"; + auto trailing_whitespace = test_data / "trailing_whitespace.d"; + auto spaces = test_data / "spaces.d"; + auto multiline = test_data / "multiline.d"; + auto no_such_file = test_data / "no_such_file.d"; // doesn't exist + auto missing_colon = test_data / "missing_colon.d"; + + { + auto res = readDeps(empty.string(), ctor::toolchain::gcc); + uASSERT(res.empty()); + } + + { + auto res = readDeps(trivial.string(), ctor::toolchain::gcc); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("x.cc"s, res[0]); + } + + { + auto res = readDeps(no_newline.string(), ctor::toolchain::gcc); + uASSERT_EQUAL(1u, res.size()); + uASSERT_EQUAL("x.cc"s, res[0]); + } + + { + auto res = readDeps(no_deps.string(), ctor::toolchain::gcc); + uASSERT_EQUAL(0u, res.size()); + } + + { + auto res = readDeps(spaces.string(), ctor::toolchain::gcc); + uASSERT_EQUAL(2u, res.size()); + uASSERT_EQUAL("x y.cc"s, res[0]); + uASSERT_EQUAL("x y.h"s, res[1]); + } + + { + auto res = readDeps(multiline.string(), ctor::toolchain::gcc); + uASSERT_EQUAL(12u, res.size()); + uASSERT_EQUAL("src/configure.cc"s, res[0]); + uASSERT_EQUAL("src/configure.h"s, res[1]); + uASSERT_EQUAL("src/getoptpp/getoptpp.hpp"s, res[2]); + uASSERT_EQUAL("src/execute.h"s, res[3]); + uASSERT_EQUAL("src/ctor.h"s, res[4]); + uASSERT_EQUAL("src/tasks.h"s, res[5]); + uASSERT_EQUAL("src/task.h"s, res[6]); + uASSERT_EQUAL("src/rebuild.h"s, res[7]); + uASSERT_EQUAL("src/externals.h"s, res[8]); + uASSERT_EQUAL("src/externals_manual.h"s, res[9]); + uASSERT_EQUAL("src/tools.h"s, res[10]); + uASSERT_EQUAL("src/util.h"s, res[11]); + } + + { + auto res = readDeps(no_such_file.string(), ctor::toolchain::gcc); + uASSERT(res.empty()); + } + + { + auto res = readDeps(missing_colon.string(), ctor::toolchain::gcc); + uASSERT(res.empty()); + } + } +}; + +// Registers the fixture into the 'registry' +static DepsTest test; diff --git a/test/deps_test_data/empty.d b/test/deps_test_data/empty.d new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/deps_test_data/empty.d diff --git a/test/deps_test_data/missing_colon.d b/test/deps_test_data/missing_colon.d new file mode 100644 index 0000000..e46996c --- /dev/null +++ b/test/deps_test_data/missing_colon.d @@ -0,0 +1 @@ +x.cc x.h diff --git a/test/deps_test_data/multiline.d b/test/deps_test_data/multiline.d new file mode 100644 index 0000000..8862ab0 --- /dev/null +++ b/test/deps_test_data/multiline.d @@ -0,0 +1,4 @@ +build/src/libctor_a-configure_cc.o: src/configure.cc src/configure.h \ + src/getoptpp/getoptpp.hpp src/execute.h src/ctor.h src/tasks.h \ + src/task.h src/rebuild.h src/externals.h src/externals_manual.h \ + src/tools.h src/util.h diff --git a/test/deps_test_data/no_deps.d b/test/deps_test_data/no_deps.d new file mode 100644 index 0000000..e7cccf6 --- /dev/null +++ b/test/deps_test_data/no_deps.d @@ -0,0 +1 @@ +x.o: diff --git a/test/deps_test_data/no_newline.d b/test/deps_test_data/no_newline.d new file mode 100644 index 0000000..88829ea --- /dev/null +++ b/test/deps_test_data/no_newline.d @@ -0,0 +1 @@ +x.o: x.cc
\ No newline at end of file diff --git a/test/deps_test_data/spaces.d b/test/deps_test_data/spaces.d new file mode 100644 index 0000000..c53fd64 --- /dev/null +++ b/test/deps_test_data/spaces.d @@ -0,0 +1 @@ +x\ y.o: x\ y.cc x\ y.h diff --git a/test/deps_test_data/trivial.d b/test/deps_test_data/trivial.d new file mode 100644 index 0000000..15a0c29 --- /dev/null +++ b/test/deps_test_data/trivial.d @@ -0,0 +1 @@ +x.o: x.cc diff --git a/test/execute_test.cc b/test/execute_test.cc index 9da18dc..11b067f 100644 --- a/test/execute_test.cc +++ b/test/execute_test.cc @@ -1,6 +1,15 @@ #include <uunit.h> +#include <fstream> +#include <map> +#include <vector> + #include <execute.h> +#include <util.h> +#include <algorithm> + +#include "paths.h" +#include "tmpfile.h" class ExecuteTest : public uUnit @@ -8,14 +17,78 @@ class ExecuteTest public: ExecuteTest() { - uTEST(ExecuteTest::runit); + uTEST(ExecuteTest::return_value); + uTEST(ExecuteTest::env); + } + + void return_value() + { + ctor::settings s; + auto cur_path = std::filesystem::path(paths::argv_0).parent_path(); + std::vector<std::string> paths{{cur_path.string()}}; + auto cmd = locate("testprog", paths); + uASSERT(!cmd.empty()); + + auto value = execute(s, cmd, {"retval", "0"}, {}, false); + uASSERT_EQUAL(0, value); + value = execute(s, cmd, {"retval", "1"}, {}, false); + uASSERT_EQUAL(1, value); + value = execute(s, "no-such-binary", {}, {}, false); + uASSERT_EQUAL(1, value); + value = execute(s, cmd, {"segfault"}, {}, false); + uASSERT_EQUAL(11, value); + value = execute(s, cmd, {"throw"}, {}, false); + uASSERT_EQUAL(6, value); + value = execute(s, cmd, {"abort"}, {}, false); + uASSERT_EQUAL(6, value); } - void runit() + void env() { - uASSERT_EQUAL(0, execute("/bin/true", {}, false)); - uASSERT_EQUAL(1, execute("/bin/false", {}, false)); - uASSERT_EQUAL(1, execute("no-such-binary", {}, false)); + using namespace std::string_literals; + + ctor::settings s; + auto cur_path = std::filesystem::path(paths::argv_0).parent_path(); + std::vector<std::string> paths{{cur_path.string()}}; + auto cmd = locate("testprog", paths); + uASSERT(!cmd.empty()); + + TmpFile tmp; + + std::map<std::string, std::string> env; + + // New env vars + env["foo"] = "bar"; + env["bar"] = "42"; + + // Overwrite the exiting LANG var + env["LANG"] = "foo"; + + auto value = execute(s, cmd, {"envdump", tmp.get()}, env, false); + uASSERT_EQUAL(0, value); + + std::vector<std::string> vars; + { + std::ifstream infile(tmp.get()); + std::string line; + while (std::getline(infile, line)) + { + vars.push_back(line); + } + } + + // Check the two explicitly set vars + auto chk = std::find(vars.begin(), vars.end(), "foo=bar"s); + uASSERT(chk != vars.end()); + chk = std::find(vars.begin(), vars.end(), "bar=42"s); + uASSERT(chk != vars.end()); + + // Check the one that should have overwritten the existing one (probably LANG=en_US.UTF-8 or something) + chk = std::find(vars.begin(), vars.end(), "LANG=foo"s); + uASSERT(chk != vars.end()); + + // Check that other vars are also there (ie. the env wasn't cleared on entry) + uASSERT(vars.size() > 3); } }; diff --git a/test/pointerlist_test.cc b/test/pointerlist_test.cc new file mode 100644 index 0000000..4274473 --- /dev/null +++ b/test/pointerlist_test.cc @@ -0,0 +1,320 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include <uunit.h> + +#include <set> +#include <algorithm> + +#include "pointerlist.h" + +class PointerListTest + : public uUnit +{ +public: + PointerListTest() + { + uTEST(PointerListTest::test_zom_pointerlist_push); + uTEST(PointerListTest::test_zom_pointerlist_from_args); + uTEST(PointerListTest::test_zom_envmap_insert); + uTEST(PointerListTest::test_zom_envmap_from_env); + uTEST(PointerListTest::test_exceptional_env); + } + + void test_zom_pointerlist_push() + { + using namespace std::string_literals; + + { // Zero + PointerList args; + uASSERT_EQUAL(0u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(0, argc); + uASSERT(nullptr != argv); + uASSERT_EQUAL(nullptr, argv[0]); + } + + { // One + PointerList args; + args.push_back("hello"); + uASSERT_EQUAL(1u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello"s, std::string(argv[0])); + } + + { // Many + PointerList args; + args.push_back("hello"); + args.push_back("dear"); + args.push_back("world"); + uASSERT_EQUAL(3u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(3, argc); + uASSERT_EQUAL("hello"s, std::string(argv[0])); + uASSERT_EQUAL("dear"s, std::string(argv[1])); + uASSERT_EQUAL("world"s, std::string(argv[2])); + } + } + + void test_zom_pointerlist_from_args() + { + using namespace std::string_literals; + + { // Zero + PointerList args(0, nullptr); + uASSERT_EQUAL(0u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(0, argc); + uASSERT(nullptr != argv); + uASSERT_EQUAL(nullptr, argv[0]); + } + + { // One + int _argc{1}; + const char* _argv[] = { "hello" }; + PointerList args(_argc, _argv); + uASSERT_EQUAL(1u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello"s, std::string(argv[0])); + } + + { // Many + int _argc{3}; + const char* _argv[] = { "hello", "dear", "world" }; + PointerList args(_argc, _argv); + uASSERT_EQUAL(3u, args.size()); + auto [argc, argv] = args.get(); + uASSERT_EQUAL(3, argc); + // order must be preserved + uASSERT_EQUAL("hello"s, std::string(argv[0])); + uASSERT_EQUAL("dear"s, std::string(argv[1])); + uASSERT_EQUAL("world"s, std::string(argv[2])); + } + } + + void test_zom_envmap_insert() + { + using namespace std::string_literals; + + { // Zero + EnvMap env; + uASSERT_EQUAL(0u, env.size()); + + auto [argc, argv] = env.get(); + uASSERT_EQUAL(0, argc); + uASSERT(nullptr != argv); + uASSERT_EQUAL(nullptr, argv[0]); + + auto str = env.stringify(); + uASSERT_EQUAL(1u, str.size()); + uASSERT_EQUAL('\0', str[0]); + } + + { // One (key only) + EnvMap env; + env.insert("hello"); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello="s, std::string(argv[0])); + uASSERT_EQUAL(""s, env["hello"]); + } + { // One (with value) + EnvMap env; + env.insert("hello=world"); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello=world"s, std::string(argv[0])); + uASSERT_EQUAL("world"s, env["hello"]); + + uASSERT_EQUAL("hello=world\0\0"s, env.stringify()); + } + + { // Overwrite one + EnvMap env; + env.insert("hello=world"); + uASSERT_EQUAL(1u, env.size()); + uASSERT_EQUAL("world"s, env["hello"s]); + + env.insert("hello=foo"); + uASSERT_EQUAL(1u, env.size()); + uASSERT_EQUAL("foo"s, env["hello"s]); + } + + { // Many + EnvMap env; + env.insert("hello=world"); + env.insert("world=leader"); + env.insert("dear=boar"); + uASSERT_EQUAL(3u, env.size()); + + uASSERT_EQUAL("boar"s, env["dear"s]); + uASSERT_EQUAL("world"s, env["hello"s]); + uASSERT_EQUAL("leader"s, env["world"s]); + + auto [argc, argv] = env.get(); + uASSERT_EQUAL(3, argc); + // store and sort to verify unordered + std::vector<std::string> vals{argv[0], argv[1], argv[2]}; + std::ranges::sort(vals); + uASSERT_EQUAL("dear=boar"s, vals[0]); + uASSERT_EQUAL("hello=world"s, vals[1]); + uASSERT_EQUAL("world=leader"s, vals[2]); + + // test all combinations since ordering is not a requirement + // exactly one of the must be true (boolean XOR) + auto str = env.stringify(); + uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) != + ("dear=boar\0world=leader\0hello=world\0\0"s == str)) != + ("hello=world\0dear=boar\0world=leader\0\0"s == str)) != + ("hello=world\0world=leader\0dear=boar\0\0"s == str)) != + ("world=leader\0dear=boar\0hello=world\0\0"s == str)) != + ("world=leader\0hello=world\0dear=boar\0\0"s == str)); + } + } + + void test_zom_envmap_from_env() + { + using namespace std::string_literals; + + { // Zero + const char* penv = nullptr; + EnvMap env(penv); + uASSERT_EQUAL(0u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(0, argc); + uASSERT(nullptr != argv); + uASSERT_EQUAL(nullptr, argv[0]); + } + + { // Zero + const char** ppenv = nullptr; + EnvMap env(ppenv); + uASSERT_EQUAL(0u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(0, argc); + uASSERT(nullptr != argv); + uASSERT_EQUAL(nullptr, argv[0]); + } + + { // One (key only) + const char* ptr = "hello\0\0"; + EnvMap env(ptr); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello="s, std::string(argv[0])); + uASSERT_EQUAL(""s, env["hello"]); + } + + { // One (key only) + const char* ptr[] = {"hello", nullptr}; + EnvMap env(ptr); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello="s, std::string(argv[0])); + uASSERT_EQUAL(""s, env["hello"]); + } + + { // One (with value) + const char* ptr = "hello=world\0\0"; + EnvMap env(ptr); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello=world"s, std::string(argv[0])); + uASSERT_EQUAL("world"s, env["hello"]); + + uASSERT_EQUAL("hello=world\0\0"s, env.stringify()); + } + + { // One (with value) + const char* ptr[] = {"hello=world\0", nullptr}; + EnvMap env(ptr); + uASSERT_EQUAL(1u, env.size()); + auto [argc, argv] = env.get(); + uASSERT_EQUAL(1, argc); + uASSERT_EQUAL("hello=world"s, std::string(argv[0])); + uASSERT_EQUAL("world"s, env["hello"]); + + uASSERT_EQUAL("hello=world\0\0"s, env.stringify()); + } + + { // Many + const char* ptr = "hello=world\0world=leader\0dear=boar\0\0"; + EnvMap env(ptr); + uASSERT_EQUAL(3u, env.size()); + + uASSERT_EQUAL("boar"s, env["dear"s]); + uASSERT_EQUAL("world"s, env["hello"s]); + uASSERT_EQUAL("leader"s, env["world"s]); + + auto [argc, argv] = env.get(); + uASSERT_EQUAL(3, argc); + // store and sort to verify unordered + std::vector<std::string> vals{argv[0], argv[1], argv[2]}; + std::ranges::sort(vals); + uASSERT_EQUAL("dear=boar"s, vals[0]); + uASSERT_EQUAL("hello=world"s, vals[1]); + uASSERT_EQUAL("world=leader"s, vals[2]); + + // test all combinations since ordering is not a requirement + // exactly one of the must be true (boolean XOR) + auto str = env.stringify(); + uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) != + ("dear=boar\0world=leader\0hello=world\0\0"s == str)) != + ("hello=world\0dear=boar\0world=leader\0\0"s == str)) != + ("hello=world\0world=leader\0dear=boar\0\0"s == str)) != + ("world=leader\0dear=boar\0hello=world\0\0"s == str)) != + ("world=leader\0hello=world\0dear=boar\0\0"s == str)); + } + + { // Many + const char* ptr[] = + {"hello=world\0", "world=leader\0", "dear=boar\0", nullptr}; + EnvMap env(ptr); + uASSERT_EQUAL(3u, env.size()); + + uASSERT_EQUAL("boar"s, env["dear"s]); + uASSERT_EQUAL("world"s, env["hello"s]); + uASSERT_EQUAL("leader"s, env["world"s]); + + auto [argc, argv] = env.get(); + uASSERT_EQUAL(3, argc); + // store and sort to verify unordered + std::vector<std::string> vals{argv[0], argv[1], argv[2]}; + std::ranges::sort(vals); + uASSERT_EQUAL("dear=boar"s, vals[0]); + uASSERT_EQUAL("hello=world"s, vals[1]); + uASSERT_EQUAL("world=leader"s, vals[2]); + + // test all combinations since ordering is not a requirement + // exactly one of the must be true (boolean XOR) + auto str = env.stringify(); + uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) != + ("dear=boar\0world=leader\0hello=world\0\0"s == str)) != + ("hello=world\0dear=boar\0world=leader\0\0"s == str)) != + ("hello=world\0world=leader\0dear=boar\0\0"s == str)) != + ("world=leader\0dear=boar\0hello=world\0\0"s == str)) != + ("world=leader\0hello=world\0dear=boar\0\0"s == str)); + } + } + + void test_exceptional_env() + { + using namespace std::string_literals; + + { // Zero + EnvMap env; + uASSERT_EQUAL(0u, env.size()); + uASSERT_EQUAL(""s, env["foo"]); // lookup of non-existing key + } + } +}; + +// Registers the fixture into the 'registry' +static PointerListTest test; diff --git a/test/source_type_test.cc b/test/source_type_test.cc index ed7e783..288f1e5 100644 --- a/test/source_type_test.cc +++ b/test/source_type_test.cc @@ -1,37 +1,40 @@ -#include <uunit.h> - -#include <libctor.h> +#include <ctor.h> #include <task_cc.h> -std::ostream& operator<<(std::ostream& stream, const Language& lang) +std::ostream& operator<<(std::ostream& stream, const ctor::language& lang); + +#include <uunit.h> + +std::ostream& operator<<(std::ostream& stream, const ctor::language& lang) { switch(lang) { - case Language::Auto: - stream << "Language::Auto"; + case ctor::language::automatic: + stream << "ctor::language::automatic"; break; - case Language::C: - stream << "Language::C"; + case ctor::language::c: + stream << "ctor::language::c"; break; - case Language::Cpp: - stream << "Language::Cpp"; + case ctor::language::cpp: + stream << "ctor::language::cpp"; break; - case Language::Asm: - stream << "Language::Asm"; + case ctor::language::assembler: + stream << "ctor::language::assembler"; break; } return stream; } + class TestableTaskCC : public TaskCC { public: - TestableTaskCC(const Source& source) + TestableTaskCC(const ctor::source& source) : TaskCC({}, {}, "build", source) {} - Language language() const + ctor::language language() const { return source_language; } @@ -50,22 +53,22 @@ public: { { // c++ TestableTaskCC task("hello.cc"); - uASSERT_EQUAL(Language::Cpp, task.language()); + uASSERT_EQUAL(ctor::language::cpp, task.language()); } { // c TestableTaskCC task("hello.c"); - uASSERT_EQUAL(Language::C, task.language()); + uASSERT_EQUAL(ctor::language::c, task.language()); } { // asm TestableTaskCC task("hello.s"); - uASSERT_EQUAL(Language::Asm, task.language()); + uASSERT_EQUAL(ctor::language::assembler, task.language()); } { // custom/explicit language - TestableTaskCC task( {"hello.foo", Language::Asm} ); - uASSERT_EQUAL(Language::Asm, task.language()); + TestableTaskCC task( {"hello.foo", ctor::language::assembler} ); + uASSERT_EQUAL(ctor::language::assembler, task.language()); } // Note: Failure state will result in exit(1) so cannot be tested diff --git a/test/suite/ctor_files/ctor.cc.bar b/test/suite/ctor_files/ctor.cc.bar index 92456cb..218f9cc 100644 --- a/test/suite/ctor_files/ctor.cc.bar +++ b/test/suite/ctor_files/ctor.cc.bar @@ -1,12 +1,12 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include <libctor.h> +#include <ctor.h> //#include "config.h" namespace { -BuildConfigurations ctorConfigs() +ctor::build_configurations ctorConfigs(const ctor::settings& settings) { return { @@ -30,17 +30,19 @@ BuildConfigurations ctorConfigs() }; } -ExternalConfigurations ctorExtConfigs() +ctor::external_configurations ctorExtConfigs(const ctor::settings& settings) { return { { .name = "bar", - .flags = { - .cxxflags = { "-D_A_", "-DBAR"}, - .cflags = { "-D_B_" }, - .ldflags = { "-D_C_" }, - .asmflags = { "-D_D_" }, + .external = ctor::external_manual{ + .flags = { + .cflags = { "-D_B_" }, + .cxxflags = { "-D_A_", "-DBAR"}, + .ldflags = { "-D_C_" }, + .asmflags = { "-D_D_" }, + }, }, // Creates --with-foo-prefix arg to configure which will be used for // -L and -I flags. diff --git a/test/suite/ctor_files/ctor.cc.base b/test/suite/ctor_files/ctor.cc.base index 6c60513..eab39c4 100644 --- a/test/suite/ctor_files/ctor.cc.base +++ b/test/suite/ctor_files/ctor.cc.base @@ -1,12 +1,12 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include <libctor.h> +#include <ctor.h> //#include "config.h" namespace { -BuildConfigurations ctorConfigs() +ctor::build_configurations ctorConfigs(const ctor::settings& settings) { return { @@ -30,17 +30,20 @@ BuildConfigurations ctorConfigs() }; } -ExternalConfigurations ctorExtConfigs() +ctor::external_configurations ctorExtConfigs(const ctor::settings& settings) { return { { .name = "bar", - .flags = { - .cxxflags = { "-D_A_", "-DFOO"}, - .cflags = { "-D_B_" }, - .ldflags = { "-D_C_" }, - .asmflags = { "-D_D_" }, + .external = ctor::external_manual + { + .flags = { + .cflags = { "-D_B_" }, + .cxxflags = { "-D_A_", "-DFOO"}, + .ldflags = { "-D_C_" }, + .asmflags = { "-D_D_" }, + }, }, // Creates --with-foo-prefix arg to configure which will be used for // -L and -I flags. diff --git a/test/suite/ctor_files/ctor.cc.multi b/test/suite/ctor_files/ctor.cc.multi index 9db2517..2b88afe 100644 --- a/test/suite/ctor_files/ctor.cc.multi +++ b/test/suite/ctor_files/ctor.cc.multi @@ -1,14 +1,14 @@ // -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. -#include <libctor.h> +#include <ctor.h> //#include "config.h" #include "foobar.h" namespace { -BuildConfigurations ctorConfigs() +ctor::build_configurations ctorConfigs(const ctor::settings& settings) { return { @@ -32,17 +32,19 @@ BuildConfigurations ctorConfigs() }; } -ExternalConfigurations ctorExtConfigs() +ctor::external_configurations ctorExtConfigs(const ctor::settings& settings) { return { { .name = "bar", - .flags = { - .cxxflags = { "-D_A_", "-DFOO"}, - .cflags = { "-D_B_" }, - .ldflags = { "-D_C_" }, - .asmflags = { "-D_D_" }, + .external = ctor::external_manual{ + .flags = { + .cflags = { "-D_B_" }, + .cxxflags = { "-D_A_", "-DFOO"}, + .ldflags = { "-D_C_" }, + .asmflags = { "-D_D_" }, + }, }, // Creates --with-foo-prefix arg to configure which will be used for // -L and -I flags. diff --git a/test/suite/test.sh b/test/suite/test.sh index c980154..c54137a 100755 --- a/test/suite/test.sh +++ b/test/suite/test.sh @@ -1,4 +1,9 @@ #!/bin/bash +: ${CXX:=g++} +: ${CTORDIR:=../../build} +: ${BUILDDIR:=build} + +CXX=$(which $CXX) function fail { @@ -12,22 +17,29 @@ function ctor ./ctor $* } +STAT_FORMAT="-c %Y" +if [[ "$OSTYPE" == "darwin"* ]]; then + # Mac OSX + STAT_FORMAT="-f %B" +fi + # Wipe the board -rm -Rf build +rm -Rf ${BUILDDIR} rm -f configuration.cc rm -f ctor +echo "** ctor_files/ctor.cc.base" cp ctor_files/ctor.cc.base ctor.cc # Compile bootstrap binary -g++ -pthread -std=c++20 -L../../build -lctor -I../../src ctor.cc -o ctor || fail ${LINENO} +$CXX -pthread $LDFLAGS $CXXFLAGS -std=c++20 -L${CTORDIR} -lctor -I../../src ctor.cc -o ctor || fail ${LINENO} # No build files should have been created yet -[ -d build ] && fail ${LINENO} +[ -d ${BUILDDIR} ] && fail ${LINENO} # capture md5 sum of ctor binary before configure is called MD5=`md5sum ctor` -ctor configure --ctor-includedir ../../src --ctor-libdir ../../build +ctor configure --ctor-includedir ../../src --ctor-libdir=${CTORDIR} --build-dir=${BUILDDIR} # ctor should be rebuilt at this point, so md5 sum should have changed (echo $MD5 | md5sum --status -c) && fail ${LINENO} @@ -36,7 +48,7 @@ ctor configure --ctor-includedir ../../src --ctor-libdir ../../build [ ! -f configuration.cc ] && fail ${LINENO} # Shouldn't compile anything yet - only configure -[ -f build/hello-hello_cc.o ] && fail ${LINENO} +[ -f ${BUILDDIR}/hello-hello_cc.o ] && fail ${LINENO} MD5=`md5sum ctor` @@ -44,12 +56,12 @@ MD5=`md5sum ctor` ctor -v # Compiled object should now exist -[ ! -f build/hello-hello_cc.o ] && fail ${LINENO} +[ ! -f ${BUILDDIR}/hello-hello_cc.o ] && fail ${LINENO} # ctor should not have been rebuilt, so md5 sum should be the same (echo $MD5 | md5sum --status -c) || fail ${LINENO} -MOD1=`stat -c %Y build/hello-hello_cc.o` +MOD1=`stat $STAT_FORMAT ${BUILDDIR}/hello-hello_cc.o` touch hello.cc sleep 1.1 @@ -57,36 +69,38 @@ sleep 1.1 ctor -v # Object file should have been recompiled -MOD2=`stat -c %Y build/hello-hello_cc.o` +MOD2=`stat $STAT_FORMAT ${BUILDDIR}/hello-hello_cc.o` [[ $MOD1 == $MOD2 ]] && fail ${LINENO} # Replacve -DFOO with -DBAR in foo external.cxxflags +echo "** ctor_files/ctor.cc.bar" cp ctor_files/ctor.cc.bar ctor.cc MD5C=`md5sum configuration.cc` MD5=`md5sum ctor` -MOD1=`stat -c %Y build/hello-hello_cc.o` +MOD1=`stat $STAT_FORMAT build/hello-hello_cc.o` sleep 1.1 # Run normally to reconfigure, rebuild ctor and rebuild hello.cc ctor -v -MOD2=`stat -c %Y build/hello-hello_cc.o` +MOD2=`stat $STAT_FORMAT ${BUILDDIR}/hello-hello_cc.o` [[ $MOD1 == $MOD2 ]] && fail ${LINENO} (echo $MD5C | md5sum --status -c) && fail ${LINENO} (echo $MD5 | md5sum --status -c) && fail ${LINENO} +echo "** ctor_files/ctor.cc.multi" cp ctor_files/ctor.cc.multi ctor.cc MD5C=`md5sum configuration.cc` MD5=`md5sum ctor` -MOD1=`stat -c %Y build/hello-hello_cc.o` +MOD1=`stat $STAT_FORMAT ${BUILDDIR}/hello-hello_cc.o` sleep 1.1 # Run normally to reconfigure, rebuild ctor and rebuild hello.cc ctor -v -MOD2=`stat -c %Y build/hello-hello_cc.o` +MOD2=`stat $STAT_FORMAT ${BUILDDIR}/hello-hello_cc.o` [[ $MOD1 == $MOD2 ]] && fail ${LINENO} (echo $MD5C | md5sum --status -c) && fail ${LINENO} (echo $MD5 | md5sum --status -c) && fail ${LINENO} @@ -94,11 +108,13 @@ MOD2=`stat -c %Y build/hello-hello_cc.o` # now touching foobar.h, should retrigger re-configuration touch foobar.h -MOD1=`stat -c %Y ctor` +MOD1=`stat $STAT_FORMAT ctor` sleep 1.1 # Run normally to reconfigure, rebuild ctor and rebuild hello.cc ctor -v -MOD2=`stat -c %Y ctor` +MOD2=`stat $STAT_FORMAT ctor` [[ $MOD1 == $MOD2 ]] && fail ${LINENO} + +exit 0 diff --git a/test/tasks_test.cc b/test/tasks_test.cc index 2e0ffc7..cbd0864 100644 --- a/test/tasks_test.cc +++ b/test/tasks_test.cc @@ -1,15 +1,16 @@ #include <uunit.h> -#include <libctor.h> +#include <ctor.h> #include <tasks.h> namespace { -BuildConfigurations ctorTestConfigs1(const Settings&) +ctor::build_configurations ctorTestConfigs1(const ctor::settings&) { return { { + .name = "Target1", .target = "target1", .sources = {"foo.cc", "bar.c"}, }, @@ -19,7 +20,7 @@ BuildConfigurations ctorTestConfigs1(const Settings&) }; } -BuildConfigurations ctorTestConfigs2(const Settings&) +ctor::build_configurations ctorTestConfigs2(const ctor::settings&) { return { @@ -33,16 +34,29 @@ BuildConfigurations ctorTestConfigs2(const Settings&) } } +namespace test_global { +ctor::toolchain toolchain{}; +ctor::arch arch{}; +} +const ctor::configuration& ctor::get_configuration() +{ + static ctor::configuration cfg{}; + cfg.build_toolchain = test_global::toolchain; + cfg.build_arch = test_global::arch; + return cfg; +} + + REG(ctorTestConfigs1); REG(ctorTestConfigs2); -std::size_t count(const std::set<std::shared_ptr<Task>>& tasks, - const std::string& name) +std::size_t count(const std::vector<std::shared_ptr<Task>>& tasks, + const std::filesystem::path& name) { auto cnt{0u}; for(const auto& task : tasks) { - if(task->target() == name) + if(task->target() == name.string()) { cnt++; } @@ -54,16 +68,17 @@ class TestTask : public Task { public: - TestTask(const std::string& name, bool dirty, + TestTask(const ctor::build_configuration& config, + const ctor::settings& settings, + const std::string& name, bool dirty, const std::vector<std::string>& deps = {}) - : Task({}, {}, {}) + : Task(config, settings, {}) , task_name(name) , task_dirty(dirty) , task_deps(deps) { } - std::string name() const override { return task_name; } int clean() override { return 0; } std::vector<std::string> depends() const override { return task_deps; } std::string target() const override { return task_name; } @@ -81,17 +96,20 @@ class TasksTest : public uUnit { public: + using fs = std::filesystem::path; + TasksTest() { uTEST(TasksTest::getTargets_test); uTEST(TasksTest::getTasks_test); uTEST(TasksTest::getNextTask_test); + uTEST(TasksTest::comparison_test); } void getTargets_test() { using namespace std::string_literals; - Settings settings{}; + ctor::settings settings{}; const auto& targets = getTargets(settings); uASSERT_EQUAL(4u, targets.size()); @@ -108,27 +126,29 @@ public: void getTasks_test() { + test_global::toolchain = ctor::toolchain::gcc; + test_global::arch = ctor::arch::unix; + using namespace std::string_literals; - Settings settings{ .builddir = "foo" }; + ctor::settings settings{ .builddir = "foo" }; { auto tasks = getTasks(settings); uASSERT_EQUAL(6u, tasks.size()); - // Note: count() is used here because the order of - // std::set<std::shared_ptr<T>> is not deterministic. - uASSERT_EQUAL(1u, count(tasks, "target1"s)); - uASSERT_EQUAL(1u, count(tasks, "target2"s)); - uASSERT_EQUAL(1u, count(tasks, "target3"s)); - uASSERT_EQUAL(1u, count(tasks, "target4"s)); - uASSERT_EQUAL(1u, count(tasks, "test/target1-foo_cc.o"s)); - uASSERT_EQUAL(1u, count(tasks, "test/target1-bar_c.o"s)); + // Note: count() is used here because the order doesn't matter + uASSERT_EQUAL(1u, count(tasks, fs("target1"))); + uASSERT_EQUAL(1u, count(tasks, fs("target2"))); + uASSERT_EQUAL(1u, count(tasks, fs("target3"))); + uASSERT_EQUAL(1u, count(tasks, fs("target4"))); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-foo_cc.o")); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-bar_c.o")); } { auto tasks = getTasks(settings, {"target1", "target3"}); uASSERT_EQUAL(4u, tasks.size()); - uASSERT_EQUAL(1u, count(tasks, "target1"s)); - uASSERT_EQUAL(1u, count(tasks, "target3"s)); - uASSERT_EQUAL(1u, count(tasks, "test/target1-foo_cc.o"s)); - uASSERT_EQUAL(1u, count(tasks, "test/target1-bar_c.o"s)); + uASSERT_EQUAL(1u, count(tasks, fs("target1"))); + uASSERT_EQUAL(1u, count(tasks, fs("target3"))); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-foo_cc.o")); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-bar_c.o")); } { auto tasks = getTasks(settings, {"no-such-target"}); @@ -139,119 +159,170 @@ public: void getNextTask_test() { using namespace std::string_literals; - Settings settings{}; + ctor::settings settings{}; { // Zero (Empty) - std::set<std::shared_ptr<Task>> allTasks; - std::set<std::shared_ptr<Task>> dirtyTasks; + std::vector<std::shared_ptr<Task>> allTasks; + std::vector<std::shared_ptr<Task>> dirtyTasks; for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(nullptr, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(nullptr, getNextTask({}, allTasks, dirtyTasks)); } { // Zero (One task, no dirty) - auto task1 = std::make_shared<TestTask>("task1", false); + ctor::build_configuration config; + auto task1 = std::make_shared<TestTask>(config, settings, "task1", false); - std::set<std::shared_ptr<Task>> allTasks; - allTasks.insert(task1); + std::vector<std::shared_ptr<Task>> allTasks; + allTasks.push_back(task1); - std::set<std::shared_ptr<Task>> dirtyTasks; + std::vector<std::shared_ptr<Task>> dirtyTasks; for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(nullptr, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(nullptr, getNextTask({}, allTasks, dirtyTasks)); } { // One (One task, one dirty) - auto task1 = std::make_shared<TestTask>("task1", true); + ctor::build_configuration config; + auto task1 = std::make_shared<TestTask>(config, settings, "task1", true); - std::set<std::shared_ptr<Task>> allTasks; - allTasks.insert(task1); + std::vector<std::shared_ptr<Task>> allTasks; + allTasks.push_back(task1); - std::set<std::shared_ptr<Task>> dirtyTasks; - dirtyTasks.insert(task1); + std::vector<std::shared_ptr<Task>> dirtyTasks; + dirtyTasks.push_back(task1); for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(task1, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(task1, getNextTask({}, allTasks, dirtyTasks)); uASSERT_EQUAL(0u, dirtyTasks.size()); } { // One (Two tasks, one dirty) - auto task1 = std::make_shared<TestTask>("task1", false); - auto task2 = std::make_shared<TestTask>("task2", true); + ctor::build_configuration config; + auto task1 = std::make_shared<TestTask>(config, settings, "task1", false); + auto task2 = std::make_shared<TestTask>(config, settings, "task2", true); - std::set<std::shared_ptr<Task>> allTasks; - allTasks.insert(task1); - allTasks.insert(task2); + std::vector<std::shared_ptr<Task>> allTasks; + allTasks.push_back(task1); + allTasks.push_back(task2); - std::set<std::shared_ptr<Task>> dirtyTasks; - dirtyTasks.insert(task2); + std::vector<std::shared_ptr<Task>> dirtyTasks; + dirtyTasks.push_back(task2); for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(task2, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(task2, getNextTask({}, allTasks, dirtyTasks)); uASSERT_EQUAL(0u, dirtyTasks.size()); } { // One (Two tasks, one dirty which depends on the other) - auto task1 = std::make_shared<TestTask>("task1", false); + ctor::build_configuration config; + auto task1 = std::make_shared<TestTask>(config, settings, "task1", false); std::vector<std::string> deps = {"task1"}; - auto task2 = std::make_shared<TestTask>("task2", true, deps); + auto task2 = + std::make_shared<TestTask>(config, settings, "task2", true, deps); - std::set<std::shared_ptr<Task>> allTasks; - allTasks.insert(task1); - allTasks.insert(task2); + std::vector<std::shared_ptr<Task>> allTasks; + allTasks.push_back(task1); + allTasks.push_back(task2); - std::set<std::shared_ptr<Task>> dirtyTasks; - dirtyTasks.insert(task2); + std::vector<std::shared_ptr<Task>> dirtyTasks; + dirtyTasks.push_back(task2); for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(task2, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(task2, getNextTask({}, allTasks, dirtyTasks)); uASSERT_EQUAL(0u, dirtyTasks.size()); } { // One (Two tasks, Both dirty, one depends on the other) - auto task1 = std::make_shared<TestTask>("task1", true); + ctor::build_configuration config{}; + auto task1 = std::make_shared<TestTask>(config, settings, "task1", true); std::vector<std::string> deps = {"task1"}; - auto task2 = std::make_shared<TestTask>("task2", true, deps); + auto task2 = + std::make_shared<TestTask>(config, settings, "task2", true, deps); - std::set<std::shared_ptr<Task>> allTasks; - allTasks.insert(task2); - allTasks.insert(task1); + std::vector<std::shared_ptr<Task>> allTasks; + allTasks.push_back(task2); + allTasks.push_back(task1); - std::set<std::shared_ptr<Task>> dirtyTasks; - dirtyTasks.insert(task2); - dirtyTasks.insert(task1); + std::vector<std::shared_ptr<Task>> dirtyTasks; + dirtyTasks.push_back(task2); + dirtyTasks.push_back(task1); for(auto& task : dirtyTasks) { uASSERT_EQUAL(0, task->registerDepTasks(allTasks)); } - uASSERT_EQUAL(task1, getNextTask(allTasks, dirtyTasks)); + uASSERT_EQUAL(task1, getNextTask({}, allTasks, dirtyTasks)); uASSERT_EQUAL(1u, dirtyTasks.size()); } + } + + void comparison_test() + { + test_global::toolchain = ctor::toolchain::gcc; + test_global::arch = ctor::arch::unix; + using namespace std::string_literals; + ctor::settings settings{ .builddir = "foo" }; + { // Test Task::operator== + auto tasks = getTasks(settings, {"target1"}); + uASSERT_EQUAL(3u, tasks.size()); + uASSERT_EQUAL(1u, count(tasks, fs("target1"))); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-foo_cc.o")); + uASSERT_EQUAL(1u, count(tasks, fs("test")/"target1-bar_c.o")); + + int cnt1{}; + int cnt2{}; + int cnt3{}; + for(const auto& task : tasks) + { + if(task->target() == fs("target1")) + { + ++cnt1; + uASSERT(*task == "target1"); + uASSERT(*task == "Target1"); + } + if(task->target() == fs("test")/"target1-foo_cc.o") + { + ++cnt2; + uASSERT(*task != "target1"); + uASSERT(*task != "Target1"); + } + if(task->target() == fs("test")/"target1-bar_c.o") + { + ++cnt3; + uASSERT(*task != "target1"); + uASSERT(*task != "Target1"); + } + } + // Assert that we did actually perform all three tests exactly once + uASSERT_EQUAL(1, cnt1); + uASSERT_EQUAL(1, cnt2); + uASSERT_EQUAL(1, cnt3); + } } }; diff --git a/test/testprog.cc b/test/testprog.cc new file mode 100644 index 0000000..93edc3f --- /dev/null +++ b/test/testprog.cc @@ -0,0 +1,55 @@ +#include <iostream> +#include <fstream> +#include <string> +#include <csignal> + +extern char **environ; + +int main(int argc, const char* argv[]) +{ + if(argc < 2) + { + return 0; + } + + std::string cmd = argv[1]; + + if(cmd == "envdump") + { + if(argc < 3) + { + return 0; + } + std::ofstream ostrm(argv[2], std::ios::binary); + for(auto current = environ; *current; ++current) + { + ostrm << (*current) << "\n"; + } + } + + if(cmd == "retval") + { + if(argc < 3) + { + return 0; + } + return std::stoi(argv[2]); + } + + if(cmd == "abort") + { + abort(); + } + + if(cmd == "segfault") + { + raise(SIGSEGV); + } + + if(cmd == "throw") + { + throw "ouch"; + } + + return 0; +} diff --git a/test/tmpfile.h b/test/tmpfile.h new file mode 100644 index 0000000..0f83a20 --- /dev/null +++ b/test/tmpfile.h @@ -0,0 +1,38 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <cstdio> + +class TmpFile +{ +public: + TmpFile(const std::string& data = {}) + { + auto tmp_dir = std::filesystem::temp_directory_path(); + auto tmp_file_template = tmp_dir / "ctor_tmp_file-"; + std::FILE* fp{nullptr}; + int counter{}; + while(!fp) + { + filename = tmp_file_template.string() + std::to_string(counter++); + fp = std::fopen(filename.data(), "wx"); + } + std::fwrite(data.data(), data.size(), 1, fp); + std::fclose(fp); + } + + ~TmpFile() + { + std::filesystem::remove(filename); + } + + const std::string& get() const + { + return filename; + } + +private: + std::string filename; +}; diff --git a/test/tools_test.cc b/test/tools_test.cc new file mode 100644 index 0000000..5ae04c3 --- /dev/null +++ b/test/tools_test.cc @@ -0,0 +1,927 @@ +#include <vector> +#include <string> +#include <ostream> +#include <initializer_list> +#include <cassert> + +#include <tools.h> + +std::ostream& operator<<(std::ostream& stream, const ctor::toolchain& toolchain) +{ + switch(toolchain) + { + case ctor::toolchain::none: + stream << "ctor::toolchain::none"; + break; + case ctor::toolchain::any: + stream << "ctor::toolchain::any"; + break; + case ctor::toolchain::gcc: + stream << "ctor::toolchain::gcc"; + break; + case ctor::toolchain::clang: + stream << "ctor::toolchain::clang"; + break; + } + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const std::vector<std::string>& vs) +{ + bool first{true}; + stream << "{ "; + for(const auto& v : vs) + { + if(!first) + { + stream << ", "; + } + stream << "'" << v << "'"; + first = false; + } + stream << " }"; + + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::c_flag& flag) +{ + stream << "{" << flag.opt << ", \"" << flag.arg << "\", " << flag.toolchain << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::cxx_flag& flag) +{ + stream << "{" << flag.opt << ", \"" << flag.arg << "\", " << flag.toolchain << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::ld_flag& flag) +{ + stream << "{" << flag.opt << ", \"" << flag.arg << "\", " << flag.toolchain << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::ar_flag& flag) +{ + stream << "{" << flag.opt << ", \"" << flag.arg << "\", " << flag.toolchain << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ctor::asm_flag& flag) +{ + stream << "{" << flag.opt << ", \"" << flag.arg << "\", " << flag.toolchain << "}"; + return stream; +} + +bool operator!=(const ctor::c_flag& a, const ctor::c_flag& b) +{ + return + a.opt != b.opt || + a.arg != b.arg || + a.toolchain != b.toolchain; +} + +bool operator!=(const ctor::cxx_flag& a, const ctor::cxx_flag& b) +{ + return + a.opt != b.opt || + a.arg != b.arg || + a.toolchain != b.toolchain; +} +bool operator!=(const ctor::ld_flag& a, const ctor::ld_flag& b) +{ + return + a.opt != b.opt || + a.arg != b.arg || + a.toolchain != b.toolchain; +} + +bool operator!=(const ctor::ar_flag& a, const ctor::ar_flag& b) +{ + return + a.opt != b.opt || + a.arg != b.arg || + a.toolchain != b.toolchain; +} +bool operator!=(const ctor::asm_flag& a, const ctor::asm_flag& b) +{ + return + a.opt != b.opt || + a.arg != b.arg || + a.toolchain != b.toolchain; +} + +#include <uunit.h> + +const ctor::configuration& ctor::get_configuration() +{ + static ctor::configuration cfg; + return cfg; +} + +std::string ctor::configuration::get(const std::string& key, [[maybe_unused]]const std::string& default_value) const +{ + if(key == ctor::cfg::host_cxx) + { + return {}; + } + + if(key == ctor::cfg::build_cxx) + { + return {}; + } + + assert(false); // bad key + + return {}; +} + +class ToolsTest + : public uUnit +{ +public: + ToolsTest() + { + uTEST(ToolsTest::getToolChain_test); + + uTEST(ToolsTest::getOption_toolchain_c_test); + uTEST(ToolsTest::getOption_toolchain_cxx_test); + uTEST(ToolsTest::getOption_toolchain_ld_test); + uTEST(ToolsTest::getOption_toolchain_ar_test); + uTEST(ToolsTest::getOption_toolchain_asm_test); + + uTEST(ToolsTest::getOption_str_c_test); + uTEST(ToolsTest::getOption_str_cxx_test); + uTEST(ToolsTest::getOption_str_ld_test); + uTEST(ToolsTest::getOption_str_ar_test); + uTEST(ToolsTest::getOption_str_asm_test); + + uTEST(ToolsTest::to_strings_c_test); + uTEST(ToolsTest::to_strings_cxx_test); + uTEST(ToolsTest::to_strings_ld_test); + uTEST(ToolsTest::to_strings_ar_test); + uTEST(ToolsTest::to_strings_asm_test); + } + + void getToolChain_test() + { + // + // gcc + // + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/gcc")); + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/gcc-10")); + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/x86_64-pc-linux-gnu-g++-9.3.0")); + + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/g++")); + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/g++-10")); + uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/x86_64-pc-linux-gnu-g++-9.3.0")); + + // + // clang + // + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang")); + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang-16")); + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/lib/llvm/16/bin/i686-pc-linux-gnu-clang-16")); + + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang++")); + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang++-16")); + uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/lib/llvm/16/bin/i686-pc-linux-gnu-clang++-16")); + } + + + void getOption_toolchain_c_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // + // gcc + // + exp = { "-o", "foo" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-g" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-MMD" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "-Ifoo" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::c_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Ofoo" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = c_option(ctor::toolchain::gcc, ctor::c_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { "-o", "foo" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-g" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-MMD" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "-Ifoo" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::c_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Ofoo" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = c_option(ctor::toolchain::clang, ctor::c_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // any + // + exp = { "{ctor::c_opt::output, \"foo\"}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::debug}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::warn_all}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::warnings_as_errors}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::generate_dep_tree}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::no_link}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::include_path, \"foo\"}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::c_std, \"foo\"}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::c_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::optimization, \"foo\"}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::position_independent_code}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::position_independent_executable}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::c_opt::custom, \"-foo\"}" }; + act = c_option(ctor::toolchain::any, ctor::c_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + } + + void getOption_toolchain_cxx_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // + // gcc + // + exp = { "-o", "foo" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-g" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-MMD" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "-Ifoo" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Ofoo" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = cxx_option(ctor::toolchain::gcc, ctor::cxx_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { "-o", "foo" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-g" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-MMD" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "-Ifoo" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Ofoo" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = cxx_option(ctor::toolchain::clang, ctor::cxx_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // any + // + exp = { "{ctor::cxx_opt::output, \"foo\"}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::debug}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::debug); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::warn_all}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::warnings_as_errors}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::generate_dep_tree}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::generate_dep_tree); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::no_link}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::no_link); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::include_path, \"foo\"}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::include_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::cpp_std, \"foo\"}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::optimization, \"foo\"}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::optimization, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::position_independent_code}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::position_independent_executable}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::cxx_opt::custom, \"-foo\"}" }; + act = cxx_option(ctor::toolchain::any, ctor::cxx_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + } + + void getOption_toolchain_ld_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // + // gcc + // + exp = { "-o", "foo" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-Lfoo" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::library_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-lfoo" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::link, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-shared" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::build_shared); + uASSERT_EQUAL(exp, act); + + exp = { "-pthread" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::threads); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = ld_option(ctor::toolchain::gcc, ctor::ld_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { "-o", "foo" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-Wall" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "-Werror" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "-Lfoo" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::library_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-lfoo" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::link, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-std=foo" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-shared" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::build_shared); + uASSERT_EQUAL(exp, act); + + exp = { "-pthread" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::threads); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIC" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "-fPIE" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = ld_option(ctor::toolchain::clang, ctor::ld_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // any + // + exp = { "{ctor::ld_opt::output, \"foo\"}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::warn_all}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::warn_all); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::warnings_as_errors}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::warnings_as_errors); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::library_path, \"foo\"}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::library_path, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::link, \"foo\"}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::link, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::cpp_std, \"foo\"}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::cpp_std, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::build_shared}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::build_shared); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::threads}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::threads); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::position_independent_code}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::position_independent_code); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::position_independent_executable}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::position_independent_executable); + uASSERT_EQUAL(exp, act); + + exp = { "{ctor::ld_opt::custom, \"-foo\"}" }; + act = ld_option(ctor::toolchain::any, ctor::ld_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + } + + void getOption_toolchain_ar_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // + // gcc + // + exp = { "-r" }; + act = ar_option(ctor::toolchain::gcc, ctor::ar_opt::replace); + uASSERT_EQUAL(exp, act); + + exp = { "-s" }; + act = ar_option(ctor::toolchain::gcc, ctor::ar_opt::add_index); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = ar_option(ctor::toolchain::gcc, ctor::ar_opt::create); + uASSERT_EQUAL(exp, act); + + exp = { "foo" }; + act = ar_option(ctor::toolchain::gcc, ctor::ar_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = ar_option(ctor::toolchain::gcc, ctor::ar_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { "-r" }; + act = ar_option(ctor::toolchain::clang, ctor::ar_opt::replace); + uASSERT_EQUAL(exp, act); + + exp = { "-s" }; + act = ar_option(ctor::toolchain::clang, ctor::ar_opt::add_index); + uASSERT_EQUAL(exp, act); + + exp = { "-c" }; + act = ar_option(ctor::toolchain::clang, ctor::ar_opt::create); + uASSERT_EQUAL(exp, act); + + exp = { "foo" }; + act = ar_option(ctor::toolchain::clang, ctor::ar_opt::output, "foo"); + uASSERT_EQUAL(exp, act); + + exp = { "-foo" }; + act = ar_option(ctor::toolchain::clang, ctor::ar_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // any + // + exp = { "{ctor::ar_opt::custom, \"-foo\"}" }; + act = ar_option(ctor::toolchain::any, ctor::ar_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); +} + + void getOption_toolchain_asm_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // + // gcc + // + exp = { "-foo" }; + act = asm_option(ctor::toolchain::gcc, ctor::asm_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { "-foo" }; + act = asm_option(ctor::toolchain::clang, ctor::asm_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + + // + // any + // + exp = { "{ctor::asm_opt::custom, \"-foo\"}" }; + act = asm_option(ctor::toolchain::any, ctor::asm_opt::custom, "-foo"); + uASSERT_EQUAL(exp, act); + } + + + void getOption_str_c_test() + { + ctor::c_flag exp(""); + ctor::c_flag act(""); + + // + // gcc + // + exp = { ctor::c_opt::include_path, "foo" }; + act = c_option("-Ifoo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + exp = { ctor::c_opt::custom, "foo" }; + act = c_option("foo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { ctor::c_opt::include_path, "foo" }; + act = c_option("-Ifoo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + + exp = { ctor::c_opt::custom, "foo" }; + act = c_option("foo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + } + + void getOption_str_cxx_test() + { + ctor::cxx_flag exp(""); + ctor::cxx_flag act(""); + + // + // gcc + // + exp = { ctor::cxx_opt::include_path, "foo" }; + act = cxx_option("-Ifoo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + exp = { ctor::cxx_opt::custom, "foo" }; + act = cxx_option("foo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { ctor::cxx_opt::include_path, "foo" }; + act = cxx_option("-Ifoo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + + exp = { ctor::cxx_opt::custom, "foo" }; + act = cxx_option("foo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + } + + void getOption_str_ld_test() + { + ctor::ld_flag exp(""); + ctor::ld_flag act(""); + + // + // gcc + // + exp = { ctor::ld_opt::library_path, "foo" }; + act = ld_option("-Lfoo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + exp = { ctor::ld_opt::custom, "foo" }; + act = ld_option("foo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { ctor::ld_opt::library_path, "foo" }; + act = ld_option("-Lfoo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + + exp = { ctor::ld_opt::custom, "foo" }; + act = ld_option("foo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + } + + void getOption_str_ar_test() + { + ctor::ar_flag exp(""); + ctor::ar_flag act(""); + + // + // gcc + // + exp = { ctor::ar_opt::custom, "foo" }; + act = ar_option("foo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { ctor::ar_opt::custom, "foo" }; + act = ar_option("foo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + } + + void getOption_str_asm_test() + { + ctor::asm_flag exp(""); + ctor::asm_flag act(""); + + // + // gcc + // + exp = { ctor::asm_opt::custom, "foo" }; + act = asm_option("foo", ctor::toolchain::gcc); + uASSERT_EQUAL(exp, act); + + // + // clang + // + exp = { ctor::asm_opt::custom, "foo" }; + act = asm_option("foo", ctor::toolchain::clang); + uASSERT_EQUAL(exp, act); + } + + + void to_strings_c_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // Mismatching toolchain (required vs actual) results in no output + // otherwise to_strings is just a proxy for c_option + act = to_strings(ctor::toolchain::gcc, {ctor::toolchain::clang, ctor::c_opt::no_link}); + uASSERT_EQUAL(exp, act); + } + + void to_strings_cxx_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // Mismatching toolchain (required vs actual) results in no output + // otherwise to_strings is just a proxy for cxx_option + act = to_strings(ctor::toolchain::gcc, {ctor::toolchain::clang, ctor::cxx_opt::no_link}); + uASSERT_EQUAL(exp, act); + } + + void to_strings_ld_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // Mismatching toolchain (required vs actual) results in no output + // otherwise to_strings is just a proxy for ld_option + act = to_strings(ctor::toolchain::gcc, {ctor::toolchain::clang, ctor::ld_opt::threads}); + uASSERT_EQUAL(exp, act); + } + + void to_strings_ar_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // Mismatching toolchain (required vs actual) results in no output + // otherwise to_strings is just a proxy for ar_option + act = to_strings(ctor::toolchain::gcc, {ctor::toolchain::clang, ctor::ar_opt::custom, "foo"}); + uASSERT_EQUAL(exp, act); + } + + void to_strings_asm_test() + { + std::vector<std::string> exp; + std::vector<std::string> act; + + // Mismatching toolchain (required vs actual) results in no output + // otherwise to_strings is just a proxy for asm_option + act = to_strings(ctor::toolchain::gcc, {ctor::toolchain::clang, ctor::asm_opt::custom, "foo"}); + uASSERT_EQUAL(exp, act); + } +}; + +// Registers the fixture into the 'registry' +static ToolsTest test; diff --git a/test/uunit b/test/uunit -Subproject bc078da645412c6b36ef59e635d6c35d11088c9 +Subproject 0f371777e02dd068f9675a05a29230221d5d6a7 |