allow user program to specify simplified build options

This commit is contained in:
Judah Caruso 2025-05-18 12:02:36 -06:00
parent ca27aedae7
commit ee1adc6803
3 changed files with 48 additions and 19 deletions

View file

@ -87,6 +87,10 @@ G: *struct {
#poke_name reload init; #poke_name reload init;
#poke_name reload setup; #poke_name reload setup;
#poke_name reload teardown; #poke_name reload teardown;
#poke_name reload build_options;
// Use the default build options
build_options :: () => reload.Simple_Build_Options.{};
reload :: #import "jx/reload"; reload :: #import "jx/reload";

View file

@ -55,5 +55,10 @@ else {
#poke_name reload init; #poke_name reload init;
#poke_name reload setup; #poke_name reload setup;
#poke_name reload teardown; #poke_name reload teardown;
#poke_name reload build_options;
// Use the default build options
build_options :: () => reload.Simple_Build_Options.{};
} }

View file

@ -14,11 +14,16 @@ Status :: enum {
hard_reload; hard_reload;
} }
Init_Proc :: #type (state: *void, allocator: Allocator, full_reset: bool); Simple_Build_Options :: struct {
Setup_Proc :: #type (); output_name: string;
Teardown_Proc :: #type (); relative_output_path: string;
Frame_Proc :: #type () -> Status; }
Init_Proc :: #type (state: *void, allocator: Allocator, full_reset: bool);
Setup_Proc :: #type ();
Teardown_Proc :: #type ();
Frame_Proc :: #type () -> Status;
Build_Options_Proc :: #type () -> Simple_Build_Options;
#scope_module; #scope_module;
@ -26,6 +31,7 @@ Frame_Proc :: #type () -> Status;
#placeholder init; #placeholder init;
#placeholder setup; #placeholder setup;
#placeholder teardown; #placeholder teardown;
#placeholder build_options;
#if !disabled { #if !disabled {
#load "reload_main.jai"; #load "reload_main.jai";
@ -52,17 +58,34 @@ Frame_Proc :: #type () -> Status;
#scope_module; #scope_module;
#if !disabled { #if !disabled {
#assert (type_of(init) == Init_Proc) "init was the wrong type!"; #assert (type_of(init) == Init_Proc) "init was the wrong type!";
#assert (type_of(setup) == Setup_Proc) "setup was the wrong type!"; #assert (type_of(setup) == Setup_Proc) "setup was the wrong type!";
#assert (type_of(teardown) == Teardown_Proc) "teardown was the wrong type!"; #assert (type_of(teardown) == Teardown_Proc) "teardown was the wrong type!";
#assert (type_of(frame) == Frame_Proc) "frame was the wrong type!"; #assert (type_of(frame) == Frame_Proc) "frame was the wrong type!";
#assert (type_of(build_options) == Build_Options_Proc) "build_options was the wrong type!";
} }
file_path :: #run loc.fully_pathed_filename; file_path :: #run loc.fully_pathed_filename;
proj_path :: #run path_strip_filename(file_path); proj_path :: #run path_strip_filename(file_path);
temp_path :: #run tprint("%/.build/tmp.jai", proj_path); temp_path :: #run tprint("%/.build/tmp.jai", proj_path);
temp_exists :: #run file_exists(temp_path); temp_exists :: #run file_exists(temp_path);
lib_name :: #run tprint("%_lib", path_basename(file_path)); lib_name :: #run tprint("%_lib", user_options.output_name);
user_options :: #run -> Simple_Build_Options {
opts := build_options();
if opts.relative_output_path.count == 0 {
opts.relative_output_path = proj_path;
} else {
opts.relative_output_path = tprint("%/%", proj_path, opts.relative_output_path);
}
if opts.output_name.count == 0 {
opts.output_name = path_strip_extension(path_filename(file_path));
}
return opts;
}
// only run the build when we the module importing us is compiled // only run the build when we the module importing us is compiled
#if !(disabled || temp_exists) #run { #if !(disabled || temp_exists) #run {
@ -70,9 +93,6 @@ lib_name :: #run tprint("%_lib", path_basename(file_path));
make_directory_if_it_does_not_exist(".build"); make_directory_if_it_does_not_exist(".build");
out_path := path_strip_extension(proj_path);
out_name := path_strip_extension(path_filename(file_path));
// build host executable // build host executable
{ {
ws := compiler_create_workspace(file_path); ws := compiler_create_workspace(file_path);
@ -80,9 +100,9 @@ lib_name :: #run tprint("%_lib", path_basename(file_path));
opts := get_build_options(ws); opts := get_build_options(ws);
opts.text_output_flags = 0; opts.text_output_flags = 0;
opts.output_path = out_path; opts.output_path = user_options.relative_output_path;
opts.output_type = .EXECUTABLE; opts.output_type = .EXECUTABLE;
opts.output_executable_name = out_name; opts.output_executable_name = user_options.output_name;
// create a temporary file that contains the hijacked main. // create a temporary file that contains the hijacked main.
// we need to #load our program library so the #placeholders // we need to #load our program library so the #placeholders
@ -116,7 +136,7 @@ lib_name :: #run tprint("%_lib", path_basename(file_path));
defer compiler_destroy_workspace(ws); defer compiler_destroy_workspace(ws);
opts := get_build_options(ws); opts := get_build_options(ws);
opts.output_path = out_path; opts.output_path = user_options.relative_output_path;
opts.output_type = .DYNAMIC_LIBRARY; opts.output_type = .DYNAMIC_LIBRARY;
opts.output_executable_name = lib_name; opts.output_executable_name = lib_name;