-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild_m68k-toolchain.sh
349 lines (298 loc) · 10.7 KB
/
build_m68k-toolchain.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/bash
#------------------------------------------------------------------------------
#
# build_m68k-toolchain.sh
#
# JP:
# m68k-toolchain をビルドする bash シェルスクリプト
#
# EN:
# This is a bash script to build m68k-toolchain.
#
#------------------------------------------------------------------------------
#
# Copyright (C) 2022 Yosshin(@yosshin4004)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#------------------------------------------------------------------------------
#
# 参考
# How to Build a GCC Cross-Compiler
# https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/
# https://gist.github.com/preshing/41d5c7248dea16238b60
# newlibベースのgccツールチェインの作成
# https://memo.saitodev.com/home/arm/arm_gcc_newlib/
# Installing GCC: Configuration
# https://pipeline.lbl.gov/code/3rd_party/licenses.win/gcc-3.4.4-999/INSTALL/configure.html
#
#-----------------------------------------------------------------------------
# 設定
#
# debian 系のディストリビューションで stable とされている構成に倣っている。
#-----------------------------------------------------------------------------
# gcc の ABI
GCC_ABI=m68k-elf
# binutils
BINUTILS_VERSION="2.35"
BINUTILS_ARCHIVE="binutils-${BINUTILS_VERSION}.tar.bz2"
BINUTILS_SHA512SUM="826c1fdac2dd7b2326c4fc5449a585cf0c1f979a98c6ee062bab5ebd6bc657e697c468fc3a4c3c5a7fa383a4902e8cacd27e1752707bbaf042b36b2653469947"
BINUTILS_URL="https://ftp.gnu.org/gnu/binutils/${BINUTILS_ARCHIVE}"
BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
# gcc
GCC_VERSION="10.2.0"
GCC_ARCHIVE="gcc-${GCC_VERSION}.tar.gz"
GCC_SHA512SUM="5118865a85e70ba58702bb3615d3d9e44dcdbc725fdb71124da78dd412388c5fc367378771bf82fed4d6e2c62fde4c7c362ec1f8ddcae762a2af957f2d605520"
GCC_URL="https://gcc.gnu.org/pub/gcc/releases/gcc-${GCC_VERSION}/${GCC_ARCHIVE}"
GCC_DIR="gcc-${GCC_VERSION}"
# newlib
NEWLIB_VERSION="3.3.0"
NEWLIB_ARCHIVE="newlib-${NEWLIB_VERSION}.tar.gz"
NEWLIB_SHA512SUM="2f0c6666487520e1a0af0b6935431f85d2359e27ded0d01d02567d0d1c6479f2f0e6bbc60405e88e46b92c2a18780a01a60fc9281f7e311cfd40b8d5881d629c"
NEWLIB_URL="ftp://sourceware.org/pub/newlib/${NEWLIB_ARCHIVE}"
NEWLIB_DIR="newlib-${NEWLIB_VERSION}"
# gcc ビルド用ワークディレクトリ
GCC_BUILD_DIR="build_gcc"
#-----------------------------------------------------------------------------
# 準備
#-----------------------------------------------------------------------------
# エラーが起きたらそこで終了させる。
# 未定義変数を参照したらエラーにする。
set -eu
CPU="m68000"
TARGET=${GCC_ABI}
PREFIX="${TARGET}-"
PROGRAM_PREFIX=${PREFIX}
NUM_PROC=$(nproc)
ROOT_DIR="${PWD}"
INSTALL_DIR="${ROOT_DIR}/m68k-toolchain"
DOWNLOAD_DIR="${ROOT_DIR}/${GCC_BUILD_DIR}/download"
BUILD_DIR="${ROOT_DIR}/${GCC_BUILD_DIR}/build"
SRC_DIR="${ROOT_DIR}/${GCC_BUILD_DIR}/src"
WITH_CPU=${CPU}
# gcc ビルド用ワークディレクトリがすでに存在するなら削除確認
if [ -d ${GCC_BUILD_DIR} ]; then
echo "${GCC_BUILD_DIR} already exists."
echo "Do you want to remove the existing directory and proceed the building process? (Y/n)"
read ANS
case $ANS in
"" | [Yy]* )
echo "Yes"
echo "removing ${GCC_BUILD_DIR} ..."
rm -rf ${GCC_BUILD_DIR}
;;
* )
echo "Aborted."
exit 1
;;
esac
fi
# インストール先ディレクトリがすでに存在するなら削除確認
if [ -d ${INSTALL_DIR} ]; then
echo "${INSTALL_DIR} already exists."
echo "Do you want to remove the existing directory and proceed the building process? (Y/n)"
read ANS
case $ANS in
"" | [Yy]* )
echo "Yes"
echo "removing ${INSTALL_DIR} ..."
rm -rf ${INSTALL_DIR}
;;
* )
echo "Aborted."
exit 1
;;
esac
fi
# ディレクトリ作成
mkdir -p ${INSTALL_DIR}
mkdir -p ${BUILD_DIR}
mkdir -p ${SRC_DIR}
mkdir -p ${DOWNLOAD_DIR}
#-----------------------------------------------------------------------------
# binutils のビルド
#-----------------------------------------------------------------------------
mkdir -p ${BUILD_DIR}/${BINUTILS_DIR}
cd ${DOWNLOAD_DIR}
if ! [ -f "${BINUTILS_ARCHIVE}" ]; then
wget ${BINUTILS_URL}
fi
if [ $(sha512sum ${BINUTILS_ARCHIVE} | awk '{print $1}') != ${BINUTILS_SHA512SUM} ]; then
echo "SHA512SUM verification of ${BINUTILS_ARCHIVE} failed!"
exit 1
fi
tar jxvf ${BINUTILS_ARCHIVE} -C ${SRC_DIR}
cd ${BUILD_DIR}/${BINUTILS_DIR}
${SRC_DIR}/${BINUTILS_DIR}/configure \
--prefix=${INSTALL_DIR} \
--program-prefix=${PROGRAM_PREFIX} \
--target=${TARGET} \
--enable-lto \
--enable-interwork \
--enable-multilib \
make -j${NUM_PROC} 2<&1 | tee build.binutils.1.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
make install -j${NUM_PROC} 2<&1 | tee build.binutils.2.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
export PATH=${INSTALL_DIR}/bin:${PATH}
cd ${ROOT_DIR}
#-----------------------------------------------------------------------------
# gcc のビルド(stage1)
#
# C クロスコンパイラを構築する。
# msys 上で起きるファイルパス問題を回避するため、configure を相対パスで
# 起動している。
#-----------------------------------------------------------------------------
mkdir -p ${BUILD_DIR}/${GCC_DIR}_stage1
cd ${DOWNLOAD_DIR}
if ! [ -f "${GCC_ARCHIVE}" ]; then
wget ${GCC_URL}
fi
if [ $(sha512sum ${GCC_ARCHIVE} | awk '{print $1}') != ${GCC_SHA512SUM} ]; then
echo "SHA512SUM verification of ${GCC_ARCHIVE} failed!"
exit 1
fi
tar xvf ${GCC_ARCHIVE} -C ${SRC_DIR}
#
# 新しい mingw 環境では以下のようなエラーとなる。
# ../../../src/gcc-10.2.0/gcc/system.h:743:30: error: expected identifier before string constant
# 743 | #define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
# 応急処置として、問題を起こす行を除去する。
# abort() は stdlib.h 内で宣言された実装のままの挙動となる。
#
if [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then
cat ${SRC_DIR}/${GCC_DIR}/gcc/system.h |\
perl -e 'my $before="#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)";my $after="/* $before */";$before=quotemeta($before);while(<>){$_=~s/$before/$after/g;print $_;}' > ${SRC_DIR}/${GCC_DIR}/gcc/system.h.tmp;
mv ${SRC_DIR}/${GCC_DIR}/gcc/system.h.tmp ${SRC_DIR}/${GCC_DIR}/gcc/system.h
fi
cd ${SRC_DIR}/${GCC_DIR}
./contrib/download_prerequisites
cd ${BUILD_DIR}/${GCC_DIR}_stage1
`realpath --relative-to=./ ${SRC_DIR}/${GCC_DIR}`/configure \
--prefix=${INSTALL_DIR} \
--program-prefix=${PROGRAM_PREFIX} \
--target=${TARGET} \
--enable-lto \
--enable-languages=c \
--without-headers \
--with-arch=m68k \
--with-cpu=${WITH_CPU} \
--with-newlib \
--enable-interwork \
--enable-multilib \
--disable-shared \
--disable-threads \
make -j${NUM_PROC} all-gcc 2<&1 | tee build.gcc-stage1.1.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
make install-gcc 2<&1 | tee build.gcc-stage1.2.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
cd ${ROOT_DIR}
#-----------------------------------------------------------------------------
# newlib のビルド
#-----------------------------------------------------------------------------
mkdir ${BUILD_DIR}/${NEWLIB_DIR}
cd ${DOWNLOAD_DIR}
if ! [ -f "${NEWLIB_ARCHIVE}" ]; then
wget ${NEWLIB_URL}
fi
if [ $(sha512sum ${NEWLIB_ARCHIVE} | awk '{print $1}') != ${NEWLIB_SHA512SUM} ]; then
echo "SHA512SUM verification of ${NEWLIB_ARCHIVE} failed!"
exit 1
fi
#
# newlib のアーカイブはシンボリックリンクを含んでいるため、windows 環境で
# 展開する時は注意が必要。以下のように tar コマンドを使うナイーブな方法では
# 正常動作しない。
# tar zxvf ${NEWLIB_ARCHIVE} -C ${SRC_DIR}
# 代替手段として、pax コマンドを利用する。
#
cd ${SRC_DIR}
pax -z -r -f ${DOWNLOAD_DIR}/${NEWLIB_ARCHIVE}
export CC_FOR_TARGET=${PROGRAM_PREFIX}gcc
export LD_FOR_TARGET=${PROGRAM_PREFIX}ld
export AS_FOR_TARGET=${PROGRAM_PREFIX}as
export AR_FOR_TARGET=${PROGRAM_PREFIX}ar
export RANLIB_FOR_TARGET=${PROGRAM_PREFIX}ranlib
#
# 次の記述は、
# if [ ! -v newlib_cflags ]; then
# としたいが、-v が使えない bash 環境が存在するため、代替手段を利用する。
# "${newlib_cflags+exists}" は、newlib_cflags が未定義なら空文字(偽)、
# そうでなければ exists(真)になる。従って -v の代替になる。
#
if [ ! "${newlib_cflags+exists}" ]; then
newlib_cflags=""
fi
export newlib_cflags="${newlib_cflags} -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__"
cd ${BUILD_DIR}/${NEWLIB_DIR}
${SRC_DIR}/${NEWLIB_DIR}/configure \
--prefix=${INSTALL_DIR} \
--target=${TARGET} \
make -j${NUM_PROC} 2<&1 | tee build.newlib.1.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
make install | tee build.newlib.2.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
cd ${ROOT_DIR}
#-----------------------------------------------------------------------------
# gcc のビルド(stage2)
#
# 残りを一括実行する。
#
# --with-arch=m68k を指定することで、ColdFire 用の libgcc バリエーションが
# 大量に生成されることを回避している。
#-----------------------------------------------------------------------------
mkdir -p ${BUILD_DIR}/${GCC_DIR}_stage2
cd ${BUILD_DIR}/${GCC_DIR}_stage2
`realpath --relative-to=./ ${SRC_DIR}/${GCC_DIR}`/configure \
--prefix=${INSTALL_DIR} \
--program-prefix=${PROGRAM_PREFIX} \
--target=${TARGET} \
--enable-lto \
--enable-languages=c,c++ \
--with-arch=m68k \
--with-cpu=${WITH_CPU} \
--with-newlib \
--enable-interwork \
--enable-multilib \
--disable-shared \
--disable-threads \
make -j${NUM_PROC} 2<&1 | tee build.gcc-stage2.1.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
make install 2<&1 | tee build.gcc-stage2.2.log
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1;
fi
cd ${ROOT_DIR}
#------------------------------------------------------------------------------
# 正常終了
#------------------------------------------------------------------------------
echo ""
echo "-----------------------------------------------------------------------------"
echo "The building process is completed successfully."
echo "-----------------------------------------------------------------------------"
echo ""
exit 0