DEADSOFTWARE

add clang support
[cpc.git] / make.sh
1 #! /bin/sh
3 set -e
5 ###^^^^^^^^^^^^^^^^^^###
6 ### Global variables ###
7 ###__________________###
9 _exec="make.sh"
10 _this="$(dirname "$(readlink -f "$0")")"
11 _cpu=
12 _target=
13 _system=
14 _compiler=
15 _linker=
16 _docompile=true
17 _dolink=true
18 _out="$_this/bin"
20 _useposix=false
22 export CPCFLAGS="$CPCFLAGS"
23 export CPLFLAGS="$CPLFLAGS"
25 ###^^^^^^^^^^^###
26 ### Functions ###
27 ###___________###
29 usage() {
30 echo "Usage: make.sh [options] cpu target os"
31 echo "Options:"
32 echo " -c path Path to compiler binary"
33 echo " -l path Path to linker binary"
34 echo " -o path Path to output binaries"
35 echo " -b Do not compile"
36 echo " -x Do not link"
37 echo "Processors:"
38 echo " 486 Intel 486+"
39 echo " arm ARM 32-bit"
40 echo "Targets:"
41 echo " native Native"
42 echo " cpfront Generic C"
43 echo "Operation systems:"
44 echo " linux GNU/Linux"
45 echo " cygwin Cygwin"
46 echo "Environment variables:"
47 echo " CC C compiler binary"
48 echo " CFLAGS C compiler options"
49 echo " CPCFLAGS CPC compiler options"
50 echo " CPLFLAGS CPL linker options"
51 exit 2
52 }
54 error() {
55 echo "$_exec:" "$@"
56 exit 1
57 }
59 copy_source() {
60 for _src
61 do
62 if test -d "$_this/src/$_src"; then
63 find "$_this/src/$_src" -mindepth 1 -maxdepth 1 -exec cp -rt "$_out" -- {} +
64 fi
65 done
66 }
68 native_compile() {
69 "$_compiler" $CPCFLAGS -legacy "$@"
70 }
72 native_link() {
73 if $_dolink; then
74 local _outexe="$1";
75 local _outsystem="$_system"
76 if [ "$_system" = "cygwin" ]; then
77 _outexe="${_outexe}.exe"
78 _outsystem="win32"
79 fi
80 shift
81 "$_linker" $CPLFALGS -os "$_outsystem" -kernel Kernel -main Kernel -legacycodedir . -o "$_outexe" "$@"
82 fi
83 }
85 cpfront_import_list() {
86 echo
87 while [ "$1" != "" ]
88 do
89 echo -n " $1"
90 shift
91 if ! [ -z "$1" ]; then
92 echo ","
93 fi
94 done
95 }
97 cpfront_main_module() {
98 local _name="$1"
99 shift
100 echo "MODULE ${_name};"
101 echo
102 echo " IMPORT $(cpfront_import_list "$@");"
103 echo
104 echo "END ${_name}."
107 cpfront_compile() {
108 "$_compiler" $CPCFLAGS -outcode CodeC -outsym SymC "$@"
111 cpfront_link() {
112 local _main="$1"
113 if $_docompile; then
114 cpfront_main_module "$@" > "${_main}.cp"
115 "$_compiler" $CPCFLAGS -outcode CodeC -outsym SymC -main "${_main}.cp"
116 fi
117 shift
118 if $_dolink; then
119 local _list=
120 for _module in "$@" "${_main}"
121 do
122 _list="$_list ${_out}/CodeC/${_module}.c"
123 done
124 local _cc_cflags=
125 case "$CC" in
126 *gcc) _cc_cflags="-std=c89 -Wno-int-conversion -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types -Wno-implicit-function-declaration" ;;
127 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" ;;
128 *) _cc_cflags="" ;;
129 esac
130 local _cpu_cflags=
131 case "$_cpu" in
132 486) _cpu_cflags="-m32" ;;
133 arm) _cpu_cflags="" ;;
134 *) error "cpfront_link(): unsupported cpu $_cpu" ;;
135 esac
136 local _system_cflags=
137 case "$_system" in
138 cygwin) _system_cflags="-liconv" ;;
139 *) _system_cflags="" ;;
140 esac
141 "$CC" -g -D_XOPEN_SOURCE=700 $_cc_cflags $_cpu_cflags $CFLAGS -o "${_main}" -I "$_this/C" "$_this/C/SYSTEM.c" $_list -lm -ldl -lffi $_system_cflags
142 fi
145 compile() {
146 case "$_target" in
147 native) native_compile "$@" ;;
148 cpfront) cpfront_compile "$@" ;;
149 *) error "compile(): unknown target $_target" ;;
150 esac
153 link() {
154 case "$_target" in
155 native) native_link "$@" ;;
156 cpfront) cpfront_link "$@" ;;
157 *) error "link(): unknown target $_target" ;;
158 esac
161 compile_all() {
162 ###^^^^^^^^^^^^^^^^^^^^^^^^###
163 ### Compile POSIX bindings ###
164 ###________________________###
166 if $_useposix; then
167 compile Posix/Mod/Ctypes.cp \
168 Posix/Mod/Csys_types.cp \
169 Posix/Mod/Cstdlib.cp Posix/Mod/Cstdio.cp Posix/Mod/Cunistd.cp \
170 Posix/Mod/Cdirent.cp Posix/Mod/Clocale.cp Posix/Mod/Ctime.cp \
171 Posix/Mod/Csys_stat.cp Posix/Mod/Cfcntl.cp Posix/Mod/Cerrno.cp \
172 Posix/Mod/Ciconv.cp Posix/Mod/Cwctype.cp Posix/Mod/Csys_mman.cp \
173 Posix/Mod/Cdlfcn.cp Posix/Mod/Csignal.cp Posix/Mod/Csetjmp.cp \
174 Posix/Mod/Clibgen.cp \
175 Posix/Mod/Cmacro.cp
176 if [ "$_target" = "cpfront" ]; then
177 compile Lib/Mod/FFI.cp
178 fi
179 fi
181 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
182 ### Compile BlackBox Framework ###
183 ###____________________________###
185 compile System/Mod/Int.odc
186 if [ "$_target" = "native" ]; then
187 compile System/Mod/Long.odc
188 compile System/Mod/Math.odc System/Mod/SMath.odc
189 else
190 compile System/Mod/Math.cp System/Mod/SMath.cp
191 fi
192 compile System/Mod/Kernel.cp \
193 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
194 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Meta.odc \
195 System/Mod/Services.odc System/Mod/Integers.odc
197 if [ "$_target" = "native" ]; then
198 mv -t System Code Sym
199 fi
201 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
202 ### Compile Linux Host subsystem ###
203 ###______________________________###
205 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
206 Host/Mod/Files.cp
208 ###^^^^^^^^^^^^^^^^^^^^^^^###
209 ### Compile Dev subsystem ###
210 ###_______________________###
212 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
213 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
214 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
216 ###^^^^^^^^^^^^^^^^^^^^^^^^###
217 ### Compile Dev2 subsystem ###
218 ###________________________###
220 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
221 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
222 Dev2/Mod/LnkWritePe.odc
224 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
225 ### Compile CPfront subsystem ###
226 ###___________________________###
228 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
230 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
231 ### Compile bbdsw-specific modules ###
232 ###________________________________###
234 if [ "$_target" = "native" ]; then
235 compile Dsw/Mod/Debug.odc
236 fi
237 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
238 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
241 link_all() {
242 local _debug_module=
243 if [ "$_target" = "native" ]; then
244 _debug_module=DswDebug
245 fi
247 link cpc486 \
248 PosixCtypes PosixCmacro \
249 Kernel Console Files Dates Math Strings Services Log \
250 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
251 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
252 DevCPL486 DevCPC486 DevCPV486 \
253 DswDocuments DswCompiler486Main
255 link cpl486 \
256 PosixCtypes PosixCmacro \
257 Kernel Console Files Math Strings Services Log \
258 HostLang HostConsole HostFiles DswLog $_debug_module \
259 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
260 Dev2LnkWriteElfStatic Dev2LnkWritePe \
261 DswLinker486Main
263 link cpfront \
264 PosixCtypes PosixCmacro \
265 Kernel Console Files Dates Math Strings Services Log \
266 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
267 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
268 CPfrontCPG CPfrontCPC CPfrontCPV\
269 DswDocuments DswCompilerCPfrontMain
271 if $_dolink; then
272 chmod a+x cpc486 cpl486 cpfront
273 fi
276 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
277 ### Parse arguments and options ###
278 ###_____________________________###
280 while getopts c:l:o:bxh _name
281 do
282 case "$_name" in
283 c) _compiler="$OPTARG" ;;
284 l) _linker="$OPTARG" ;;
285 o) _out="$OPTARG" ;;
286 b) _docompile=false ;;
287 x) _dolink=false ;;
288 h|?) usage ;;
289 esac
290 done
292 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
293 usage
294 fi
296 shift $(($OPTIND - 1))
297 _cpu="$1"
298 _target="$2"
299 _system="$3"
301 if [ -z "$CC" ]; then
302 export CC=cc
303 fi
305 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
306 ### Check for supported cpu/target/os ###
307 ###___________________________________###
309 case "$_cpu" in
310 386|486|586|686) _cpu=486 ;;
311 arm|armv6|armv7) _cpu=arm ;;
312 "") error "cpu not specified" ;;
313 *) error "unsupported cpu $_cpu" ;;
314 esac
316 case "$_target" in
317 native) _target=native ;;
318 cpfront|c) _target=cpfront ;;
319 "") error "target not specified" ;;
320 *) error "unsupported target $_target" ;;
321 esac
323 case "$_system" in
324 linux) _useposix=true ;;
325 cygwin) _useposix=true ;;
326 "") error "operation system not specified" ;;
327 *) error "unsuported operation system $_system" ;;
328 esac
330 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
331 ### Select default compiler if not specified ###
332 ###__________________________________________###
334 if [ -z "$_compiler" ]; then
335 case "$_target" in
336 native)
337 case "$_cpu" in
338 486) _compiler=cpc486 ;;
339 *) error "no standard compiler for cpu $_cpu" ;;
340 esac
341 ;;
342 cpfront) _compiler=cpfront ;;
343 *) error "no standard compiler for target $_target" ;;
344 esac
345 fi
347 if [ -z "$_linker" ]; then
348 case "$_target" in
349 native)
350 case "$_cpu" in
351 486) _linker=cpl486 ;;
352 *) error "no standard linker for cpu $_cpu" ;;
353 esac
354 ;;
355 cpfront) _linker= ;;
356 *) error "no standard linker for target $_target" ;;
357 esac
358 fi
360 if $_docompile; then
361 if command -v "$_compiler" > /dev/null; then
362 _compiler="$(command -v "$_compiler")"
363 else
364 error "compiler not installed!"
365 fi
366 fi
368 if $_dolink && [ "$_target" != "cpfront" ]; then
369 if command -v "$_linker" > /dev/null; then
370 _linker="$(command -v "$_linker")"
371 else
372 error "linker not installed!"
373 fi
374 fi
376 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
377 ### Copy sources for changed system ###
378 ###_________________________________###
380 if $_docompile; then
381 rm -rf -- "$_out"
382 fi
384 mkdir -p -- "$_out"
385 _out="$(readlink -f "$_out")"
386 copy_source "generic" "$_cpu"
387 if $_useposix; then
388 copy_source "posix/generic" "posix/$_cpu"
389 fi
390 copy_source "$_system/generic" "$_system/$_cpu"
391 copy_source "$_target/generic" "$_target/$_cpu"
392 if $_useposix; then
393 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
394 fi
395 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
396 cd "$_out"
398 ###^^^^^^^^^^^^^^^###
399 ### Build modules ###
400 ###_______________###
402 if $_docompile; then
403 compile_all
404 fi
406 ###^^^^^^^^^^^^^^###
407 ### Link modules ###
408 ###______________###
410 link_all