From ee1adc6803360c40e399192322c4b0b2cdc86ac1 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Sun, 18 May 2025 12:02:36 -0600 Subject: [PATCH] allow user program to specify simplified build options --- reload/examples/everything-you-need.jai | 4 ++ reload/examples/quickstart.jai | 5 +++ reload/module.jai | 58 +++++++++++++++++-------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/reload/examples/everything-you-need.jai b/reload/examples/everything-you-need.jai index 4653c73..e836841 100644 --- a/reload/examples/everything-you-need.jai +++ b/reload/examples/everything-you-need.jai @@ -87,6 +87,10 @@ G: *struct { #poke_name reload init; #poke_name reload setup; #poke_name reload teardown; +#poke_name reload build_options; + +// Use the default build options +build_options :: () => reload.Simple_Build_Options.{}; reload :: #import "jx/reload"; diff --git a/reload/examples/quickstart.jai b/reload/examples/quickstart.jai index abc98fb..2f59685 100644 --- a/reload/examples/quickstart.jai +++ b/reload/examples/quickstart.jai @@ -55,5 +55,10 @@ else { #poke_name reload init; #poke_name reload setup; #poke_name reload teardown; + + #poke_name reload build_options; + + // Use the default build options + build_options :: () => reload.Simple_Build_Options.{}; } diff --git a/reload/module.jai b/reload/module.jai index 4cb1369..390b332 100644 --- a/reload/module.jai +++ b/reload/module.jai @@ -14,11 +14,16 @@ Status :: enum { hard_reload; } -Init_Proc :: #type (state: *void, allocator: Allocator, full_reset: bool); -Setup_Proc :: #type (); -Teardown_Proc :: #type (); -Frame_Proc :: #type () -> Status; +Simple_Build_Options :: struct { + output_name: string; + relative_output_path: string; +} +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; @@ -26,6 +31,7 @@ Frame_Proc :: #type () -> Status; #placeholder init; #placeholder setup; #placeholder teardown; +#placeholder build_options; #if !disabled { #load "reload_main.jai"; @@ -52,17 +58,34 @@ Frame_Proc :: #type () -> Status; #scope_module; #if !disabled { - #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(teardown) == Teardown_Proc) "teardown was the wrong type!"; - #assert (type_of(frame) == Frame_Proc) "frame 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(teardown) == Teardown_Proc) "teardown 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; -proj_path :: #run path_strip_filename(file_path); -temp_path :: #run tprint("%/.build/tmp.jai", proj_path); -temp_exists :: #run file_exists(temp_path); -lib_name :: #run tprint("%_lib", path_basename(file_path)); +file_path :: #run loc.fully_pathed_filename; +proj_path :: #run path_strip_filename(file_path); +temp_path :: #run tprint("%/.build/tmp.jai", proj_path); +temp_exists :: #run file_exists(temp_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 #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"); - out_path := path_strip_extension(proj_path); - out_name := path_strip_extension(path_filename(file_path)); - // build host executable { 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.text_output_flags = 0; - opts.output_path = out_path; + opts.output_path = user_options.relative_output_path; 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. // 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); 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_executable_name = lib_name;