DEADSOFTWARE

cpmake: fix open source out of any subsystem
[cpc.git] / make-all.sh
1 #! /bin/bash
3 set -e
5 abspath() {
6 [[ "$1" == /* ]] && echo "$1" || echo "$(pwd)/$1"
7 }
9 ###^^^^^^^^^^^^^^^^^^###
10 ### Global variables ###
11 ###__________________###
13 _this="$(dirname "$(abspath "$0")")"
14 _exec="make-all.sh"
15 _compiler=
16 _linker=
18 _cross=false
19 _bootstrap=false
20 _stages=2
22 _cpu=
23 _target=
24 _system=
25 _host_cpu=
26 _host_target=
27 _host_system=
29 export CPCFLAGS="$CPCFLAGS"
30 export CPLFLAGS="$CPLFLAGS"
32 ###^^^^^^^^^^^###
33 ### Functions ###
34 ###___________###
36 usage() {
37 echo "Usage: make-all.sh [options] cpu target os"
38 echo "Options:"
39 echo " -c path Path to compiler binary"
40 echo " -l path Path to linker binary"
41 echo " -b Bootstrap compiler (result in bootstrap/<cpu>-cpfront-<os>/)"
42 echo " -g num Set max verify stages (0..2)"
43 # echo " -e Cross compile (result in stage-cross)"
44 # echo " -u cpu Host cpu"
45 # echo " -t target Host compiler"
46 # echo " -s os Host operaion system"
47 echo "Processors:"
48 echo " 486 Intel 486+"
49 echo " arm ARM 32-bit"
50 echo " powerpc PowerPC 32-bit"
51 echo "Targets:"
52 echo " native Native"
53 echo " cpfront Generic C"
54 echo "Operation systems:"
55 echo " linux GNU/Linux"
56 echo " cygwin Cygwin"
57 echo " osx Mac OS X"
58 echo "Environment variables:"
59 echo " CC C compiler binary"
60 echo " CFLAGS C compiler options"
61 echo " CPCFLAGS CPC compiler options"
62 echo " CPLFLAGS CPL linker options"
63 exit 2
64 }
66 error() {
67 echo "$_exec:" "$@"
68 exit 1
69 }
71 make_stage() {
72 local _dir="$1"
73 local _this_cpu="$2"
74 local _this_target="$3"
75 local _this_system="$4"
76 shift 4
77 "$_this/make.sh" -o "$_dir" -c "$_compiler" -l "$_linker" "$@" "$_this_cpu" "$_this_target" "$_this_system"
78 local _ext=
79 if [ "$_host_system" = "cygwin" ]; then
80 _ext=".exe"
81 elif [ "$_host_system" = "osx" ]; then
82 _ext=".out"
83 fi
84 if [ "$_target" = "cpfront" ]; then
85 _compiler="$_dir/cpfront${_ext}"
86 _linker=
87 else
88 if [ "$_cpu" = "486" ]; then
89 _compiler="$_dir/cpc486${_ext}"
90 _linker="$_dir/cpl486${_ext}"
91 else
92 error "unsupported cpu $_cpu"
93 fi
94 fi
95 }
97 make_verify_stage() {
98 local _N="$1"
99 shift
100 if $_cross; then
101 make_stage "$_this/stage$_N" "$@" "$_host_cpu" "$_host_target" "$_host_system"
102 else
103 make_stage "$_this/stage$_N" "$@" "$_cpu" "$_target" "$_system"
104 fi
107 ###^^^^^^^^^^^^^^^^^^^^^^^^^^^^^###
108 ### Parse arguments and options ###
109 ###_____________________________###
111 while getopts c:l:u:t:s:g:beh _name
112 do
113 case "$_name" in
114 c) _compiler="$OPTARG" ;;
115 l) _linker="$OPTARG" ;;
116 u) _host_cpu="$OPTARG" ;;
117 t) _host_target="$OPTARG" ;;
118 s) _host_system="$OPTARG" ;;
119 g) _stages="$OPTARG" ;;
120 b) _bootstrap=true ;;
121 e) _cross=true ;;
122 h|?) usage ;;
123 esac
124 done
126 if [ "$_stages" -lt "0" -o "$_stages" -gt "2" ]; then
127 usage
128 fi
130 if [ "$(expr $# - $OPTIND + 1)" != "3" ]; then
131 usage
132 fi
134 shift $(($OPTIND - 1))
135 _cpu="$1"
136 _target="$2"
137 _system="$3"
139 if [ -z "$_host_cpu" ]; then
140 _host_cpu="$_cpu"
141 fi
143 if [ -z "$_host_target" ]; then
144 _host_target="$_target"
145 fi
147 if [ -z "$_host_system" ]; then
148 _host_system="$_system"
149 fi
151 ###^^^^^^^^^^^^^^^^^^^^###
152 ### Bootstrap compiler ###
153 ###____________________###
155 if $_bootstrap; then
156 echo "==> Bootstrap"
157 _compiler=
158 _linker=
159 if $_cross; then
160 make_stage "$_this/bootstrap/$_host_cpu-cpfront-$_host_system" "$_host_cpu" "cpfront" "$_host_system" -b
161 else
162 make_stage "$_this/bootstrap/$_host_cpu-cpfront-$_host_system" "$_cpu" "cpfront" "$_system" -b
163 fi
164 else
165 echo "==> Stage 0"
166 make_verify_stage 0
167 fi
169 ###^^^^^^^^^^^^^^^^^^###
170 ### Recompile itself ###
171 ###__________________###
173 if [ "1" -le "$_stages" ]; then
174 echo "==> Stage 1"
175 make_verify_stage 1
176 fi
178 if [ "2" -le "$_stages" ]; then
179 echo "==> Stage 2"
180 make_verify_stage 2
181 fi
183 ###^^^^^^^^^^^^^^^###
184 ### Cross compile ###
185 ###_______________###
187 if $_cross; then
188 echo "==> Cross compile"
189 make_stage "$_this/stage-cross/$_cpu-$_target-$_system" "$_cpu" "$_target" "$_system"
190 fi
192 echo "==> Done"