Rewrite the lunabuild toolchain with enhanced feature (#60)
[lunaix-os.git] / slides / c0-workspace / all-build.sh
1 #! /bin/bash
2
3 # shell folder is the floder of all-build.sh, i.e. shell-folder equals <path-to-lunaix-os>/slides/c0-workspace
4 shell_folder=$(cd "$(dirname "$0")" || exit; pwd)
5
6 # global variables
7 export PREFIX="$HOME/cross-compiler"            # this prefix keeps the same with gcc-build and bochs-build
8 # export PREFIX="$shell_folder"/../../tools     # this prefix points to <paht-to-lunaix-os>/tools, e.g., ~/projects/lunaix-os/tools
9 export TARGET=i686-elf
10 export PATH="$PREFIX/bin:$PATH"
11 export BXSHARE="$PREFIX/share/bochs"
12
13 # compile flags
14 export CC=gcc
15 export CXX="g++"
16 export CFLAGS="-Wall -O2 -fomit-frame-pointer -pipe"
17 export CXXFLAGS="$CFLAGS"
18
19
20 # Configure Options
21 # modify them if you need more/less options. Don't forget <white-space> before ending \
22
23 # bochs configure options
24 read -r -d '' bochs_no_gdb_configure <<- EOM
25 --enable-x86-64 \
26 --enable-debugger \
27 --enable-debugger-gui \
28 --enable-x86-debugger \
29 --enable-iodebug \
30 --with-x --with-x11
31 EOM
32
33 # qemu configure options
34 read -r -d '' qemu_configure <<- EOM
35 EOM
36
37 # gcc configrue options
38 read -r -d '' gcc_configure <<- EOM
39 --disable-nls \
40 --enable-language=c,c++ \
41 --without-headers 
42 EOM
43
44 # binutils configure options
45 read -r -d '' binutils_configure <<- EOM
46 --with-sysroot \
47 --disable-nls \
48 --disable-werror 
49 EOM
50
51
52
53 # terminal colors
54 purple='\e[35m'
55 green='\e[32m'
56 red='\e[31m'
57 return='\e[0m'
58
59 function red(){
60     echo -e "$red$1$return"
61 }
62
63 function green(){
64     echo -e "$green$1$return"
65 }
66
67 function purple () {
68     echo -e "$purple$1$return"
69 }
70
71
72
73 if [[ $1 = '-h' ]] || [[ $1 = '--help' ]]; then
74     echo "Init tools for downloading, compiling, and installing corss-compile and debug tool-chain for Lunxia-OS"
75     echo 'Options:'
76     echo '    -h, --help                Show this help message'
77     echo '    -d, --download            Download toolchain'
78     echo '    -c, --compile             Compile toolchain, without qemu'
79     echo '    -fc, --fully-compile      Compile toolchain, with qemu'
80 fi
81
82 if [[ $1 = "-d" ]] || [[ $1 = "--download" ]] || [[ ! -d "$shell_folder"/tools ]]; then
83     if [[ ! -d "$shell_folder"/tools/src ]]; then
84         read -p 'tools/src not exists, create? <y/n>: ' download
85         if [[ $download = "y" ]]; then
86             mkdir -p "$shell_folder"/tools/src
87         fi
88     fi
89     if [[ $1 = "-d" ]] || [[ $1 = "--download" ]]; then
90         echo 'Downloading debug tools...'
91         purple "=> bochs-2.7"
92         if  [ -f "$shell_folder"/tools/src/bochs-2.7.tar.gz ]; then
93             green 'bochs already exists, nothing changed, run `rm -rf tools/src/bochs-2.7.tar.gz` to force re-download'
94         else
95             if wget -t 5 -T 5 -c --quiet --show-progress -O "$shell_folder"/tools/src/bochs-2.7.tar.gz  https://sourceforge.net/projects/bochs/files/bochs/2.7/bochs-2.7.tar.gz ; then
96                 green "bochs download success"
97             else
98                 red 'bochs download fail, removing temp files... exiting...'
99                 rm -f "$shell_folder"/tools/src/bochs-2.7.tar.gz
100                 exit 255
101             fi
102         fi
103         purple "=> qemu-7.2.0"
104         if  wget -T 5 -c --quiet --show-progress -P "$shell_folder"/tools/src https://download.qemu.org/qemu-7.2.0-rc4.tar.xz; then
105             green "qemu download success"
106         else
107             red 'qemu download fail, exiting... Re-run `bash init.sh -d` to continue qemu download'
108             exit 255
109         fi
110         echo 'Downloading cross-compiler...'
111         purple "=> gcc-12.2.0"
112         if  wget -T 5 -c --quiet --show-progress -P "$shell_folder"/tools/src https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz; then
113             green "gcc download success"
114         else
115             red 'gcc download fail, exiting... Re-run `bash init.sh -d` to continue gcc download'
116             exit 255
117         fi
118         purple "=> binutils-2.38"
119         if  wget -c -T 5 --quiet --show-progress -P "$shell_folder"/tools/src https://ftp.gnu.org/gnu/binutils/binutils-2.38.tar.gz;then
120             green "binutils download success"
121         else
122             red 'binutils downlaod fail, exiting... Re-run `bash init.sh -d` to continue binutils download'
123             exit 255
124         fi
125     fi
126 fi
127
128 if [[ $1 = "-c" ]] || [[ $1 = "--compile" ]] || [[ $1 = "-fc" ]] || [[ $1 = '--fully-compile' ]]; then
129     log="$shell_folder"/tools/log
130     mkdir -p "$log"
131
132     # print info
133     echo -e "Target platform $green$TARGET${return}"
134     echo -e "Cross-compile tools will be installed: ${green}$PREFIX$return"
135     echo -e "Compile logs will be written to: ${green}$log$return"
136     echo -e "Modify first few lines of init.sh to change compile options"
137     if [[ $1 = '-fc' ]]; then
138         echo -e "Please make sure at least ${green}18GB free space$return left, since ${green}qemu$return may take ${green}10GB$return and ${green}gcc$return will take another ${green}4.5GB$return"
139         echo -e "Compile may take ${green}20 minutes${return}, start in 3 seconds..."
140     else
141         echo -e "Please make sure at least ${green}6GB free space$return left, since ${green}gcc$return will take ${green}4.5GB$return"
142         echo -e "Compile may take ${green}5 minutes${return}, start in 3 seconds..."
143     fi
144     sleep 3s
145
146     # basics
147     purple "=> Installing basic utils:"
148     if ! (sudo apt update && sudo apt install -y build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev libsdl1.2-dev texinfo | tee "$log"/basic-utils.log); then
149         red "install basic utils failed, exiting"
150         exit 255
151     fi
152
153     # bochs
154     purple "=> Compile bochs-2.7:"
155     cd "$shell_folder"/tools/src || (red "cd to tools/src fail, nothong changed, exiting..."; exit 255)
156     green "Compile options: $bochs_no_gdb_configure"
157     echo "Extracting..."
158     sleep 3s
159     if ! (tar xzf "$shell_folder"/tools/src/bochs-2.7.tar.gz); then
160         red "extract bochs-2.7.tar.gz fail, exiting"
161         exit 255
162     fi
163     if ! (mkdir -p build-bochs); then
164         red "creating build-bochs fail, exiting..."
165         exit 255
166     fi
167     # bochs-2.7 no-gdb
168     cd "$shell_folder"/tools/src/build-bochs || (red 'cd to build-bochs fail' ;exit)
169     if ! (../bochs-2.7/configure --prefix="$PREFIX" $bochs_no_gdb_configure 2>&1 | tee "$log"/bochs-debugger-configure.log); then
170         red 'bochs-2.7 no-gdb configure fail, exiting...'
171         exit 255
172     fi
173     if ! (make -j "$(nproc)" 2>&1 | tee "$log"/bochs-debugger-make.log); then
174         red "bochs-2.7 no-gdb make fail, exiting..."
175         exit 255
176     fi
177     cp bochs bochsdbg
178     if ! (make install -j "$(nproc)" 2>&1 | tee "$log"/bochs-debugger-make-install.log); then
179         red "bochs-2.7 no-gdb make install fail, exiting..."
180         exit 255
181     fi
182     green "bochs, bochs-gdb, bochsdbg and bximage successfully compiled and installed. PS: ignore bochsdbg not found, it doesn't matter"
183
184     # qemu
185     if [[ $1 = "-fc" ]] || [[ $1 = "--fully-compile" ]]; then
186         purple "=> Compile qemu-7.2.0"
187         cd "$shell_folder"/tools/src || (red "cd to tools/src fail, nothong changed, exiting..." && exit 255)
188         green "Compile options: $qemu_configure"
189         echo "Extracting..."
190         sleep 2s
191         if ! (tar xJf "$shell_folder"/tools/src/qemu-7.2.0-rc4.tar.xz); then
192             red "extract qemu-7.2.0 fail, exiting"
193             exit 255
194         fi
195         if ! (mkdir -p build-qemu); then
196             red "creating build-qemu fail, exiting..."
197             exit 255
198         fi
199         cd "$shell_folder"/tools/src/build-qemu || (red 'cd to build-qemu fail' ;exit)
200         if ! (../qemu-7.2.0-rc4/configure --prefix="$PREFIX" $qemu_configure 2>&1 | tee "$log"/qemu-configure.log); then
201             red "qemu-7.2.0 configure fail, exiting..."
202             exit 255
203         fi
204         if ! (make -j "$(nproc)" 2>&1 | tee "$log"/qemu-make.log); then
205             red "qemu-7.2.0 make fail, exiting..."
206             exit 255
207         fi
208         if ! (make install -j "$(nproc)" 2>&1 | tee "$log"/qemu-make-install.log); then
209             red "qemu-7.2.0 make install fail, exiting..."
210             exit 255
211         fi
212         green "qemu successfully compiled and installed"
213     fi
214
215     # binutils
216     purple "=> Compile binutils-2.38"
217     cd "$shell_folder"/tools/src || (red "cd to tools/src fail, nothong changed, exiting..." && exit 255)
218     green "Compile options: $binutils_configure"
219     echo "Extracting..."
220     sleep 3s
221     if ! (tar xzf "$shell_folder"/tools/src/binutils-2.38.tar.gz); then
222         red "extract binutils-2.38 fail, exiting"
223         exit 255
224     fi
225     if ! (mkdir -p build-binutils); then
226         red "creating build-binutils fail, exiting..."
227         exit 255
228     fi
229     cd "$shell_folder"/tools/src/build-binutils || (red 'cd to build-binutils fail' ;exit)
230     if ! (../binutils-2.38/configure --target=$TARGET --prefix="$PREFIX" $binutils_configure 2>&1 | tee "$log"/binutil-configure.log); then
231         red "binutils-2.38 configure fail, exiting..."
232         exit 255
233     fi
234     if ! (make -j "$(nproc)" 2>&1 | tee "$log"/binutil-make.log); then
235         red "binutils-2.38 make fail, exiting..."
236         exit 255
237     fi
238     if ! (make install -j "$(nproc)" 2>&1 | tee "$log"/binutil-make-install.log); then
239         red "binutils-2.38 make install fail, exiting..."
240         exit 255
241     fi
242     green "binutils successfully compiled and installed"
243
244     # gcc
245     purple "=> Compile gcc"
246     cd "$shell_folder"/tools/src || (echo "cd to tools fail, nothong changed, exiting..." && exit)
247     echo "Searching $TARGET-as..."
248     which -- $TARGET-as || (red "$TARGET-as is not in the PATH, aborting..."; exit)
249     green "Compile options: $binutils_configure"
250     echo "Extracting..."
251     sleep 3s
252     if ! (tar xzf "$shell_folder"/tools/src/gcc-12.2.0.tar.gz); then
253         red "extract gcc-12.2.0.tar.gz fail, exiting..."
254         exit 255
255     fi
256     if ! (mkdir -p build-gcc); then
257         red "creating build-gcc fail, exiting..."
258         exit 255
259     fi
260     cd "$shell_folder"/tools/src/build-gcc || (red 'cd to build-gcc fail' ;exit)
261     if ! (../gcc-12.2.0/configure --target=$TARGET --prefix="$PREFIX" $gcc_configure 2>&1 | tee "$log"/gcc-configure.log); then
262         red "gcc-12.2.0 configure fail, exiting..."
263         exit 255
264     fi
265     if ! (make -j "$(nproc)" all-gcc 2>&1 | tee "$log"/gcc-make-all-gcc.log); then
266         red "gcc-12.2.0 all-gcc make fail, exiting..."
267         exit 255
268     fi
269     if ! (make -j "$(nproc)" all-target-libgcc 2>&1 | tee "$log"/gcc-make-all-target-libgcc.log); then
270         red "gcc-12.2.0 all-target-libgcc make fail, exiting..."
271         exit 255
272     fi
273     if ! (make install-gcc 2>&1 | tee "$log"/gcc-make-install-gcc.log); then
274         red "gcc-12.2.0 all-gcc make install fail, exiting..."
275         exit 255
276     fi
277     if ! (make install-target-libgcc 2>&1 | tee "$log"/gcc-make-install-target-libgcc.log); then
278         red "gcc-12.2.0 all-target-libgcc make install fail, exiting..."
279         exit 255
280     fi
281     green "gcc successfully compiled and installed"
282
283     cd "$shell_folder" || exit;
284     if [[ $1 = "-fc" ]] || [[ $1 = '--fully-compile' ]]; then
285         purple "You can run bochs, bochs-gdb, qemu, $TARGET-gcc, $TARGET-ld, $TARGET-as now"
286     else
287         purple "You can run bochs, bochs-gdb, $TARGET-gcc, $TARGET-ld, $TARGET-as now"
288     fi
289
290     green "Run \`source all-build.sh\` first or manually add tools/bin into your PATH"
291 fi
292