Skip to content

Commit 2bba334

Browse files
authored
fix: build static library as position-independent code (#224)
1 parent 8ee0d4d commit 2bba334

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

.github/workflows/tests.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,44 @@ jobs:
5959
shell: bash
6060
run: ./ci/integ/unit-test.sh ${{ matrix.os }}
6161

62+
pic-check:
63+
runs-on: ${{ matrix.runner }}
64+
timeout-minutes: 10
65+
container: ${{ matrix.container }}
66+
strategy:
67+
fail-fast: false
68+
matrix:
69+
include:
70+
- os: al2023
71+
container: public.ecr.aws/amazonlinux/amazonlinux:2023
72+
runner: ubuntu-latest
73+
- os: al2023-arm
74+
container: public.ecr.aws/amazonlinux/amazonlinux:2023
75+
runner: ubuntu-24.04-arm
76+
- os: alpine
77+
container: public.ecr.aws/docker/library/alpine:3.23
78+
runner: ubuntu-latest
79+
80+
steps:
81+
- name: Install checkout prerequisites
82+
shell: sh
83+
run: |
84+
if command -v dnf > /dev/null 2>&1; then
85+
dnf install -y tar gzip git
86+
elif command -v apk > /dev/null 2>&1; then
87+
apk add --no-cache bash tar git
88+
fi
89+
90+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
91+
92+
- name: Install dependencies
93+
shell: bash
94+
run: ./ci/integ/install-deps.sh ${{ matrix.os }}
95+
96+
- name: Position-independence check
97+
shell: bash
98+
run: ./ci/integ/pic-check.sh ${{ matrix.os }}
99+
62100
integration-test-oci:
63101
runs-on: ${{ matrix.build.runner }}
64102
timeout-minutes: 10

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ add_library(${PROJECT_NAME}
1919

2020
set_target_properties(${PROJECT_NAME} PROPERTIES
2121
SOVERSION 0
22-
VERSION ${PROJECT_VERSION}-dev)
22+
VERSION ${PROJECT_VERSION}-dev
23+
# Emit position-independent code so the static archive can be linked into a
24+
# shared object (e.g. the Java runtime interface client's JNI .so).
25+
# PIC links fine into executables too, so this is safe for all consumers.
26+
POSITION_INDEPENDENT_CODE ON)
2327

2428
target_include_directories(${PROJECT_NAME} PUBLIC
2529
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>

ci/integ/docker/Dockerfile.oci-smoke

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ COPY main.cpp main.cpp
1313

1414
RUN g++ -std=c++11 -O2 -Iinclude main.cpp runtime.a -lcurl -pthread -o bootstrap
1515

16+
# Position-independence guard. The link above only proves the archive works in a
17+
# position-dependent executable; it stays green even for a non-PIC build. But
18+
# downstream consumers embed this .a in a shared object -- e.g. the Java runtime
19+
# interface client's JNI .so
20+
RUN g++ -shared -Wl,--whole-archive runtime.a -Wl,--no-whole-archive \
21+
-lcurl -pthread -o /build/libaws-lambda-runtime-pic-check.so
22+
1623
FROM public.ecr.aws/lambda/provided:al2023
1724

1825
COPY --from=builder /build/bootstrap ${LAMBDA_RUNTIME_DIR}/bootstrap

ci/integ/pic-check.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
OS=${1:-}
5+
6+
case "$OS" in
7+
ubuntu|arch)
8+
export CC=/usr/bin/clang CXX=/usr/bin/clang++
9+
;;
10+
esac
11+
12+
BUILD_DIR=build-pic-check
13+
cmake -B "$BUILD_DIR" -GNinja -DCMAKE_BUILD_TYPE=Release
14+
cmake --build "$BUILD_DIR"
15+
16+
"${CXX:-c++}" -shared \
17+
-Wl,--whole-archive "$BUILD_DIR/libaws-lambda-runtime.a" -Wl,--no-whole-archive \
18+
-lcurl -pthread \
19+
-o "$BUILD_DIR/libaws-lambda-runtime-pic-check.so"
20+
21+
echo "PIC check passed: static archive links into a shared object"

0 commit comments

Comments
 (0)