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