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