summaryrefslogtreecommitdiff
path: root/Toolchain/BuildIt.sh
blob: 5ac3b8274a37d1a3f3599f0648f70ab35520adc5 (plain)
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
#!/usr/bin/env bash
set -e
# This file will need to be run in bash, for now.


# === CONFIGURATION AND SETUP ===

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "$DIR"

ARCH=${ARCH:-"i686"}
TARGET="$ARCH-pc-serenity"
PREFIX="$DIR/Local"
SYSROOT="$DIR/../Root"

MAKE=make
MD5SUM=md5sum
NPROC=nproc

if [ `uname -s` = "OpenBSD" ]; then
    MAKE=gmake
    MD5SUM="md5 -q"
    NPROC="sysctl -n hw.ncpuonline"
    export CC=egcc
    export CXX=eg++
    export with_gmp=/usr/local
    export LDFLAGS=-Wl,-z,notext
fi

echo PREFIX is "$PREFIX"
echo SYSROOT is "$SYSROOT"

mkdir -p "$DIR/Tarballs"

BINUTILS_VERSION="2.33.1"
BINUTILS_MD5SUM="1a6b16bcc926e312633fcc3fae14ba0a"
BINUTILS_NAME="binutils-$BINUTILS_VERSION"
BINUTILS_PKG="${BINUTILS_NAME}.tar.gz"
BINUTILS_BASE_URL="http://ftp.gnu.org/gnu/binutils"

GCC_VERSION="9.2.0"
GCC_MD5SUM="e03739b042a14376d727ddcfd05a9bc3"
GCC_NAME="gcc-$GCC_VERSION"
GCC_PKG="${GCC_NAME}.tar.gz"
GCC_BASE_URL="http://ftp.gnu.org/gnu/gcc"


# === CHECK CACHE AND REUSE ===

pushd "$DIR"
    if [ "${TRY_USE_LOCAL_TOOLCHAIN}" = "y" ] ; then
        echo "Checking cached toolchain:"

        DEPS_CONFIG="
            uname=$(uname),TARGET=${TARGET},
            BuildItHash=$($MD5SUM $(basename $0)),
            MAKE=${MAKE},MD5SUM=${MD5SUM},NPROC=${NPROC},
            CC=${CC},CXX=${CXX},with_gmp=${with_gmp},LDFLAGS=${LDFLAGS},
            BINUTILS_VERSION=${BINUTILS_VERSION},BINUTILS_MD5SUM=${BINUTILS_MD5SUM},
            GCC_VERSION=${GCC_VERSION},GCC_MD5SUM=${GCC_MD5SUM}"
        echo "Config is:${DEPS_CONFIG}"
        if ! DEPS_HASH=$($DIR/ComputeDependenciesHash.sh $MD5SUM <<<"${DEPS_CONFIG}"); then
            echo "Dependency hashing failed"
            echo "Will rebuild toolchain from scratch, and NOT SAVE THE RESULT."
            echo "Someone should look into this, but for now it'll work, albeit inefficient."
            # Should be empty anyway, but just to make sure:
            DEPS_HASH=""
        elif [ -r "Cache/ToolchainLocal_${DEPS_HASH}.tar.gz" ] ; then
            echo "Cache at Cache/ToolchainLocal_${DEPS_HASH}.tar.gz exists!"
            echo "Extracting toolchain from cache:"
            tar xzf "Cache/ToolchainLocal_${DEPS_HASH}.tar.gz"
            echo "Done 'building' the toolchain."
            exit 0
        else
            echo "Cache at Cache/ToolchainLocal_${DEPS_HASH}.tar.gz does not exist."
            echo "Will rebuild toolchain from scratch, and save the result."
        fi

    fi
popd


# === DOWNLOAD AND PATCH ===

