Skip to content

Commit fd6fcee

Browse files
committed
feat(aot): support command line args in main fn
1 parent fdfd05f commit fd6fcee

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

compiler+runtime/include/cpp/jank/c_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ extern "C"
300300
jank_bool init_default_ctx,
301301
int (*fn)(int const, char const **));
302302

303+
jank_object_ref jank_parse_command_line_args(int const argc, char const **argv);
304+
303305
#ifdef __cplusplus
304306
}
305307
#endif

compiler+runtime/include/cpp/jank/util/cli.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@ namespace jank::util::cli
5454
command command{ command::repl };
5555
};
5656

57+
struct empty_options
58+
{
59+
/* TODO: Use a native_persistent_vector instead. */
60+
std::vector<native_transient_string> opts;
61+
};
62+
5763
jtl::result<options, int> parse(int const argc, char const **argv);
64+
empty_options parse_empty(int const argc, char const **argv);
5865
}

compiler+runtime/src/cpp/jank/aot/processor.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ extern "C" jank_object_ref jank_load_jank_compiler_native();
7272
extern "C" jank_object_ref jank_load_clojure_core();
7373
extern "C" jank_object_ref jank_var_intern_c(char const *, char const *);
7474
extern "C" jank_object_ref jank_deref(jank_object_ref);
75-
extern "C" jank_object_ref jank_call0(jank_object_ref);
75+
extern "C" jank_object_ref jank_call2(jank_object_ref, jank_object_ref, jank_object_ref);
76+
extern "C" jank_object_ref jank_parse_command_line_args(int, char const **);
7677
)");
7778

7879
for(auto const &m : __rt_ctx->loaded_modules_in_order)
@@ -99,9 +100,14 @@ int main(int argc, const char** argv)
99100
util::format_to(sb, "{}();\n", module::module_to_load_function(m));
100101
}
101102

103+
sb(R"(auto const apply{ jank_var_intern_c("clojure.core", "apply") };)");
104+
sb("\n");
105+
sb(R"(auto const command_line_args{ jank_parse_command_line_args(argc, argv) };)");
106+
sb("\n");
107+
102108
util::format_to(sb, R"(auto const fn(jank_var_intern_c("{}", "-main"));)", module);
103109
sb("\n");
104-
sb(R"(jank_call0(jank_deref(fn));
110+
sb(R"(jank_call2(jank_deref(apply), jank_deref(fn), command_line_args);
105111
106112
return 0;
107113
@@ -150,8 +156,7 @@ int main(int argc, const char** argv)
150156
auto const clang_inferred_path{ llvm::sys::findProgramByName("clang++") };
151157
if(!clang_inferred_path)
152158
{
153-
return jtl::err(
154-
compiler_err{ 1, "clang++ executable not found. Ensure it exists on the path!" });
159+
return compiler_err{ 1, "clang++ executable not found. Ensure it exists on the path!" };
155160
}
156161
clang::driver::Driver driver(clang_inferred_path.get(),
157162
target_triple,
@@ -231,7 +236,7 @@ int main(int argc, const char** argv)
231236

232237
if(!C || C->containsError())
233238
{
234-
return jtl::err(compiler_err{ 1, "Failed to build compilation steps." });
239+
return compiler_err{ 1, "Failed to build compilation steps." };
235240
}
236241

237242
/* Execute the compilation jobs (preprocess, compile, assemble)
@@ -260,7 +265,7 @@ int main(int argc, const char** argv)
260265

261266
if(diags.hasErrorOccurred())
262267
{
263-
return jtl::err(compiler_err{ 1, "Compilation failed with errors.\n" });
268+
return compiler_err{ 1, "Compilation failed with errors.\n" };
264269
}
265270

266271
if(Result == 0)
@@ -271,7 +276,7 @@ int main(int argc, const char** argv)
271276
}
272277
else
273278
{
274-
return jtl::err(compiler_err{ Result, "Compilation command execution failed.\n" });
279+
return compiler_err{ Result, "Compilation command execution failed.\n" };
275280
}
276281

277282
return jtl::ok();

compiler+runtime/src/cpp/jank/c_api.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,4 +995,17 @@ extern "C"
995995

996996
return 0;
997997
}
998+
999+
jank_object_ref jank_parse_command_line_args(int const argc, char const **argv)
1000+
{
1001+
obj::transient_vector trans;
1002+
auto const opts{ util::cli::parse_empty(argc, argv).opts };
1003+
1004+
for(auto &arg : opts)
1005+
{
1006+
trans.conj_in_place(make_box(arg));
1007+
}
1008+
1009+
return trans.to_persistent().erase();
1010+
}
9981011
}

compiler+runtime/src/cpp/jank/util/cli.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,20 @@ namespace jank::util::cli
131131

132132
return ok(opts);
133133
}
134+
135+
empty_options parse_empty(int const argc, char const **argv)
136+
{
137+
CLI::App cli{ "jank default cli" };
138+
empty_options opts;
139+
cli.allow_extras();
140+
141+
cli.parse(argc, argv);
142+
143+
if(cli.remaining_size() > 0)
144+
{
145+
opts.opts = cli.remaining();
146+
}
147+
148+
return opts;
149+
}
134150
}

0 commit comments

Comments
 (0)