Skip to content

Commit 6fa8509

Browse files
Merge pull request #1076 from LedgerHQ/feat/regain_control_over_libs_2_master
Regain control over the SDK libraries [new variant using rebuilt newlib from Debian sources)
2 parents fddf1d7 + 3954b8f commit 6fa8509

File tree

10 files changed

+285
-3
lines changed

10 files changed

+285
-3
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Build SDK libraries and make PR to update them
2+
permissions:
3+
contents: read
4+
5+
on:
6+
workflow_dispatch:
7+
inputs:
8+
target_sdk_branch:
9+
type: string
10+
required: false
11+
default: 'master'
12+
create_pr:
13+
type: boolean
14+
required: false
15+
default: false
16+
17+
env:
18+
GIT_USER_EMAIL: '[email protected]'
19+
GIT_USER_NAME: 'SDKLibsUpdaterGithub'
20+
UPDATE_BRANCH: 'sdk_libs_update'
21+
22+
jobs:
23+
build:
24+
runs-on: ubuntu-latest
25+
container:
26+
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder-lite:latest
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
target:
31+
- core: cortex-m3
32+
se: st33
33+
- core: cortex-m35p+nodsp
34+
se: st33k1
35+
36+
steps:
37+
- uses: actions/checkout@v4
38+
with:
39+
sparse-checkout: |
40+
tools/build_clangrt_builtins.sh
41+
tools/build_newlib.sh
42+
sparse-checkout-cone-mode: false
43+
44+
- run: ./tools/build_clangrt_builtins.sh -t ${{ matrix.target.core }} -o artifact/arch/${{ matrix.target.se }}/lib
45+
46+
- run: ./tools/build_newlib.sh -t ${{ matrix.target.core }} -o artifact/arch/${{ matrix.target.se }}/lib
47+
48+
- uses: actions/upload-artifact@v4
49+
with:
50+
name: arch-${{ matrix.target.se }}
51+
path: artifact/
52+
53+
merge:
54+
needs: build
55+
runs-on: ubuntu-latest
56+
steps:
57+
- uses: actions/upload-artifact/merge@v4
58+
with:
59+
name: arch
60+
pattern: arch-*
61+
delete-merged: true
62+
63+
pr_create:
64+
needs: merge
65+
runs-on: ubuntu-latest
66+
if: ${{ success() && inputs.create_pr }}
67+
continue-on-error: true
68+
permissions:
69+
contents: write
70+
pull-requests: write
71+
steps:
72+
- name: Clone repository
73+
uses: actions/checkout@v4
74+
with:
75+
# by default the action uses fetch-depth = 1, which creates
76+
# shallow repositories from which we can't push
77+
fetch-depth: 0
78+
ref: ${{ inputs.target_sdk_branch }}
79+
80+
- name: Download Binaries artifact
81+
uses: actions/download-artifact@v4
82+
with:
83+
name: arch
84+
85+
- name: PR creation
86+
env:
87+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
88+
TARGET_BRANCH: ${{ inputs.target_sdk_branch }}
89+
run: |
90+
git config --global user.email "$GIT_USER_EMAIL"
91+
git config --global user.name "$GIT_USER_NAME"
92+
git switch --create "$UPDATE_BRANCH"
93+
git add -A .
94+
git commit -m 'Updating static SDK libraries'
95+
git push -u origin "$UPDATE_BRANCH"
96+
gh pr create -B "$TARGET_BRANCH" --title '[SDK_LIBS_UPDATE] Updating static SDK libraries' --body 'Created by Github workflow "${{ github.workflow }}", job "${{ github.job }}", run "${{ github.run_id }}".'