pushd "$DIR/Tarballs"
    md5="$($MD5SUM $BINUTILS_PKG | cut -f1 -d' ')"
    echo "bu md5='$md5'"
    if [ ! -e $BINUTILS_PKG ] || [ "$md5" != ${BINUTILS_MD5SUM} ] ; then
        rm -f $BINUTILS_PKG
        wget "$BINUTILS_BASE_URL/$BINUTILS_PKG"
    else
        echo "Skipped downloading binutils"
    fi

    md5="$($MD5SUM ${GCC_PKG} | cut -f1 -d' ')"
    echo "gc md5='$md5'"
    if [ ! -e $GCC_PKG ] || [ "$md5" != ${GCC_MD5SUM} ] ; then
        rm -f $GCC_PKG
        wget "$GCC_BASE_URL/$GCC_NAME/$GCC_PKG"
    else
        echo "Skipped downloading gcc"
    fi

    if [ ! -d ${BINUTILS_NAME} ]; then
        echo "Extracting binutils..."
        tar -xzf ${BINUTILS_PKG}

        pushd ${BINUTILS_NAME}
            git init >/dev/null
            git add . >/dev/null
            git commit -am "BASE" >/dev/null
            git apply "$DIR"/Patches/binutils.patch >/dev/null
        popd
    else
        echo "Skipped extracting binutils"
    fi

    if [ ! -d $GCC_NAME ]; then
        echo "Extracting gcc..."
        tar -xzf $GCC_PKG

        pushd $GCC_NAME
            git init >/dev/null
            git add . >/dev/null
            git commit -am "BASE" >/dev/null
            git apply "$DIR"/Patches/gcc.patch >/dev/null
        popd
    else
        echo "Skipped extracting gcc"
    fi

    if [ "$(uname)" = "Darwin" ]; then
        pushd "gcc-${GCC_VERSION}"
        ./contrib/download_prerequisites
        popd
    fi

popd


# === COMPILE AND INSTALL ===

mkdir -p "$PREFIX"
mkdir -p "$DIR/Build/binutils"
mkdir -p "$DIR/Build/gcc"

if [ -z "$MAKEJOBS" ]; then
    MAKEJOBS=$($NPROC)
fi

pushd "$DIR/Build/"
    unset PKG_CONFIG_LIBDIR # Just in case

    pushd binutils
        "$DIR"/Tarballs/binutils-2.33.1/configure --prefix="$PREFIX" \
                                                --target="$TARGET" \
                                                --with-sysroot="$SYSROOT" \
                                                --enable-shared \
                                                --disable-nls || exit 1
        if [ "$(uname)" = "Darwin" ]; then
            # under macOS generated makefiles are not resolving the "intl"
            # dependency properly to allow linking its own copy of
            # libintl when building with --enable-shared.
            "$MAKE" -j "$MAKEJOBS" || true
            pushd intl
            "$MAKE" all-yes
            popd
        fi
        "$MAKE" -j "$MAKEJOBS" || exit 1
        "$MAKE" install || exit 1
    popd

    pushd gcc
        if [ `uname -s` = "OpenBSD" ]; then
            perl -pi -e 's/-no-pie/-nopie/g' "$DIR"/Tarballs/gcc-9.2.0/gcc/configure
        fi

        "$DIR"/Tarballs/gcc-9.2.0/configure --prefix="$PREFIX" \
                                            --target="$TARGET" \
                                            --with-sysroot="$SYSROOT" \
                                            --disable-nls \
                                            --with-newlib \
                                            --enable-shared \
                                            --enable-languages=c,c++ || exit 1

        echo "XXX build gcc and libgcc"
        "$MAKE" -j "$MAKEJOBS" all-gcc all-target-libgcc || exit 1
        echo "XXX install gcc and libgcc"
        "$MAKE" install-gcc install-target-libgcc || exit 1

        echo "XXX serenity libc and libm"
        ( cd "$DIR/../Libraries/LibC/" && "$MAKE" clean && "$MAKE" EXTRA_LIBC_DEFINES="-DBUILDING_SERENITY_TOOLCHAIN" && "$MAKE" install )
        ( cd "$DIR/../Libraries/LibM/" && "$MAKE" clean && "$MAKE" && "$MAKE" install )

        echo "XXX build libstdc++"
        "$MAKE" all-target-libstdc++-v3 || exit 1
        echo "XXX install libstdc++"
        "$MAKE" install-target-libstdc++-v3 || exit 1

        if [ `uname -s` = "OpenBSD" ]; then
            cd "$DIR"/Local/libexec/gcc/i686-pc-serenity/9.2.0 && ln -sf liblto_plugin.so.0.0 liblto_plugin.so
        fi
    popd
popd


# == SAVE TO CACHE ==

pushd "$DIR"
    if [ "${TRY_USE_LOCAL_TOOLCHAIN}" = "y" ] ; then
        # TODO: Compress with -z.  It's factor 3, and costs no time.
        echo "Caching toolchain:"

        if [ -z "${DEPS_HASH}" ] ; then
            echo "NOT SAVED, because hashing failed."
            echo "It's computed in the beginning; see there for the error message."
        elif [ -e "Cache/ToolchainLocal_${DEPS_HASH}.tar.gz" ] ; then
            # Note: This checks for *existence*.  Initially we checked for
            # *readability*. If Travis borks permissions, there's not much we can do.
            echo "Cache exists but was not used?!"
            echo "Not touching cache then."
        else
            mkdir -p Cache/
            tar czf "Cache/ToolchainLocal_${DEPS_HASH}.tar.gz" Local/
        fi
    fi
popd