mistake in build command
[vg.git] / vg_build.h
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4
5 #include "vg_log.h"
6
7 struct compiler_info
8 {
9 char name[64],
10 file[512],
11 link[512],
12 library[512],
13 include[512],
14 build_dir[512],
15 executable[512];
16
17 enum optimization_profile
18 {
19 k_optimization_profile_debug,
20 k_optimization_profile_release
21 }
22 optimization_profile;
23
24 enum target_file
25 {
26 k_target_file_game,
27 k_target_file_server
28 }
29 target_file;
30
31 enum compiler
32 {
33 k_compiler_clang,
34 k_compiler_gcc,
35 k_compiler_mingw
36 }
37 compiler;
38
39 int clean;
40 }
41
42 static vg_compiler;
43
44 void vg_build_syscall(const char *fmt, ...)
45 {
46 va_list args;
47 va_start( args, fmt );
48
49 char call[4096];
50 vsnprintf( call, vg_list_size( call ), fmt, args );
51
52 va_end( args );
53
54 puts( call );
55
56 if( system(call) )
57 exit(0);
58 }
59
60 void vg_build_object( const char *file )
61 {
62 strcat( vg_compiler.file, file );
63 }
64
65 void vg_build_library_dir( const char *ldir )
66 {
67 strcat( vg_compiler.library, ldir );
68 }
69
70 void vg_build_link( const char *link )
71 {
72 strcat( vg_compiler.link, link );
73 }
74
75 void vg_build_include( const char *inc )
76 {
77 strcat( vg_compiler.include, inc );
78 }
79
80 const char *vg_compiler_str(void)
81 {
82 return (const char *[]){ "clang", "gcc", "i686-w64-mingw32-gcc" }
83 [vg_compiler.compiler];
84 }
85
86 void vg_build_start( const char *name, enum compiler compiler )
87 {
88 vg_compiler.file[0] = '\0';
89 vg_compiler.link[0] = '\0';
90 vg_compiler.include[0] = '\0';
91 vg_compiler.library[0] = '\0';
92 vg_compiler.compiler = compiler;
93 strcpy( vg_compiler.name, name );
94
95 snprintf( vg_compiler.build_dir, 512,
96 "bin/%s-%s",
97 name,
98 vg_compiler_str() );
99
100 if( vg_compiler.clean ){
101 vg_build_syscall( "rm -rf %s", vg_compiler.build_dir );
102 }
103
104 vg_build_syscall( "mkdir -p %s", vg_compiler.build_dir );
105 vg_build_include( "-I. -I./vg " );
106 vg_build_library_dir( "-L. " );
107 }
108
109 void vg_build_add_link_for_graphics(void)
110 {
111 if( (vg_compiler.compiler == k_compiler_gcc) ||
112 (vg_compiler.compiler == k_compiler_clang ) )
113 {
114 vg_build_link( "-lSDL2 -lGL -lX11 -lXxf86vm -lXrandr -lXi -ldl " );
115 }
116 else
117 {
118 vg_build_link( "-lmingw32 -lSDL2main -lSDL2 -lopengl32 -mwindows \\\n" );
119 vg_build_link( " -Wl,--dynamicbase -Wl,--nxcompat \\\n" );
120
121 /* + 26.05.23: Suddenly something is pulling in winpthread.
122 * cant work out whats doing it or why. */
123 vg_build_link( " -Wl,-Bstatic,--whole-archive \\\n" );
124 vg_build_link( " -lwinpthread \\\n" );
125 vg_build_link( " -Wl,--no-whole-archive " );
126 }
127
128 vg_build_object( "vg/dep/glad/glad.c " );
129
130 vg_build_link( "-lm " );
131 if( vg_compiler.compiler == k_compiler_mingw ){
132 //vg_build_link( "-mthreads " );
133 //vg_build_link( "-static-libgcc " );
134 }
135 else{
136 vg_build_link( "-pthread " );
137 }
138 }
139
140 void vg_build_add_link_for_game(void)
141 {
142 if( (vg_compiler.compiler == k_compiler_gcc) ||
143 (vg_compiler.compiler == k_compiler_clang ) )
144 {
145 vg_build_link( "-lsteam_api " );
146 }
147 else
148 {
149 vg_build_library_dir( "-L./vg/dep/sdl " );
150 vg_build_link( "vg/dep/steam/steam_api.dll " );
151 }
152
153 vg_build_include( "-I./vg/dep " );
154 vg_build_library_dir( "-L./vg/dep/steam " );
155 }
156
157 void vg_build_bin_dependency_file( const char *src )
158 {
159 vg_build_syscall( "cp %s %s", src, vg_compiler.build_dir );
160 }
161
162 void vg_build_symbolic_link( const char *folder, const char *bin_name )
163 {
164 char dest[512];
165 snprintf( dest, 512, "%s/%s", vg_compiler.build_dir, bin_name );
166
167 if( !access( dest, F_OK ) )
168 vg_build_syscall( "unlink %s", dest );
169
170 vg_build_syscall( "ln -srf %s %s", folder, dest );
171 }
172
173 void vg_build_copy_graphics_dependencies(void)
174 {
175 if( vg_compiler.compiler == k_compiler_mingw )
176 {
177 vg_build_bin_dependency_file( "vg/dep/sdl/SDL2.dll" );
178 }
179 }
180
181 void vg_build_copy_game_dependencies(void)
182 {
183 vg_build_bin_dependency_file(
184 "vg/submodules/SDL_GameControllerDB/gamecontrollerdb.txt" );
185
186 if( (vg_compiler.compiler == k_compiler_gcc) ||
187 (vg_compiler.compiler == k_compiler_clang) )
188 {
189 vg_build_bin_dependency_file( "vg/dep/steam/libsteam_api.so" );
190 }
191 else
192 {
193 vg_build_bin_dependency_file( "vg/dep/steam/steam_api.dll" );
194 }
195 }
196
197 void vg_build_mode_release(void)
198 {
199 vg_compiler.optimization_profile = k_optimization_profile_release;
200 }
201
202 void vg_build_mode_debug(void)
203 {
204 vg_compiler.optimization_profile = k_optimization_profile_debug;
205 }
206
207 void vg_build_clean(void){
208 vg_compiler.clean = 1;
209 }
210
211 void vg_build(void)
212 {
213 char cmd[8192];
214 cmd[0] = '\0';
215
216 /* Compiler */
217 strcat( cmd, "ccache " );
218 strcat( cmd, vg_compiler_str() );
219 strcat( cmd, " -std=gnu99 -D_REENTRANT \\\n" );
220
221 /* Debugging information */
222 if( vg_compiler.optimization_profile == k_optimization_profile_debug )
223 {
224 strcat( cmd, " -O0 -ggdb3 -fno-omit-frame-pointer " );
225
226 if( (vg_compiler.compiler == k_compiler_gcc) ||
227 (vg_compiler.compiler == k_compiler_clang ) )
228 {
229 strcat( cmd, "-rdynamic -fsanitize=address "
230 "-fPIE -fstack-protector-strong " );
231 }
232
233 strcat( cmd, "\\\n" );
234 }
235 else
236 {
237 strcat( cmd, " -O3 -DVG_RELEASE\\\n" );
238 }
239
240 /* Warnings */
241 strcat( cmd,
242 " -Wall\\\n"
243 " -Wno-unused-function -Wno-unused-variable -Wno-format-truncation\\\n"
244 " -Wno-unused-command-line-argument -Wno-unused-but-set-variable\\\n"
245 );
246
247 if( vg_compiler.compiler == k_compiler_clang ){
248 strcat( cmd,
249 " -ferror-limit=5\\\n" );
250 }
251
252 /* Include */
253 strcat( cmd, " " );
254 strcat( cmd, vg_compiler.include );
255 strcat( cmd, "\\\n" );
256
257 /* Library */
258 strcat( cmd, " " );
259 strcat( cmd, vg_compiler.library );
260 strcat( cmd, "\\\n" );
261
262 /* Targets */
263 strcat( cmd, " " );
264 strcat( cmd, vg_compiler.file );
265 strcat( cmd, "\\\n" );
266
267 /* Output */
268 strcat( cmd, " -o " );
269 vg_compiler.executable[0] = '\0';
270 strcat( vg_compiler.executable, vg_compiler.build_dir );
271 strcat( vg_compiler.executable, "/" );
272 strcat( vg_compiler.executable, vg_compiler.name );
273
274 if( vg_compiler.compiler == k_compiler_mingw )
275 strcat( vg_compiler.executable, ".exe" );
276
277 strcat( cmd, vg_compiler.executable );
278 strcat( cmd, "\\\n" );
279
280 /* Link */
281 strcat( cmd, " " );
282 strcat( cmd, vg_compiler.link );
283 strcat( cmd, "\\\n" );
284
285 strcat( cmd, " -Wl,-rpath=./" );
286
287 vg_build_syscall( cmd );
288 }