Makefile.standard_app

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,16 @@ APP_FLAGS_APP_LOAD_PARAMS = $(shell printf '0x%x' $$(( $(STANDARD_APP_FLAGS) + $
255255
CC = $(CLANGPATH)clang
256256
AS = $(CLANGPATH)clang
257257
ifeq ($(TARGET_NAME),TARGET_NANOS)
258-
LD = $(GCCPATH)arm-none-eabi-gcc
258+
LD = $(GCCPATH)arm-none-eabi-gcc
259+
LDLIBS += -lgcc
259260
else
260-
LD = $(CLANGPATH)clang
261+
LD = $(CLANGPATH)clang
262+
LDLIBS += -lclang_rt.builtins
261263
endif
262264

263265
AFLAGS += --target=arm-none-eabi
264266

265-
LDLIBS += -lm -lgcc -lc
267+
LDLIBS += -lm -lc
266268

267269
#####################################################################
268270
# MISC #

arch/st33/lib/libc.a

-311 KB
Binary file not shown.
143 KB
Binary file not shown.

arch/st33/lib/libm.a

-61.5 KB
Binary file not shown.

arch/st33k1/lib/libc.a

-160 KB
Binary file not shown.
143 KB
Binary file not shown.

arch/st33k1/lib/libm.a

-78.9 KB
Binary file not shown.

tools/build_clangrt_builtins.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
TARGET_CPU=""
6+
OUTPUT_DIR=""
7+
8+
while getopts "t:o:" opt
9+
do
10+
case "$opt" in
11+
t)
12+
TARGET_CPU="$OPTARG"
13+
;;
14+
o)
15+
OUTPUT_DIR="$OPTARG"
16+
;;
17+
?)
18+
exit 1
19+
;;
20+
esac
21+
done
22+
shift "$((OPTIND - 1))"
23+
24+
if [ -z "$TARGET_CPU" ] || [ -z "$OUTPUT_DIR" ]
25+
then
26+
echo "Usage: $0 -t TARGET_CPU -o OUTPUT_FILE" >&2
27+
exit 1
28+
fi
29+
30+
mkdir -p "$OUTPUT_DIR"
31+
OUTPUT_DIR=$(realpath "$OUTPUT_DIR")
32+
33+
# enable source repository
34+
sed -i 's/^\(Types: deb\)$/\1 deb-src/g' /etc/apt/sources.list.d/debian.sources
35+
36+
apt update
37+
38+
apt install -y --no-install-recommends \
39+
dpkg-dev \
40+
llvm-dev
41+
42+
LLVM_VERSION=$(clang --version | head -n1 | rev | cut -d" " -f1 | rev)
43+
LLVM_MAJOR_VERSION=$(echo "$LLVM_VERSION" | cut -d. -f1)
44+
45+
cd /tmp
46+
47+
LLVM_DIR="llvm-toolchain-$LLVM_MAJOR_VERSION-$LLVM_VERSION"
48+
if [ ! -d "$LLVM_DIR" ]
49+
then
50+
# install Debian source package
51+
apt source "llvm-toolchain-$LLVM_MAJOR_VERSION"
52+
fi
53+
54+
cd "$LLVM_DIR"
55+
rm -rf build
56+
mkdir build
57+
cd build
58+
59+
TARGET=arm-none-eabi
60+
SYSROOT=/usr/lib/arm-none-eabi
61+
62+
cmake ../compiler-rt \
63+
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
64+
-DCOMPILER_RT_OS_DIR="baremetal" \
65+
-DCOMPILER_RT_BUILD_BUILTINS=ON \
66+
-DCOMPILER_RT_BUILD_CRT=OFF \
67+
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
68+
-DCOMPILER_RT_BUILD_XRAY=OFF \
69+
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
70+
-DCOMPILER_RT_BUILD_PROFILE=OFF \
71+
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
72+
-DCOMPILER_RT_BUILD_ORC=OFF \
73+
-DCMAKE_C_COMPILER="$(which clang)" \
74+
-DCMAKE_C_COMPILER_TARGET="${TARGET}" \
75+
-DCMAKE_ASM_COMPILER_TARGET="${TARGET}" \
76+
-DCMAKE_AR="$(which llvm-ar)" \
77+
-DCMAKE_NM="$(which llvm-nm)" \
78+
-DCMAKE_RANLIB="$(which llvm-ranlib)" \
79+
-DCOMPILER_RT_BAREMETAL_BUILD=ON \
80+
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
81+
-DLLVM_CONFIG_PATH="$(which llvm-config)" \
82+
-DCOMPILER_RT_HAS_FPIC_FLAG=OFF \
83+
-DCMAKE_C_FLAGS="-mcpu=${TARGET_CPU} -mlittle-endian -mthumb -Oz -g0 -fropi -frwpi" \
84+
-DCMAKE_ASM_FLAGS="-mcpu=${TARGET_CPU} -mlittle-endian -mthumb" \
85+
-DCMAKE_SYSROOT="$SYSROOT"
86+
make -j
87+
mkdir -p "$OUTPUT_DIR"
88+
cp lib/baremetal/libclang_rt.builtins-arm.a "$OUTPUT_DIR/libclang_rt.builtins.a"

