platform utils
[vg.git] / vg_compiler.sh
1 #!/bin/bash
2 # Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3
4 vg_root=`readlink $0`
5 vg_root=`dirname $vg_root`
6 vg_version="0.2"
7
8 opt_assets=false
9 opt_release=false
10 opt_play=false
11 opt_linux=false
12 opt_windows=false
13 opt_tools=false
14 opt_recompile_mini_audio=false
15 opt_steam=""
16
17 target_platform_include=""
18 target_standard="-std=c99"
19 target_link_steam=false
20
21 # Util
22 # ===========================================
23 error(){
24 echo -e "\033[1;31mError:\e[0m $@"
25 exit 1
26 }
27
28 warning(){
29 echo -e "\033[1;33mWarning:\e[0m $@"
30 }
31
32 success(){
33 echo -e "\033[1;32mSuccess:\e[0m $@"
34 }
35
36 logit(){
37 echo -e "\033[0;37m$@\e[0m"
38 }
39
40 titleit(){
41 echo ""
42 echo -e "\033[1;35m$@\e[0m"
43 echo "=========================="
44 echo ""
45 }
46
47 checkfatal(){
48 if [ $? -ne 0 ]; then
49 error "compiler signaled fail"
50 exit 1
51 fi
52 }
53
54 # Platforms
55 # ===========================================
56
57 target_os_windows(){
58 target_ext=".exe"
59 target_compiler="i686-w64-mingw32-gcc"
60 target_libs="-lglfw3 -lopengl32 -lm -mwindows"
61 target_libs_steam="$vg_root/dep/steam/steam_api.dll"
62 target_dir="build.win32"
63 target_steam_api="steam_api.dll"
64 target_miniaudio="$vg_root/dep/dr_soft/miniaudio_win32.o"
65 target_platform_include=""
66
67 if [ $opt_release = true ]; then
68 target_opts="-O3"
69 else
70 target_opts="-ggdb3"
71 fi
72 }
73
74 target_os_linux(){
75 target_ext=""
76 target_compiler="gcc"
77 target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -pthread -lXi -ldl"
78 target_libs_steam="-lsteam_api"
79 #target_platform_include="-include $vg_root/platformutils/force_link_glibc_2.23.h"
80 target_platform_include=""
81 target_dir="build.linux"
82 target_steam_api="libsteam_api.so"
83 target_miniaudio="$vg_root/dep/dr_soft/miniaudio_linux.o"
84 if [ $opt_release = true ]; then
85 target_opts="-O3"
86 else
87 target_opts="-O0 -fsanitize=address -ggdb3 -fno-omit-frame-pointer"
88 fi
89 }
90
91 precompile_x(){
92 sources="$1"
93 output="-o $2"
94 cmd="$target_compiler -D_REENTRANT $target_standard $target_opts $target_platform_include
95 -c $sources
96 $output"
97
98 logit " $cmd"
99 $cmd
100
101 checkfatal
102 success "$2 built"
103 }
104
105 compile_x(){
106 include="-I. -I$vg_root/dep -I$vg_root/src"
107 libs="-L./ -L$vg_root/dep/glfw -L$vg_root/dep/steam"
108
109 steam_part=""
110 if [ "$opt_steam" != "" ]; then
111 steam_part="$opt_steam $target_libs_steam"
112 fi
113
114 warnings="-Wall -Wstrict-aliasing=3 -Wno-unused-function"
115 sources="$1 $vg_root/dep/glad/glad.c $target_miniaudio"
116 output="-o $2$target_ext"
117
118 cmd="$target_compiler -D_REENTRANT $target_standard $target_opts $target_platform_include
119 $warnings
120 $include
121 $libs
122 $sources
123 $output
124 $target_libs $steam_part
125 -Wl,-rpath=./"
126
127 logit " $cmd\n"
128 $cmd
129
130 checkfatal
131 success "$2$target_ext built"
132 }
133
134 compile_main(){
135 titleit "Main build: $target_dir"
136 mkdir -p $target_dir
137
138 if [ "$opt_recompile_mini_audio" == true ]; then
139 precompile_x $vg_root/dep/dr_soft/miniaudio_impl.c $target_miniaudio
140 fi
141
142 compile_x $vg_src $target_dir/$vg_target
143 echo ""
144 logit "Setting up build structure"
145
146 # Setup build folder
147 mkdir $target_dir/cfg -p
148 mkdir $target_dir/textures -p
149 mkdir $target_dir/sound -p
150 mkdir $target_dir/maps -p
151 mkdir $target_dir/sav -p
152
153 # Copy libraries
154 if [ "$opt_steam" != "" ]; then
155 cp $vg_root/dep/steam/$target_steam_api $target_dir/$target_steam_api
156 fi
157
158 # Clear and copy assets
159 rm -r $target_dir/textures
160 rm -r $target_dir/sound
161 rm -r $target_dir/maps
162
163 cp -r .temp_textures $target_dir
164 mv $target_dir/.temp_textures $target_dir/textures
165 cp -r sound $target_dir
166 cp -r maps $target_dir
167 }
168
169 compile_tools(){
170 # These should only be compiled for native platform
171 titleit "Tools"
172
173 mkdir $vg_root/bin -p
174
175 steam_prev=$opt_steam
176 opt_steam=""
177
178 compile_x $vg_root/src/fontcomp.c $vg_root/bin/fontcomp
179 compile_x $vg_root/src/texsheet.c $vg_root/bin/texsheet
180 compile_x $vg_root/src/qoiconv.c $vg_root/bin/qoiconv
181
182 opt_steam=$steam_prev
183 }
184
185 compile_assets(){
186 titleit "Assets"
187 [[ -d .temp_textures ]] && rm -r .temp_textures
188 mkdir .temp_textures
189
190 # Convert all png to qoi
191 echo "Compile textures:"
192 for f in textures/*.png;
193 do logit " qoi: $f";
194 $vg_root/bin/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
195 done
196
197 # Autocombine textures
198 echo " [combine]:"
199
200 auto_combine=""
201 cd textures_combine
202 for f in *.png;
203 do logit " combine: $f";
204 auto_combine="$auto_combine $f"
205 done
206 $vg_root/bin/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
207 cd ..
208
209 # Compile font file
210 echo ""
211 echo "Compile fonts:"
212 $vg_root/bin/fontcomp$target_ext $vg_root/src/fonts/vg_font.png $vg_root/src/vg/vg_pxfont.h
213 }
214
215 # ==============================================================
216 # Compile process
217
218 options=rptlwa
219 longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full,miniaudio,template
220 parsed=$(getopt --options=$options --longoptions=$longopts --name "vgc" -- "$@")
221
222 if [ $? -ne 0 ]; then
223 error "getopt failed"
224 exit 1
225 fi
226
227 eval set -- "$parsed"
228 while true; do
229 case "$1" in
230 -a|--assets)
231 opt_assets=true
232 shift;
233 ;;
234 -r|--release)
235 opt_release=true
236 shift;
237 ;;
238 -p|--play)
239 opt_play=true
240 shift;
241 ;;
242 -l|--build-linux)
243 opt_linux=true
244 shift;
245 ;;
246 -w|--build-windows)
247 opt_windows=true
248 shift;
249 ;;
250 -t|--build-tools)
251 opt_tools=true
252 shift;
253 ;;
254 --full)
255 opt_assets=true
256 opt_release=true
257 opt_linux=true
258 opt_windows=true
259 opt_tools=true
260 opt_steam="-DVG_STEAM"
261 opt_recompile_mini_audio=true
262 shift;
263 ;;
264 --miniaudio)
265 opt_recompile_mini_audio=true
266 shift;
267 ;;
268 --steam)
269 opt_steam="-DVG_STEAM"
270 shift;
271 ;;
272 --template)
273
274 if [ -f "main.c" ]; then error "main.c already exists";
275 elif [ -f "vg_config.h" ]; then error "vg_config.h already exists";
276 elif [ -f "vg.conf" ]; then error "vg.conf already exists";
277 else
278 cp $vg_root/src/template/main.c main.c
279 cp $vg_root/src/template/vg_config.h vg_config.h
280 cp $vg_root/src/template/vg.conf vg.conf
281 fi
282
283 shift;
284 ;;
285 --)
286 shift
287 break
288 ;;
289 *)
290 error "programming error"
291 break
292 ;;
293 esac
294 done
295
296 if [[ -f vg.conf ]]; then
297 source vg.conf
298 else
299 error "Directory is not a VG project"
300 exit 1
301 fi
302
303 detect_os(){
304 if [[ "$OSTYPE" != "linux-gnu"* ]]; then
305 host_os=win32
306 target_os_windows
307 else
308 host_os=linux
309 target_os_linux
310 fi
311 }
312
313 detect_os
314
315 titleit " vgc ver: $vg_version\n host: $host_os"
316 logit " assets: $opt_assets"
317 logit " release: $opt_release"
318 logit " play: $opt_play"
319 logit " build-linux: $opt_linux"
320 logit "build-windows: $opt_windows"
321 logit " build-tools: $opt_tools"
322 logit " steam: $opt_steam"
323 logit " miniaudio: $opt_recompile_mini_audio"
324
325 # Main build steps
326
327 if [ $opt_linux = true ]; then target_os_linux; compile_main; fi
328 if [ $opt_windows = true ]; then
329 target_os_windows
330 compile_main
331
332 cp $vg_root/dep/glfw/glfw3.dll $target_dir/glfw3.dll
333 fi
334 if [ $opt_tools = true ]; then detect_os; compile_tools; fi
335 if [ $opt_assets = true ]; then compile_assets; fi
336
337 if [ $opt_play = true ]; then
338
339 if [ $host_os = linux ]; then
340 target_os_linux
341 else
342 target_os_windows
343 fi
344
345 echo ""
346 logit "======= exec: $target_dir/$vg_target$target_ext ======="
347 echo ""
348
349 cd $target_dir
350 ./$vg_target$target_ext
351 cd ./../
352
353 fi