DEADSOFTWARE

flush called only for exclusive-mode files
[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 "Targets:"
37 echo " native Native"
38 echo " cpfront Generic C"
39 echo "Operation systems:"
40 echo " linux GNU/Linux"
41 exit 2
42 }
44 error() {
45 echo "$_exec:" "$@"
46 exit 1
47 }
49 copy_source() {
50 for _src
51 do
52 if test -d "$_this/src/$_src"; then
53 find "$_this/src/$_src" -mindepth 1 -maxdepth 1 -exec cp -rt "$_out" -- {} +
54 fi
55 done
56 }
58 native_compile() {
59 "$_compiler" -legacy "$@"
60 }
62 native_link() {
63 if $_dolink; then
64 "$_linker" -os "$_system" -kernel Kernel -main Kernel -legacycodedir . -o "$@"
65 fi
66 }
68 cpfront_import_list() {
69 echo
70 while [ "$1" != "" ]
71 do
72 echo -n " $1"
73 shift
74 if ! [ -z "$1" ]; then
75 echo ","
76 fi
77 done
78 }
80 cpfront_main_module() {
81 local _name="$1"
82 shift
83 echo "MODULE ${_name};\n\n IMPORT $(cpfront_import_list "$@");\n\nEND ${_name}."
84 }
86 cpfront_compile() {
87 "$_compiler" -outcode CodeC -outsym SymC "$@"
88 }
90 cpfront_link() {
91 local _main="$1"
92 if $_docompile; then
93 cpfront_main_module "$@" > "${_main}.cp"
94 "$_compiler" -outcode CodeC -outsym SymC -main "${_main}.cp"
95 fi
96 shift
97 if $_dolink; then
98 local _list=
99 for _module in "$@" "${_main}"
100 do
101 _list="$_list ${_out}/CodeC/${_module}.c"
102 done
103 ### !!! ADD $CC $CFLAGS $LDFLAGS ###
104 gcc -m32 -g -O0 -lm -ldl -o "$_out/$_main" \
105 -Wno-int-conversion \
106 -Wno-int-to-pointer-cast \
107 -Wno-incompatible-pointer-types \
108 -Wno-implicit-function-declaration \
109 -I "$_this/C" "$_this/C/SYSTEM.c" $_list
110 fi
113 compile() {
114 case "$_target" in
115 native) native_compile "$@" ;;
116 cpfront) cpfront_compile "$@" ;;
117 *) error "compile(): unknown target $_target" ;;
118 esac
121 link() {
122 case "$_target" in
123 native) native_link "$@" ;;
124 cpfront) cpfront_link "$@" ;;
125 *) error "link(): unknown target $_target" ;;
126 esac
129 compile_all() {
130 ###^^^^^^^^^^^^^^^^^^^^^^^^###
131 ### Compile POSIX bindings ###
132 ###________________________###
134 case "$_system" in
135 linux)
136 compile C99/Mod/types.cp \
137 C99/Mod/sys_types.cp \
138 C99/Mod/stdlib.cp C99/Mod/stdio.cp C99/Mod/unistd.cp \
139 C99/Mod/dirent.cp C99/Mod/locale.cp C99/Mod/time.cp \
140 C99/Mod/sys_stat.cp C99/Mod/fcntl.cp C99/Mod/errno.cp \
141 C99/Mod/iconv.cp C99/Mod/wctype.cp C99/Mod/sys_mman.cp \
142 C99/Mod/dlfcn.cp C99/Mod/signal.cp C99/Mod/setjmp.cp \
143 C99/Mod/libgen.cp \
144 C99/Mod/macro.cp
145 ;;
146 esac
148 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
149 ### Compile BlackBox Framework ###
150 ###____________________________###
152 compile System/Mod/Int.odc
153 if [ "$_target" = "native" ]; then
154 compile System/Mod/Long.odc
155 compile System/Mod/Math.odc System/Mod/SMath.odc
156 else
157 compile System/Mod/Math.cp System/Mod/SMath.cp
158 fi
159 compile System/Mod/Kernel.cp \
160 System/Mod/Console.odc System/Mod/Files.odc System/Mod/Dates.odc \
161 System/Mod/Log.odc System/Mod/Strings.odc System/Mod/Services.odc \
162 System/Mod/Integers.odc
164 if [ "$_target" = "native" ]; then
165 mv -t System Code Sym
166 fi
168 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
169 ### Compile Linux Host subsystem ###
170 ###______________________________###
172 compile Host/Mod/Lang.cp Host/Mod/Dates.cp Host/Mod/Console.cp \
173 Host/Mod/Files.cp
175 ###^^^^^^^^^^^^^^^^^^^^^^^###
176 ### Compile Dev subsystem ###
177 ###_______________________###
179 compile Dev/Mod/CPM.cp Dev/Mod/CPT.odc Dev/Mod/CPS.odc Dev/Mod/CPB.odc \
180 Dev/Mod/CPP.odc Dev/Mod/CPE.odc Dev/Mod/CPH.odc Dev/Mod/CPL486.odc \
181 Dev/Mod/CPC486.odc Dev/Mod/CPV486.odc
183 ###^^^^^^^^^^^^^^^^^^^^^^^^###
184 ### Compile Dev2 subsystem ###
185 ###________________________###
187 compile Dev2/Mod/LnkBase.odc Dev2/Mod/LnkChmod.odc Dev2/Mod/LnkLoad.odc \
188 Dev2/Mod/LnkWriteElf.odc Dev2/Mod/LnkWriteElfStatic.odc \
189 Dev2/Mod/LnkWritePe.odc
191 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^###
192 ### Compile CPfront subsystem ###
193 ###___________________________###
195 compile CPfront/Mod/CPG.odc CPfront/Mod/CPC.odc CPfront/Mod/CPV.odc
197 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
198 ### Compile bbdsw-specific modules ###
199 ###________________________________###
201 if [ "$_target" = "native" ]; then
202 compile Dsw/Mod/Debug.odc
203 fi
204 compile Dsw/Mod/Documents.cp Dsw/Mod/Log.odc Dsw/Mod/Compiler486Main.cp \
205 Dsw/Mod/CompilerCPfrontMain.cp Dsw/Mod/Linker486Main.cp
208 link_all() {
209 local _debug_module=
210 if [ "$_target" = "native" ]; then
211 _debug_module=DswDebug
212 fi
214 link cpc486 \
215 C99types C99macro \
216 Kernel Console Files Dates Math Strings Services Log \
217 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
218 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
219 DevCPL486 DevCPC486 DevCPV486 \
220 DswDocuments DswCompiler486Main
222 link cpl486 \
223 C99types C99macro \
224 Kernel Console Files Math Strings Services Log \
225 HostLang HostConsole HostFiles DswLog $_debug_module \
226 Dev2LnkBase Dev2LnkChmod Dev2LnkLoad Dev2LnkWriteElf \
227 Dev2LnkWriteElfStatic Dev2LnkWritePe \
228 DswLinker486Main
230 link cpfront \
231 C99types C99macro \
232 Kernel Console Files Dates Math Strings Services Log \
233 HostLang HostConsole HostFiles HostDates DswLog $_debug_module \
234 DevCPM DevCPT DevCPS DevCPB DevCPP DevCPE DevCPH \
235 CPfrontCPG CPfrontCPC CPfrontCPV\
236 DswDocuments DswCompilerCPfrontMain
238 if $_dolink; then
239 chmod a+x cpc486 cpl486 cpfront
240 fi
243 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
244 ### Parse arguments and options ###
245 ###_____________________________###
247 while getopts c:l:o:bxh _name
248 do
249 case "$_name" in
250 c) _compiler="$OPTARG" ;;
251 l) _linker="$OPTARG" ;;
252 o) _out="$OPTARG" ;;
253 b) _docompile=false ;;
254 x) _dolink=false ;;
255 h|?) usage ;;
256 esac
257 done
259 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
260 usage
261 fi
263 shift $(($OPTIND - 1))
264 _cpu="$1"
265 _target="$2"
266 _system="$3"
268 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
269 ### Check for supported cpu/target/os ###
270 ###___________________________________###
272 case "$_cpu" in
273 386|486|586|686) _cpu=486 ;;
274 "") error "cpu not specified" ;;
275 *) error "unsupported cpu $_cpu" ;;
276 esac
278 case "$_target" in
279 native) _target=native ;;
280 cpfront|c) _target=cpfront ;;
281 "") error "target not specified" ;;
282 *) error "unsupported target $_target" ;;
283 esac
285 case "$_system" in
286 linux) _useposix=true ;;
287 "") error "operation system not specified" ;;
288 *) error "unsuported operation system $_system" ;;
289 esac
291 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
292 ### Select default compiler if not specified ###
293 ###__________________________________________###
295 if [ -z "$_compiler" ]; then
296 case "$_target" in
297 native)
298 case "$_cpu" in
299 486) _compiler=cpc486 ;;
300 *) error "no standard compiler for cpu $_cpu" ;;
301 esac
302 ;;
303 cpfront) _compiler=cpfront ;;
304 *) error "no standard compiler for target $_target" ;;
305 esac
306 fi
308 if [ -z "$_linker" ]; then
309 case "$_target" in
310 native)
311 case "$_cpu" in
312 486) _linker=cpl486 ;;
313 *) error "no standard linker for cpu $_cpu" ;;
314 esac
315 ;;
316 cpfront) _linker= ;;
317 *) error "no standard linker for target $_target" ;;
318 esac
319 fi
321 if $_docompile; then
322 if command -v "$_compiler" > /dev/null; then
323 _compiler="$(command -v "$_compiler")"
324 else
325 error "compiler not installed!"
326 fi
327 fi
329 if $_dolink && [ "$_target" != "cpfront" ]; then
330 if command -v "$_linker" > /dev/null; then
331 _linker="$(command -v "$_linker")"
332 else
333 error "linker not installed!"
334 fi
335 fi
337 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
338 ### Copy sources for changed system ###
339 ###_________________________________###
341 $_docompile && rm -rf -- "$_out"
343 mkdir -p -- "$_out"
344 _out="$(readlink -f "$_out")"
345 copy_source "generic" "$_cpu"
346 if $_useposix; then
347 copy_source "posix/generic" "posix/$_cpu"
348 fi
349 copy_source "$_system/generic" "$_system/$_cpu"
350 copy_source "$_target/generic" "$_target/$_cpu"
351 if $_useposix; then
352 copy_source "$_target/posix/generic" "$_target/posix/$_cpu"
353 fi
354 copy_source "$_target/$_system/generic" "$_target/$_system/$_cpu"
355 cd "$_out"
357 ###^^^^^^^^^^^^^^^###
358 ### Build modules ###
359 ###_______________###
361 if $_docompile; then
362 compile_all
363 fi
365 ###^^^^^^^^^^^^^^###
366 ### Link modules ###
367 ###______________###
369 link_all