Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active May 30, 2023 05:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/3ff4ba67e4150c9abdccf3c499b211de to your computer and use it in GitHub Desktop.
Save Low-power/3ff4ba67e4150c9abdccf3c499b211de to your computer and use it in GitHub Desktop.
#!/bin/sh
# Copyright 2015-2022 Rivoreo
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# crontab(5) example:
# LC_TIME=C
# 10 8 * * 1 dt=`date +\%F.\%H-\%M` && tmux -L download-and-build-gcc-snapshot new-session -d "exec script -t -c 'exec sh src/download-and-build-gcc-snapshot.sh' src/download-and-build-gcc-snapshot.log.$dt 2> src/download-and-build-gcc-snapshot.log.$dt.time"
GCC_MAJOR_VERSION=12
SOURCE_PREFIX="$HOME/src"
#BUILD_PLATFORM=x86_64-unknown-freebsd11
#EXTRA_CONFIGURE_FLAGS="--enable-plugin --disable-libsanitizer"
BUILD_PLATFORM=powerpc-unknown-linux-gnu
EXTRA_CONFIGURE_FLAGS="--enable-gnu-unique-object --enable-plugin --disable-softfloat --enable-targets=powerpc-linux,powerpc64-linux --enable-multiarch --with-long-double-128 --disable-multilib"
#BUILD_PLATFORM=powerpc-ibm-aix7.1
#EXTRA_CONFIGURE_FLAGS="--with-ld=/usr/bin/ld --enable-decimal-float=dpd --with-long-double-128"
#export gcc_cv_as_hidden=no
GCC_SNAPSHOT_URL=http://ftp.tsukuba.wide.ad.jp/software/gcc/snapshots/LATEST-$GCC_MAJOR_VERSION/
WGET=wget
WGET_FLAGS="--timeout 30 --waitretry 1 --tries 1024"
MAKE=make
#MAKE=gmake
set -x
set -e
if ! ncpus="`getconf NPROCESSORS_ONLN || getconf _NPROCESSORS_ONLN || sysctl -n kern.smp.cpus || nproc`"; then
if available_processors="`bindprocessor -q`"; then
ncpus=`printf %s "$available_processors" | grep -Eo " [0-9]+" | wc -l`
else
ncpus=1
fi
fi
true > /tmp/empty
if sed -i "" "" /tmp/empty; then
sed_in_place() {
sed -i "" "$@"
}
else
sed_in_place() {
sed -i "$@"
}
fi
latest_source_package_file_name="^ *<tr><td><a href=\"gcc\\-$GCC_MAJOR_VERSION\\-[0-9]{8}\\.tar\\.xz\">gcc\\-$GCC_MAJOR_VERSION\\-[0-9]{8}\\.tar\\.xz</a></td>"
latest_source_package_file_name="`$WGET $WGET_FLAGS \"$GCC_SNAPSHOT_URL\" -O - | grep -Eo \"$latest_source_package_file_name\"`"
latest_source_package_file_name="${latest_source_package_file_name#*<a href=\"}"
latest_source_package_file_name="${latest_source_package_file_name%%\">*}"
source_directory_name="${latest_source_package_file_name%.tar.xz}"
if [ -f "$SOURCE_PREFIX/$latest_source_package_file_name" ]; then
printf "Source package %s is already downloaded\\n" "$latest_source_package_file_name" 1>&2
exit 0
fi
if [ -d "$SOURCE_PREFIX/$source_directory_name" ]; then
printf "Source directory %s already exists\\n" "$source_directory_name" 1>&2
exit 0
fi
cd "$SOURCE_PREFIX"
$WGET $WGET_FLAGS "$GCC_SNAPSHOT_URL$latest_source_package_file_name"
tar -x -f "$latest_source_package_file_name"
if [ ! -d "$source_directory_name" ]; then
printf "Expected directory %s doesn't exist after extracting source package\\n" "$source_directory_name" 1>&2
exit 1
fi
sed_in_place -E -e "s/^([[:space:]]+)(hardcode_libdir_flag_spec[_a-zA-Z0-9]*=[\"'](\\\$\\{wl\\}){0,1}(\\-(rpath|blibpath|R)|\\+b ).+)/\\1#\\2/" -e "s/^([[:space:]]+)(runpath_var=[\"']{0,1}LD_RUN_PATH[\"']{0,1})/\\1#\\2/" -e 's/^([[:space:]]+)(hardcode_into_libs=yes)/\1#\2/' -e 's#( \-install_name )\\\$rpath/#\1#g' "$source_directory_name"/configure "$source_directory_name"/*/configure "$source_directory_name"/*/*/configure || true
sed_in_place s/need_relink=yes/need_relink=no/ "$source_directory_name"/ltmain.sh
mkdir "$source_directory_name-build"
cd "$source_directory_name-build"
# Use GCC 9.4 as bootstrap compiler
export PATH=/opt/gcc-9.4/bin:$PATH
export CC=gcc-9.4
export CXX=g++-9.4
export CFLAGS=-Os
export CXXFLAGS=-Os
export BOOT_CFLAGS="-g -Os"
export BOOT_CXXFLAGS="-g -Os"
"../$source_directory_name/configure" --build=$BUILD_PLATFORM --prefix=/usr/local --sysconfdir=/etc --localstatedir=/var --libexecdir='${prefix}/lib' --enable-version-specific-runtime-libs --disable-rpath --with-system-zlib --enable-languages=c,c++,objc,obj-c++,fortran,lto --enable-initfini-array --enable-gnu-indirect-function --program-suffix=${source_directory_name#gcc} $EXTRA_CONFIGURE_FLAGS
time $MAKE -j $((ncpus+2))
$MAKE install prefix=/opt/$source_directory_name gxx_include_dir=/opt/$source_directory_name/include/c++/${source_directory_name#gcc-} libexecdir=/opt/$source_directory_name/lib
cd /opt/$source_directory_name/lib/gcc/$BUILD_PLATFORM/
for d in lib64 lib; do
[ -f $d/libgcc_s.so.1 ] && mv $d/libgcc_s.so.1 $d/libgcc_s.so $GCC_MAJOR_VERSION.*.*/ && break
done
rmdir lib64 lib > /dev/null 2>&1 || true
/opt/$source_directory_name/bin/$source_directory_name --version
/opt/$source_directory_name/bin/$source_directory_name -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment