瀏覽代碼

Generate archive libraries of dependencies

default_compile_flags
vector-of-bool 5 年之前
父節點
當前提交
52138dc6b6
共有 3 個檔案被更改,包括 75 行新增18 行删除
  1. +59
    -10
      src/dds/build/plan.cpp
  2. +12
    -7
      src/dds/build/plan.hpp
  3. +4
    -1
      src/dds/ddslim.main.cpp

+ 59
- 10
src/dds/build/plan.cpp 查看文件

@@ -1,5 +1,7 @@
#include "./plan.hpp"

#include <dds/proc.hpp>

#include <range/v3/action/join.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/join.hpp>
@@ -9,15 +11,17 @@

#include <spdlog/spdlog.h>

#include <chrono>
#include <mutex>
#include <thread>

using namespace dds;

library_plan library_plan::create(const library& lib, const library_build_params& params) {
std::vector<compile_file_plan> compile_files;
std::vector<create_archive_plan> create_archives;
std::vector<create_exe_plan> link_executables;
std::vector<compile_file_plan> compile_files;
std::vector<create_exe_plan> link_executables;
std::optional<create_archive_plan> create_archive;
bool should_create_archive = false;

std::vector<source_file> app_sources;
std::vector<source_file> test_sources;
@@ -43,6 +47,7 @@ library_plan library_plan::create(const library& lib, const library_build_params
} else if (sfile.kind == source_kind::app) {
app_sources.push_back(sfile);
} else {
should_create_archive = true;
lib_sources.push_back(sfile);
}
}
@@ -52,17 +57,14 @@ library_plan library_plan::create(const library& lib, const library_build_params
spdlog::critical("Apps/tests not implemented on this code path");
}

if (!lib_sources.empty()) {
if (should_create_archive) {
create_archive_plan ar_plan;
ar_plan.in_sources = lib_sources //
| ranges::views::transform([](auto&& sf) { return sf.path; }) //
| ranges::to_vector;
ar_plan.name = lib.name();
ar_plan.out_dir = params.out_subdir;
create_archives.push_back(std::move(ar_plan));
create_archive.emplace(std::move(ar_plan));
}

return library_plan{compile_files, create_archives, link_executables, params.out_subdir};
return library_plan{params.out_subdir, compile_files, create_archive, link_executables};
}

namespace {
@@ -121,6 +123,35 @@ bool parallel_run(Range&& rng, int n_jobs, Fn&& fn) {

} // namespace

fs::path create_archive_plan::archive_file_path(const toolchain& tc) const noexcept {
fs::path fname = fmt::format("{}{}{}", "lib", name, tc.archive_suffix());
return fname;
}

void create_archive_plan::archive(const toolchain& tc,
path_ref out_prefix,
const std::vector<fs::path>& objects) const {
archive_spec ar;
ar.input_files = objects;
ar.out_path = out_prefix / archive_file_path(tc);
auto ar_cmd = tc.create_archive_command(ar);
auto out_relpath = fs::relative(ar.out_path, out_prefix).string();

spdlog::info("[{}] Archive: {}", name, out_relpath);
auto start_time = std::chrono::steady_clock::now();
auto ar_res = run_proc(ar_cmd);
auto end_time = std::chrono::steady_clock::now();
auto dur_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
spdlog::info("[{}] Archive: {} - {:n}ms", name, out_relpath, dur_ms.count());

if (!ar_res.okay()) {
spdlog::error("Creating static library archive failed: {}", out_relpath);
spdlog::error("Subcommand FAILED: {}\n{}", quote_command(ar_cmd), ar_res.output);
throw std::runtime_error(
fmt::format("Creating archive [{}] failed for '{}'", out_relpath, name));
}
}

void build_plan::compile_all(const toolchain& tc, int njobs, path_ref out_prefix) const {
std::vector<std::pair<fs::path, std::reference_wrapper<const compile_file_plan>>> comps;
for (const auto& lib : create_libraries) {
@@ -137,4 +168,22 @@ void build_plan::compile_all(const toolchain& tc, int njobs, path_ref out_prefix
if (!okay) {
throw std::runtime_error("Compilation failed.");
}
}
}

void build_plan::archive_all(const toolchain& tc, int njobs, path_ref out_prefix) const {
parallel_run(create_libraries, njobs, [&](const library_plan& lib) {
if (!lib.create_archive) {
return;
}
const auto& objects
= ranges::views::all(lib.compile_files) //
| ranges::views::filter(
[](auto&& comp) { return comp.source.kind == source_kind::source; }) //
| ranges::views::transform([&](auto&& comp) {
return out_prefix / lib.out_subdir / comp.get_object_file_path(tc);
}) //
| ranges::to_vector //
;
lib.create_archive->archive(tc, out_prefix, objects);
});
}

+ 12
- 7
src/dds/build/plan.hpp 查看文件

@@ -10,9 +10,13 @@
namespace dds {

struct create_archive_plan {
std::vector<fs::path> in_sources;
std::string name;
fs::path out_dir;
std::string name;
fs::path out_dir;

fs::path archive_file_path(const toolchain& tc) const noexcept;

void
archive(const toolchain& tc, path_ref out_prefix, const std::vector<fs::path>& objects) const;
};

struct create_exe_plan {
@@ -22,10 +26,10 @@ struct create_exe_plan {
};

struct library_plan {
std::vector<compile_file_plan> compile_files;
std::vector<create_archive_plan> create_archives;
std::vector<create_exe_plan> link_executables;
fs::path out_subdir;
fs::path out_subdir;
std::vector<compile_file_plan> compile_files;
std::optional<create_archive_plan> create_archive;
std::vector<create_exe_plan> link_executables;

static library_plan create(const library& lib, const library_build_params& params);
};
@@ -39,6 +43,7 @@ struct build_plan {
}

void compile_all(const toolchain& tc, int njobs, path_ref out_prefix) const;
void archive_all(const toolchain& tc, int njobs, path_ref out_prefix) const;
};

} // namespace dds

+ 4
- 1
src/dds/ddslim.main.cpp 查看文件

@@ -372,7 +372,10 @@ struct cli_deps {
});

auto plan = dds::create_deps_build_plan(deps);
plan.compile_all(tc_filepath.get_toolchain(), 6, build_dir.Get());
auto tc = tc_filepath.get_toolchain();
auto bdir = build_dir.Get();
plan.compile_all(tc, 6, bdir);
plan.archive_all(tc, 6, bdir);
return 0;
}
} build{*this};

Loading…
取消
儲存