Skip to content

Commit 5330e18

Browse files
authored
feat: generate dylib preprocessor guard (#77)
1 parent 28d9027 commit 5330e18

File tree

1 file changed

+35
-9
lines changed
  • ecsact/cli/commands/build/recipe

1 file changed

+35
-9
lines changed

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <curl/curl.h>
1010
#undef fopen
1111
#include <boost/url.hpp>
12+
#include <boost/algorithm/string/case_conv.hpp>
1213
#include "magic_enum.hpp"
1314
#include "ecsact/cli/detail/proc_exec.hh"
1415
#include "ecsact/cli/commands/build/cc_compiler_config.hh"
@@ -241,17 +242,41 @@ static auto handle_source( //
241242
return 0;
242243
}
243244

245+
constexpr auto GENERATED_DYLIB_DISCLAIMER = R"(
246+
////////////////////////////////////////////////////////////////////////////////
247+
// THIS FILE IS GENERATED - DO NOT EDIT //
248+
////////////////////////////////////////////////////////////////////////////////
249+
)";
250+
251+
constexpr auto LOAD_AT_RUNTIME_GUARD = R"(
252+
#ifndef ECSACT_{0}_API_LOAD_AT_RUNTIME
253+
# error "Expected ECSACT_{0}_API_LOAD_AT_RUNTIME to be set"
254+
#endif // ECSACT_{0}_API_LOAD_AT_RUNTIME
255+
256+
#ifdef ECSACT_{0}_API
257+
# error "ECSACT_{0}_API may not be set while using generated dylib source"
258+
#endif // ECSACT_{0}_API
259+
)";
260+
244261
static auto generate_dylib_imports( //
245262
auto&& imports,
246263
std::ostream& output
247264
) -> void {
248265
auto mods = ecsact::cli::detail::get_ecsact_modules(as_vec(imports));
266+
output << GENERATED_DYLIB_DISCLAIMER;
249267
output << "#include <string>\n";
250268
output << "#include \"ecsact/runtime/common.h\"\n";
251269
for(auto&& [module_name, _] : mods.module_methods) {
252270
output << std::format("#include \"ecsact/runtime/{}.h\"\n", module_name);
253271
}
254272

273+
for(auto&& [module_name, _] : mods.module_methods) {
274+
output << std::format( //
275+
LOAD_AT_RUNTIME_GUARD,
276+
boost::to_upper_copy(module_name)
277+
);
278+
}
279+
255280
output << "\n";
256281

257282
for(std::string_view imp : imports) {
@@ -470,6 +495,8 @@ auto cl_compile(compile_options options) -> int {
470495
// compile_proc_args.push_back("/bigobj");
471496
// }
472497

498+
// cl_args.push_back("/we4530"); // treat exceptions as errors
499+
cl_args.push_back("/wd4530"); // ignore use of exceptions warning
473500
cl_args.push_back("/MD");
474501
cl_args.push_back("/DNDEBUG");
475502
cl_args.push_back("/O2");
@@ -620,6 +647,14 @@ auto ecsact::cli::cook_recipe( //
620647

621648
auto source_files = std::vector<fs::path>{};
622649

650+
{
651+
// No need to add to source_files since it will be grabbed in the directory
652+
// iterator
653+
auto dylib_src = src_dir / "ecsact-generated-dylib.cc";
654+
auto dylib_src_stream = std::ofstream{dylib_src};
655+
generate_dylib_imports(recipe.imports(), dylib_src_stream);
656+
}
657+
623658
for(auto entry : fs::recursive_directory_iterator(src_dir)) {
624659
if(!entry.is_regular_file()) {
625660
continue;
@@ -698,15 +733,6 @@ auto ecsact::cli::cook_recipe( //
698733
inc_dirs.push_back(install_prefix / "include");
699734
#endif
700735

701-
auto dylib_src = src_dir / "ecsact-generated-dylib.cc";
702-
703-
{
704-
auto dylib_src_stream = std::ofstream{dylib_src};
705-
generate_dylib_imports(recipe.imports(), dylib_src_stream);
706-
}
707-
708-
source_files.push_back(dylib_src);
709-
710736
if(is_cl_like(compiler.compiler_type)) {
711737
exit_code = cl_compile({
712738
.work_dir = recipe_options.work_dir,

0 commit comments

Comments
 (0)