Skip to content

Commit 0b16ecd

Browse files
committed
Restrict platforms to x86_64 and arm64, simplify build-jni-lib.sh, throw UnknownPlatformException if arch is not supported
1 parent 5def05c commit 0b16ecd

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

aws-lambda-java-runtime-interface-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You can include this package in your preferred base image to make that base imag
1313

1414
Choose a preferred base image. The Runtime Interface Client is tested on Amazon Linux, Alpine, Ubuntu, Debian, and CentOS. The requirements are that the image is:
1515

16-
* built for x86_64 and ARM
16+
* built for x86_64 and ARM64
1717
* contains Java >= 8
1818
* contains glibc >= 2.17 or musl
1919

aws-lambda-java-runtime-interface-client/RELEASE.CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
### Dec 30, 2021
1+
### January 07, 2022
22
`2.1.0`
3-
- Added support for ARM64 architecture
3+
- fix: Added support for ARM64 architecture
44

55
### Sept 29, 2021
66
`2.0.0`

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/NativeClient.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class NativeClient {
2222
static {
2323
boolean loaded = false;
2424
for (int i = 0; !loaded && i < libsToTry.length; ++i) {
25-
try (InputStream lib = NativeClient.class.getResourceAsStream(
26-
Paths.get(architecturePathSuffix, libsToTry[i]).toString())) {
25+
try (InputStream lib = NativeClient.class.getResourceAsStream(Paths.get(architecturePathSuffix, libsToTry[i]).toString())) {
2726
Files.copy(lib, Paths.get(nativeLibPath), StandardCopyOption.REPLACE_EXISTING);
2827
System.load(nativeLibPath);
2928
loaded = true;
@@ -47,19 +46,20 @@ class NativeClient {
4746
}
4847

4948
/**
49+
* Implementation based on AWS CRT (ref. https://github.com/awslabs/aws-crt-java/blob/0e9c3db8b07258b57c2503cfc47c787ccef10670/src/main/java/software/amazon/awssdk/crt/CRT.java#L106-L134)
5050
* @return a string describing the detected architecture the RIC is executing on
51-
* @throws RuntimeException
51+
* @throws UnknownPlatformException if the architecture is not known to the RIC
5252
*/
5353
static String getArchIdentifier() {
5454
String arch = System.getProperty("os.arch");
5555

56-
if (arch.matches("^(x8664|amd64|ia32e|em64t|x64|x86_64|x8632|x86|i[3-6]86|ia32|x32)$")) {
57-
return "x86";
58-
} else if (arch.matches("^(armeabi.*|arm64.*|aarch64.*|arm)$")) {
59-
return "arm";
56+
if (arch.matches("^(x8664|amd64|ia32e|em64t|x64|x86_64)$")) {
57+
return "x86_64";
58+
} else if (arch.startsWith("arm64") || arch.startsWith("aarch64")) {
59+
return "arm64";
6060
}
6161

62-
throw new RuntimeException("architecture not supported: " + arch);
62+
throw new UnknownPlatformException("Lambda Java RIC: architecture not supported: " + arch);
6363
}
6464

6565
static native void initializeClient(byte[] userAgent);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.amazonaws.services.lambda.runtime.api.client.runtimeapi;
2+
3+
/**
4+
* Copyright (c) 2019 Amazon. All rights reserved.
5+
*/
6+
public class UnknownPlatformException extends RuntimeException {
7+
public UnknownPlatformException(String message) {
8+
super(message);
9+
}
10+
}

aws-lambda-java-runtime-interface-client/src/main/jni/build-jni-lib.sh

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ SRC_DIR=$(dirname "$0")
77
DST_DIR=${1}
88
CURL_VERSION=7.77.0
99

10-
# compile the native library for x86
11-
mkdir "${DST_DIR}"/classes/x86
12-
docker build --platform=linux/amd64 -f "${SRC_DIR}/Dockerfile.glibc" --build-arg CURL_VERSION=${CURL_VERSION} -t lambda-java-jni-lib-glibc-x86 "${SRC_DIR}"
13-
docker run --rm --entrypoint /bin/cat lambda-java-jni-lib-glibc-x86 /src/aws-lambda-runtime-interface-client.so > "${DST_DIR}"/classes/x86/aws-lambda-runtime-interface-client.glibc.so
10+
# Not using associative arrays to maintain bash 3 compatibility with building on MacOS
11+
# MacOS ships with bash 3 and associative arrays require bash 4+
12+
# Declaring a map as an array with the column character as a separator :
13+
declare -a ARCHITECTURES_TO_PLATFORM=(
14+
"x86_64:linux/amd64"
15+
"arm64:linux/arm64/v8"
16+
)
1417

15-
docker build --platform=linux/amd64 -f "${SRC_DIR}/Dockerfile.musl" --build-arg CURL_VERSION=${CURL_VERSION} -t lambda-java-jni-lib-musl-x86 "${SRC_DIR}"
16-
docker run --rm --entrypoint /bin/cat lambda-java-jni-lib-musl-x86 /src/aws-lambda-runtime-interface-client.so > "${DST_DIR}"/classes/x86/aws-lambda-runtime-interface-client.musl.so
18+
declare -a TARGETS=("glibc" "musl")
1719

18-
# compile the native library for ARM
19-
mkdir "${DST_DIR}"/classes/arm
20-
docker build --platform=linux/arm64/v8 -f "${SRC_DIR}/Dockerfile.glibc" --build-arg CURL_VERSION=${CURL_VERSION} -t lambda-java-jni-lib-glibc-arm "${SRC_DIR}"
21-
docker run --rm --entrypoint /bin/cat lambda-java-jni-lib-glibc-arm /src/aws-lambda-runtime-interface-client.so > "${DST_DIR}"/classes/arm/aws-lambda-runtime-interface-client.glibc.so
20+
for pair in "${ARCHITECTURES_TO_PLATFORM[@]}"; do
21+
arch=${pair%%:*}
22+
platform=${pair#*:}
2223

23-
docker build --platform=linux/arm64/v8 -f "${SRC_DIR}/Dockerfile.musl" --build-arg CURL_VERSION=${CURL_VERSION} -t lambda-java-jni-lib-musl-arm "${SRC_DIR}"
24-
docker run --rm --entrypoint /bin/cat lambda-java-jni-lib-musl-arm /src/aws-lambda-runtime-interface-client.so > "${DST_DIR}"/classes/arm/aws-lambda-runtime-interface-client.musl.so
24+
mkdir -p "${DST_DIR}"/classes/"${arch}"
25+
26+
for target in "${TARGETS[@]}"; do
27+
echo "Compiling the native library for target ${target} on architecture ${arch} using Docker platform ${platform}"
28+
docker build --platform="${platform}" -f "${SRC_DIR}"/Dockerfile."${target}" --build-arg CURL_VERSION=${CURL_VERSION} -t lambda-java-jni-lib-"${target}"-"${arch}" "${SRC_DIR}"
29+
docker run --rm --entrypoint /bin/cat lambda-java-jni-lib-"${target}"-"${arch}" /src/aws-lambda-runtime-interface-client.so > "${DST_DIR}"/classes/"${arch}"/aws-lambda-runtime-interface-client."${target}".so
30+
done
31+
done

0 commit comments

Comments
 (0)