3d options
[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"
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 -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"
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 -D_REENTRANT $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 -Wstrict-aliasing=3 -Wno-unused-function"
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 }
177
178 compile_tools(){
179 # These should only be compiled for native platform
180 titleit "Tools"
181
182 mkdir $vg_root/bin -p
183
184 steam_prev=$opt_steam
185 opt_steam=""
186
187 compile_x $vg_root/src/fontcomp.c $vg_root/bin/fontcomp
188 compile_x $vg_root/src/texsheet.c $vg_root/bin/texsheet
189 compile_x $vg_root/src/qoiconv.c $vg_root/bin/qoiconv
190
191 opt_steam=$steam_prev
192 }
193
194 compile_assets(){
195 titleit "Assets"
196 [[ -d .temp_textures ]] && rm -r .temp_textures
197 mkdir .temp_textures
198
199 # Convert all png to qoi
200 echo "Compile textures:"
201 for f in textures/*.png;
202 do logit " qoi: $f";
203 $vg_root/bin/qoiconv$target_ext $f .temp_textures/"$(basename "$f" .png).qoi"
204 done
205
206 # Autocombine textures
207 echo " [combine]:"
208
209 auto_combine=""
210 cd textures_combine
211 for f in *.png;
212 do logit " combine: $f";
213 auto_combine="$auto_combine $f"
214 done
215 $vg_root/bin/texsheet$taget_ext ../.temp_textures/autocombine.qoi ../sprites_autocombine.h sprites_auto_combine $auto_combine
216 cd ..
217
218 # Compile font file
219 echo ""
220 echo "Compile fonts:"
221 $vg_root/bin/fontcomp$target_ext $vg_root/src/fonts/vg_font.png $vg_root/src/vg/vg_pxfont.h
222 }
223
224 # ==============================================================
225 # Compile process
226
227 options=rptlwa
228 longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full,miniaudio,template
229 parsed=$(getopt --options=$options --longoptions=$longopts --name "vgc" -- "$@")
230
231 if [ $? -ne 0 ]; then
232 error "getopt failed"
233 exit 1
234 fi
235
236 eval set -- "$parsed"
237 while true; do
238 case "$1" in
239 -a|--assets)
240 opt_assets=true
241 shift;
242 ;;
243 -r|--release)
244 opt_release=true
245 shift;
246 ;;
247 -p|--play)
248 opt_play=true
249 shift;
250 ;;
251 -l|--build-linux)
252 opt_linux=true
253 shift;
254 ;;
255 -w|--build-windows)
256 opt_windows=true
257 shift;
258 ;;
259 -t|--build-tools)
260 opt_tools=true
261 shift;
262 ;;
263 --full)
264 opt_assets=true
265 opt_release=true
266 opt_linux=true
267 opt_windows=true
268 opt_tools=true
269 opt_steam="-DVG_STEAM"
270 opt_recompile_mini_audio=true
271 shift;
272 ;;
273 --miniaudio)
274 opt_recompile_mini_audio=true
275 shift;
276 ;;
277 --steam)
278 opt_steam="-DVG_STEAM"
279 shift;
280 ;;
281 --template)
282
283 if [ -f "main.c" ]; then error "main.c already exists";
284 elif [ -f "vg_config.h" ]; then error "vg_config.h already exists";
285 elif [ -f "vg.conf" ]; then error "vg.conf already exists";
286 else
287 cp $vg_root/src/template/main.c main.c
288 cp $vg_root/src/template/vg_config.h vg_config.h
289 cp $vg_root/src/template/vg.conf vg.conf
290 fi
291
292 shift;
293 ;;
294 --)
295 shift
296 break
297 ;;
298 *)
299 error "programming error"
300 break
301 ;;
302 esac
303 done
304
305 if [[ -f vg.conf ]]; then
306 source vg.conf
307 else
308 error "Directory is not a VG project"
309 exit 1
310 fi
311
312 detect_os(){
313 if [[ "$OSTYPE" != "linux-gnu"* ]]; then
314 host_os=win32
315 target_os_windows
316 else
317 host_os=linux
318 target_os_linux
319 fi
320 }
321
322 detect_os
323
324 titleit " vgc ver: $vg_version\n host: $host_os"
325 logit " assets: $opt_assets"
326 logit " release: $opt_release"
327 logit " play: $opt_play"
328 logit " build-linux: $opt_linux"
329 logit "build-windows: $opt_windows"
330 logit " build-tools: $opt_tools"
331 logit " steam: $opt_steam"
332 logit " miniaudio: $opt_recompile_mini_audio"
333
334 # Main build steps
335
336 if [ $opt_tools = true ]; then detect_os; compile_tools; fi
337 if [ $opt_assets = true ]; then compile_assets; fi
338 if [ $opt_linux = true ]; then target_os_linux; compile_main; fi
339 if [ $opt_windows = true ]; then
340 target_os_windows
341 compile_main
342
343 cp $vg_root/dep/glfw/glfw3.dll $target_dir/glfw3.dll
344 fi
345
346 success "Build completed (check compiler results)"
347
348 if [ $opt_play = true ]; then
349
350 if [ $host_os = linux ]; then
351 target_os_linux
352 else
353 target_os_windows
354 fi
355
356 echo ""
357 logit "======= exec: $target_dir/$vg_target$target_ext ======="
358 echo ""
359
360 cd $target_dir
361 ./$vg_target$target_ext
362 cd ./../
363
364 fi