Skip to content

Commit 2bd8c2c

Browse files
pritamstyz4everppaul
andauthored
fix aarch64 build,verify and strip binaries (#15)
* fix aarch64 build,verify and strip binaries * fix strip cmd * fix verification * build zstd aarch64 with zlib support * support liblzma * fix path --------- Co-authored-by: ppaul <pritam.paul@yahooinc.com>
1 parent 307c6a9 commit 2bd8c2c

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

build_skopeo.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cd skopeo-${SKOPEO_VERSION}
2323
build_skopeo() {
2424
local arch=$1
2525
local output_name=$2
26+
local verify_arch=$3
2627
echo "Building skopeo for architecture: ${arch}"
2728

2829
# Build with the specified architecture
@@ -45,19 +46,19 @@ build_skopeo() {
4546
fi
4647

4748
# Check architecture
48-
if file ../${output_name} | grep -q "${arch}"; then
49-
echo "The binary ${output_name} is for the correct architecture (${arch})."
49+
if file ${CURR_DIR}/${output_name} | grep -q "${verify_arch}, version 1 (SYSV), statically linked"; then
50+
echo "The binary ${output_name} is for the correct architecture (${verify_arch})."
5051
else
51-
echo "Error: The binary ${output_name} is not for the correct architecture (expected ${arch})."
52+
echo "Error: The binary ${output_name} is not for the correct architecture (expected ${verify_arch})."
5253
exit 1
5354
fi
5455
}
5556

5657
# Build for amd64
57-
build_skopeo "amd64" "${SKOPEO_PACKAGE_AMD64}"
58+
build_skopeo "amd64" "${SKOPEO_PACKAGE_AMD64}" "x86-64"
5859

5960
# Build for arm64
60-
build_skopeo "arm64" "${SKOPEO_PACKAGE_ARM64}"
61+
build_skopeo "arm64" "${SKOPEO_PACKAGE_ARM64}" "ARM aarch64"
6162

6263
cd ..
6364
echo "Builds completed. Binaries located in ${CURR_DIR}."

build_zstd.sh

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ apt-get update && apt-get install -y build-essential wget cmake gcc-aarch64-linu
55

66
ARCHITECTURES=("x86_64" "aarch64")
77
COMPILERS=("gcc" "aarch64-linux-gnu-gcc")
8-
OUTPUT_FILES=("${BUILD_DIR}/zstd-linux-x86_64" "${BUILD_DIR}/zstd-linux-aarch64")
8+
OUTPUT_FILES=("zstd-linux-x86_64" "zstd-linux-aarch64")
9+
VERIFY=("x86-64" "ARM aarch64")
10+
STRIP=("strip" "aarch64-linux-gnu-strip")
11+
CURR_DIR=$(pwd)
912

1013
# Function to check if binary is static and has correct architecture
1114
check_binary() {
1215
local binary="$1"
1316
local arch="$2"
1417

18+
# Verify the binary
19+
echo "Verifying $binary..."
20+
ldd $binary
21+
file $binary
22+
1523
# Check if binary is statically linked
1624
if ldd "$binary" 2>&1 | grep -q "not a dynamic executable"; then
1725
echo "The binary $binary is statically linked."
@@ -21,42 +29,87 @@ check_binary() {
2129
fi
2230

2331
# Check architecture
24-
if file "$binary" | grep -q "$arch"; then
32+
if file "$binary" | grep -q "$arch, version 1 (GNU/Linux), statically linked"; then
2533
echo "The binary $binary is for the correct architecture $arch."
2634
else
2735
echo "Error: The binary $binary is not for the correct architecture (expected $arch)."
2836
exit 1
2937
fi
3038
}
3139

40+
# build zlib for aarch64
41+
build_zlib() {
42+
wget https://zlib.net/zlib-1.3.1.tar.gz
43+
tar -xzf zlib-1.3.1.tar.gz
44+
cd zlib-1.3.1
45+
CC=aarch64-linux-gnu-gcc ./configure --prefix=/tmp/zlib-aarch64 --static
46+
make
47+
make install
48+
file /tmp/zlib-aarch64/lib/libz.a
49+
aarch64-linux-gnu-objdump -f /tmp/zlib-aarch64/lib/libz.a | grep architecture
50+
cd ..
51+
}
52+
53+
build_liblzma() {
54+
wget https://tukaani.org/xz/xz-5.4.4.tar.gz
55+
tar -xzf xz-5.4.4.tar.gz
56+
cd xz-5.4.4
57+
CC=aarch64-linux-gnu-gcc ./configure --prefix=/tmp/liblzma-aarch64 --host=aarch64-linux-gnu --enable-static --disable-shared
58+
make
59+
make install
60+
file /tmp/liblzma-aarch64/lib/liblzma.a
61+
aarch64-linux-gnu-objdump -f /tmp/liblzma-aarch64/lib/liblzma.a | grep architecture
62+
cd ..
63+
}
64+
65+
3266
# Ensure BUILD_DIR and ZSTD_VERSION are set
3367
if [ -z "$BUILD_DIR" ] || [ -z "$ZSTD_VERSION" ]; then
3468
echo "BUILD_DIR and ZSTD_VERSION must be set."
3569
exit 1
3670
fi
3771

38-
mkdir -p "$BUILD_DIR"
39-
cd "$BUILD_DIR"
40-
4172
if [ ! -d "zstd-${ZSTD_VERSION}" ]; then
4273
echo "Downloading zstd version ${ZSTD_VERSION}..."
4374
wget https://github.com/facebook/zstd/releases/download/v${ZSTD_VERSION}/zstd-${ZSTD_VERSION}.tar.gz
4475
tar -xzf zstd-${ZSTD_VERSION}.tar.gz
4576
fi
46-
cd "zstd-${ZSTD_VERSION}"
77+
78+
mkdir -p "$BUILD_DIR"
4779

4880
# Build and verify binaries for each architecture
4981
for i in "${!ARCHITECTURES[@]}"; do
5082
arch="${ARCHITECTURES[$i]}"
83+
verify="${VERIFY[$i]}"
5184
compiler="${COMPILERS[$i]}"
5285
output_file="${OUTPUT_FILES[$i]}"
86+
strip_cmd="${STRIP[$i]}"
87+
88+
cf_flags="-static -O2 -pthread"
89+
ld_flags="-static"
90+
91+
if [ "$arch" == "aarch64" ]; then
92+
build_zlib
93+
build_liblzma
94+
cf_flags="-static -O2 -pthread -I/tmp/zlib-aarch64/include -I/tmp/liblzma-aarch64/include"
95+
ld_flags="-static -L/tmp/zlib-aarch64/lib -lz -L/tmp/liblzma-aarch64/lib -llzma"
96+
cd $CURR_DIR
97+
fi
98+
99+
cp -r "zstd-${ZSTD_VERSION}" "$BUILD_DIR"
100+
cd "$BUILD_DIR"
101+
ls -lrt
53102

54103
echo "Building zstd statically for $arch..."
55-
make clean
56-
CC="$compiler" CFLAGS="-static -O2 -pthread" LDFLAGS="-static" make -j4 zstd
57-
cp zstd "$output_file"
104+
make clean -C "zstd-${ZSTD_VERSION}"
105+
CC="$compiler" CFLAGS="${cf_flags}" LDFLAGS="${ld_flags}" make -j4 -C "zstd-${ZSTD_VERSION}" zstd
106+
"$strip_cmd" -s "zstd-${ZSTD_VERSION}/programs/zstd"
107+
mv "zstd-${ZSTD_VERSION}/programs/zstd" "$CURR_DIR/$output_file"
58108

109+
chmod +x $CURR_DIR/$output_file
110+
rm -rf *
111+
cd "$CURR_DIR"
59112
echo "Verifying $output_file..."
60-
check_binary "$output_file" "$arch"
113+
check_binary "$output_file" "$verify"
61114
done
62-
echo "Binaries are located at ${OUTPUT_FILES[*]}."
115+
echo "Binaries are located at ${OUTPUT_FILES[*]}."

screwdriver.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ jobs:
4646
image: buildpack-deps:oracular
4747
environment:
4848
ZSTD_VERSION: 1.5.6
49-
BUILD_DIR: "/tmp/ztsd_static_build"
49+
BUILD_DIR: "/tmp/zstd_static_build"
5050
USER_SHELL_BIN: bash
5151
steps:
5252
- make: ./build_zstd.sh
5353
- package: |
5454
a=($RELEASE_FILES)
55-
tar -czf ${a[1]} ${BUILD_DIR}/zstd-linux-x86_64 && store-cli set ${a[1]} --type=cache --scope=event
56-
tar -czf ${a[2]} ${BUILD_DIR}/zstd-linux-aarch64 && store-cli set ${a[2]} --type=cache --scope=event
55+
tar -czf ${a[1]} ./zstd-linux-x86_64 && store-cli set ${a[1]} --type=cache --scope=event
56+
tar -czf ${a[2]} ./zstd-linux-aarch64 && store-cli set ${a[2]} --type=cache --scope=event
5757
5858
zstd-mac:
5959
requires: [~pr, ~commit]

0 commit comments

Comments
 (0)