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