Skip to content

Commit 04ae855

Browse files
Kelwanzaucy
andauthored
feat(build): allow plugins relative to recipe file (#72)
Co-authored-by: Ezekiel Warren <[email protected]>
1 parent 35453db commit 04ae855

File tree

10 files changed

+514
-432
lines changed

10 files changed

+514
-432
lines changed

MODULE.bazel.lock

Lines changed: 4 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecsact/cli/commands/build.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ auto ecsact::cli::detail::build_command( //
8686

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

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

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

175+
auto cook_options = cook_recipe_options{
176+
.files = file_paths,
177+
.work_dir = work_dir,
178+
.output_path = output_path,
179+
.additional_plugin_dirs = {recipe_path.parent_path()},
180+
};
174181
auto runtime_output_path = cook_recipe(
175182
argv[0],
176-
file_paths,
177183
std::get<build_recipe>(recipe),
178184
*compiler,
179-
work_dir,
180-
output_path
185+
cook_options
181186
);
182187

183188
if(!runtime_output_path) {

ecsact/cli/commands/build/BUILD.bazel

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,36 @@ package(default_visibility = ["//:__subpackages__"])
55

66
cc_library(
77
name = "build_recipe",
8-
copts = copts,
98
srcs = ["build_recipe.cc"],
109
hdrs = ["build_recipe.hh"],
10+
copts = copts,
1111
deps = [
12+
"//ecsact/cli:report",
1213
"//ecsact/cli/commands/build:get_modules",
1314
"@yaml-cpp",
1415
],
1516
)
1617

1718
cc_library(
1819
name = "cc_compiler",
19-
copts = copts,
2020
srcs = ["cc_compiler.cc"],
2121
hdrs = ["cc_compiler.hh"],
22+
copts = copts,
2223
deps = [
2324
":cc_compiler_config",
2425
":cc_compiler_util",
2526
"//ecsact/cli:report",
27+
"@boost.process",
2628
"@magic_enum",
2729
"@nlohmann_json//:json",
28-
"@boost.process",
2930
],
3031
)
3132

3233
cc_library(
3334
name = "cc_compiler_config",
34-
copts = copts,
3535
srcs = ["cc_compiler_config.cc"],
3636
hdrs = ["cc_compiler_config.hh"],
37+
copts = copts,
3738
deps = [
3839
":cc_compiler_util",
3940
"//ecsact/cli:report",
@@ -43,29 +44,29 @@ cc_library(
4344

4445
cc_library(
4546
name = "cc_compiler_util",
46-
copts = copts,
4747
srcs = ["cc_compiler_util.cc"],
4848
hdrs = ["cc_compiler_util.hh"],
49+
copts = copts,
4950
deps = [
5051
"@boost.process",
5152
],
5253
)
5354

5455
cc_library(
5556
name = "cc_defines_gen",
56-
hdrs = ["cc_defines_gen.hh"],
5757
srcs = ["cc_defines_gen.cc"],
58+
hdrs = ["cc_defines_gen.hh"],
5859
copts = copts,
5960
deps = [
60-
"@boost.algorithm",
6161
":get_modules",
62+
"@boost.algorithm",
6263
],
6364
)
6465

6566
cc_library(
6667
name = "get_modules",
67-
hdrs = ["get_modules.hh"],
6868
srcs = ["get_modules.cc"],
69+
hdrs = ["get_modules.hh"],
6970
copts = copts,
7071
deps = [
7172
"@ecsact_runtime//:core",

ecsact/cli/commands/build/build_recipe.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <ranges>
99
#include "yaml-cpp/yaml.h"
1010
#include "ecsact/cli/commands/build/get_modules.hh"
11+
#include "ecsact/cli/report.hh"
1112

1213
namespace fs = std::filesystem;
1314

@@ -221,7 +222,8 @@ auto ecsact::build_recipe::from_yaml_file( //
221222
}
222223

223224
return recipe;
224-
} catch(const YAML::BadFile&) {
225+
} catch(const YAML::BadFile& err) {
226+
ecsact::cli::report_error("YAML PARSE: {}", err.what());
225227
return build_recipe_parse_error::bad_file;
226228
}
227229
}

ecsact/cli/commands/build/recipe/cook.cc

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ static auto download_file(boost::url url, fs::path out_file_path) -> int {
122122
}
123123

124124
static auto handle_source( //
125-
ecsact::build_recipe::source_fetch src,
126-
fs::path work_dir
125+
ecsact::build_recipe::source_fetch src,
126+
const ecsact::cli::cook_recipe_options& options
127127
) -> int {
128128
auto outdir = src.outdir //
129-
? work_dir / *src.outdir
130-
: work_dir;
129+
? options.work_dir / *src.outdir
130+
: options.work_dir;
131131
auto url = boost::url{src.url};
132132
auto out_file_path = outdir / fs::path{url.path().c_str()}.filename();
133133

@@ -148,17 +148,18 @@ static auto handle_source( //
148148
}
149149

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

157157
for(auto plugin : src.plugins) {
158-
auto plugin_path = ecsact::cli::resolve_plugin_path( //
159-
plugin,
160-
default_plugins_dir
161-
);
158+
auto plugin_path = ecsact::cli::resolve_plugin_path({
159+
.plugin_arg = plugin,
160+
.default_plugins_dir = default_plugins_dir,
161+
.additional_plugin_dirs = options.additional_plugin_dirs,
162+
});
162163

163164
if(!plugin_path) {
164165
return 1;
@@ -174,12 +175,12 @@ static auto handle_source( //
174175
}
175176

176177
static auto handle_source( //
177-
ecsact::build_recipe::source_path src,
178-
fs::path work_dir
178+
ecsact::build_recipe::source_path src,
179+
const ecsact::cli::cook_recipe_options& options
179180
) -> int {
180181
auto outdir = src.outdir //
181-
? work_dir / *src.outdir
182-
: work_dir;
182+
? options.work_dir / *src.outdir
183+
: options.work_dir;
183184

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

504505
auto ecsact::cli::cook_recipe( //
505506
const char* argv0,
506-
std::vector<fs::path> files,
507507
const ecsact::build_recipe& recipe,
508508
cc_compiler compiler,
509-
fs::path work_dir,
510-
fs::path output_path
509+
const cook_recipe_options& recipe_options
511510
) -> std::optional<std::filesystem::path> {
512511
auto exit_code = int{};
513512

514513
for(auto& src : recipe.sources()) {
515-
exit_code =
516-
std::visit([&](auto& src) { return handle_source(src, work_dir); }, src);
514+
exit_code = std::visit(
515+
[&](auto& src) { return handle_source(src, recipe_options); },
516+
src
517+
);
517518

518519
if(exit_code != 0) {
519520
return {};
520521
}
521522
}
522523

524+
auto output_path = recipe_options.output_path;
525+
523526
if(output_path.has_extension()) {
524527
auto has_allowed_output_extension = false;
525528
for(auto allowed_ext : compiler.allowed_output_extensions) {
526-
if(allowed_ext == output_path.extension().string()) {
529+
if(allowed_ext == recipe_options.output_path.extension().string()) {
527530
has_allowed_output_extension = true;
528531
}
529532
}
530533

531534
if(!has_allowed_output_extension) {
532535
ecsact::cli::report_error(
533536
"Invalid output extension {}",
534-
output_path.extension().string()
537+
recipe_options.output_path.extension().string()
535538
);
536539

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

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

546-
auto src_dir = work_dir / "src";
547-
auto inc_dir = work_dir / "include";
549+
auto src_dir = recipe_options.work_dir / "src";
550+
auto inc_dir = recipe_options.work_dir / "include";
548551

549552
auto inc_dirs = std::vector{inc_dir};
550553

@@ -640,7 +643,7 @@ auto ecsact::cli::cook_recipe( //
640643

641644
if(is_cl_like(compiler.compiler_type)) {
642645
exit_code = cl_compile({
643-
.work_dir = work_dir,
646+
.work_dir = recipe_options.work_dir,
644647
.compiler = compiler,
645648
.inc_dirs = inc_dirs,
646649
.system_libs = as_vec(recipe.system_libs()),
@@ -651,7 +654,7 @@ auto ecsact::cli::cook_recipe( //
651654
});
652655
} else {
653656
exit_code = clang_gcc_compile({
654-
.work_dir = work_dir,
657+
.work_dir = recipe_options.work_dir,
655658
.compiler = compiler,
656659
.inc_dirs = inc_dirs,
657660
.system_libs = as_vec(recipe.system_libs()),

ecsact/cli/commands/build/recipe/cook.hh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77

88
namespace ecsact::cli {
99

10+
struct cook_recipe_options {
11+
std::vector<std::filesystem::path> files;
12+
std::filesystem::path work_dir;
13+
std::filesystem::path output_path;
14+
15+
/** Other directories to check for codegen plugins */
16+
std::vector<std::filesystem::path> additional_plugin_dirs;
17+
};
18+
1019
/**
1120
* @returns the cooked runtime path
1221
*/
1322
auto cook_recipe(
14-
const char* argv0,
15-
std::vector<std::filesystem::path> files,
16-
const ecsact::build_recipe& recipe,
17-
cc_compiler compiler,
18-
std::filesystem::path work_dir,
19-
std::filesystem::path output_path
23+
const char* argv0,
24+
const ecsact::build_recipe& recipe,
25+
cc_compiler compiler,
26+
const cook_recipe_options& recipe_options
2027
) -> std::optional<std::filesystem::path>;
2128

2229
} // namespace ecsact::cli

ecsact/cli/commands/codegen.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ int ecsact::cli::detail::codegen_command(int argc, const char* argv[]) {
7878
for(auto plugin_arg : args.at("--plugin").asStringList()) {
7979
auto checked_plugin_paths = std::vector<fs::path>{};
8080
auto plugin_path = resolve_plugin_path(
81-
plugin_arg,
82-
default_plugins_dir,
81+
{
82+
.plugin_arg = plugin_arg,
83+
.default_plugins_dir = default_plugins_dir,
84+
.additional_plugin_dirs = {},
85+
},
8386
checked_plugin_paths
8487
);
8588

0 commit comments

Comments
 (0)