DEADSOFTWARE

added Meta for native compiler
[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="-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 $_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 fi
172 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
173 ### Compile BlackBox Framework ###
174 ###____________________________###
176 compile System/Mod/Int.odc
177 if [ "$_target" = "native" ]; then
178 compile System/Mod/Long.odc
179 compile System/Mod/Math.odc System/Mod/SMath.odc
180 else
181 compile System/Mod/Math.cp System/Mod/SMath.cp
182 fi
183 compile System/Mod/Kernel.cp \
184 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
185 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Services.odc \
186 System/Mod/Integers.odc
188 if [ "$_target" = "native" ]; then
189 compile System/Mod/Meta.odc
190 mv -t System Code Sym
191 fi
193 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
194 ### Compile Linux Host subsystem ###
195 ###______________________________###
197 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
198 Host/Mod/Files.cp
200 ###^^^^^^^^^^^^^^^^^^^^^^^###
201 ### Compile Dev subsystem ###
202 ###_______________________###
204 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
205 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
206 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
208 ###^^^^^^^^^^^^^^^^^^^^^^^^###
209 ### Compile Dev2 subsystem ###
210 ###________________________###
212 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
213 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
214 Dev2/Mod/LnkWritePe.odc
216 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
217 ### Compile CPfront subsystem ###
218 ###___________________________###
220 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
222 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
223 ### Compile bbdsw-specific modules ###
224 ###________________________________###
226 if [ "$_target" = "native" ]; then
227 compile Dsw/Mod/Debug.odc
228 fi
229 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
230 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
233 link_all() {
234 local _debug_module=
235 if [ "$_target" = "native" ]; then
236 _debug_module=DswDebug
237 fi
239 link cpc486 \
240 PosixCtypes PosixCmacro \
241 Kernel Console Files Dates Math Strings Services Log \
242 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
243 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
244 DevCPL486 DevCPC486 DevCPV486 \
245 DswDocuments DswCompiler486Main
247 link cpl486 \
248 PosixCtypes PosixCmacro \
249 Kernel Console Files Math Strings Services Log \
250 HostLang HostConsole HostFiles DswLog $_debug_module \
251 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
252 Dev2LnkWriteElfStatic Dev2LnkWritePe \
253 DswLinker486Main
255 link cpfront \
256 PosixCtypes PosixCmacro \
257 Kernel Console Files Dates Math Strings Services Log \
258 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
259 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
260 CPfrontCPG CPfrontCPC CPfrontCPV\
261 DswDocuments DswCompilerCPfrontMain
263 if $_dolink; then
264 chmod a+x cpc486 cpl486 cpfront
265 fi
268 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
269 ### Parse arguments and options ###
270 ###_____________________________###
272 while getopts c:l:o:bxh _name
273 do
274 case "$_name" in
275 c) _compiler="$OPTARG" ;;
276 l) _linker="$OPTARG" ;;
277 o) _out="$OPTARG" ;;
278 b) _docompile=false ;;
279 x) _dolink=false ;;
280 h|?) usage ;;
281 esac
282 done
284 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
285 usage
286 fi
288 shift $(($OPTIND - 1))
289 _cpu="$1"
290 _target="$2"
291 _system="$3"
293 if [ -z "$CC" ]; then
294 export CC=cc
295 fi
297 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
298 ### Check for supported cpu/target/os ###
299 ###___________________________________###
301 case "$_cpu" in
302 386|486|586|686) _cpu=486 ;;
303 arm|armv6|armv7) _cpu=arm ;;
304 "") error "cpu not specified" ;;
305 *) error "unsupported cpu $_cpu" ;;
306 esac
308 case "$_target" in
309 native) _target=native ;;
310 cpfront|c) _target=cpfront ;;
311 "") error "target not specified" ;;
312 *) error "unsupported target $_target" ;;
313 esac
315 case "$_system" in
316 linux) _useposix=true ;;
317 cygwin) _useposix=true ;;
318 "") error "operation system not specified" ;;
319 *) error "unsuported operation system $_system" ;;
320 esac
322 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
323 ### Select default compiler if not specified ###
324 ###__________________________________________###
326 if [ -z "$_compiler" ]; then
327 case "$_target" in
328 native)
329 case "$_cpu" in
330 486) _compiler=cpc486 ;;
331 *) error "no standard compiler for cpu $_cpu" ;;
332 esac
333 ;;
334 cpfront) _compiler=cpfront ;;
335 *) error "no standard compiler for target $_target" ;;
336 esac
337 fi
339 if [ -z "$_linker" ]; then
340 case "$_target" in
341 native)
342 case "$_cpu" in
343 486) _linker=cpl486 ;;
344 *) error "no standard linker for cpu $_cpu" ;;
345 esac
346 ;;
347 cpfront) _linker= ;;
348 *) error "no standard linker for target $_target" ;;
349 esac
350 fi
352 if $_docompile; then
353 if command -v "$_compiler" > /dev/null; then
354 _compiler="$(command -v "$_compiler")"
355 else
356 error "compiler not installed!"
357 fi
358 fi
360 if $_dolink && [ "$_target" != "cpfront" ]; then
361 if command -v "$_linker" > /dev/null; then
362 _linker="$(command -v "$_linker")"
363 else
364 error "linker not installed!"
365 fi
366 fi
368 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
369 ### Copy sources for changed system ###
370 ###_________________________________###
372 if $_docompile; then
373 rm -rf -- "$_out"
374 fi
376 mkdir -p -- "$_out"
377 _out="$(readlink -f "$_out")"
378 copy_source "generic" "$_cpu"
379 if $_useposix; then
380 copy_source "posix/generic" "posix/$_cpu"
381 fi
382 copy_source "$_system/generic" "$_system/$_cpu"
383 copy_source "$_target/generic" "$_target/$_cpu"
384 if $_useposix; then
385 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
386 fi
387 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
388 cd "$_out"
390 ###^^^^^^^^^^^^^^^###
391 ### Build modules ###
392 ###_______________###
394 if $_docompile; then
395 compile_all
396 fi
398 ###^^^^^^^^^^^^^^###
399 ### Link modules ###
400 ###______________###
402 link_all