Browse Source

toolchain_prep simplifies creation of toolchain objects

default_compile_flags
vector-of-bool 5 years ago
parent
commit
a5251f877e
4 changed files with 93 additions and 26 deletions
  1. +7
    -0
      src/dds/toolchain/prep.cpp
  2. +30
    -0
      src/dds/toolchain/prep.hpp
  3. +38
    -17
      src/dds/toolchain/toolchain.cpp
  4. +18
    -9
      src/dds/toolchain/toolchain.hpp

+ 7
- 0
src/dds/toolchain/prep.cpp View File

#include "./prep.hpp"

#include <dds/toolchain/toolchain.hpp>

using namespace dds;

toolchain toolchain_prep::realize() const { return toolchain::realize(*this); }

+ 30
- 0
src/dds/toolchain/prep.hpp View File

#pragma once

#include <string>
#include <vector>

namespace dds {

class toolchain;

struct toolchain_prep {
using string_seq = std::vector<std::string>;
string_seq c_compile;
string_seq cxx_compile;
string_seq include_template;
string_seq define_template;
string_seq link_archive;
string_seq link_exe;
string_seq warning_flags;

std::string archive_prefix;
std::string archive_suffix;
std::string object_prefix;
std::string object_suffix;
std::string exe_prefix;
std::string exe_suffix;

toolchain realize() const;
};

} // namespace dds

+ 38
- 17
src/dds/toolchain/toolchain.cpp View File

#include "./toolchain.hpp" #include "./toolchain.hpp"


#include <dds/toolchain/prep.hpp>
#include <dds/util/algo.hpp> #include <dds/util/algo.hpp>
#include <dds/util/string.hpp> #include <dds/util/string.hpp>




} // namespace } // namespace


toolchain toolchain::realize(const toolchain_prep& prep) {
toolchain ret;
ret._c_compile = prep.c_compile;
ret._cxx_compile = prep.cxx_compile;
ret._inc_template = prep.include_template;
ret._def_template = prep.define_template;
ret._link_archive = prep.link_archive;
ret._link_exe = prep.link_exe;
ret._warning_flags = prep.warning_flags;
ret._archive_prefix = prep.archive_prefix;
ret._archive_suffix = prep.archive_suffix;
ret._object_prefix = prep.object_prefix;
ret._object_suffix = prep.object_suffix;
ret._exe_prefix = prep.exe_prefix;
ret._exe_suffix = prep.exe_suffix;
return ret;
}

