Logo

index : gist

Random things

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/gist.git/html/jai/hot_reload/build.jai blob: 1686dcfe38746656c9e18d392113abc76e97bcf2 [raw] [clear marker]

        
0
1program_args: []string;
2
3args_help: bool;
4args_compiler_silent: bool;
5args_program_run: bool;
6args_build_release: bool;
7args_memory_debug: bool;
8args_ua_alloc: bool;
9args_hot_reload: bool;
10args_rebuild_editor: bool;
11
12
13Release_Kind :: enum_flags {
14 DEBUG;
15 SHARED;
16 RELEASE;
17}
18
19
20Program :: struct {
21 name: string;
22 workspace: Workspace;
23}
24
25
26build :: () {
27 set_build_options_dc(.{ do_output = false });
28 args := get_build_options().compile_time_command_line;
29
30 // args
31 args_help = array_find(args, "help");
32 args_compiler_silent = array_find(args, "silent");
33 args_program_run = array_find(args, "run");
34 args_build_release = array_find(args, "release");
35 args_memory_debug = array_find(args, "memory");
36 args_ua_alloc = array_find(args, "ua");
37 args_hot_reload = array_find(args, "hotreload");
38 args_rebuild_editor = array_find(args, "rebuild");
39
40
41 // program args
42 program_args := program_args_collect(args);
43
44
45 /** Workspaces */
46 workspace_host := create_workspace("host");
47 workspace_editor := create_workspace("editor");
48
49 if args_help {
50 args_help_print();
51 return;
52 }
53
54 make_directory_if_it_does_not_exist("bin");
55 make_directory_if_it_does_not_exist("bin/debug");
56 make_directory_if_it_does_not_exist("bin/release");
57 make_directory_if_it_does_not_exist("bin/debug_hot");
58 make_directory_if_it_does_not_exist("bin/release_hot");
59
60 release_as: Release_Kind = ifx args_build_release then .RELEASE else .DEBUG;
61 if args_hot_reload then release_as |= .SHARED;
62
63 should_run_program := args_program_run ^ (args_hot_reload & args_program_run);
64
65
66 workspace_setup(workspace_editor, run=should_run_program, release_as, #code {
67 add_build_string(tprint("IS_HOT_RELOAD :: %;", args_hot_reload), workspace);
68 add_build_file(tprint("%src/main.jai", #filepath), workspace);
69 });
70
71 if args_hot_reload && !args_rebuild_editor {
72 workspace_setup(workspace_host, run=true, .DEBUG, #code {
73 add_build_string(
74 tprint("FP_EDITOR_SO :: \"%\";", "bin/debug_hot/editor.so"), workspace
75 );
76 add_build_file(tprint("%src/host.jai", #filepath), workspace);
77 });
78 }
79}
80
81build_debug :: (w: Workspace, target_options: *Build_Options) {
82 log("Choosing debug options...");
83 target_options.backend =.X64;
84 target_options.output_path = "bin/debug";
85 set_optimization(target_options, Optimization_Type.DEBUG, true);
86 set_build_options(target_options.*, w);
87}
88
89build_release :: (w: Workspace, target_options: *Build_Options) {
90 log("Choosing release options...");
91 target_options.backend = .LLVM;
92 target_options.output_path = "bin/release";
93 set_optimization(target_options, Optimization_Type.VERY_OPTIMIZED);
94 set_build_options(target_options.*, w);
95}
96
97build_dynamic_debug :: (w: Workspace, target_options: *Build_Options) {
98 log("Choosing dyn debug options ...");
99 target_options.backend = .X64;
100 target_options.output_type = .DYNAMIC_LIBRARY;
101 target_options.output_path = "bin/debug_hot";
102 set_optimization(target_options, Optimization_Type.DEBUG, true);
103 set_build_options(target_options.*, w);
104}
105
106build_dynamic_release :: (w: Workspace, target_options: *Build_Options) {
107 log("Choosing dyn release options ...");
108 target_options.backend = .LLVM;
109 target_options.output_type = .DYNAMIC_LIBRARY;
110 target_options.output_path = "bin/release_hot";
111 set_optimization(target_options, Optimization_Type.VERY_OPTIMIZED);
112 set_build_options(target_options.*, w);
113}
114
115
116args_help_print :: () {
117 help_message := #string _END_
118Usage: jai build.jai - [OPTIONS] :: [PROGRAM ARGS]
119
120Options:
121 help Prints this help menu.
122 silent Disables compiler/linker statistics.
123 run Runs your program afterwards.
124 release Builds with release options. If omitted, it builds a debug build.
125 memory Enables the memory leak detector.
126
127Passing Args to your Program:
128 If you want to supply args to your program, pass it like that:
129
130 `jai build.jai - run :: my_arg1 foo bar abc ABC`
131
132 Everything after the `::` get's forwarded to your program.
133
134_END_;
135 log(help_message);
136}
137
138program_args_collect :: (args: []string, divider: string = "::") -> [..]string {
139 buf: [..]string;
140
141 success, match := array_find(args, divider);
142 if success for i: match+1..args.count-1 array_add(*buf, args[i]);
143
144 return buf;
145}
146
147message_loop :: () -> success: bool {
148 while true {
149 message := compiler_wait_for_message();
150 if !message break;
151 if message.kind == {
152 case .COMPLETE;
153 message_complete := cast(*Message_Complete) message;
154 return message_complete.error_code == 0;
155 }
156 }
157 return false;
158}
159
160create_workspace :: (name: string) -> Program #expand {
161 w := compiler_create_workspace(name);
162 if !w{
163 log("Workspace creation failed: %", name);
164 `return;
165 }
166 return { name, w };
167}
168
169workspace_setup :: (using program: Program, run: bool, release_kind: Release_Kind, $intercept: Code) #expand {
170
171 insert_constant :: (label: string, any: Any) #expand {
172 add_build_string(tprint("% :: %;", label, any), `workspace);
173 }
174
175
176 print("The workspace w is %\n", workspace);
177 target_options := get_build_options(workspace);
178 target_options.output_executable_name = name;
179
180 if args_compiler_silent target_options.text_output_flags = 0;
181
182 if release_kind == {
183 case .RELEASE;
184 build_release(workspace, *target_options);
185 case (.SHARED | .RELEASE);
186 build_dynamic_release(workspace, *target_options);
187 case (.SHARED | .DEBUG);
188 build_dynamic_debug(workspace, *target_options);
189 case .DEBUG;
190 build_debug(workspace, *target_options);
191 case;
192 compiler_report("Unkown release kind");
193 }
194
195 compiler_begin_intercept(workspace);
196
197 #insert,scope() intercept;
198
199 insert_constant("IS_DEVELOPER", !args_build_release);
200 insert_constant("IS_UA_ALLOCATOR", args_ua_alloc);
201 insert_constant("MEMORY_DEBUGGER_ENABLED", args_memory_debug);
202
203 compiler_response := message_loop();
204 compiler_end_intercept(workspace);
205
206 if !compiler_response {
207 log("Compiler response failed.");
208 return;
209 }
210
211 if run then run_build_result_of_workspace(workspace, program_args);
212}
213
214
215main :: () {}
216
217#run build();
218
219
220#import "Basic";
221#import "Compiler";
222#import "File";
223#import "String";
224#import "Autorun";
225#import "Print_Vars";
226
227
228
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit