DEADSOFTWARE

implement Kernel.Call for CPfront using libffi
[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 " cygwin Cygwin"
43 echo "Environment variables:"
44 echo " CC C compiler binary"
45 echo " CFLAGS C compiler options"
46 exit 2
47 }
49 error() {
50 echo "$_exec:" "$@"
51 exit 1
52 }
54 copy_source() {
55 for _src
56 do
57 if test -d "$_this/src/$_src"; then
58 find "$_this/src/$_src" -mindepth 1 -maxdepth 1 -exec cp -rt "$_out" -- {} +
59 fi
60 done
61 }
63 native_compile() {
64 "$_compiler" -legacy "$@"
65 }
67 native_link() {
68 if $_dolink; then
69 local _outexe="$1";
70 local _outsystem="$_system"
71 if [ "$_system" = "cygwin" ]; then
72 _outexe="${_outexe}.exe"
73 _outsystem="win32"
74 fi
75 shift
76 "$_linker" -os "$_outsystem" -kernel Kernel -main Kernel -legacycodedir . -o "$_outexe" "$@"
77 fi
78 }
80 cpfront_import_list() {
81 echo
82 while [ "$1" != "" ]
83 do
84 echo -n " $1"
85 shift
86 if ! [ -z "$1" ]; then
87 echo ","
88 fi
89 done
90 }
92 cpfront_main_module() {
93 local _name="$1"
94 shift
95 echo "MODULE ${_name};"
96 echo
97 echo " IMPORT $(cpfront_import_list "$@");"
98 echo
99 echo "END ${_name}."
102 cpfront_compile() {
103 "$_compiler" -outcode CodeC -outsym SymC "$@"
106 cpfront_link() {
107 local _main="$1"
108 if $_docompile; then
109 cpfront_main_module "$@" > "${_main}.cp"
110 "$_compiler" -outcode CodeC -outsym SymC -main "${_main}.cp"
111 fi
112 shift
113 if $_dolink; then
114 local _list=
115 for _module in "$@" "${_main}"
116 do
117 _list="$_list ${_out}/CodeC/${_module}.c"
118 done
119 local _cc_cflags=
120 case "$CC" in
121 *gcc) _cc_cflags="-g -Wno-int-conversion -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types -Wno-implicit-function-declaration" ;;
122 *) _cc_cflags="" ;;
123 esac
124 local _cpu_cflags=
125 case "$_cpu" in
126 486) _cpu_cflags="-m32" ;;
127 arm) _cpu_cflags="" ;;
128 *) error "cpfront_link(): unsupported cpu $_cpu" ;;
129 esac
130 local _system_cflags=
131 case "$_system" in
132 cygwin) _system_cflags="-liconv" ;;
133 *) _system_cflags="" ;;
134 esac
135 "$CC" $_cc_cflags $_cpu_cflags $CFLAGS -o "${_main}" -I "$_this/C" "$_this/C/SYSTEM.c" $_list -lm -ldl -lffi $_system_cflags
136 fi
139 compile() {
140 case "$_target" in
141 native) native_compile "$@" ;;
142 cpfront) cpfront_compile "$@" ;;
143 *) error "compile(): unknown target $_target" ;;
144 esac
147 link() {
148 case "$_target" in
149 native) native_link "$@" ;;
150 cpfront) cpfront_link "$@" ;;
151 *) error "link(): unknown target $_target" ;;
152 esac
155 compile_all() {
156 ###^^^^^^^^^^^^^^^^^^^^^^^^###
157 ### Compile POSIX bindings ###
158 ###________________________###
160 if $_useposix; then
161 compile Posix/Mod/Ctypes.cp \
162 Posix/Mod/Csys_types.cp \
163 Posix/Mod/Cstdlib.cp Posix/Mod/Cstdio.cp Posix/Mod/Cunistd.cp \
164 Posix/Mod/Cdirent.cp Posix/Mod/Clocale.cp Posix/Mod/Ctime.cp \
165 Posix/Mod/Csys_stat.cp Posix/Mod/Cfcntl.cp Posix/Mod/Cerrno.cp \
166 Posix/Mod/Ciconv.cp Posix/Mod/Cwctype.cp Posix/Mod/Csys_mman.cp \
167 Posix/Mod/Cdlfcn.cp Posix/Mod/Csignal.cp Posix/Mod/Csetjmp.cp \
168 Posix/Mod/Clibgen.cp \
169 Posix/Mod/Cmacro.cp
170 if [ "$_target" = "cpfront" ]; then
171 compile Lib/Mod/FFI.cp
172 fi
173 fi
175 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
176 ### Compile BlackBox Framework ###
177 ###____________________________###
179 compile System/Mod/Int.odc
180 if [ "$_target" = "native" ]; then
181 compile System/Mod/Long.odc
182 compile System/Mod/Math.odc System/Mod/SMath.odc
183 else
184 compile System/Mod/Math.cp System/Mod/SMath.cp
185 fi
186 compile System/Mod/Kernel.cp \
187 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
188 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Meta.odc \
189 System/Mod/Services.odc System/Mod/Integers.odc
191 if [ "$_target" = "native" ]; then
192 mv -t System Code Sym
193 fi
195 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
196 ### Compile Linux Host subsystem ###
197 ###______________________________###
199 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
200 Host/Mod/Files.cp
202 ###^^^^^^^^^^^^^^^^^^^^^^^###
203 ### Compile Dev subsystem ###
204 ###_______________________###
206 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
207 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
208 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
210 ###^^^^^^^^^^^^^^^^^^^^^^^^###
211 ### Compile Dev2 subsystem ###
212 ###________________________###
214 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
215 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
216 Dev2/Mod/LnkWritePe.odc
218 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
219 ### Compile CPfront subsystem ###
220 ###___________________________###
222 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
224 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
225 ### Compile bbdsw-specific modules ###
226 ###________________________________###
228 if [ "$_target" = "native" ]; then
229 compile Dsw/Mod/Debug.odc
230 fi
231 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
232 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
235 link_all() {
236 local _debug_module=
237 if [ "$_target" = "native" ]; then
238 _debug_module=DswDebug
239 fi
241 link cpc486 \
242 PosixCtypes PosixCmacro \
243 Kernel Console Files Dates Math Strings Services Log \
244 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
245 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
246 DevCPL486 DevCPC486 DevCPV486 \
247 DswDocuments DswCompiler486Main
249 link cpl486 \
250 PosixCtypes PosixCmacro \
251 Kernel Console Files Math Strings Services Log \
252 HostLang HostConsole HostFiles DswLog $_debug_module \
253 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
254 Dev2LnkWriteElfStatic Dev2LnkWritePe \
255 DswLinker486Main
257 link cpfront \
258 PosixCtypes PosixCmacro \
259 Kernel Console Files Dates Math Strings Services Log \
260 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
261 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
262 CPfrontCPG CPfrontCPC CPfrontCPV\
263 DswDocuments DswCompilerCPfrontMain
265 if $_dolink; then
266 chmod a+x cpc486 cpl486 cpfront
267 fi
270 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
271 ### Parse arguments and options ###
272 ###_____________________________###
274 while getopts c:l:o:bxh _name
275 do
276 case "$_name" in
277 c) _compiler="$OPTARG" ;;
278 l) _linker="$OPTARG" ;;
279 o) _out="$OPTARG" ;;
280 b) _docompile=false ;;
281 x) _dolink=false ;;
282 h|?) usage ;;
283 esac
284 done
286 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
287 usage
288 fi
290 shift $(($OPTIND - 1))
291 _cpu="$1"
292 _target="$2"
293 _system="$3"
295 if [ -z "$CC" ]; then
296 export CC=cc
297 fi
299 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
300 ### Check for supported cpu/target/os ###
301 ###___________________________________###
303 case "$_cpu" in
304 386|486|586|686) _cpu=486 ;;
305 arm|armv6|armv7) _cpu=arm ;;
306 "") error "cpu not specified" ;;
307 *) error "unsupported cpu $_cpu" ;;
308 esac
310 case "$_target" in
311 native) _target=native ;;
312 cpfront|c) _target=cpfront ;;
313 "") error "target not specified" ;;
314 *) error "unsupported target $_target" ;;
315 esac
317 case "$_system" in
318 linux) _useposix=true ;;
319 cygwin) _useposix=true ;;
320 "") error "operation system not specified" ;;
321 *) error "unsuported operation system $_system" ;;
322 esac
324 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
325 ### Select default compiler if not specified ###
326 ###__________________________________________###
328 if [ -z "$_compiler" ]; then
329 case "$_target" in
330 native)
331 case "$_cpu" in
332 486) _compiler=cpc486 ;;
333 *) error "no standard compiler for cpu $_cpu" ;;
334 esac
335 ;;
336 cpfront) _compiler=cpfront ;;
337 *) error "no standard compiler for target $_target" ;;
338 esac
339 fi
341 if [ -z "$_linker" ]; then
342 case "$_target" in
343 native)
344 case "$_cpu" in
345 486) _linker=cpl486 ;;
346 *) error "no standard linker for cpu $_cpu" ;;
347 esac
348 ;;
349 cpfront) _linker= ;;
350 *) error "no standard linker for target $_target" ;;
351 esac
352 fi
354 if $_docompile; then
355 if command -v "$_compiler" > /dev/null; then
356 _compiler="$(command -v "$_compiler")"
357 else
358 error "compiler not installed!"
359 fi
360 fi
362 if $_dolink && [ "$_target" != "cpfront" ]; then
363 if command -v "$_linker" > /dev/null; then
364 _linker="$(command -v "$_linker")"
365 else
366 error "linker not installed!"
367 fi
368 fi
370 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
371 ### Copy sources for changed system ###
372 ###_________________________________###
374 if $_docompile; then
375 rm -rf -- "$_out"
376 fi
378 mkdir -p -- "$_out"
379 _out="$(readlink -f "$_out")"
380 copy_source "generic" "$_cpu"
381 if $_useposix; then
382 copy_source "posix/generic" "posix/$_cpu"
383 fi
384 copy_source "$_system/generic" "$_system/$_cpu"
385 copy_source "$_target/generic" "$_target/$_cpu"
386 if $_useposix; then
387 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
388 fi
389 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
390 cd "$_out"
392 ###^^^^^^^^^^^^^^^###
393 ### Build modules ###
394 ###_______________###
396 if $_docompile; then
397 compile_all
398 fi
400 ###^^^^^^^^^^^^^^###
401 ### Link modules ###
402 ###______________###
404 link_all