Skip to content

Commit fac1ff0

Browse files
meeqrasky
authored andcommitted
Add script to build + install GDB in the toolchain (#679)
1 parent 5024b45 commit fac1ff0

File tree

4 files changed

+209
-68
lines changed

4 files changed

+209
-68
lines changed

.github/workflows/build-toolchain-matrix.yml

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ on:
77
push:
88
paths:
99
- 'tools/build-toolchain.sh'
10+
- 'tools/build-gdb.sh'
1011
pull_request:
1112
paths:
1213
- 'tools/build-toolchain.sh'
13-
14+
- 'tools/build-gdb.sh'
15+
1416
jobs:
1517
Build-Toolchain:
1618
# targets the oldest ubuntu image available to create valid packages for as many versions possible.
@@ -22,6 +24,7 @@ jobs:
2224
gmp-version: ${{ steps.gcc-version-generator.outputs.GMP_VERSION }}
2325
mpc-version: ${{ steps.gcc-version-generator.outputs.MPC_VERSION }}
2426
mpfr-version: ${{ steps.gcc-version-generator.outputs.MPFR_VERSION }}
27+
gdb-version: ${{ steps.gcc-version-generator.outputs.GDB_VERSION }}
2528
make-version: ${{ steps.gcc-version-generator.outputs.MAKE_VERSION }}
2629
strategy:
2730
fail-fast: false
@@ -33,7 +36,8 @@ jobs:
3336
runs-on: ${{ matrix.host-os }}
3437

3538
env:
36-
Build_Directory: libdragon
39+
Install_Directory: libdragon
40+
Build_Directory: scratch
3741

3842
steps:
3943
- name: Install native system build dependencies
@@ -89,6 +93,7 @@ jobs:
8993
echo "BINUTILS_VERSION=$(grep -Po 'BINUTILS_V=\K[^"]*' ./tools/build-toolchain.sh)" >> $GITHUB_OUTPUT
9094
echo "GCC_VERSION=$(grep -Po 'GCC_V=\K[^"]*' ./tools/build-toolchain.sh)" >> $GITHUB_OUTPUT
9195
echo "NEWLIB_VERSION=$(grep -Po 'NEWLIB_V=\K[^"]*' ./tools/build-toolchain.sh)" >> $GITHUB_OUTPUT
96+
echo "GDB_VERSION=$(grep -Po 'GDB_V=\K[^"]*' ./tools/build-gdb.sh)" >> $GITHUB_OUTPUT
9297
9398
echo "GMP_VERSION=$(grep -Po 'GMP_V=\K[^"]*' ./tools/build-toolchain.sh)" >> $GITHUB_OUTPUT
9499
echo "MPC_VERSION=$(grep -Po 'MPC_V=\K[^"]*' ./tools/build-toolchain.sh)" >> $GITHUB_OUTPUT
@@ -100,11 +105,14 @@ jobs:
100105
- name: Build N64 MIPS GCC toolchain for ${{ matrix.target-platform }}
101106
run: |
102107
# required for newlib (as not the default?!)
103-
export PATH="$PATH:${{ runner.temp }}/${{ env.Build_Directory }}"
108+
export PATH="$PATH:${{ runner.temp }}/${{ env.Install_Directory }}"
109+
export BUILD_PATH=${{ runner.temp }}/${{ env.Build_Directory }}
110+
export N64_INST=${{ runner.temp }}/${{ env.Install_Directory }}
111+
export N64_HOST=${{ matrix.host }}
104112
cd ./tools/
105-
N64_INST=${{ runner.temp }}/${{ env.Build_Directory }} N64_HOST=${{ matrix.host }} MAKE_V=${{ matrix.makefile-version }} ./build-toolchain.sh
106-
107-
echo Remove un-necessary content
113+
MAKE_V=${{ matrix.makefile-version }} ./build-toolchain.sh
114+
./build-gdb.sh
115+
echo "Removing un-necessary content"
108116
rm -rf ${N64_INST}/share/locale/*
109117
continue-on-error: true
110118

@@ -152,8 +160,8 @@ jobs:
152160
$Package_Name-env.sh=/etc/profile.d/$Package_Name-env.sh
153161
continue-on-error: true
154162
env:
155-
Package_Source_Directory: ${{ runner.temp }}/${{ env.Build_Directory }}/
156-
Package_Installation_Directory: /opt/${{ env.Build_Directory }}
163+
Package_Source_Directory: ${{ runner.temp }}/${{ env.Install_Directory }}/
164+
Package_Installation_Directory: /opt/${{ env.Install_Directory }}
157165
Package_Name: gcc-toolchain-mips64
158166
Package_Version: ${{ steps.gcc-version-generator.outputs.GCC_VERSION }}
159167
Package_Revision: ${{ github.run_id }}
@@ -169,7 +177,7 @@ jobs:
169177
with:
170178
name: gcc-toolchain-mips64-${{ matrix.target-platform }}
171179
path: |
172-
${{ runner.temp }}/${{ env.Build_Directory }}
180+
${{ runner.temp }}/${{ env.Install_Directory }}
173181
continue-on-error: true
174182

175183
- name: Publish Linux-x86_64 Build Artifacts
@@ -219,6 +227,7 @@ jobs:
219227
CHANGELOG_TEXT+=" * GCC: V${{ needs.build-toolchain.outputs.gcc-version }}</br>"
220228
CHANGELOG_TEXT+=" * Newlib: V${{ needs.build-toolchain.outputs.newlib-version }}</br>"
221229
CHANGELOG_TEXT+=" * BinUtils: V${{ needs.build-toolchain.outputs.binutils-version }}</br>"
230+
CHANGELOG_TEXT+=" * GDB: V${{ needs.build-toolchain.outputs.gdb-version }}</br>"
222231
CHANGELOG_TEXT+='</br>'
223232
CHANGELOG_TEXT+="With dependencies:</br>"
224233
CHANGELOG_TEXT+=" * GMP: V${{ needs.build-toolchain.outputs.gmp-version }}</br>"

Dockerfile

+53-35
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,72 @@
11
# syntax=docker/dockerfile:1
22
# V0 - Use this comment to force a re-build without changing the contents
33

4+
ARG BASE_IMAGE=ubuntu:22.04
5+
46
# Stage 1 - Build the toolchain
5-
FROM ubuntu:22.04
7+
FROM ${BASE_IMAGE} AS builder
68

79
# Setup paths for the libdragon toolchain
810
ARG N64_INST=/n64_toolchain
11+
ARG BUILD_PATH=/tmp/build
12+
ARG DOWNLOAD_PATH=/tmp/download
913
ENV N64_INST=${N64_INST}
14+
ENV BUILD_PATH=${BUILD_PATH}
15+
ENV DOWNLOAD_PATH=${DOWNLOAD_PATH}
16+
17+
# Install system dependencies
18+
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
19+
--mount=target=/var/cache/apt,type=cache,sharing=locked \
20+
rm -f /etc/apt/apt.conf.d/docker-clean \
21+
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache \
22+
&& apt update \
23+
&& apt upgrade -y \
24+
&& apt install -y --no-install-recommends \
25+
bzip2 \
26+
ca-certificates \
27+
file \
28+
g++ \
29+
gcc \
30+
git \
31+
libmpc-dev \
32+
libmpfr-dev \
33+
make \
34+
texinfo \
35+
wget \
36+
zlib1g-dev \
37+
&& apt autoremove -yq
1038

11-
# install dependencies
12-
RUN apt-get update
13-
RUN apt-get install -yq \
14-
wget \
15-
bzip2 \
16-
gcc \
17-
g++ \
18-
make \
19-
file \
20-
libmpfr-dev \
21-
libmpc-dev \
22-
zlib1g-dev \
23-
texinfo \
24-
git
25-
26-
# Build toolchain
27-
COPY ./tools/build-toolchain.sh /tmp/tools/build-toolchain.sh
28-
WORKDIR /tmp/tools
29-
RUN ./build-toolchain.sh
30-
31-
# Remove locale strings which are not so important in our use case
32-
RUN rm -rf ${N64_INST}/share/locale/*
39+
# Copy the build scripts into the container
40+
COPY ./tools/build-toolchain.sh ./tools/build-gdb.sh /tools/
3341

42+
# Run the build scripts and cleanup unnecessary files
43+
RUN --mount=target=${DOWNLOAD_PATH},type=cache,sharing=locked \
44+
/tools/build-toolchain.sh && \
45+
/tools/build-gdb.sh && \
46+
rm -rf ${N64_INST}/share/locale/*
3447

3548
# Stage 2 - Prepare minimal image
36-
FROM ubuntu:22.04
49+
FROM ${BASE_IMAGE} AS runner
3750

3851
# Setup paths for the libdragon toolchain
3952
ARG N64_INST=/n64_toolchain
4053
ENV N64_INST=${N64_INST}
4154
ENV PATH="${N64_INST}/bin:$PATH"
4255

43-
# Install toolchain (built in Stage 1)
44-
COPY --from=0 ${N64_INST} ${N64_INST}
45-
46-
# install dependencies & perform cleanup
47-
RUN apt-get update \
48-
&& apt-get install -yq \
49-
gcc \
50-
g++ \
51-
make \
52-
git \
53-
&& apt-get clean \
56+
# Install system dependencies
57+
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
58+
--mount=target=/var/cache/apt,type=cache,sharing=locked \
59+
rm -f /etc/apt/apt.conf.d/docker-clean \
60+
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache \
61+
&& apt update \
62+
&& apt upgrade -y \
63+
&& apt install -y --no-install-recommends \
64+
g++ \
65+
gcc \
66+
git \
67+
make \
68+
xxd \
5469
&& apt autoremove -yq
70+
71+
# Copy over built toolchain from previous stage
72+
COPY --from=builder ${N64_INST} ${N64_INST}

tools/build-gdb.sh

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#! /bin/bash
2+
# N64 MIPS GDB toolchain build/install script for Unix distributions
3+
# (c) DragonMinded and libDragon Contributors.
4+
# See the root folder for license information.
5+
6+
# Bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
7+
set -euo pipefail
8+
IFS=$'\n\t'
9+
10+
# Check that N64_INST is defined
11+
if [ -z "${N64_INST-}" ]; then
12+
echo "N64_INST environment variable is not defined."
13+
echo "Please define N64_INST and point it to the requested installation directory"
14+
exit 1
15+
fi
16+
17+
# Dependency source libs (Versions)
18+
GDB_V=16.2
19+
20+
# Defines the build system variables to allow cross compilation.
21+
N64_HOST=${N64_HOST:-""}
22+
N64_TARGET=${N64_TARGET:-mips64-elf}
23+
24+
# Set N64_INST before calling the script to change the default installation directory path
25+
INSTALL_PATH="${N64_INST}"
26+
# Path where the toolchain will be built.
27+
BUILD_PATH="${BUILD_PATH:-toolchain}"
28+
DOWNLOAD_PATH="${DOWNLOAD_PATH:-$BUILD_PATH}"
29+
30+
# Determine how many parallel Make jobs to run based on CPU count
31+
JOBS="${JOBS:-$(getconf _NPROCESSORS_ONLN)}"
32+
JOBS="${JOBS:-1}" # If getconf returned nothing, default to 1
33+
34+
# GDB configure arguments to use system GMP/MPC/MFPF
35+
GDB_CONFIGURE_ARGS=()
36+
37+
# Resolve absolute paths for build and download directories
38+
BUILD_PATH=$(cd "$BUILD_PATH" && pwd)
39+
DOWNLOAD_PATH=$(cd "$DOWNLOAD_PATH" && pwd)
40+
41+
# Check if a command-line tool is available: status 0 means "yes"; status 1 means "no"
42+
command_exists () {
43+
(command -v "$1" >/dev/null 2>&1)
44+
return $?
45+
}
46+
47+
# Download the file URL using wget or curl (depending on which is installed)
48+
download () {
49+
if command_exists wget ; then (cd "$DOWNLOAD_PATH" && wget -c "$1")
50+
elif command_exists curl ; then (cd "$DOWNLOAD_PATH" && curl -LO "$1")
51+
else
52+
echo "Install wget or curl to download toolchain sources" 1>&2
53+
return 1
54+
fi
55+
}
56+
57+
# Dependency downloads and unpack
58+
test -f "$DOWNLOAD_PATH/gdb-$GDB_V.tar.gz" || download "https://ftp.gnu.org/gnu/gdb/gdb-$GDB_V.tar.gz"
59+
test -d "$BUILD_PATH/gdb-$GDB_V" || tar -xzf "$DOWNLOAD_PATH/gdb-$GDB_V.tar.gz" -C "$BUILD_PATH"
60+
61+
# Resolve dependencies on macOS via homebrew
62+
if [[ $OSTYPE == 'darwin'* ]]; then
63+
# Tell GDB configure to use Homebrew's GMP, MPFR, MPC, and Zlib.
64+
# These should have already been installed by build-toolchain.sh
65+
GDB_CONFIGURE_ARGS=(
66+
"--with-gmp=$(brew --prefix)"
67+
"--with-mpfr=$(brew --prefix)"
68+
"--with-mpc=$(brew --prefix)"
69+
"--with-zlib=$(brew --prefix)"
70+
)
71+
else
72+
# Configure GDB arguments for non-macOS platforms
73+
GDB_CONFIGURE_ARGS+=("--with-system-zlib")
74+
fi
75+
76+
# Add host to GDG configure arguments if defined
77+
if [[ -n "${N64_HOST}" ]]; then
78+
GDB_CONFIGURE_ARGS+=("--host=${N64_HOST}")
79+
fi
80+
81+
# Add target to GDB configure arguments if defined
82+
if [[ -n "${N64_TARGET}" ]]; then
83+
GDB_CONFIGURE_ARGS+=("--target=${N64_TARGET}")
84+
fi
85+
86+
# Compile GDB
87+
pushd "$BUILD_PATH/gdb-$GDB_V"
88+
./configure "${GDB_CONFIGURE_ARGS[@]}" \
89+
--prefix="$INSTALL_PATH" \
90+
--disable-docs \
91+
--disable-gdbserver \
92+
--disable-binutils \
93+
--disable-gas \
94+
--disable-sim \
95+
--disable-gprof \
96+
--disable-inprocess-agent
97+
make all -j "$JOBS"
98+
make install-strip || sudo make install-strip || su -c "make install-strip"
99+
popd
100+
101+
# Final message
102+
echo
103+
echo "***********************************************"
104+
echo "GDB correctly built and installed to LibDragon toolchain"
105+
echo "Installation directory: \"${N64_INST}\""
106+
echo "Build directory: \"${BUILD_PATH}\" (can be removed now)"

0 commit comments

Comments
 (0)