Skip to content

Commit f6ee792

Browse files
committed
Add support for Rosetta 2 on Silicon Mac.
This will allow users of the new Mac arm64 architecture to keep using the x86_64 java binaries through the Rosetta 2 compatiblity layer. It relies on the `sdkman_rosetta2_compatible` configuration to be set to `true` in their `.sdkman/etc/config` file.
1 parent 45c1d9d commit f6ee792

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/main/bash/sdkman-init.sh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ if [ -z "$SDKMAN_DIR" ]; then
2929
export SDKMAN_DIR="$HOME/.sdkman"
3030
fi
3131

32-
# infer platform
32+
# Load the sdkman config if it exists.
33+
if [ -f "${SDKMAN_DIR}/etc/config" ]; then
34+
source "${SDKMAN_DIR}/etc/config"
35+
fi
3336

37+
# infer platform
3438
function infer_platform() {
3539
local kernel
3640
local machine
@@ -66,8 +70,12 @@ function infer_platform() {
6670
x86_64)
6771
echo "DarwinX64"
6872
;;
69-
arm64)
70-
echo "DarwinARM64"
73+
arm64)
74+
if [[ "$sdkman_rosetta2_compatible" == 'true' ]]; then
75+
echo "DarwinX64"
76+
else
77+
echo "DarwinARM64"
78+
fi
7179
;;
7280
*)
7381
echo "DarwinX64"
@@ -127,11 +135,6 @@ done
127135
IFS="$OLD_IFS"
128136
unset OLD_IFS scripts f
129137

130-
# Load the sdkman config if it exists.
131-
if [ -f "${SDKMAN_DIR}/etc/config" ]; then
132-
source "${SDKMAN_DIR}/etc/config"
133-
fi
134-
135138
# Create upgrade delay file if it doesn't exist
136139
if [[ ! -f "${SDKMAN_DIR}/var/delay_upgrade" ]]; then
137140
touch "${SDKMAN_DIR}/var/delay_upgrade"

src/test/groovy/sdkman/specs/PlatformSpec.groovy

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,48 @@ class PlatformSpec extends SdkmanEnvSpecification {
66
def setup() {
77
sdkmanBashEnvBuilder.withCandidates(["groovy"])
88
}
9-
9+
1010
def "should set platform based on uname"() {
1111
given:
1212
unameStub.forKernel(kernel).forMachine(machine)
1313
bash = sdkmanBashEnvBuilder.withUnameStub(unameStub).build()
1414
bash.start()
1515
bash.execute("source $bootstrapScript")
1616
bash.execute('echo $SDKMAN_PLATFORM')
17-
17+
1818
expect:
1919
bash.output.contains(platform)
20-
20+
2121
where:
22-
kernel | machine | platform
23-
"Linux" | "i686" | "LinuxX32"
24-
"Linux" | "x86_64" | "LinuxX64"
25-
"Linux" | "armv7l" | "LinuxARM32"
26-
"Linux" | "armv8l" | "LinuxARM64"
27-
"Linux" | "aarch64" | "LinuxARM64"
28-
"Linux" | "" | "LinuxX64"
29-
"Darwin" | "x86_64" | "DarwinX64"
30-
"Darwin" | "arm64" | "DarwinARM64"
31-
"Darwin" | "" | "DarwinX64"
32-
"MSYS64" | "i686" | "MSYS64"
33-
"MSYS64" | "" | "MSYS64"
22+
kernel | machine | platform
23+
"Linux" | "i686" | "LinuxX32"
24+
"Linux" | "x86_64" | "LinuxX64"
25+
"Linux" | "armv7l" | "LinuxARM32"
26+
"Linux" | "armv8l" | "LinuxARM64"
27+
"Linux" | "aarch64" | "LinuxARM64"
28+
"Linux" | "" | "LinuxX64"
29+
"Darwin" | "x86_64" | "DarwinX64"
30+
"Darwin" | "arm64" | "DarwinARM64"
31+
"Darwin" | "" | "DarwinX64"
32+
"MSYS64" | "i686" | "MSYS64"
33+
"MSYS64" | "" | "MSYS64"
34+
}
35+
36+
def "should enable rosetta 2 compatibility mode with environment variable"() {
37+
given:
38+
unameStub.forKernel("Darwin").forMachine("arm64")
39+
bash = sdkmanBashEnvBuilder
40+
.withUnameStub(unameStub)
41+
.withConfiguration("sdkman_rosetta2_compatible", "true")
42+
.build()
43+
bash.start()
44+
bash.execute("source $bootstrapScript")
45+
46+
when:
47+
bash.execute('echo $SDKMAN_PLATFORM')
48+
49+
then:
50+
!bash.output.contains("DarwinARM64")
51+
bash.output.contains("DarwinX64")
3452
}
3553
}

0 commit comments

Comments
 (0)