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