Skip to content

Commit

Permalink
feat(build): allow plugins relative to recipe file (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Ezekiel Warren <[email protected]>
  • Loading branch information
Kelwan and zaucy authored Mar 28, 2024
1 parent 35453db commit 04ae855
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 432 deletions.
27 changes: 4 additions & 23 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions ecsact/cli/commands/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ auto ecsact::cli::detail::build_command( //

auto output_path = fs::path{args.at("--output").asString()};

auto recipe = build_recipe::from_yaml_file(args.at("--recipe").asString());
auto recipe_path = fs::path{args.at("--recipe").asString()};
auto recipe = build_recipe::from_yaml_file(recipe_path);

auto temp_dir = args["--temp_dir"].isString() //
? fs::path{args["--temp_dir"].asString()}
Expand Down Expand Up @@ -171,13 +172,17 @@ auto ecsact::cli::detail::build_command( //
compiler->compiler_version
);

auto cook_options = cook_recipe_options{
.files = file_paths,
.work_dir = work_dir,
.output_path = output_path,
.additional_plugin_dirs = {recipe_path.parent_path()},
};
auto runtime_output_path = cook_recipe(
argv[0],
file_paths,
std::get<build_recipe>(recipe),
*compiler,
work_dir,
output_path
cook_options
);

if(!runtime_output_path) {
Expand Down
17 changes: 9 additions & 8 deletions ecsact/cli/commands/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ package(default_visibility = ["//:__subpackages__"])

cc_library(
name = "build_recipe",
copts = copts,
srcs = ["build_recipe.cc"],
hdrs = ["build_recipe.hh"],
copts = copts,
deps = [
"//ecsact/cli:report",
"//ecsact/cli/commands/build:get_modules",
"@yaml-cpp",
],
)

cc_library(
name = "cc_compiler",
copts = copts,
srcs = ["cc_compiler.cc"],
hdrs = ["cc_compiler.hh"],
copts = copts,
deps = [
":cc_compiler_config",
":cc_compiler_util",
"//ecsact/cli:report",
"@boost.process",
"@magic_enum",
"@nlohmann_json//:json",
"@boost.process",
],
)

cc_library(
name = "cc_compiler_config",
copts = copts,
srcs = ["cc_compiler_config.cc"],
hdrs = ["cc_compiler_config.hh"],
copts = copts,
deps = [
":cc_compiler_util",
"//ecsact/cli:report",
Expand All @@ -43,29 +44,29 @@ cc_library(

cc_library(
name = "cc_compiler_util",
copts = copts,
srcs = ["cc_compiler_util.cc"],
hdrs = ["cc_compiler_util.hh"],
copts = copts,
deps = [
"@boost.process",
],
)

cc_library(
name = "cc_defines_gen",
hdrs = ["cc_defines_gen.hh"],
srcs = ["cc_defines_gen.cc"],
hdrs = ["cc_defines_gen.hh"],
copts = copts,
deps = [
"@boost.algorithm",
":get_modules",
"@boost.algorithm",
],
)

cc_library(
name = "get_modules",
hdrs = ["get_modules.hh"],
srcs = ["get_modules.cc"],
hdrs = ["get_modules.hh"],
copts = copts,
deps = [
"@ecsact_runtime//:core",
Expand Down
4 changes: 3 additions & 1 deletion ecsact/cli/commands/build/build_recipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ranges>
#include "yaml-cpp/yaml.h"
#include "ecsact/cli/commands/build/get_modules.hh"
#include "ecsact/cli/report.hh"

namespace fs = std::filesystem;

Expand Down Expand Up @@ -221,7 +222,8 @@ auto ecsact::build_recipe::from_yaml_file( //
}

return recipe;
} catch(const YAML::BadFile&) {
} catch(const YAML::BadFile& err) {
ecsact::cli::report_error("YAML PARSE: {}", err.what());
return build_recipe_parse_error::bad_file;
}
}
53 changes: 28 additions & 25 deletions ecsact/cli/commands/build/recipe/cook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ static auto download_file(boost::url url, fs::path out_file_path) -> int {
}

static auto handle_source( //
ecsact::build_recipe::source_fetch src,
fs::path work_dir
ecsact::build_recipe::source_fetch src,
const ecsact::cli::cook_recipe_options& options
) -> int {
auto outdir = src.outdir //
? work_dir / *src.outdir
: work_dir;
? options.work_dir / *src.outdir
: options.work_dir;
auto url = boost::url{src.url};
auto out_file_path = outdir / fs::path{url.path().c_str()}.filename();

Expand All @@ -148,17 +148,18 @@ static auto handle_source( //
}

static auto handle_source( //
ecsact::build_recipe::source_codegen src,
fs::path work_dir
ecsact::build_recipe::source_codegen src,
const ecsact::cli::cook_recipe_options& options
) -> int {
auto default_plugins_dir = ecsact::cli::get_default_plugins_dir();
auto plugin_paths = std::vector<fs::path>{};

for(auto plugin : src.plugins) {
auto plugin_path = ecsact::cli::resolve_plugin_path( //
plugin,
default_plugins_dir
);
auto plugin_path = ecsact::cli::resolve_plugin_path({
.plugin_arg = plugin,
.default_plugins_dir = default_plugins_dir,
.additional_plugin_dirs = options.additional_plugin_dirs,
});

if(!plugin_path) {
return 1;
Expand All @@ -174,12 +175,12 @@ static auto handle_source( //
}

static auto handle_source( //
ecsact::build_recipe::source_path src,
fs::path work_dir
ecsact::build_recipe::source_path src,
const ecsact::cli::cook_recipe_options& options
) -> int {
auto outdir = src.outdir //
? work_dir / *src.outdir
: work_dir;
? options.work_dir / *src.outdir
: options.work_dir;

auto ec = std::error_code{};
fs::create_directories(outdir, ec);
Expand Down Expand Up @@ -503,35 +504,37 @@ auto cl_compile(compile_options options) -> int {

auto ecsact::cli::cook_recipe( //
const char* argv0,
std::vector<fs::path> files,
const ecsact::build_recipe& recipe,
cc_compiler compiler,
fs::path work_dir,
fs::path output_path
const cook_recipe_options& recipe_options
) -> std::optional<std::filesystem::path> {
auto exit_code = int{};

for(auto& src : recipe.sources()) {
exit_code =
std::visit([&](auto& src) { return handle_source(src, work_dir); }, src);
exit_code = std::visit(
[&](auto& src) { return handle_source(src, recipe_options); },
src
);

if(exit_code != 0) {
return {};
}
}

auto output_path = recipe_options.output_path;

if(output_path.has_extension()) {
auto has_allowed_output_extension = false;
for(auto allowed_ext : compiler.allowed_output_extensions) {
if(allowed_ext == output_path.extension().string()) {
if(allowed_ext == recipe_options.output_path.extension().string()) {
has_allowed_output_extension = true;
}
}

if(!has_allowed_output_extension) {
ecsact::cli::report_error(
"Invalid output extension {}",
output_path.extension().string()
recipe_options.output_path.extension().string()
);

return {};
Expand All @@ -543,8 +546,8 @@ auto ecsact::cli::cook_recipe( //

ecsact::cli::report_info("Compiling {}", output_path.string());

auto src_dir = work_dir / "src";
auto inc_dir = work_dir / "include";
auto src_dir = recipe_options.work_dir / "src";
auto inc_dir = recipe_options.work_dir / "include";

auto inc_dirs = std::vector{inc_dir};

Expand Down Expand Up @@ -640,7 +643,7 @@ auto ecsact::cli::cook_recipe( //

if(is_cl_like(compiler.compiler_type)) {
exit_code = cl_compile({
.work_dir = work_dir,
.work_dir = recipe_options.work_dir,
.compiler = compiler,
.inc_dirs = inc_dirs,
.system_libs = as_vec(recipe.system_libs()),
Expand All @@ -651,7 +654,7 @@ auto ecsact::cli::cook_recipe( //
});
} else {
exit_code = clang_gcc_compile({
.work_dir = work_dir,
.work_dir = recipe_options.work_dir,
.compiler = compiler,
.inc_dirs = inc_dirs,
.system_libs = as_vec(recipe.system_libs()),
Expand Down
19 changes: 13 additions & 6 deletions ecsact/cli/commands/build/recipe/cook.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@

namespace ecsact::cli {

struct cook_recipe_options {
std::vector<std::filesystem::path> files;
std::filesystem::path work_dir;
std::filesystem::path output_path;

/** Other directories to check for codegen plugins */
std::vector<std::filesystem::path> additional_plugin_dirs;
};

/**
* @returns the cooked runtime path
*/
auto cook_recipe(
const char* argv0,
std::vector<std::filesystem::path> files,
const ecsact::build_recipe& recipe,
cc_compiler compiler,
std::filesystem::path work_dir,
std::filesystem::path output_path
const char* argv0,
const ecsact::build_recipe& recipe,
cc_compiler compiler,
const cook_recipe_options& recipe_options
) -> std::optional<std::filesystem::path>;

} // namespace ecsact::cli
7 changes: 5 additions & 2 deletions ecsact/cli/commands/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ int ecsact::cli::detail::codegen_command(int argc, const char* argv[]) {
for(auto plugin_arg : args.at("--plugin").asStringList()) {
auto checked_plugin_paths = std::vector<fs::path>{};
auto plugin_path = resolve_plugin_path(
plugin_arg,
default_plugins_dir,
{
.plugin_arg = plugin_arg,
.default_plugins_dir = default_plugins_dir,
.additional_plugin_dirs = {},
},
checked_plugin_paths
);

Expand Down
Loading

0 comments on commit 04ae855

Please sign in to comment.