DEADSOFTWARE

fix directive/expression interpretation in false blocks
[cpc.git] / make.sh
1 #! /bin/bash
3 set -e
5 abspath() {
6 [[ "$1" == /* ]] && echo "$1" || echo "$(pwd)/$1"
7 }
9 ###^^^^^^^^^^^^^^^^^^###
10 ### Global variables ###
11 ###__________________###
13 _exec="make.sh"
14 _this="$(dirname "$(abspath "$0")")"
15 _cpu=
16 _target=
17 _system=
18 _compiler=
19 _linker=
20 _docompile=true
21 _dolink=true
22 _out="$_this/bin"
24 _useposix=false
26 export CPCFLAGS="$CPCFLAGS"
27 export CPLFLAGS="$CPLFLAGS"
29 ###^^^^^^^^^^^###
30 ### Functions ###
31 ###___________###
33 usage() {
34 echo "Usage: make.sh [options] cpu target os"
35 echo "Options:"
36 echo " -c path Path to compiler binary"
37 echo " -l path Path to linker binary"
38 echo " -o path Path to output binaries"
39 echo " -b Do not compile"
40 echo " -x Do not link"
41 echo "Processors:"
42 echo " 486 Intel 486+"
43 echo " arm ARM 32-bit"
44 echo " powerpc PowerPC 32-bit"
45 echo "Targets:"
46 echo " native Native"
47 echo " cpfront Generic C"
48 echo "Operation systems:"
49 echo " linux GNU/Linux"
50 echo " cygwin Cygwin"
51 echo " osx Mac OS X"
52 echo "Environment variables:"
53 echo " CC C compiler binary"
54 echo " CFLAGS C compiler options"
55 echo " CPCFLAGS CPC compiler options"
56 echo " CPLFLAGS CPL linker options"
57 exit 2
58 }
60 error() {
61 echo "$_exec:" "$@"
62 exit 1
63 }
65 copy_source() {
66 for _src
67 do
68 if test -d "$_this/src/$_src"; then
69 find "$_this/src/$_src" -mindepth 1 -maxdepth 1 -exec cp -r {} "$_out" \;
70 fi
71 done
72 }
74 native_compile() {
75 "$_compiler" $CPCFLAGS -legacy "$@"
76 }
78 native_link() {
79 if $_dolink; then
80 local _outexe="$1";
81 local _outsystem="$_system"
82 if [ "$_system" = "cygwin" ]; then
83 _outexe="${_outexe}.exe"
84 _outsystem="win32"
85 elif [ "$_system" = "osx" ]; then
86 _outexe="${_outexe}.out"
87 fi
88 shift
89 "$_linker" $CPLFALGS -os "$_outsystem" -kernel Kernel -main Kernel -legacycodedir . -o "$_outexe" "$@"
90 fi
91 }
93 cpfront_import_list() {
94 echo
95 while [ "$1" != "" ]
96 do
97 echo -n " $1"
98 shift
99 if ! [ -z "$1" ]; then
100 echo ","
101 fi
102 done
105 cpfront_main_module() {
106 local _name="$1"
107 shift
108 echo "MODULE ${_name};"
109 echo
110 echo " IMPORT $(cpfront_import_list "$@");"
111 echo
112 echo "END ${_name}."
115 cpfront_compile() {
116 "$_compiler" $CPCFLAGS -outcode CodeC -outsym SymC "$@"
119 cpfront_link() {
120 local _main="$1"
121 if $_docompile; then
122 cpfront_main_module "$@" > "${_main}.cp"
123 "$_compiler" $CPCFLAGS -outcode CodeC -outsym SymC -main "${_main}.cp"
124 fi
125 shift
126 if $_dolink; then
127 local _list=
128 for _module in "$@" "${_main}"
129 do
130 _list="$_list ${_out}/CodeC/${_module}.c"
131 done
132 local _cc_cflags=
133 case "$CC" in
134 *gcc) _cc_cflags="-std=c89 -Wno-int-conversion -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types -Wno-implicit-function-declaration" ;;
135 *gcc-4.2) _cc_cflags="-std=c89 -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -Wno-implicit-function-declaration" ;;
136 clang|clang-*) _cc_cflags="-std=c89 -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-pointer-sign -Wno-unused-value -Wno-return-type" ;;
137 *tcc) _cc_cflags="-std=c89 -w -fsigned-char" ;;
138 *) _cc_cflags="" ;;
139 esac
140 local _cpu_cflags=
141 case "$_cpu" in
142 486) _cpu_cflags="-m32" ;;
143 arm) _cpu_cflags="" ;;
144 powerpc) _cpu_cflags="-m32" ;;
145 *) error "cpfront_link(): unsupported cpu $_cpu" ;;
146 esac
147 local _system_cflags=
148 case "$_system" in
149 cygwin) _system_cflags="-liconv" ;;
150 osx) _system_cflags="-D_DARWIN_C_SOURCE -liconv" ;;
151 *) _system_cflags="" ;;
152 esac
153 local _out_exe="${_main}"
154 case "$_system" in
155 cygwin) _out_exe="${_main}.exe" ;;
156 osx) _out_exe="${_main}.out" ;;
157 *) ;;
158 esac
159 "$CC" -g -D_XOPEN_SOURCE=700 $_cc_cflags $_cpu_cflags $CFLAGS -o "${_out_exe}" -I "$_this/C" "$_this/C/SYSTEM.c" $_list -lm -ldl -lffi $_system_cflags
160 fi
163 compile() {
164 case "$_target" in
165 native) native_compile "$@" ;;
166 cpfront) cpfront_compile "$@" ;;
167 *) error "compile(): unknown target $_target" ;;
168 esac
171 link() {
172 case "$_target" in
173 native) native_link "$@" ;;
174 cpfront) cpfront_link "$@" ;;
175 *) error "link(): unknown target $_target" ;;
176 esac
179 compile_all() {
180 ###^^^^^^^^^^^^^^^^^^^^^^^^###
181 ### Compile POSIX bindings ###
182 ###________________________###
184 if $_useposix; then
185 compile Posix/Mod/Ctypes.cp \
186 Posix/Mod/Csys_types.cp \
187 Posix/Mod/Cstdlib.cp Posix/Mod/Cstdio.cp Posix/Mod/Cunistd.cp \
188 Posix/Mod/Cdirent.cp Posix/Mod/Clocale.cp Posix/Mod/Ctime.cp \
189 Posix/Mod/Csys_stat.cp Posix/Mod/Cfcntl.cp Posix/Mod/Cerrno.cp \
190 Posix/Mod/Ciconv.cp Posix/Mod/Cwctype.cp Posix/Mod/Csys_mman.cp \
191 Posix/Mod/Cdlfcn.cp Posix/Mod/Csignal.cp Posix/Mod/Csetjmp.cp \
192 Posix/Mod/Clibgen.cp \
193 Posix/Mod/Cmacro.cp
194 if [ "$_target" = "cpfront" ]; then
195 compile Lib/Mod/FFI.cp
196 fi
197 fi
199 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
200 ### Compile BlackBox Framework ###
201 ###____________________________###
203 compile System/Mod/Int.odc
204 if [ "$_target" = "native" ]; then
205 compile System/Mod/Long.odc
206 compile System/Mod/Math.odc System/Mod/SMath.odc
207 else
208 compile System/Mod/Math.cp System/Mod/SMath.cp
209 fi
210 compile System/Mod/Kernel.cp \
211 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
212 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Meta.odc \
213 System/Mod/Services.odc System/Mod/Integers.odc
215 if [ "$_target" = "native" ]; then
216 mv -t System Code Sym
217 fi
219 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
220 ### Compile Linux Host subsystem ###
221 ###______________________________###
223 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
224 Host/Mod/Files.cp
226 ###^^^^^^^^^^^^^^^^^^^^^^^###
227 ### Compile Dev subsystem ###
228 ###_______________________###
230 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPR.cp Dev/Mod/CPS.odc \
231 Dev/Mod/CPB.odc Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc \
232 Dev/Mod/CPL486.odc Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
234 ###^^^^^^^^^^^^^^^^^^^^^^^^###
235 ### Compile Dev2 subsystem ###
236 ###________________________###
238 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
239 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
240 Dev2/Mod/LnkWritePe.odc
242 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
243 ### Compile CPfront subsystem ###
244 ###___________________________###
246 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
248 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
249 ### Compile bbdsw-specific modules ###
250 ###________________________________###
252 if [ "$_target" = "native" ]; then
253 compile Dsw/Mod/Debug.odc
254 fi
255 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
256 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
259 link_all() {
260 local _debug_module=
261 if [ "$_target" = "native" ]; then
262 _debug_module=DswDebug
263 fi
265 link cpc486 \
266 PosixCtypes PosixCmacro \
267 Kernel Console Files Dates Math Strings Services Log \
268 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
269 DevCPM DevCPT DevCPR DevCPS DevCPB DevCPP DevCPE DevCPH \
270 DevCPL486 DevCPC486 DevCPV486 \
271 DswDocuments DswCompiler486Main
273 link cpl486 \
274 PosixCtypes PosixCmacro \
275 Kernel Console Files Math Strings Services Log \
276 HostLang HostConsole HostFiles DswLog $_debug_module \
277 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
278 Dev2LnkWriteElfStatic Dev2LnkWritePe \
279 DswLinker486Main
281 link cpfront \
282 PosixCtypes PosixCmacro \
283 Kernel Console Files Dates Math Strings Services Log \
284 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
285 DevCPM DevCPT DevCPR DevCPS DevCPB DevCPP DevCPE DevCPH \
286 CPfrontCPG CPfrontCPC CPfrontCPV\
287 DswDocuments DswCompilerCPfrontMain
289 if $_dolink; then
290 chmod a+x cpc486 cpl486 cpfront
291 fi
294 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
295 ### Parse arguments and options ###
296 ###_____________________________###
298 while getopts c:l:o:bxh _name
299 do
300 case "$_name" in
301 c) _compiler="$OPTARG" ;;
302 l) _linker="$OPTARG" ;;
303 o) _out="$OPTARG" ;;
304 b) _docompile=false ;;
305 x) _dolink=false ;;
306 h|?) usage ;;
307 esac
308 done
310 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
311 usage
312 fi
314 shift $(($OPTIND - 1))
315 _cpu="$1"
316 _target="$2"
317 _system="$3"
319 if [ -z "$CC" ]; then
320 export CC=cc
321 fi
323 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
324 ### Check for supported cpu/target/os ###
325 ###___________________________________###
327 case "$_cpu" in
328 386|486|586|686) _cpu=486 ;;
329 arm|armv6|armv7) _cpu=arm ;;
330 powerpc|ppc|ppc32) _cpu=powerpc ;;
331 "") error "cpu not specified" ;;
332 *) error "unsupported cpu $_cpu" ;;
333 esac
335 case "$_target" in
336 native) _target=native ;;
337 cpfront|c) _target=cpfront ;;
338 "") error "target not specified" ;;
339 *) error "unsupported target $_target" ;;
340 esac
342 case "$_system" in
343 linux) _useposix=true ;;
344 cygwin) _useposix=true ;;
345 osx) _useposix=true ;;
346 "") error "operation system not specified" ;;
347 *) error "unsuported operation system $_system" ;;
348 esac
350 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
351 ### Select default compiler if not specified ###
352 ###__________________________________________###
354 if [ -z "$_compiler" ]; then
355 case "$_target" in
356 native)
357 case "$_cpu" in
358 486) _compiler=cpc486 ;;
359 *) error "no standard compiler for cpu $_cpu" ;;
360 esac
361 ;;
362 cpfront) _compiler=cpfront ;;
363 *) error "no standard compiler for target $_target" ;;
364 esac
365 fi
367 if [ -z "$_linker" ]; then
368 case "$_target" in
369 native)
370 case "$_cpu" in
371 486) _linker=cpl486 ;;
372 *) error "no standard linker for cpu $_cpu" ;;
373 esac
374 ;;
375 cpfront) _linker= ;;
376 *) error "no standard linker for target $_target" ;;
377 esac
378 fi
380 if $_docompile; then
381 if command -v "$_compiler" > /dev/null; then
382 _compiler="$(command -v "$_compiler")"
383 else
384 error "compiler not installed!"
385 fi
386 fi
388 if $_dolink && [ "$_target" != "cpfront" ]; then
389 if command -v "$_linker" > /dev/null; then
390 _linker="$(command -v "$_linker")"
391 else
392 error "linker not installed!"
393 fi
394 fi
396 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
397 ### Copy sources for changed system ###
398 ###_________________________________###
400 if $_docompile; then
401 rm -rf -- "$_out"
402 fi
404 mkdir -p -- "$_out"
405 _out="$(abspath "$_out")"
406 copy_source "generic" "$_cpu"
407 if $_useposix; then
408 copy_source "posix/generic" "posix/$_cpu"
409 fi
410 copy_source "$_system/generic" "$_system/$_cpu"
411 copy_source "$_target/generic" "$_target/$_cpu"
412 if $_useposix; then
413 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
414 fi
415 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
416 cd "$_out"
418 ###^^^^^^^^^^^^^^^###
419 ### Build modules ###
420 ###_______________###
422 if $_docompile; then
423 compile_all
424 fi
426 ###^^^^^^^^^^^^^^###
427 ### Link modules ###
428 ###______________###
430 link_all