DEADSOFTWARE

added cygwin support (only 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 " 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 mv -t System Code Sym
190 fi
192 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
193 ### Compile Linux Host subsystem ###
194 ###______________________________###
196 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
197 Host/Mod/Files.cp
199 ###^^^^^^^^^^^^^^^^^^^^^^^###
200 ### Compile Dev subsystem ###
201 ###_______________________###
203 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
204 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
205 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
207 ###^^^^^^^^^^^^^^^^^^^^^^^^###
208 ### Compile Dev2 subsystem ###
209 ###________________________###
211 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
212 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
213 Dev2/Mod/LnkWritePe.odc
215 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
216 ### Compile CPfront subsystem ###
217 ###___________________________###
219 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
221 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
222 ### Compile bbdsw-specific modules ###
223 ###________________________________###
225 if [ "$_target" = "native" ]; then
226 compile Dsw/Mod/Debug.odc
227 fi
228 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
229 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
232 link_all() {
233 local _debug_module=
234 if [ "$_target" = "native" ]; then
235 _debug_module=DswDebug
236 fi
238 link cpc486 \
239 PosixCtypes PosixCmacro \
240 Kernel Console Files Dates Math Strings Services Log \
241 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
242 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
243 DevCPL486 DevCPC486 DevCPV486 \
244 DswDocuments DswCompiler486Main
246 link cpl486 \
247 PosixCtypes PosixCmacro \
248 Kernel Console Files Math Strings Services Log \
249 HostLang HostConsole HostFiles DswLog $_debug_module \
250 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
251 Dev2LnkWriteElfStatic Dev2LnkWritePe \
252 DswLinker486Main
254 link cpfront \
255 PosixCtypes PosixCmacro \
256 Kernel Console Files Dates Math Strings Services Log \
257 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
258 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
259 CPfrontCPG CPfrontCPC CPfrontCPV\
260 DswDocuments DswCompilerCPfrontMain
262 if $_dolink; then
263 chmod a+x cpc486 cpl486 cpfront
264 fi
267 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
268 ### Parse arguments and options ###
269 ###_____________________________###
271 while getopts c:l:o:bxh _name
272 do
273 case "$_name" in
274 c) _compiler="$OPTARG" ;;
275 l) _linker="$OPTARG" ;;
276 o) _out="$OPTARG" ;;
277 b) _docompile=false ;;
278 x) _dolink=false ;;
279 h|?) usage ;;
280 esac
281 done
283 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
284 usage
285 fi
287 shift $(($OPTIND - 1))
288 _cpu="$1"
289 _target="$2"
290 _system="$3"
292 if [ -z "$CC" ]; then
293 export CC=cc
294 fi
296 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
297 ### Check for supported cpu/target/os ###
298 ###___________________________________###
300 case "$_cpu" in
301 386|486|586|686) _cpu=486 ;;
302 arm|armv6|armv7) _cpu=arm ;;
303 "") error "cpu not specified" ;;
304 *) error "unsupported cpu $_cpu" ;;
305 esac
307 case "$_target" in
308 native) _target=native ;;
309 cpfront|c) _target=cpfront ;;
310 "") error "target not specified" ;;
311 *) error "unsupported target $_target" ;;
312 esac
314 case "$_system" in
315 linux) _useposix=true ;;
316 cygwin) _useposix=true ;;
317 "") error "operation system not specified" ;;
318 *) error "unsuported operation system $_system" ;;
319 esac
321 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
322 ### Select default compiler if not specified ###
323 ###__________________________________________###
325 if [ -z "$_compiler" ]; then
326 case "$_target" in
327 native)
328 case "$_cpu" in
329 486) _compiler=cpc486 ;;
330 *) error "no standard compiler for cpu $_cpu" ;;
331 esac
332 ;;
333 cpfront) _compiler=cpfront ;;
334 *) error "no standard compiler for target $_target" ;;
335 esac
336 fi
338 if [ -z "$_linker" ]; then
339 case "$_target" in
340 native)
341 case "$_cpu" in
342 486) _linker=cpl486 ;;
343 *) error "no standard linker for cpu $_cpu" ;;
344 esac
345 ;;
346 cpfront) _linker= ;;
347 *) error "no standard linker for target $_target" ;;
348 esac
349 fi
351 if $_docompile; then
352 if command -v "$_compiler" > /dev/null; then
353 _compiler="$(command -v "$_compiler")"
354 else
355 error "compiler not installed!"
356 fi
357 fi
359 if $_dolink && [ "$_target" != "cpfront" ]; then
360 if command -v "$_linker" > /dev/null; then
361 _linker="$(command -v "$_linker")"
362 else
363 error "linker not installed!"
364 fi
365 fi
367 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
368 ### Copy sources for changed system ###
369 ###_________________________________###
371 if $_docompile; then
372 rm -rf -- "$_out"
373 fi
375 mkdir -p -- "$_out"
376 _out="$(readlink -f "$_out")"
377 copy_source "generic" "$_cpu"
378 if $_useposix; then
379 copy_source "posix/generic" "posix/$_cpu"
380 fi
381 copy_source "$_system/generic" "$_system/$_cpu"
382 copy_source "$_target/generic" "$_target/$_cpu"
383 if $_useposix; then
384 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
385 fi
386 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
387 cd "$_out"
389 ###^^^^^^^^^^^^^^^###
390 ### Build modules ###
391 ###_______________###
393 if $_docompile; then
394 compile_all
395 fi
397 ###^^^^^^^^^^^^^^###
398 ### Link modules ###
399 ###______________###
401 link_all