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