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