Skip to content

Commit 11a3380

Browse files
sstricklCommit Queue
authored andcommitted
[pkg/vm] Handle cross compilation in the precompiler2 script.
When cross compiling, gen_snapshot for the host architecture is in ${BUILD_DIR}/clang_<arch>/gen_snapshot. TEST=manual testing with native X64 and cross-compiling ARM64 on X64. Change-Id: I9f6af045675612651c659683081aca3e65acad1b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404421 Reviewed-by: Slava Egorov <[email protected]>
1 parent c8151ab commit 11a3380

File tree

2 files changed

+103
-11
lines changed

2 files changed

+103
-11
lines changed

pkg/vm/tool/precompiler2

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
set -e
1616

1717
function follow_links() {
18-
file="$1"
18+
local file="$1"
1919
while [ -h "$file" ]; do
2020
# On Mac OS, readlink -f doesn't work.
2121
file="$(readlink "$file")"
@@ -31,6 +31,8 @@ CUR_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
3131

3232
SDK_DIR="$CUR_DIR/../../.."
3333

34+
. "$CUR_DIR/shared_functions.sh"
35+
3436
OPTIONS=()
3537
GEN_KERNEL_OPTIONS=()
3638
PACKAGES=
@@ -98,19 +100,37 @@ else
98100
OUT_DIR="$SDK_DIR/out"
99101
fi
100102

101-
HOST_ARCH="X64"
102-
if [[ `uname -m` == 'arm64' ]]; then
103-
HOST_ARCH="ARM64"
104-
fi
103+
HOST_ARCH="$(host_arch)"
105104

106105
export DART_CONFIGURATION=${DART_CONFIGURATION:-Release$HOST_ARCH}
106+
TARGET_ARCH="$(parse_target_arch "$DART_CONFIGURATION")"
107107
BUILD_DIR="$OUT_DIR/$DART_CONFIGURATION"
108-
109-
DART="${SDK_DIR}/tools/sdks/dart-sdk/bin/dart"
110-
if [ ! -f "$DART" ]; then
111-
DART="$BUILD_DIR/dart"
108+
if [ ! -d "$BUILD_DIR" ]; then
109+
echo "$BUILD_DIR is not a directory"
110+
exit 1
112111
fi
113112

113+
function find_dart {
114+
local tools_dart="${SDK_DIR}/tools/sdks/dart-sdk/bin/dart"
115+
if [ -f "$tools_dart" ]; then
116+
echo "$tools_dart"
117+
else
118+
local host_dart=""
119+
if [[ "$HOST_ARCH" == "$TARGET_ARCH" ]]; then
120+
host_dart="$BUILD_DIR/dart"
121+
else
122+
host_dart="$OUT_DIR/Release$HOST_ARCH/dart"
123+
fi
124+
if [ ! -f "$host_dart" ]; then
125+
echo "Cannot find dart at either $tools_dart or $host_dart"
126+
exit 1
127+
fi
128+
echo "$host_dart"
129+
fi
130+
}
131+
132+
DART="$(find_dart)"
133+
114134
function gen_kernel() {
115135
if [[ "$DART_GN_ARGS" == *"precompile_tools=true"* ]]; then
116136
# Precompile gen_kernel to an AOT app.
@@ -129,10 +149,29 @@ gen_kernel --platform "${BUILD_DIR}/vm_platform_strong.dill" \
129149
-o "$SNAPSHOT_FILE.dill" \
130150
"$SOURCE_FILE"
131151

132-
152+
GEN_SNAPSHOT="gen_snapshot"
153+
# If the target architecture differs from the host architecture, use
154+
# gen_snapshot for the host architecture for cross compilation, which is
155+
# denoted by an X between the build mode and the architecture name,
156+
# e.g., ReleaseXARM64 on X64.
157+
if [[ ( "$DART_CONFIGURATION" =~ (Debug|Release|Product)X[^6] ) && \
158+
( "$HOST_ARCH" != "$TARGET_ARCH" ) ]]; then
159+
case "$HOST_ARCH" in
160+
"X64")
161+
GEN_SNAPSHOT="clang_x64/${GEN_SNAPSHOT}"
162+
;;
163+
"ARM64")
164+
GEN_SNAPSHOT="clang_arm64/${GEN_SNAPSHOT}"
165+
;;
166+
*)
167+
echo "Unexpected host architecture $HOST_ARCH for cross compilation"
168+
exit 1
169+
;;
170+
esac
171+
fi
133172

134173
# Step 2: Generate snapshot from the Kernel binary.
135-
"$BUILD_DIR"/gen_snapshot \
174+
${BUILD_DIR}/${GEN_SNAPSHOT} \
136175
${GEN_SNAPSHOT_FLAGS} \
137176
"$GEN_SNAPSHOT_OPTION" \
138177
"$GEN_SNAPSHOT_FILENAME" \

pkg/vm/tool/shared_functions.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
# Shared functions for both precompiler2 and dart_precompiled_runtime2.
6+
7+
function host_arch() {
8+
# Use uname to determine the host architecture.
9+
case `uname -m` in
10+
x86_64)
11+
echo "X64"
12+
;;
13+
arm64)
14+
echo "ARM64"
15+
;;
16+
arm)
17+
echo "ARM"
18+
;;
19+
*)
20+
echo "Unknown host architecture" `uname -m`
21+
exit 1
22+
;;
23+
esac
24+
}
25+
26+
function parse_target_arch() {
27+
case "$1" in
28+
*X64)
29+
echo "X64"
30+
;;
31+
*ARM64 | \
32+
*ARM64C)
33+
echo "ARM64"
34+
;;
35+
*ARM)
36+
echo "ARM"
37+
;;
38+
*IA32)
39+
echo "IA32"
40+
;;
41+
*RISCV32)
42+
echo "RISCV32"
43+
;;
44+
*RISCV64)
45+
echo "RISCV64"
46+
;;
47+
*)
48+
echo "Cannot deduce target architecture from $1"
49+
exit 1
50+
;;
51+
esac
52+
}
53+

0 commit comments

Comments
 (0)