-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild_binutils.sh
executable file
·217 lines (203 loc) · 6.92 KB
/
build_binutils.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
#!/usr/bin/env bash
source utils.sh
# A Script to build GNU binutils
set -e
# Binutils version
BINUTILS_VER="2_41"
BINUTILS_AVX_FLAGS="${NO_AVX_FLAGS}"
for arg in "$@"; do
case "${arg}" in
"--use-jemalloc")
USE_JEMALLOC=1
;;
"--avx2")
BINUTILS_AVX_FLAGS="${AVX_FLAGS}"
AVX_OPT=1
;;
esac
done
if [[ ${USE_JEMALLOC} -eq 1 ]]; then
build_jemalloc() {
cd "${BUILDDIR}"
jemalloc_fetch_vars
if [[ ${NO_JEMALLOC} -eq 1 ]]; then
JEMALLOC_ARGS="--shallow-clone"
if [[ ${AVX_OPT} -eq 1 ]]; then
JEMALLOC_ARGS="--shallow-clone --avx2"
fi
bash "${BUILDDIR}/build_jemalloc.sh" "${JEMALLOC_ARGS}"
fi
}
fi
# The main build function that builds GNU binutils.
build_binutils() {
if [[ ${USE_JEMALLOC} -eq 1 ]]; then
echo "Building jemalloc libs if not built already"
build_jemalloc
fi
export CC="gcc"
export CXX="g++"
export CFLAGS="-march=x86-64 ${BINUTILS_AVX_FLAGS} -flto=auto -flto-compression-level=10 ${COMMON_GCC_CFLAGS[*]}"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="${COMMON_GCC_LDFLAGS}"
if [[ ${USE_JEMALLOC} -eq 1 ]]; then
export LDFLAGS+=" ${JEMALLOC_FLAGS}"
fi
case $1 in
"X86")
echo "Starting Binutils Build for x86-64"
rm -rf "$2" && mkdir -p "$2" && cd "$2"
"${BINUTILS_DIR}"/configure \
--enable-relro \
--enable-targets=x86_64-pep \
--prefix="$3" \
--target=x86_64-pc-linux-gnu \
--with-pic \
"${COMMON_BINUTILS_ARGS[@]}"
;;
"ARM64")
echo "Starting Binutils Build for arm64"
rm -rf "$2" && mkdir -p "$2" && cd "$2"
"${BINUTILS_DIR}"/configure \
--disable-multilib \
--disable-nls \
--prefix="$3" \
--target=aarch64-linux-gnu \
--with-gnu-as \
--with-gnu-ld \
"${COMMON_BINUTILS_ARGS[@]}"
;;
"ARM")
echo "Starting Binutils Build for arm"
rm -rf "$2" && mkdir -p "$2" && cd "$2"
"${BINUTILS_DIR}"/configure \
--disable-multilib \
--disable-nls \
--prefix="$3" \
--target=arm-linux-gnueabi \
--with-gnu-as \
--with-gnu-ld \
"${COMMON_BINUTILS_ARGS[@]}"
;;
*)
echo "Invalid target: $1. Supported targets are: ARM,ARM64,X86"
exit 1
;;
esac
make -j$(($(getconf _NPROCESSORS_ONLN) + 2)) >/dev/null
make install -j$(($(getconf _NPROCESSORS_ONLN) + 2)) >/dev/null
unset CC CXX CFLAGS CXXFLAGS LDFLAGS
}
for arg in "$@"; do
case "${arg}" in
"--no-update")
NO_UPDATE=1
;;
esac
done
for arg in "$@"; do
case "${arg}" in
"--shallow-clone")
SHALLOW_CLONE=1
;;
esac
done
for arg in "$@"; do
case "${arg}" in
"--sync-source-only")
if [[ -d ${BINUTILS_DIR} ]]; then
if [[ ${NO_UPDATE} -eq 0 ]]; then
echo "Existing binutils source found. Fetching new changes"
cd "${BINUTILS_DIR}"
if [[ ${SHALLOW_CLONE} -eq 1 ]]; then
binutils_fetch "fetch" "${BINUTILS_VER}" "--depth=1"
git reset --hard FETCH_HEAD
git clean -dfx
else
is_shallow=$(git rev-parse --is-shallow-repository 2>/dev/null)
if [ "$is_shallow" = "true" ]; then
binutils_fetch "fetch" "${BINUTILS_VER}" "--depth=1"
git reset --hard FETCH_HEAD
git clean -dfx
else
git reset --hard HEAD
binutils_fetch "pull" "${BINUTILS_VER}"
fi
fi
sed -i '/^development=/s/true/false/' bfd/development.sh
cd "${BUILDDIR}"
fi
else
echo "Cloning binutils repo"
if [[ ${SHALLOW_CLONE} -eq 1 ]]; then
binutils_clone "${BINUTILS_VER}" "--depth=1"
else
binutils_clone "${BINUTILS_VER}"
fi
sed -i '/^development=/s/true/false/' binutils-gdb/bfd/development.sh
fi
exit 0
;;
esac
done
for arg in "$@"; do
case "${arg}" in
"--install-dir"*)
INSTALL_DIR="${arg#*--install-dir}"
INSTALL_DIR=${INSTALL_DIR:1}
;;
"--build-dir"*)
BINUTILS_BUILD="${arg#*--build-dir}"
BINUTILS_BUILD=${BINUTILS_BUILD:1}
;;
esac
done
for arg in "$@"; do
case "${arg}" in
"--targets"*)
targets="${arg#*--targets}"
targets=${targets:1}
IFS=', ' read -r -a archs <<<"${targets}"
echo "Build dir path: ${BINUTILS_BUILD}"
echo "Installing at: ${INSTALL_DIR}"
if [[ -d ${BINUTILS_DIR} ]]; then
if [[ ${NO_UPDATE} -eq 0 ]]; then
echo "Existing binutils source found. Fetching new changes"
cd "${BINUTILS_DIR}"
if [[ ${SHALLOW_CLONE} -eq 1 ]]; then
binutils_fetch "fetch" "${BINUTILS_VER}" "--depth=1"
git reset --hard FETCH_HEAD
git clean -dfx
else
if $(git rev-parse --is-shallow-repository); then
binutils_fetch "fetch" "${BINUTILS_VER}" "--depth=1"
git reset --hard FETCH_HEAD
git clean -dfx
else
git reset --hard HEAD
binutils_fetch "pull" "${BINUTILS_VER}"
fi
fi
sed -i '/^development=/s/true/false/' bfd/development.sh
cd "${BUILDDIR}"
fi
else
echo "Cloning binutils repo"
if [[ ${SHALLOW_CLONE} -eq 1 ]]; then
binutils_clone "${BINUTILS_VER}" "--depth=1"
else
binutils_clone "${BINUTILS_VER}"
fi
sed -i '/^development=/s/true/false/' binutils-gdb/bfd/development.sh
fi
for arch in "${archs[@]}"; do
build_binutils "${arch}" "${BINUTILS_BUILD}" "${INSTALL_DIR}"
done
exit 0
;;
*)
echo "Invalid argument passed: ${arg}"
exit 1
;;
esac
done