DEADSOFTWARE

add SYSTEM.CONV
[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="-g -Wno-int-conversion -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types -Wno-implicit-function-declaration" ;;
127 *) _cc_cflags="" ;;
128 esac
129 local _cpu_cflags=
130 case "$_cpu" in
131 486) _cpu_cflags="-m32" ;;
132 arm) _cpu_cflags="" ;;
133 *) error "cpfront_link(): unsupported cpu $_cpu" ;;
134 esac
135 local _system_cflags=
136 case "$_system" in
137 cygwin) _system_cflags="-liconv" ;;
138 *) _system_cflags="" ;;
139 esac
140 "$CC" $_cc_cflags $_cpu_cflags $CFLAGS -o "${_main}" -I "$_this/C" "$_this/C/SYSTEM.c" $_list -lm -ldl -lffi $_system_cflags
141 fi
144 compile() {
145 case "$_target" in
146 native) native_compile "$@" ;;
147 cpfront) cpfront_compile "$@" ;;
148 *) error "compile(): unknown target $_target" ;;
149 esac
152 link() {
153 case "$_target" in
154 native) native_link "$@" ;;
155 cpfront) cpfront_link "$@" ;;
156 *) error "link(): unknown target $_target" ;;
157 esac
160 compile_all() {
161 ###^^^^^^^^^^^^^^^^^^^^^^^^###
162 ### Compile POSIX bindings ###
163 ###________________________###
165 if $_useposix; then
166 compile Posix/Mod/Ctypes.cp \
167 Posix/Mod/Csys_types.cp \
168 Posix/Mod/Cstdlib.cp Posix/Mod/Cstdio.cp Posix/Mod/Cunistd.cp \
169 Posix/Mod/Cdirent.cp Posix/Mod/Clocale.cp Posix/Mod/Ctime.cp \
170 Posix/Mod/Csys_stat.cp Posix/Mod/Cfcntl.cp Posix/Mod/Cerrno.cp \
171 Posix/Mod/Ciconv.cp Posix/Mod/Cwctype.cp Posix/Mod/Csys_mman.cp \
172 Posix/Mod/Cdlfcn.cp Posix/Mod/Csignal.cp Posix/Mod/Csetjmp.cp \
173 Posix/Mod/Clibgen.cp \
174 Posix/Mod/Cmacro.cp
175 if [ "$_target" = "cpfront" ]; then
176 compile Lib/Mod/FFI.cp
177 fi
178 fi
180 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
181 ### Compile BlackBox Framework ###
182 ###____________________________###
184 compile System/Mod/Int.odc
185 if [ "$_target" = "native" ]; then
186 compile System/Mod/Long.odc
187 compile System/Mod/Math.odc System/Mod/SMath.odc
188 else
189 compile System/Mod/Math.cp System/Mod/SMath.cp
190 fi
191 compile System/Mod/Kernel.cp \
192 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
193 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Meta.odc \
194 System/Mod/Services.odc System/Mod/Integers.odc
196 if [ "$_target" = "native" ]; then
197 mv -t System Code Sym
198 fi
200 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
201 ### Compile Linux Host subsystem ###
202 ###______________________________###
204 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
205 Host/Mod/Files.cp
207 ###^^^^^^^^^^^^^^^^^^^^^^^###
208 ### Compile Dev subsystem ###
209 ###_______________________###
211 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
212 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
213 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
215 ###^^^^^^^^^^^^^^^^^^^^^^^^###
216 ### Compile Dev2 subsystem ###
217 ###________________________###
219 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
220 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
221 Dev2/Mod/LnkWritePe.odc
223 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
224 ### Compile CPfront subsystem ###
225 ###___________________________###
227 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
229 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
230 ### Compile bbdsw-specific modules ###
231 ###________________________________###
233 if [ "$_target" = "native" ]; then
234 compile Dsw/Mod/Debug.odc
235 fi
236 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
237 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
240 link_all() {
241 local _debug_module=
242 if [ "$_target" = "native" ]; then
243 _debug_module=DswDebug
244 fi
246 link cpc486 \
247 PosixCtypes PosixCmacro \
248 Kernel Console Files Dates Math Strings Services Log \
249 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
250 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
251 DevCPL486 DevCPC486 DevCPV486 \
252 DswDocuments DswCompiler486Main
254 link cpl486 \
255 PosixCtypes PosixCmacro \
256 Kernel Console Files Math Strings Services Log \
257 HostLang HostConsole HostFiles DswLog $_debug_module \
258 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
259 Dev2LnkWriteElfStatic Dev2LnkWritePe \
260 DswLinker486Main
262 link cpfront \
263 PosixCtypes PosixCmacro \
264 Kernel Console Files Dates Math Strings Services Log \
265 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
266 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
267 CPfrontCPG CPfrontCPC CPfrontCPV\
268 DswDocuments DswCompilerCPfrontMain
270 if $_dolink; then
271 chmod a+x cpc486 cpl486 cpfront
272 fi
275 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
276 ### Parse arguments and options ###
277 ###_____________________________###
279 while getopts c:l:o:bxh _name
280 do
281 case "$_name" in
282 c) _compiler="$OPTARG" ;;
283 l) _linker="$OPTARG" ;;
284 o) _out="$OPTARG" ;;
285 b) _docompile=false ;;
286 x) _dolink=false ;;
287 h|?) usage ;;
288 esac
289 done
291 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
292 usage
293 fi
295 shift $(($OPTIND - 1))
296 _cpu="$1"
297 _target="$2"
298 _system="$3"
300 if [ -z "$CC" ]; then
301 export CC=cc
302 fi
304 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
305 ### Check for supported cpu/target/os ###
306 ###___________________________________###
308 case "$_cpu" in
309 386|486|586|686) _cpu=486 ;;
310 arm|armv6|armv7) _cpu=arm ;;
311 "") error "cpu not specified" ;;
312 *) error "unsupported cpu $_cpu" ;;
313 esac
315 case "$_target" in
316 native) _target=native ;;
317 cpfront|c) _target=cpfront ;;
318 "") error "target not specified" ;;
319 *) error "unsupported target $_target" ;;
320 esac
322 case "$_system" in
323 linux) _useposix=true ;;
324 cygwin) _useposix=true ;;
325 "") error "operation system not specified" ;;
326 *) error "unsuported operation system $_system" ;;
327 esac
329 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
330 ### Select default compiler if not specified ###
331 ###__________________________________________###
333 if [ -z "$_compiler" ]; then
334 case "$_target" in
335 native)
336 case "$_cpu" in
337 486) _compiler=cpc486 ;;
338 *) error "no standard compiler for cpu $_cpu" ;;
339 esac
340 ;;
341 cpfront) _compiler=cpfront ;;
342 *) error "no standard compiler for target $_target" ;;
343 esac
344 fi
346 if [ -z "$_linker" ]; then
347 case "$_target" in
348 native)
349 case "$_cpu" in
350 486) _linker=cpl486 ;;
351 *) error "no standard linker for cpu $_cpu" ;;
352 esac
353 ;;
354 cpfront) _linker= ;;
355 *) error "no standard linker for target $_target" ;;
356 esac
357 fi
359 if $_docompile; then
360 if command -v "$_compiler" > /dev/null; then
361 _compiler="$(command -v "$_compiler")"
362 else
363 error "compiler not installed!"
364 fi
365 fi
367 if $_dolink && [ "$_target" != "cpfront" ]; then
368 if command -v "$_linker" > /dev/null; then
369 _linker="$(command -v "$_linker")"
370 else
371 error "linker not installed!"
372 fi
373 fi
375 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
376 ### Copy sources for changed system ###
377 ###_________________________________###
379 if $_docompile; then
380 rm -rf -- "$_out"
381 fi
383 mkdir -p -- "$_out"
384 _out="$(readlink -f "$_out")"
385 copy_source "generic" "$_cpu"
386 if $_useposix; then
387 copy_source "posix/generic" "posix/$_cpu"
388 fi
389 copy_source "$_system/generic" "$_system/$_cpu"
390 copy_source "$_target/generic" "$_target/$_cpu"
391 if $_useposix; then
392 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
393 fi
394 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
395 cd "$_out"
397 ###^^^^^^^^^^^^^^^###
398 ### Build modules ###
399 ###_______________###
401 if $_docompile; then
402 compile_all
403 fi
405 ###^^^^^^^^^^^^^^###
406 ### Link modules ###
407 ###______________###
409 link_all