Skip to content

Commit 58a56e0

Browse files
committed
gnu gcc builder script #686
1 parent e884f97 commit 58a56e0

1 file changed

Lines changed: 195 additions & 0 deletions

File tree

bin/spc-gnu-build-gcc

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/env bash
2+
3+
# This file is using docker to build gcc and binutils and copy them to the host
4+
set -e
5+
6+
# Set default GCC version if not provided
7+
if [ -z "$GCC_VERSION" ]; then
8+
GCC_VERSION="10.5.0"
9+
fi
10+
11+
# Set default BINUTILS version if not provided
12+
if [ -z "$BINUTILS_VERSION" ]; then
13+
BINUTILS_VERSION="2.41"
14+
fi
15+
16+
# Detect docker can run
17+
if ! which docker >/dev/null; then
18+
echo "Docker is not installed, please install docker first !"
19+
exit 1
20+
fi
21+
DOCKER_EXECUTABLE="docker"
22+
# shellcheck disable=SC2046
23+
if [ $(id -u) -ne 0 ]; then
24+
if ! docker info > /dev/null 2>&1; then
25+
if [ "$SPC_USE_SUDO" != "yes" ] && [ "$SPC_DOCKER_DEBUG" != "yes" ]; then
26+
echo "Docker command requires sudo"
27+
# shellcheck disable=SC2039
28+
echo -n 'To use sudo to run docker, run "export SPC_USE_SUDO=yes" and run command again'
29+
exit 1
30+
fi
31+
DOCKER_EXECUTABLE="sudo docker"
32+
fi
33+
fi
34+
35+
# to check if qemu-docker run
36+
if [ "$SPC_USE_ARCH" = "" ]; then
37+
SPC_USE_ARCH=current
38+
fi
39+
case $SPC_USE_ARCH in
40+
current)
41+
BASE_ARCH=$(uname -m)
42+
if [ "$BASE_ARCH" = "arm64" ]; then
43+
BASE_ARCH=aarch64
44+
fi
45+
;;
46+
aarch64)
47+
BASE_ARCH=aarch64
48+
# shellcheck disable=SC2039
49+
echo -e "\e[033m* Using different arch needs to setup qemu-static for docker !\e[0m"
50+
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
51+
;;
52+
*)
53+
echo "Current arch is not supported to run in docker: $SPC_USE_ARCH"
54+
exit 1
55+
;;
56+
esac
57+
58+
# Detect docker env is setup
59+
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-gnu-gcc-builder-$SPC_USE_ARCH; then
60+
echo "Docker container does not exist. Building docker image ..."
61+
$DOCKER_EXECUTABLE build --build-arg GCC_VERSION=$GCC_VERSION --build-arg BINUTILS_VERSION=$BINUTILS_VERSION -t cwcc-spc-gnu-gcc-builder-$SPC_USE_ARCH -f- . <<EOF
62+
FROM centos:7
63+
ARG GCC_VERSION
64+
ARG BINUTILS_VERSION
65+
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/*.repo && \
66+
sed -i 's/^#.*baseurl=http/baseurl=http/g' /etc/yum.repos.d/*.repo && \
67+
sed -i 's/^mirrorlist=http/#mirrorlist=http/g' /etc/yum.repos.d/*.repo
68+
RUN yum clean all && \
69+
yum makecache && \
70+
yum update -y && \
71+
localedef -c -i en_US -f UTF-8 en_US.UTF-8
72+
73+
RUN yum install -y centos-release-scl
74+
75+
RUN if [ "$BASE_ARCH" = "aarch64" ]; then \
76+
sed -i 's|mirror.centos.org/centos|vault.centos.org/altarch|g' /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo ; \
77+
sed -i 's|mirror.centos.org/centos|vault.centos.org/altarch|g' /etc/yum.repos.d/CentOS-SCLo-scl.repo ; \
78+
else \
79+
sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/*.repo ; \
80+
fi
81+
RUN sed -i 's/^#.*baseurl=http/baseurl=http/g' /etc/yum.repos.d/*.repo && \
82+
sed -i 's/^mirrorlist=http/#mirrorlist=http/g' /etc/yum.repos.d/*.repo
83+
84+
RUN yum update -y && \
85+
yum install -y devtoolset-10-gcc-* wget tar gzip bzip2 make file texinfo flex bison patch \
86+
gmp-devel mpfr-devel libmpc-devel zlib-devel perl gcc gcc-c++
87+
88+
RUN echo "source scl_source enable devtoolset-10" >> /etc/bashrc
89+
RUN source /etc/bashrc
90+
91+
# Download GCC and Binutils sources during image build
92+
RUN mkdir -p /build/sources && \
93+
cd /build/sources && \
94+
wget https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz && \
95+
wget https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.gz
96+
97+
WORKDIR /build
98+
EOF
99+
fi
100+
101+
# Create the dist directory
102+
mkdir -p "$(pwd)/dist"
103+
104+
# Run the build process in the Docker container
105+
# The -v flag creates a volume mount that shares a directory between host and container
106+
# This allows files created in the container's /dist directory to be directly accessible on the host
107+
echo "Starting GCC and Binutils build process..."
108+
$DOCKER_EXECUTABLE run --rm -v "$(pwd)/dist:/dist" -e GCC_VERSION="$GCC_VERSION" -e BINUTILS_VERSION="$BINUTILS_VERSION" cwcc-spc-gnu-gcc-builder-$SPC_USE_ARCH /bin/bash -c "
109+
set -e
110+
source /etc/bashrc
111+
cd /build
112+
113+
# Extract sources
114+
echo 'Extracting GCC and Binutils sources...'
115+
tar -xzf /build/sources/gcc-${GCC_VERSION}.tar.gz
116+
tar -xzf /build/sources/binutils-${BINUTILS_VERSION}.tar.gz
117+
118+
# Download prerequisites for GCC
119+
cd /build/gcc-\${GCC_VERSION}
120+
./contrib/download_prerequisites
121+
122+
# Build Binutils first
123+
echo 'Building Binutils...'
124+
cd /build
125+
mkdir -p binutils-build
126+
cd binutils-build
127+
../binutils-${BINUTILS_VERSION}/configure \
128+
--prefix=/usr/local/gcc10 \
129+
--with-lib-path=/usr/local/gcc10/lib:/usr/local/gcc10/lib64 \
130+
--enable-gold \
131+
--enable-ld=default \
132+
--enable-plugins \
133+
--enable-shared \
134+
--disable-werror \
135+
--enable-64-bit-bfd \
136+
--with-system-zlib
137+
138+
# Build GCC
139+
echo 'Building GCC...'
140+
cd /build
141+
mkdir -p gcc-build
142+
cd gcc-build
143+
../gcc-${GCC_VERSION}/configure \
144+
--prefix=/usr/local/gcc10 \
145+
--enable-bootstrap \
146+
--enable-languages=c,c++,lto \
147+
--enable-static \
148+
--enable-shared \
149+
--enable-threads=posix \
150+
--enable-checking=release \
151+
--disable-multilib \
152+
--with-zlib \
153+
--enable-default-pie \
154+
--enable-default-ssp \
155+
--disable-libunwind-exceptions \
156+
--enable-gnu-unique-object \
157+
--enable-linker-build-id \
158+
--with-gcc-major-version-only \
159+
--enable-initfini-array \
160+
--enable-plugins \
161+
--enable-gnu-indirect-function \
162+
--with-tune=generic \
163+
--with-arch_32=x86-64 \
164+
--build=x86_64-redhat-linux \
165+
--disable-libquadmath \
166+
--disable-libquadmath-support \
167+
--disable-nls \
168+
--with-pic \
169+
CFLAGS='-O2 -fPIC -fPIE -g0' \
170+
CXXFLAGS='-O2 -fPIC -fPIE -g0'
171+
172+
# Build Binutils first
173+
echo 'Building Binutils...'
174+
cd /build
175+
make -C binutils-build -j$(nproc)
176+
177+
# Install Binutils directly to /usr/local/gcc10
178+
echo 'Installing Binutils...'
179+
make -C binutils-build install
180+
181+
# Now build GCC (after binutils is installed)
182+
echo 'Building GCC...'
183+
make -C gcc-build -j$(nproc)
184+
echo 'Installing GCC...'
185+
make -C gcc-build install
186+
187+
# Create tarball
188+
echo 'Creating tarball...'
189+
cd /
190+
cd /usr/local/gcc10 && tar -czf /dist/gcc-${GCC_VERSION}-binutils-${BINUTILS_VERSION}.tar.gz .
191+
192+
echo 'Build completed successfully!'
193+
"
194+
195+
echo "GCC and Binutils have been built and packaged to $(pwd)/dist/gcc-${GCC_VERSION}-binutils-${BINUTILS_VERSION}.tar.gz on the host machine"

0 commit comments

Comments
 (0)