DEADSOFTWARE

added arm support via cpfront
[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 ###^^^^^^^^^^^###
23 ### Functions ###
24 ###___________###
26 usage() {
27 echo "Usage: make.sh [options] cpu target os"
28 echo "Options:"
29 echo " -c path Path to compiler binary"
30 echo " -l path Path to linker binary"
31 echo " -o path Path to output binaries"
32 echo " -b Do not compile"
33 echo " -x Do not link"
34 echo "Processors:"
35 echo " 486 Intel 486+"
36 echo " arm ARM 32-bit"
37 echo "Targets:"
38 echo " native Native"
39 echo " cpfront Generic C"
40 echo "Operation systems:"
41 echo " linux GNU/Linux"
42 echo "Environment variables:"
43 echo " CC C compiler binary"
44 echo " CFLAGS C compiler options"
45 exit 2
46 }
48 error() {
49 echo "$_exec:" "$@"
50 exit 1
51 }
53 copy_source() {
54 for _src
55 do
56 if test -d "$_this/src/$_src"; then
57 find "$_this/src/$_src" -mindepth 1 -maxdepth 1 -exec cp -rt "$_out" -- {} +
58 fi
59 done
60 }
62 native_compile() {
63 "$_compiler" -legacy "$@"
64 }
66 native_link() {
67 if $_dolink; then
68 "$_linker" -os "$_system" -kernel Kernel -main Kernel -legacycodedir . -o "$@"
69 fi
70 }
72 cpfront_import_list() {
73 echo
74 while [ "$1" != "" ]
75 do
76 echo -n " $1"
77 shift
78 if ! [ -z "$1" ]; then
79 echo ","
80 fi
81 done
82 }
84 cpfront_main_module() {
85 local _name="$1"
86 shift
87 echo "MODULE ${_name};\n\n IMPORT $(cpfront_import_list "$@");\n\nEND ${_name}."
88 }
90 cpfront_compile() {
91 "$_compiler" -outcode CodeC -outsym SymC "$@"
92 }
94 cpfront_link() {
95 local _main="$1"
96 if $_docompile; then
97 cpfront_main_module "$@" > "${_main}.cp"
98 "$_compiler" -outcode CodeC -outsym SymC -main "${_main}.cp"
99 fi
100 shift
101 if $_dolink; then
102 local _list=
103 for _module in "$@" "${_main}"
104 do
105 _list="$_list ${_out}/CodeC/${_module}.c"
106 done
107 local _cc_cflags=
108 case "$CC" in
109 *gcc) _cc_cflags="-Wno-int-conversion -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types -Wno-implicit-function-declaration" ;;
110 *) _cc_cflags="" ;;
111 esac
112 local _cpu_cflags=
113 case "$_cpu" in
114 486) _cpu_cflags="-m32" ;;
115 arm) _cpu_cflags="" ;;
116 *) error "cpfront_link(): unsupported cpu $_cpu" ;;
117 esac
118 "$CC" $_cc_cflags $_cpu_cflags -lm -ldl $CFLAGS -o "${_main}" -I "$_this/C" "$_this/C/SYSTEM.c" $_list
119 fi
122 compile() {
123 case "$_target" in
124 native) native_compile "$@" ;;
125 cpfront) cpfront_compile "$@" ;;
126 *) error "compile(): unknown target $_target" ;;
127 esac
130 link() {
131 case "$_target" in
132 native) native_link "$@" ;;
133 cpfront) cpfront_link "$@" ;;
134 *) error "link(): unknown target $_target" ;;
135 esac
138 compile_all() {
139 ###^^^^^^^^^^^^^^^^^^^^^^^^###
140 ### Compile POSIX bindings ###
141 ###________________________###
143 if $_useposix; then
144 compile Posix/Mod/Ctypes.cp \
145 Posix/Mod/Csys_types.cp \
146 Posix/Mod/Cstdlib.cp Posix/Mod/Cstdio.cp Posix/Mod/Cunistd.cp \
147 Posix/Mod/Cdirent.cp Posix/Mod/Clocale.cp Posix/Mod/Ctime.cp \
148 Posix/Mod/Csys_stat.cp Posix/Mod/Cfcntl.cp Posix/Mod/Cerrno.cp \
149 Posix/Mod/Ciconv.cp Posix/Mod/Cwctype.cp Posix/Mod/Csys_mman.cp \
150 Posix/Mod/Cdlfcn.cp Posix/Mod/Csignal.cp Posix/Mod/Csetjmp.cp \
151 Posix/Mod/Clibgen.cp \
152 Posix/Mod/Cmacro.cp
153 fi
155 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
156 ### Compile BlackBox Framework ###
157 ###____________________________###
159 compile System/Mod/Int.odc
160 if [ "$_target" = "native" ]; then
161 compile System/Mod/Long.odc
162 compile System/Mod/Math.odc System/Mod/SMath.odc
163 else
164 compile System/Mod/Math.cp System/Mod/SMath.cp
165 fi
166 compile System/Mod/Kernel.cp \
167 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
168 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Services.odc \
169 System/Mod/Integers.odc
171 if [ "$_target" = "native" ]; then
172 mv -t System Code Sym
173 fi
175 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
176 ### Compile Linux Host subsystem ###
177 ###______________________________###
179 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
180 Host/Mod/Files.cp
182 ###^^^^^^^^^^^^^^^^^^^^^^^###
183 ### Compile Dev subsystem ###
184 ###_______________________###
186 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
187 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
188 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
190 ###^^^^^^^^^^^^^^^^^^^^^^^^###
191 ### Compile Dev2 subsystem ###
192 ###________________________###
194 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
195 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
196 Dev2/Mod/LnkWritePe.odc
198 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
199 ### Compile CPfront subsystem ###
200 ###___________________________###
202 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
204 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
205 ### Compile bbdsw-specific modules ###
206 ###________________________________###
208 if [ "$_target" = "native" ]; then
209 compile Dsw/Mod/Debug.odc
210 fi
211 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
212 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
215 link_all() {
216 local _debug_module=
217 if [ "$_target" = "native" ]; then
218 _debug_module=DswDebug
219 fi
221 link cpc486 \
222 PosixCtypes PosixCmacro \
223 Kernel Console Files Dates Math Strings Services Log \
224 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
225 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
226 DevCPL486 DevCPC486 DevCPV486 \
227 DswDocuments DswCompiler486Main
229 link cpl486 \
230 PosixCtypes PosixCmacro \
231 Kernel Console Files Math Strings Services Log \
232 HostLang HostConsole HostFiles DswLog $_debug_module \
233 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
234 Dev2LnkWriteElfStatic Dev2LnkWritePe \
235 DswLinker486Main
237 link cpfront \
238 PosixCtypes PosixCmacro \
239 Kernel Console Files Dates Math Strings Services Log \
240 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
241 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
242 CPfrontCPG CPfrontCPC CPfrontCPV\
243 DswDocuments DswCompilerCPfrontMain
245 if $_dolink; then
246 chmod a+x cpc486 cpl486 cpfront
247 fi
250 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
251 ### Parse arguments and options ###
252 ###_____________________________###
254 while getopts c:l:o:bxh _name
255 do
256 case "$_name" in
257 c) _compiler="$OPTARG" ;;
258 l) _linker="$OPTARG" ;;
259 o) _out="$OPTARG" ;;
260 b) _docompile=false ;;
261 x) _dolink=false ;;
262 h|?) usage ;;
263 esac
264 done
266 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
267 usage
268 fi
270 shift $(($OPTIND - 1))
271 _cpu="$1"
272 _target="$2"
273 _system="$3"
275 if [ -z "$CC" ]; then
276 export CC=cc
277 fi
279 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
280 ### Check for supported cpu/target/os ###
281 ###___________________________________###
283 case "$_cpu" in
284 386|486|586|686) _cpu=486 ;;
285 arm|armv6|armv7) _cpu=arm ;;
286 "") error "cpu not specified" ;;
287 *) error "unsupported cpu $_cpu" ;;
288 esac
290 case "$_target" in
291 native) _target=native ;;
292 cpfront|c) _target=cpfront ;;
293 "") error "target not specified" ;;
294 *) error "unsupported target $_target" ;;
295 esac
297 case "$_system" in
298 linux) _useposix=true ;;
299 "") error "operation system not specified" ;;
300 *) error "unsuported operation system $_system" ;;
301 esac
303 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
304 ### Select default compiler if not specified ###
305 ###__________________________________________###
307 if [ -z "$_compiler" ]; then
308 case "$_target" in
309 native)
310 case "$_cpu" in
311 486) _compiler=cpc486 ;;
312 *) error "no standard compiler for cpu $_cpu" ;;
313 esac
314 ;;
315 cpfront) _compiler=cpfront ;;
316 *) error "no standard compiler for target $_target" ;;
317 esac
318 fi
320 if [ -z "$_linker" ]; then
321 case "$_target" in
322 native)
323 case "$_cpu" in
324 486) _linker=cpl486 ;;
325 *) error "no standard linker for cpu $_cpu" ;;
326 esac
327 ;;
328 cpfront) _linker= ;;
329 *) error "no standard linker for target $_target" ;;
330 esac
331 fi
333 if $_docompile; then
334 if command -v "$_compiler" > /dev/null; then
335 _compiler="$(command -v "$_compiler")"
336 else
337 error "compiler not installed!"
338 fi
339 fi
341 if $_dolink && [ "$_target" != "cpfront" ]; then
342 if command -v "$_linker" > /dev/null; then
343 _linker="$(command -v "$_linker")"
344 else
345 error "linker not installed!"
346 fi
347 fi
349 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
350 ### Copy sources for changed system ###
351 ###_________________________________###
353 if $_docompile; then
354 rm -rf -- "$_out"
355 fi
357 mkdir -p -- "$_out"
358 _out="$(readlink -f "$_out")"
359 copy_source "generic" "$_cpu"
360 if $_useposix; then
361 copy_source "posix/generic" "posix/$_cpu"
362 fi
363 copy_source "$_system/generic" "$_system/$_cpu"
364 copy_source "$_target/generic" "$_target/$_cpu"
365 if $_useposix; then
366 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
367 fi
368 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
369 cd "$_out"
371 ###^^^^^^^^^^^^^^^###
372 ### Build modules ###
373 ###_______________###
375 if $_docompile; then
376 compile_all
377 fi
379 ###^^^^^^^^^^^^^^###
380 ### Link modules ###
381 ###______________###
383 link_all