2013-11-12 00:04:21 +04:00
|
|
|
#!/usr/bin/env bash
|
2017-01-18 12:47:13 +03:00
|
|
|
#
|
|
|
|
# Build and install gcc/gcc++ as a cross-compiler with target OSX,
|
|
|
|
# using `clang`.
|
|
|
|
#
|
|
|
|
# You may want to run this script if you want to build software using
|
|
|
|
# gcc. Please refer to the README.md for details.
|
|
|
|
#
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-15 22:06:50 +04:00
|
|
|
pushd "${0%/*}" &>/dev/null
|
|
|
|
|
2014-03-25 02:11:25 +04:00
|
|
|
unset LIBRARY_PATH
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
DESC=gcc
|
2014-07-18 00:43:29 +04:00
|
|
|
USESYSTEMCOMPILER=1
|
2013-12-17 00:53:21 +04:00
|
|
|
source tools/tools.sh
|
2013-11-12 21:14:54 +04:00
|
|
|
|
2013-11-12 00:04:21 +04:00
|
|
|
# GCC version to build
|
2013-12-15 22:06:50 +04:00
|
|
|
# (<4.7 will not work properly with libc++)
|
2014-10-01 21:53:41 +04:00
|
|
|
if [ -z "$GCC_VERSION" ]; then
|
2019-08-19 21:46:00 +03:00
|
|
|
GCC_VERSION=9.2.0
|
2014-10-01 21:53:41 +04:00
|
|
|
#GCC_VERSION=5-20140928 # snapshot
|
|
|
|
fi
|
2013-11-12 00:04:21 +04:00
|
|
|
|
|
|
|
# GCC mirror
|
2019-10-09 12:34:47 +03:00
|
|
|
GCC_MIRROR="https://mirror.koddos.net/gcc"
|
2013-11-12 00:04:21 +04:00
|
|
|
|
|
|
|
require wget
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
pushd $BUILD_DIR &>/dev/null
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
function remove_locks()
|
|
|
|
{
|
2019-11-02 15:26:21 +03:00
|
|
|
rm -rf $BUILD_DIR/have_gcc*
|
2013-12-17 00:53:21 +04:00
|
|
|
}
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
source $BASE_DIR/tools/trap_exit.sh
|
2013-12-15 22:06:50 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ ! -f "have_gcc_${GCC_VERSION}_${TARGET}" ]; then
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
pushd $TARBALL_DIR &>/dev/null
|
2014-04-22 21:34:08 +04:00
|
|
|
if [[ $GCC_VERSION != *-* ]]; then
|
2019-06-01 20:57:44 +03:00
|
|
|
wget -c "$GCC_MIRROR/releases/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.xz"
|
2014-04-22 21:34:08 +04:00
|
|
|
else
|
2019-06-01 20:57:44 +03:00
|
|
|
wget -c "$GCC_MIRROR/snapshots/$GCC_VERSION/gcc-$GCC_VERSION.tar.xz"
|
2014-04-22 21:34:08 +04:00
|
|
|
fi
|
2013-12-17 00:53:21 +04:00
|
|
|
popd &>/dev/null
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-15 22:06:50 +04:00
|
|
|
echo "cleaning up ..."
|
2013-11-12 00:04:21 +04:00
|
|
|
rm -rf gcc* 2>/dev/null
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
extract "$TARBALL_DIR/gcc-$GCC_VERSION.tar.xz"
|
2014-03-25 02:11:25 +04:00
|
|
|
echo ""
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
pushd gcc*$GCC_VERSION* &>/dev/null
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
rm -f $TARGET_DIR/bin/*-gcc*
|
|
|
|
rm -f $TARGET_DIR/bin/*-g++*
|
2013-11-16 21:36:44 +04:00
|
|
|
|
2019-06-02 12:54:02 +03:00
|
|
|
if [ $(osxcross-cmp $GCC_VERSION '>' 5.0.0) -eq 1 ] &&
|
|
|
|
[ $(osxcross-cmp $GCC_VERSION '<' 5.3.0) -eq 1 ]; then
|
2015-10-04 19:58:46 +03:00
|
|
|
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66035
|
2019-11-02 15:26:21 +03:00
|
|
|
patch -p1 < $PATCH_DIR/gcc-pr66035.patch
|
2015-10-04 19:58:46 +03:00
|
|
|
fi
|
|
|
|
|
2019-06-02 12:54:02 +03:00
|
|
|
if [ $(osxcross-cmp $GCC_VERSION '>=' 6.1.0) -eq 1 ] &&
|
|
|
|
[ $(osxcross-cmp $GCC_VERSION '<=' 6.3.0) -eq 1 ]; then
|
2017-08-14 00:42:37 +03:00
|
|
|
# https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00129.html
|
2019-11-02 15:26:21 +03:00
|
|
|
patch -p1 < $PATCH_DIR/gcc-6-buildfix.patch
|
2017-08-14 00:42:37 +03:00
|
|
|
fi
|
|
|
|
|
2019-06-02 12:54:02 +03:00
|
|
|
if [ $(osxcross-cmp $GCC_VERSION '==' 6.3.0) -eq 1 ]; then
|
2017-08-14 00:42:37 +03:00
|
|
|
# https://gcc.gnu.org/viewcvs/gcc/trunk/gcc/config/darwin-driver.c?r1=244010&r2=244009&pathrev=244010
|
2019-11-02 15:26:21 +03:00
|
|
|
patch -p1 < $PATCH_DIR/darwin-driver.c.patch
|
2017-01-28 12:24:46 +03:00
|
|
|
fi
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ $(osxcross-cmp $SDK_VERSION '>=' 10.14) -eq 1 ] &&
|
2019-06-02 12:54:02 +03:00
|
|
|
[ $(osxcross-cmp $GCC_VERSION '<' 9.0.0) -eq 1 ]; then
|
|
|
|
files_to_patch=(
|
|
|
|
libsanitizer/asan/asan_mac.cc
|
|
|
|
libsanitizer/sanitizer_common/sanitizer_platform_limits_posix.cc
|
|
|
|
libsanitizer/sanitizer_common/sanitizer_posix.cc
|
|
|
|
libsanitizer/sanitizer_common/sanitizer_mac.cc
|
|
|
|
gcc/ada/init.c
|
|
|
|
gcc/config/darwin-driver.c
|
|
|
|
)
|
|
|
|
|
|
|
|
for file in ${files_to_patch[*]}; do
|
|
|
|
if [ -f $file ]; then
|
2019-06-04 19:34:11 +03:00
|
|
|
echo "patching $PWD/$file"
|
2019-06-02 12:54:02 +03:00
|
|
|
$SED -i 's/#include <sys\/sysctl.h>/#define _Atomic volatile\n#include <sys\/sysctl.h>\n#undef _Atomic/g' $file
|
|
|
|
$SED -i 's/#include <sys\/mount.h>/#define _Atomic volatile\n#include <sys\/mount.h>\n#undef _Atomic/g' $file
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2013-11-12 00:04:21 +04:00
|
|
|
mkdir -p build
|
2013-12-17 00:53:21 +04:00
|
|
|
pushd build &>/dev/null
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2014-04-14 23:59:03 +04:00
|
|
|
if [[ $PLATFORM == *BSD ]]; then
|
2014-04-06 17:58:15 +04:00
|
|
|
export CPATH="/usr/local/include:/usr/pkg/include:$CPATH"
|
|
|
|
export LDFLAGS="-L/usr/local/lib -L/usr/pkg/lib $LDFLAGS"
|
|
|
|
export LD_LIBRARY_PATH="/usr/local/lib:/usr/pkg/lib:$LD_LIBRARY_PATH"
|
2014-04-14 23:59:03 +04:00
|
|
|
elif [ "$PLATFORM" == "Darwin" ]; then
|
|
|
|
export CPATH="/opt/local/include:$CPATH"
|
|
|
|
export LDFLAGS="-L/opt/local/lib $LDFLAGS"
|
|
|
|
export LD_LIBRARY_PATH="/opt/local/lib:$LD_LIBRARY_PATH"
|
|
|
|
fi
|
|
|
|
|
|
|
|
EXTRACONFFLAGS=""
|
|
|
|
|
|
|
|
if [ "$PLATFORM" != "Darwin" ]; then
|
2019-11-02 15:26:21 +03:00
|
|
|
EXTRACONFFLAGS+="--with-ld=$TARGET_DIR/bin/x86_64-apple-$TARGET-ld "
|
|
|
|
EXTRACONFFLAGS+="--with-as=$TARGET_DIR/bin/x86_64-apple-$TARGET-as "
|
2013-12-15 22:06:50 +04:00
|
|
|
fi
|
|
|
|
|
2014-10-01 21:53:41 +04:00
|
|
|
LANGS="c,c++,objc,obj-c++"
|
|
|
|
|
|
|
|
if [ -n "$ENABLE_FORTRAN" ]; then
|
|
|
|
LANGS+=",fortran"
|
|
|
|
fi
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ $(osxcross-cmp $SDK_VERSION "<=" 10.13) -eq 1 ]; then
|
2019-06-01 20:57:44 +03:00
|
|
|
EXTRACONFFLAGS+="--with-multilib-list=m32,m64 --enable-multilib "
|
|
|
|
else
|
|
|
|
EXTRACONFFLAGS+="--disable-multilib "
|
|
|
|
fi
|
|
|
|
|
2013-11-12 00:04:21 +04:00
|
|
|
../configure \
|
2019-11-02 15:26:21 +03:00
|
|
|
--target=x86_64-apple-$TARGET \
|
|
|
|
--with-sysroot=$SDK \
|
2014-04-06 17:58:15 +04:00
|
|
|
--disable-nls \
|
2014-10-01 21:53:41 +04:00
|
|
|
--enable-languages=$LANGS \
|
2014-04-06 17:58:15 +04:00
|
|
|
--without-headers \
|
|
|
|
--enable-lto \
|
|
|
|
--enable-checking=release \
|
2014-07-18 00:02:41 +04:00
|
|
|
--disable-libstdcxx-pch \
|
2019-11-02 15:26:21 +03:00
|
|
|
--prefix=$TARGET_DIR \
|
2014-07-18 00:02:41 +04:00
|
|
|
--with-system-zlib \
|
2014-04-14 23:59:03 +04:00
|
|
|
$EXTRACONFFLAGS
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-15 22:06:50 +04:00
|
|
|
$MAKE -j$JOBS
|
2014-04-23 00:40:05 +04:00
|
|
|
$MAKE install
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2014-04-22 23:17:15 +04:00
|
|
|
GCC_VERSION=`echo $GCC_VERSION | tr '-' ' ' | awk '{print $1}'`
|
2014-04-23 21:31:49 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
pushd $TARGET_DIR/x86_64-apple-$TARGET/include &>/dev/null
|
2014-04-23 21:31:49 +04:00
|
|
|
pushd c++/${GCC_VERSION}* &>/dev/null
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
cat $PATCH_DIR/libstdcxx.patch | \
|
|
|
|
$SED "s/darwin13/$TARGET/g" | \
|
2014-04-23 21:31:49 +04:00
|
|
|
patch -p0 -l &>/dev/null || true
|
|
|
|
|
|
|
|
popd &>/dev/null
|
|
|
|
popd &>/dev/null
|
2014-04-22 23:17:15 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
popd &>/dev/null # build
|
|
|
|
popd &>/dev/null # gcc
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
touch "have_gcc_${GCC_VERSION}_${TARGET}"
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
fi # have gcc
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
popd &>/dev/null # build dir
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2014-07-18 00:43:29 +04:00
|
|
|
unset USESYSTEMCOMPILER
|
|
|
|
source tools/tools.sh
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
pushd $TARGET_DIR/bin &>/dev/null
|
2013-12-15 22:06:50 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ ! -f i386-apple-$TARGET-base-gcc ]; then
|
|
|
|
mv x86_64-apple-$TARGET-gcc \
|
|
|
|
x86_64-apple-$TARGET-base-gcc
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
mv x86_64-apple-$TARGET-g++ \
|
|
|
|
x86_64-apple-$TARGET-base-g++
|
2019-06-01 20:57:44 +03:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ $(osxcross-cmp $SDK_VERSION "<=" 10.13) -eq 1 ]; then
|
|
|
|
create_symlink x86_64-apple-$TARGET-base-gcc \
|
|
|
|
i386-apple-$TARGET-base-gcc
|
2016-10-29 20:26:05 +03:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
create_symlink x86_64-apple-$TARGET-base-g++ \
|
|
|
|
i386-apple-$TARGET-base-g++
|
2019-06-01 20:57:44 +03:00
|
|
|
fi
|
2013-11-12 00:04:21 +04:00
|
|
|
fi
|
|
|
|
|
2014-04-07 00:27:59 +04:00
|
|
|
echo "compiling wrapper ..."
|
2013-12-15 22:06:50 +04:00
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
TARGETCOMPILER=gcc \
|
|
|
|
$BASE_DIR/wrapper/build_wrapper.sh
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2013-12-17 00:53:21 +04:00
|
|
|
popd &>/dev/null # wrapper dir
|
2013-11-12 00:04:21 +04:00
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
2019-11-02 15:26:21 +03:00
|
|
|
if [ $(osxcross-cmp $SDK_VERSION "<=" 10.13) -eq 1 ]; then
|
2019-06-01 20:57:44 +03:00
|
|
|
test_compiler o32-gcc $BASE_DIR/oclang/test.c
|
|
|
|
test_compiler o32-g++ $BASE_DIR/oclang/test.cpp
|
|
|
|
fi
|
2013-11-12 00:04:21 +04:00
|
|
|
|
2019-06-01 20:57:44 +03:00
|
|
|
test_compiler o64-gcc $BASE_DIR/oclang/test.c
|
2013-11-12 00:04:21 +04:00
|
|
|
test_compiler o64-g++ $BASE_DIR/oclang/test.cpp
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
echo "Done! Now you can use o32-gcc/o32-g++ and o64-gcc/o64-g++ as compiler"
|
|
|
|
echo ""
|
|
|
|
echo "Example usage:"
|
|
|
|
echo ""
|
2019-11-02 15:26:21 +03:00
|
|
|
echo "Example 1: CC=o32-gcc ./configure --host=i386-apple-$TARGET"
|
|
|
|
echo "Example 2: CC=i386-apple-$TARGET-gcc ./configure --host=i386-apple-$TARGET"
|
2013-11-12 00:04:21 +04:00
|
|
|
echo "Example 3: o64-gcc -Wall test.c -o test"
|
2019-11-02 15:26:21 +03:00
|
|
|
echo "Example 4: x86_64-apple-$TARGET-strip -x test"
|
2013-11-12 00:04:21 +04:00
|
|
|
echo ""
|