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)
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
10 export PATH="$PREFIX/bin:$PATH"
11 export BXSHARE="$PREFIX/share/bochs"
16 export CFLAGS="-Wall -O2 -fomit-frame-pointer -pipe"
17 export CXXFLAGS="$CFLAGS"
21 # modify them if you need more/less options. Don't forget <white-space> before ending \
23 # bochs configure options
24 read -r -d '' bochs_no_gdb_configure <<- EOM
27 --enable-debugger-gui \
28 --enable-x86-debugger \
33 # qemu configure options
34 read -r -d '' qemu_configure <<- EOM
37 # gcc configrue options
38 read -r -d '' gcc_configure <<- EOM
40 --enable-language=c,c++ \
44 # binutils configure options
45 read -r -d '' binutils_configure <<- EOM
60 echo -e "$red$1$return"
64 echo -e "$green$1$return"
68 echo -e "$purple$1$return"
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"
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'
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
89 if [[ $1 = "-d" ]] || [[ $1 = "--download" ]]; then
90 echo 'Downloading debug tools...'
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'
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"
98 red 'bochs download fail, removing temp files... exiting...'
99 rm -f "$shell_folder"/tools/src/bochs-2.7.tar.gz
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"
107 red 'qemu download fail, exiting... Re-run `bash init.sh -d` to continue qemu download'
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"
115 red 'gcc download fail, exiting... Re-run `bash init.sh -d` to continue gcc download'
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"
122 red 'binutils downlaod fail, exiting... Re-run `bash init.sh -d` to continue binutils download'
128 if [[ $1 = "-c" ]] || [[ $1 = "--compile" ]] || [[ $1 = "-fc" ]] || [[ $1 = '--fully-compile' ]]; then
129 log="$shell_folder"/tools/log
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..."
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..."
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"
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"
159 if ! (tar xzf "$shell_folder"/tools/src/bochs-2.7.tar.gz); then
160 red "extract bochs-2.7.tar.gz fail, exiting"
163 if ! (mkdir -p build-bochs); then
164 red "creating build-bochs fail, exiting..."
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...'
173 if ! (make -j "$(nproc)" 2>&1 | tee "$log"/bochs-debugger-make.log); then
174 red "bochs-2.7 no-gdb make fail, exiting..."
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..."
182 green "bochs, bochs-gdb, bochsdbg and bximage successfully compiled and installed. PS: ignore bochsdbg not found, it doesn't matter"
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"
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"
195 if ! (mkdir -p build-qemu); then
196 red "creating build-qemu fail, exiting..."
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..."
204 if ! (make -j "$(nproc)" 2>&1 | tee "$log"/qemu-make.log); then
205 red "qemu-7.2.0 make fail, exiting..."
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..."
212 green "qemu successfully compiled and installed"
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"
221 if ! (tar xzf "$shell_folder"/tools/src/binutils-2.38.tar.gz); then
222 red "extract binutils-2.38 fail, exiting"
225 if ! (mkdir -p build-binutils); then
226 red "creating build-binutils fail, exiting..."
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..."
234 if ! (make -j "$(nproc)" 2>&1 | tee "$log"/binutil-make.log); then
235 red "binutils-2.38 make fail, exiting..."
238 if ! (make install -j "$(nproc)" 2>&1 | tee "$log"/binutil-make-install.log); then
239 red "binutils-2.38 make install fail, exiting..."
242 green "binutils successfully compiled and installed"
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"
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..."
256 if ! (mkdir -p build-gcc); then
257 red "creating build-gcc fail, exiting..."
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..."
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..."
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..."
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..."
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..."
281 green "gcc successfully compiled and installed"
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"
287 purple "You can run bochs, bochs-gdb, $TARGET-gcc, $TARGET-ld, $TARGET-as now"
290 green "Run \`source all-build.sh\` first or manually add tools/bin into your PATH"