new maths functions
[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="-lglfw3dll -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 -DVG_RELEASE"
69 else
70 target_opts="-ggdb3"
71 fi
72 }
73
74 target_os_linux(){
75 target_ext=""
76 target_compiler="clang"
77 target_libs="-lGL -lglfw3 -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 -DVG_RELEASE"
86 else
87 target_opts="-O0 -fsanitize=address -ggdb3 -fno-omit-frame-pointer"
88 fi
89 }
90
91 precompile_x(){
92 if ! [ -x "$(command -v $target_compiler)" ]; then
93 warning "Compiler $target_compiler is not installed. Skipping item"
94 return 0
95 fi
96 sources="$1"
97 output="-o $2"
98 cmd="$target_compiler $target_standard $target_opts $target_platform_include
99 -c $sources
100 $output"
101
102 logit " $cmd"
103 $cmd
104
105 checkfatal
106 success "$2 built"
107 }
108
109 compile_x(){
110 if ! [ -x "$(command -v $target_compiler)" ]; then
111 warning "Compiler $target_compiler is not installed. Skipping item"
112 return 0
113 fi
114
115 include="-I. -I$vg_root/dep -I$vg_root/src"
116 libs="-L./ -L$vg_root/dep/glfw -L$vg_root/dep/steam"
117
118 steam_part=""
119 if [ "$opt_steam" != "" ]; then
120 steam_part="$opt_steam $target_libs_steam"
121 fi
122
123 warnings="-Wall -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable"
124 sources="$1 $vg_root/dep/glad/glad.c $target_miniaudio"
125 output="-o $2$target_ext"
126
127 cmd="$target_compiler -D_REENTRANT $target_standard $target_opts $target_platform_include
128 $warnings
129 $include
130 $libs
131 $sources
132 $output
133 $target_libs $steam_part
134 -Wl,-rpath=./"
135
136 logit " $cmd\n"
137 $cmd
138
139 checkfatal
140 success "$2$target_ext built"
141 }
142
143 compile_main(){
144 titleit "Main build: $target_dir"
145 mkdir -p $target_dir
146
147 if [ "$opt_recompile_mini_audio" == true ]; then
148 precompile_x $vg_root/dep/dr_soft/miniaudio_impl.c $target_miniaudio
149 fi
150
151 compile_x $vg_src $target_dir/$vg_target
152 echo ""
153 logit "Setting up build structure"
154
155 # Setup build folder
156 mkdir $target_dir/cfg -p
157 mkdir $target_dir/textures -p
158 mkdir $target_dir/sound -p
159 mkdir $target_dir/maps -p
160 mkdir $target_dir/sav -p
161
162 # Copy libraries
163 if [ "$opt_steam" != "" ]; then
164 cp $vg_root/dep/steam/$target_steam_api $target_dir/$target_steam_api
165 fi
166
167 # Clear and copy assets
168 rm -r $target_dir/textures
169 rm -r $target_dir/sound
170 rm -r $target_dir/maps
171
172 cp -r .temp_textures $target_dir
173 mv $target_dir/.temp_textures $target_dir/textures
174 cp -r sound $target_dir
175 cp -r maps $target_dir
176 cp -r models $target_dir
177 }
178
179 compile_tools(){
180 # These should only be compiled for native platform
181 titleit "Tools"
182
183 mkdir $vg_root/bin -p
184
185 steam_prev=$opt_steam
186 opt_steam=""
187
188 compile_x $vg_root/src/fontcomp.c $vg_root/bin/fontcomp
189 compile_x $vg_root/src/texsheet.c $vg_root/bin/texsheet
190 compile_x $vg_root/src/qoiconv.c $vg_root/bin/qoiconv
191
192 opt_steam=$steam_prev
193 }
194
195 compile_assets(){
196 titleit "Assets"
197 [[ -d .temp_textures ]] && rm -r .temp_textures
198 mkdir .temp_textures
199
200 # Convert all png to qoi
201 echo "Compile textures:"
202 for f in textures/*.png;
203 do logit " qoi: $f";
204 $vg_root/bin/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
205 done
206
207 if [[ -d "textures_combine" ]]; then
208 # Autocombine textures
209 echo " [combine]:"
210
211 auto_combine=""
212 cd textures_combine
213 for f in *.png;
214 do logit " combine: $f";
215 auto_combine="$auto_combine $f"
216 done
217 $vg_root/bin/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
218 cd ..
219 fi
220
221 # Compile font file
222 echo ""
223 echo "Compile fonts:"
224 $vg_root/bin/fontcomp$target_ext $vg_root/src/fonts/vg_font.png $vg_root/src/vg/vg_pxfont.h
225 }
226
227 # ==============================================================
228 # Compile process
229
230 options=rptlwa
231 longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full,miniaudio,template
232 parsed=$(getopt --options=$options --longoptions=$longopts --name "vgc" -- "$@")
233
234 if [ $? -ne 0 ]; then
235 error "getopt failed"
236 exit 1
237 fi
238
239 eval set -- "$parsed"
240 while true; do
241 case "$1" in
242 -a|--assets)
243 opt_assets=true
244 shift;
245 ;;
246 -r|--release)
247 opt_release=true
248 shift;
249 ;;
250 -p|--play)
251 opt_play=true
252 shift;
253 ;;
254 -l|--build-linux)
255 opt_linux=true
256 shift;
257 ;;
258 -w|--build-windows)
259 opt_windows=true
260 shift;
261 ;;
262 -t|--build-tools)
263 opt_tools=true
264 shift;
265 ;;
266 --full)
267 opt_assets=true
268 opt_release=true
269 opt_linux=true
270 opt_windows=true
271 opt_tools=true
272 opt_steam="-DVG_STEAM"
273 opt_recompile_mini_audio=true
274 shift;
275 ;;
276 --miniaudio)
277 opt_recompile_mini_audio=true
278 shift;
279 ;;
280 --steam)
281 opt_steam="-DVG_STEAM"
282 shift;
283 ;;
284 --template)
285
286 if [ -f "main.c" ]; then error "main.c already exists";
287 elif [ -f "vg_config.h" ]; then error "vg_config.h already exists";
288 elif [ -f "vg.conf" ]; then error "vg.conf already exists";
289 else
290 cp $vg_root/src/template/main.c main.c
291 cp $vg_root/src/template/vg_config.h vg_config.h
292 cp $vg_root/src/template/vg.conf vg.conf
293 fi
294
295 shift;
296 ;;
297 --)
298 shift
299 break
300 ;;
301 *)
302 error "programming error"
303 break
304 ;;
305 esac
306 done
307
308 if [[ -f vg.conf ]]; then
309 source vg.conf
310 else
311 error "Directory is not a VG project"
312 exit 1
313 fi
314
315 detect_os(){
316 if [[ "$OSTYPE" != "linux-gnu"* ]]; then
317 host_os=win32
318 target_os_windows
319 else
320 host_os=linux
321 target_os_linux
322 fi
323 }
324
325 detect_os
326
327 titleit " vgc ver: $vg_version\n host: $host_os"
328 logit " assets: $opt_assets"
329 logit " release: $opt_release"
330 logit " play: $opt_play"
331 logit " build-linux: $opt_linux"
332 logit "build-windows: $opt_windows"
333 logit " build-tools: $opt_tools"
334 logit " steam: $opt_steam"
335 logit " miniaudio: $opt_recompile_mini_audio"
336
337 # Main build steps
338
339 if [ $opt_tools = true ]; then detect_os; compile_tools; fi
340 if [ $opt_assets = true ]; then compile_assets; fi
341 if [ $opt_linux = true ]; then target_os_linux; compile_main; fi
342 if [ $opt_windows = true ]; then
343 target_os_windows
344 compile_main
345
346 cp $vg_root/dep/glfw/glfw3.dll $target_dir/glfw3.dll
347 fi
348
349 success "Build completed (check compiler results)"
350
351 if [ $opt_play = true ]; then
352
353 if [ $host_os = linux ]; then
354 target_os_linux
355 else
356 target_os_windows
357 fi
358
359 echo ""
360 logit "======= exec: $target_dir/$vg_target$target_ext ======="
361 echo ""
362
363 cd $target_dir
364 ./$vg_target$target_ext
365 cd ./../
366
367 fi