Skip to content

Commit

Permalink
Improve build system
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed May 29, 2019
1 parent 20b0577 commit 16f6d6a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 113 deletions.
140 changes: 80 additions & 60 deletions include/poac/core/stroite/core/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ namespace poac::core::stroite::core {

std::map<std::string, YAML::Node> node;
std::map<std::string, std::map<std::string, std::string>> depends_ts;
std::optional<std::map<std::string, YAML::Node>> deps_node;

bool verbose;
bool verbose; // TODO: 一旦依存関係を集めて,一気に変換だとメモリが保たない.なので,インクリメンタルに変換するのが良い.


std::vector<std::string>
Expand All @@ -66,6 +65,33 @@ namespace poac::core::stroite::core {
return core::cache::check_src_cpp(compile_conf, depends_ts, source_files, verbose);
}

void make_include_search_path() { // TODO: hashチェック時の大量の文字列配列が恐らくキツイ.
namespace fs = boost::filesystem;
namespace lock = deper::lock;
namespace yaml = io::file::yaml;
namespace path = io::file::path;

if (const auto locked_deps = lock::load_ignore_timestamp()) {
for (const auto& [name, dep] : locked_deps->backtracked) {
const std::string current_package_name = naming::to_current(dep.source, name, dep.version);
const fs::path include_dir = path::current_deps_dir / current_package_name / "include";

if (path::validate_dir(include_dir)) {
compile_conf.include_search_path.push_back(include_dir.string());
}
else {
throw except::error(
name + " is not installed.\n"
"Please build after running `poac install`");
}
}
}
else {
throw except::error(
"Could not load poac.lock.\n"
"Please build after running `poac install`");
}
}
void configure_compile(const bool usemain)
{
namespace yaml = io::file::yaml;
Expand All @@ -74,17 +100,16 @@ namespace poac::core::stroite::core {
compile_conf.system = compiler;
compile_conf.version_prefix = utils::options::default_version_prefix();
compile_conf.cpp_version = yaml::get_with_throw<unsigned int>(node.at("cpp_version"));
compile_conf.include_search_path = utils::options::make_include_search_path(static_cast<bool>(deps_node));
// compile_conf.include_search_path = utils::options::make_include_search_path(exist_deps_key);
compile_conf.other_args = utils::options::make_compile_other_args(node);
compile_conf.source_files = hash_source_files(search::cpp(base_dir), usemain);
compile_conf.macro_defns = utils::options::make_macro_defns(node);
compile_conf.base_dir = base_dir;
compile_conf.output_root = path::current_build_cache_obj_dir;
}
std::optional<std::vector<std::string>>
_compile() {
compile() {
namespace fs = boost::filesystem;
namespace io = io::file;

for (const auto& s : compile_conf.source_files) {
// sourceファイルを一つづつコンパイルする.
Expand All @@ -98,7 +123,7 @@ namespace poac::core::stroite::core {
output_string += file_name + ": " + hash + "\n";
}
fs::create_directories(fs::path(hash_name).parent_path());
io::path::write_to_file(ofs, hash_name, output_string);
io::file::path::write_to_file(ofs, hash_name, output_string);
}

}
Expand All @@ -120,108 +145,103 @@ namespace poac::core::stroite::core {
return obj_files;
}

std::vector<std::string>
make_link_other_args() {
namespace yaml = io::file::yaml;
if (const auto link_args = yaml::get<std::vector<std::string>>(node.at("build"), "link_args")) {
return *link_args;
}
else {
return {};
}
}
auto make_link() {
void make_link(const std::map<std::string, YAML::Node>& deps_node) {
namespace fs = boost::filesystem;
namespace yaml = io::file::yaml;

std::vector<std::string> library_search_path;
std::vector<std::string> static_link_libs;
std::vector<std::string> library_path;

if (deps_node) {
for (const auto& [name, next_node] : *deps_node) {
const auto[src, name2] = naming::get_source(name);
const std::string version = naming::get_version(next_node, src);
for (const auto& [raw_name, next_node] : deps_node) {
const auto [src, name] = naming::get_source(raw_name);
const std::string version = naming::get_version(next_node, src);

// FIXME: srcではなく,build systemを読む.
if (src != "poac") {
const std::string pkgname = naming::to_cache(src, name2, version);
const fs::path pkgpath = io::file::path::current_deps_dir / pkgname;
// FIXME: srcではなく,build systemを読む.
if (src != "poac") {
const std::string caching_name = naming::to_cache(src, name, version); // TODO: これ,なんで,cacheなのに,
const fs::path pkgpath = io::file::path::current_deps_dir / caching_name; // TODO: depsを読んでるん???

// TODO: できればlockファイルに書かれたパッケージの./depsディレクトリのpoac.ymlを読むのが好ましい
if (const fs::path lib_dir = pkgpath / "lib"; fs::exists(lib_dir)) {
library_search_path.push_back(lib_dir.string());
// TODO: できればlockファイルに書かれたパッケージの./depsディレクトリのpoac.ymlを読むのが好ましい
if (const fs::path lib_dir = pkgpath / "lib"; fs::exists(lib_dir)) {
link_conf.library_search_path.push_back(lib_dir.string());

if (const auto link = yaml::get<std::vector<std::string>>(next_node, "link", "include")) {
for (const auto& l : *link) {
static_link_libs.push_back(l);
}
}
else {
static_link_libs.push_back(pkgname);
if (const auto link = yaml::get<std::vector<std::string>>(next_node, "link", "include")) {
for (const auto& l : *link) {
link_conf.static_link_libs.push_back(l);
}
}
else {
link_conf.static_link_libs.push_back(caching_name);
}
}
}
}
return std::make_tuple(library_search_path, static_link_libs, library_path);
}
void configure_link(const std::vector<std::string>& obj_files_path)
void configure_link(const std::vector<std::string>& obj_files_path) // TODO: obj_files_path以外は,インスタンス時に作れるからメモリの無駄遣いにならない.
{
namespace yaml = io::file::yaml;

link_conf.obj_files_path = obj_files_path;

link_conf.system = compiler;
link_conf.project_name = project_name;
link_conf.output_root = io::file::path::current_build_bin_dir;
link_conf.obj_files_path = obj_files_path;
const auto links = make_link();
link_conf.library_search_path = std::get<0>(links);
link_conf.static_link_libs = std::get<1>(links);
link_conf.library_path = std::get<2>(links);
link_conf.other_args = make_link_other_args();
// make_link();
// link_conf.library_search_path = std::get<0>(links);
// link_conf.static_link_libs = std::get<1>(links);
// link_conf.library_path = std::get<2>(links);
if (const auto link_args = yaml::get<std::vector<std::string>>(node.at("build"), "link_args")) {
link_conf.other_args = *link_args;
}
}
auto _link()
auto link()
{
return core::compiler::link(link_conf, verbose);
}

void configure_static_lib(const std::vector<std::string>& obj_files_path)
{
namespace io = io::file;
static_lib_conf.project_name = project_name;
static_lib_conf.output_root = io::path::current_build_lib_dir;
static_lib_conf.output_root = io::file::path::current_build_lib_dir;
static_lib_conf.obj_files_path = obj_files_path;
}
auto _gen_static_lib()
auto gen_static_lib()
{
return core::compiler::gen_static_lib(static_lib_conf, verbose);
}

void configure_dynamic_lib(const std::vector<std::string>& obj_files_path)
{
namespace io = io::file;
dynamic_lib_conf.system = compiler;
dynamic_lib_conf.project_name = project_name;
// outputを一箇所か分散か選べるように.boost::hoghoeみたいに,enumのオプションを渡すとOK
// 一箇所ってのは,./ poac build -> ./_buildだけど,depsも./_buildに配置されるやつ
dynamic_lib_conf.output_root = io::path::current_build_lib_dir;
dynamic_lib_conf.output_root = io::file::path::current_build_lib_dir;
dynamic_lib_conf.obj_files_path = obj_files_path;
}
auto _gen_dynamic_lib()
auto gen_dynamic_lib()
{
return core::compiler::gen_dynamic_lib(dynamic_lib_conf, verbose);
}

// TODO: poac.ymlのhashもcheckしてほしい
// TODO: poac.ymlのhashもcheck
// TODO: 自らのinclude,dirも,(存在するなら!) includeパスに渡してほしい.そうすると,poacでinclude<poac/poac.hpp>できる
explicit builder(const bool verbose, const boost::filesystem::path& base_path=boost::filesystem::current_path())
// TODO: この段階で,どこまでするのかが分かれば,コンパイルしないのに,コンパイル用の設定を生成した,とかが無くなって良さそう.
explicit builder(const bool verbose, const boost::filesystem::path& base_dir=boost::filesystem::current_path())
{
namespace yaml = io::file::yaml;

const auto config_file = yaml::load_config_by_dir_with_throw(base_path);
const auto config_file = yaml::load_config_by_dir_with_throw(base_dir);
node = yaml::get_by_width(config_file, "name", "version", "cpp_version", "build");
deps_node = yaml::get<std::map<std::string, YAML::Node>>(config_file, "deps");
project_name = naming::slash_to_hyphen(node.at("name").as<std::string>());


// Create link configure and include search path
if (const auto deps_node = yaml::get<std::map<std::string, YAML::Node>>(config_file, "deps")) {
make_link(*deps_node);
make_include_search_path();
}


compiler = utils::detect::compiler();
base_dir = base_path;
project_name = naming::slash_to_hyphen(node.at("name").as<std::string>());
this->base_dir = base_dir;
this->verbose = verbose;
}
};
Expand Down
33 changes: 0 additions & 33 deletions include/poac/core/stroite/utils/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,39 +154,6 @@ namespace poac::core::stroite::utils::options {
return macro_defns;
}

std::vector<std::string>
make_include_search_path(const bool exist_deps_key) {
namespace fs = boost::filesystem;
namespace lock = deper::lock;
namespace yaml = io::file::yaml;
namespace path = io::file::path;

std::vector<std::string> include_search_path;
if (exist_deps_key) { // depsキーが存在する // TODO: subcmd/build.hppで,存在確認が取れている
if (const auto locked_deps = lock::load_ignore_timestamp()) {
for (const auto& [name, dep] : locked_deps->backtracked) {
const std::string current_package_name = naming::to_current(dep.source, name, dep.version);
const fs::path include_dir = path::current_deps_dir / current_package_name / "include";

if (path::validate_dir(include_dir)) {
include_search_path.push_back(include_dir.string());
}
else {
throw except::error(
name + " is not installed.\n"
"Please build after running `poac install`");
}
}
}
else {
throw except::error(
"Could not load poac.lock.\n"
"Please build after running `poac install`");
}
}
return include_search_path;
}

std::vector<std::string>
make_compile_other_args(const std::map<std::string, YAML::Node>& node) {
namespace yaml = io::file::yaml;
Expand Down
12 changes: 6 additions & 6 deletions include/poac/subcmd/build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ namespace poac::subcmd {
for (const auto& l : library_path) {
bs.link_conf.library_path.push_back(l);
}
return handle_compile_message(bs._link());
return handle_compile_message(bs.link());
}
std::optional<std::string>
handle_compile(
core::stroite::core::builder& bs,
const std::vector<std::string>& library_path)
{
if (const auto obj_files_path = bs._compile()) {
if (const auto obj_files_path = bs.compile()) {
return handle_link(bs, *obj_files_path, library_path);
}
else { // Compile failure
Expand All @@ -82,15 +82,15 @@ namespace poac::subcmd {
const std::vector<std::string>& obj_files_path)
{
bs.configure_static_lib(obj_files_path);
return handle_generate_message(bs._gen_static_lib());
return handle_generate_message(bs.gen_static_lib());
}
std::optional<std::string>
handle_generate_dynamic_lib(
core::stroite::core::builder& bs,
const std::vector<std::string>& obj_files_path)
{
bs.configure_dynamic_lib(obj_files_path);
return handle_generate_message(bs._gen_dynamic_lib());
return handle_generate_message(bs.gen_dynamic_lib());
}

void handle_generate_lib(
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace poac::subcmd {
if (bs.compile_conf.source_files.empty()) { // No need for compile and link
return is_exist_lib(bs.project_name);
}
if (auto obj_files_path = bs._compile()) {
if (auto obj_files_path = bs.compile()) {
for (const auto o : *obj_files_path) {
deps_obj_files_path.push_back(o);
}
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace poac::subcmd {
if (!bs.compile_conf.source_files.empty()) {
io::cli::echo(io::cli::to_status(name));

if (const auto obj_files_path = bs._compile()) {
if (const auto obj_files_path = bs.compile()) {
handle_generate_lib(bs, *obj_files_path);
io::cli::echo();
return *obj_files_path;
Expand Down
4 changes: 2 additions & 2 deletions include/poac/subcmd/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ namespace poac::subcmd {
// continue;
}
else {
if (const auto obj_files_path = bs._compile()) {
if (const auto obj_files_path = bs.compile()) {
bs.configure_link(*obj_files_path);
bs.link_conf.project_name = bin_name;
bs.link_conf.output_root = io::file::path::current_build_test_bin_dir;
bs.link_conf.static_link_libs.push_back(static_link_lib);
if (bs._link()) {
if (bs.link()) {
std::cout << io::cli::green << "Compiled: " << io::cli::reset
<< "Output to `" +
fs::relative(bin_path).string() +
Expand Down
6 changes: 6 additions & 0 deletions poac.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Please do not edit this file.
timestamp: 1550874492
dependencies:
jbeder/yaml-cpp:
version: yaml-cpp-0.6.2
source: github
Loading

0 comments on commit 16f6d6a

Please sign in to comment.