tools/build_newlib.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
3+
set -exu
4+
5+
TARGET_CPU=""
6+
OUTPUT_DIR=""
7+
8+
while getopts "t:o:" opt
9+
do
10+
case "$opt" in
11+
t)
12+
TARGET_CPU="$OPTARG"
13+
;;
14+
o)
15+
OUTPUT_DIR="$OPTARG"
16+
;;
17+
?)
18+
exit 1
19+
;;
20+
esac
21+
done
22+
shift "$((OPTIND - 1))"
23+
24+
if [ -z "${TARGET_CPU}" ] || [ -z "${OUTPUT_DIR}" ]
25+
then
26+
echo "Usage: $0 -t TARGET_CPU -o OUTPUT_FILE" >&2
27+
exit 1
28+
fi
29+
30+
mkdir -p "$OUTPUT_DIR"
31+
OUTPUT_DIR=$(realpath "$OUTPUT_DIR")
32+
33+
# Working in a temporary folder
34+
mkdir tmp && cd tmp
35+
36+
# Enabling source repository
37+
sed -i 's/^\(Types: deb\)$/\1 deb-src/g' /etc/apt/sources.list.d/debian.sources
38+
39+
# Installing newlib build dependencies
40+
apt update
41+
apt install -y --no-install-recommends texinfo dpkg-dev
42+
43+
# Get the latest fixed version for this OS version
44+
apt source newlib
45+
46+
# Entering the source directory
47+
cd */
48+
49+
# ENV SETUP
50+
WORKDIR="$(pwd)"
51+
#SRC="${WORKDIR}/newlib"
52+
BUILD="${WORKDIR}/arm_none_eabi_build"
53+
INSTALL="${WORKDIR}/arm_none_eabi_install"
54+
55+
# Build configuration
56+
mkdir -p "${BUILD}" "${INSTALL}"
57+
cd "${BUILD}"
58+
59+
CFLAGS_FOR_TARGET="-ffunction-sections -fdata-sections -fshort-wchar -DPREFER_SIZE_OVER_SPEED -mcpu=${TARGET_CPU} -mthumb -mlittle-endian" \
60+
../configure \
61+
`# Setup` \
62+
--host=x86_64-linux-gnu `# Host` \
63+
--target=arm-none-eabi ` # Target` \
64+
--disable-multilib `# Building only one library` \
65+
--prefix="${INSTALL}" `# Installation prefix` \
66+
`# Options` \
67+
--disable-silent-rules `# verbose build output (undo: "make V=0")` \
68+
--disable-dependency-tracking `# speeds up one-time build` \
69+
--enable-newlib-reent-small `# enable small reentrant struct support` \
70+
--disable-newlib-fvwrite-in-streamio `# disable iov in streamio ` \
71+
--disable-newlib-fseek-optimization `# disable fseek optimization` \
72+
--disable-newlib-wide-orient `# Turn off wide orientation in streamio`\
73+
--enable-newlib-nano-malloc `# use small-footprint nano-malloc implementation`\
74+
--disable-newlib-unbuf-stream-opt `# disable unbuffered stream optimization in streamio` \
75+
--enable-lite-exit `# enable light weight exit` \
76+
--enable-newlib-global-atexit `# enable atexit data structure as global` \
77+
--enable-newlib-nano-formatted-io `# Use small-footprint nano-formatted-IO implementation`\
78+
--disable-newlib-supplied-syscalls `# disable newlib from supplying syscalls`\
79+
--disable-nls `# do not use Native Language Support` \
80+
--enable-target-optspace `# optimize for space (emits -g -Os)`
81+
82+
83+
# Compilation
84+
make "-j$(nproc)"
85+
make install
86+
87+
# Removing the .ARM.exidx section
88+
arm-none-eabi-objcopy -R .ARM.exidx "${INSTALL}/arm-none-eabi/lib/libc.a"
89+
90+
# Stripping debug symbols
91+
arm-none-eabi-strip --strip-debug "${INSTALL}/arm-none-eabi/lib/libc.a"
92+
arm-none-eabi-strip --strip-debug "${INSTALL}/arm-none-eabi/lib/libm.a"
93+
94+
# Copy back
95+
cp "${INSTALL}/arm-none-eabi/lib/libc.a" "$OUTPUT_DIR"
96+
cp "${INSTALL}/arm-none-eabi/lib/libm.a" "$OUTPUT_DIR"

0 commit comments

Comments
 (0)