DEADSOFTWARE

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