toolchain toolchain::load_from_file(fs::path p) { toolchain toolchain::load_from_file(fs::path p) {
opt_string inc_template; opt_string inc_template;
opt_string def_template; opt_string def_template;
opt_string link_exe_template; opt_string link_exe_template;
opt_string warning_flags; opt_string warning_flags;


opt_string archive_prefix;
opt_string archive_suffix; opt_string archive_suffix;
opt_string object_suffix; opt_string object_suffix;
opt_string exe_suffix; opt_string exe_suffix;
|| try_single("Create-Archive-Template", create_archive_template) || try_single("Create-Archive-Template", create_archive_template)
|| try_single("Link-Executable-Template", link_exe_template) || try_single("Link-Executable-Template", link_exe_template)
|| try_single("Warning-Flags", warning_flags) || try_single("Warning-Flags", warning_flags)
|| try_single("Archive-Prefix", archive_prefix)
|| try_single("Archive-Suffix", archive_suffix) || try_single("Archive-Suffix", archive_suffix)
|| try_single("Object-Suffix", object_suffix) || try_single("Object-Suffix", object_suffix)
|| try_single("Executable-Suffix", exe_suffix) || try_single("Executable-Suffix", exe_suffix)
create_archive_template.value(), create_archive_template.value(),
link_exe_template.value(), link_exe_template.value(),
warning_flags.value_or(""), warning_flags.value_or(""),
archive_prefix.value_or("lib"),
archive_suffix.value(), archive_suffix.value(),
object_suffix.value(), object_suffix.value(),
exe_suffix.value(), exe_suffix.value(),


vector<string> toolchain::create_archive_command(const archive_spec& spec) const noexcept { vector<string> toolchain::create_archive_command(const archive_spec& spec) const noexcept {
vector<string> cmd; vector<string> cmd;
for (auto& arg : _archive_template) {
for (auto& arg : _link_archive) {
if (arg == "<IN>") { if (arg == "<IN>") {
std::transform(spec.input_files.begin(), std::transform(spec.input_files.begin(),
spec.input_files.end(), spec.input_files.end(),


vector<string> toolchain::create_link_executable_command(const link_exe_spec& spec) const noexcept { vector<string> toolchain::create_link_executable_command(const link_exe_spec& spec) const noexcept {
vector<string> cmd; vector<string> cmd;
for (auto& arg : _link_exe_template) {
for (auto& arg : _link_exe) {
if (arg == "<IN>") { if (arg == "<IN>") {
std::transform(spec.inputs.begin(), std::transform(spec.inputs.begin(),
spec.inputs.end(), spec.inputs.end(),
} }


if (starts_with(s, "gcc") || starts_with(s, "clang")) { if (starts_with(s, "gcc") || starts_with(s, "clang")) {
ret._inc_template = {"-isystem", "<PATH>"};
ret._def_template = {"-D", "<DEF>"};
ret._archive_suffix = ".a";
ret._object_suffix = ".o";
ret._warning_flags = {"-Wall", "-Wextra"};
ret._archive_template = {"ar", "rcs", "<OUT>", "<IN>"};
ret._inc_template = {"-isystem", "<PATH>"};
ret._def_template = {"-D", "<DEF>"};
ret._archive_suffix = ".a";
ret._object_suffix = ".o";
ret._warning_flags = {"-Wall", "-Wextra"};
ret._link_archive = {"ar", "rcs", "<OUT>", "<IN>"};


std::vector<std::string> common_flags = { std::vector<std::string> common_flags = {
"<FLAGS>", "<FLAGS>",
extend(ret._cxx_compile, common_flags); extend(ret._cxx_compile, common_flags);
extend(ret._cxx_compile, cxx_flags); extend(ret._cxx_compile, cxx_flags);


ret._link_exe_template.push_back(cxx_compiler_name);
extend(ret._link_exe_template,
ret._link_exe.push_back(cxx_compiler_name);
extend(ret._link_exe,
{ {
"-g", "-g",
"-fPIC", "-fPIC",
std::vector<std::string_view> common_flags = {"/Z7", "/O2", "/MT", "/DEBUG"}; std::vector<std::string_view> common_flags = {"/Z7", "/O2", "/MT", "/DEBUG"};
extend(ret._c_compile, common_flags); extend(ret._c_compile, common_flags);
extend(ret._cxx_compile, common_flags); extend(ret._cxx_compile, common_flags);
ret._archive_suffix = ".lib";
ret._object_suffix = ".obj";
ret._exe_suffix = ".exe";
ret._archive_template = {"lib", "/nologo", "/OUT:<OUT>", "<IN>"};
ret._link_exe_template
= {"cl.exe", "/nologo", "/std:c++latest", "/EHsc", "<IN>", "/Fe<OUT>"};
ret._warning_flags = {"/W4"};
ret._archive_suffix = ".lib";
ret._object_suffix = ".obj";
ret._exe_suffix = ".exe";
ret._link_archive = {"lib", "/nologo", "/OUT:<OUT>", "<IN>"};
ret._link_exe = {"cl.exe", "/nologo", "/std:c++latest", "/EHsc", "<IN>", "/Fe<OUT>"};
ret._warning_flags = {"/W4"};
} else { } else {
return std::nullopt; return std::nullopt;
} }

+ 18
- 9
src/dds/toolchain/toolchain.hpp View File

fs::path output; fs::path output;
}; };


class toolchain_prep;

class toolchain { class toolchain {
using string_seq = std::vector<std::string>; using string_seq = std::vector<std::string>;


string_seq _c_compile;
string_seq _cxx_compile;
string_seq _inc_template;
string_seq _def_template;
string_seq _archive_template;
string_seq _link_exe_template;
string_seq _warning_flags;
string_seq _c_compile;
string_seq _cxx_compile;
string_seq _inc_template;
string_seq _def_template;
string_seq _link_archive;
string_seq _link_exe;
string_seq _warning_flags;

std::string _archive_prefix;
std::string _archive_suffix; std::string _archive_suffix;
std::string _object_prefix;
std::string _object_suffix; std::string _object_suffix;
std::string _exe_prefix;
std::string _exe_suffix; std::string _exe_suffix;


public: public:
std::string_view archive_template, std::string_view archive_template,
std::string_view link_exe_template, std::string_view link_exe_template,
std::string_view warning_flags, std::string_view warning_flags,
std::string_view archive_prefix,
std::string_view archive_suffix, std::string_view archive_suffix,
std::string_view object_suffix, std::string_view object_suffix,
std::string_view exe_suffix) std::string_view exe_suffix)
, _cxx_compile(split_shell_string(cxx_compile)) , _cxx_compile(split_shell_string(cxx_compile))
, _inc_template(split_shell_string(inc_template)) , _inc_template(split_shell_string(inc_template))
, _def_template(split_shell_string(def_template)) , _def_template(split_shell_string(def_template))
, _archive_template(split_shell_string(archive_template))
, _link_exe_template(split_shell_string(link_exe_template))
, _link_archive(split_shell_string(archive_template))
, _link_exe(split_shell_string(link_exe_template))
, _warning_flags(split_shell_string(warning_flags)) , _warning_flags(split_shell_string(warning_flags))
, _archive_prefix(archive_prefix)
, _archive_suffix(archive_suffix) , _archive_suffix(archive_suffix)
, _object_suffix(object_suffix) , _object_suffix(object_suffix)
, _exe_suffix(exe_suffix) {} , _exe_suffix(exe_suffix) {}


static toolchain realize(const toolchain_prep&);
static toolchain load_from_file(fs::path); static toolchain load_from_file(fs::path);


auto& archive_suffix() const noexcept { return _archive_suffix; } auto& archive_suffix() const noexcept { return _archive_suffix; }

Loading…
Cancel
Save