Skip to content

Commit 595a252

Browse files
New script to build libc and libm
(cherry picked from commit 5014f15)
1 parent 35fb038 commit 595a252

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tools/build_newlib.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
31+
mkdir -p "$OUTPUT_DIR"
32+
OUTPUT_DIR=$(realpath "$OUTPUT_DIR")
33+
34+
35+
# ENV SETUP
36+
WORKDIR="$(pwd)"
37+
SRC="${WORKDIR}/newlib"
38+
BUILD="${SRC}/arm_none_eabi_build"
39+
INSTALL="${SRC}/arm_none_eabi_install"
40+
cd "${WORKDIR}"
41+
42+
# Installing newlib build dependencies
43+
apt update
44+
apt install -y --no-install-recommends texinfo
45+
46+
# Get the latest fixed version
47+
git clone --depth=1 --single-branch --branch newlib-4.5.0 https://sourceware.org/git/newlib-cygwin.git "${SRC}"
48+
49+
# Build configuration
50+
mkdir -p "${BUILD}" "${INSTALL}"
51+
cd "${BUILD}"
52+
53+
CFLAGS_FOR_TARGET="-ffunction-sections -fdata-sections -fshort-wchar -DPREFER_SIZE_OVER_SPEED -mcpu=${TARGET_CPU} -mthumb -mlittle-endian" \
54+
../configure \
55+
`# Setup` \
56+
--host=x86_64-linux-gnu `# Host` \
57+
--target=arm-none-eabi ` # Target` \
58+
--disable-multilib `# Building only one library` \
59+
--prefix="${INSTALL}" `# Installation prefix` \
60+
`# Options` \
61+
--disable-silent-rules `# verbose build output (undo: "make V=0")` \
62+
--disable-dependency-tracking `# speeds up one-time build` \
63+
--enable-newlib-reent-small `# enable small reentrant struct support` \
64+
--disable-newlib-fvwrite-in-streamio `# disable iov in streamio ` \
65+
--disable-newlib-fseek-optimization `# disable fseek optimization` \
66+
--disable-newlib-wide-orient `# Turn off wide orientation in streamio`\
67+
--enable-newlib-nano-malloc `# use small-footprint nano-malloc implementation`\
68+
--disable-newlib-unbuf-stream-opt `# disable unbuffered stream optimization in streamio` \
69+
--enable-lite-exit `# enable light weight exit` \
70+
--enable-newlib-global-atexit `# enable atexit data structure as global` \
71+
--enable-newlib-nano-formatted-io `# Use small-footprint nano-formatted-IO implementation`\
72+
--disable-newlib-supplied-syscalls `# disable newlib from supplying syscalls`\
73+
--disable-nls `# do not use Native Language Support` \
74+
--enable-target-optspace `# optimize for space (emits -g -Os)`
75+
76+
77+
# Compilation
78+
make "-j$(nproc)"
79+
make install
80+
81+
# Removing the .ARM.exidx section
82+
arm-none-eabi-objcopy -R .ARM.exidx "${INSTALL}/arm-none-eabi/lib/libc.a"
83+
84+
# Stripping debug symbols
85+
arm-none-eabi-strip --strip-debug "${INSTALL}/arm-none-eabi/lib/libc.a"
86+
arm-none-eabi-strip --strip-debug "${INSTALL}/arm-none-eabi/lib/libm.a"
87+
88+
# Copy back
89+
mkdir -p "$OUTPUT_DIR"
90+
cp "${INSTALL}/arm-none-eabi/lib/libc.a" "$OUTPUT_DIR"
91+
cp "${INSTALL}/arm-none-eabi/lib/libm.a" "$OUTPUT_DIR"
92+

0 commit comments

Comments
 (0)