Skip to content

Commit e534983

Browse files
iinozemtsevCommit Queue
authored andcommitted
Linkable AOT snapshots on macOS
Update `aot_snapshot` template to optionally build a shared library from `app-aot-assembly` sanpshot. Change-Id: Ifb7cee65b5699099fa676312fff7d687cfb52d98 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/407060 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Ivan Inozemtsev <[email protected]>
1 parent d678a7e commit e534983

File tree

4 files changed

+100
-22
lines changed

4 files changed

+100
-22
lines changed

BUILD.gn

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ group("most") {
2929
":dartanalyzer",
3030
":ddc",
3131
":runtime",
32-
":samples",
3332
]
3433
}
3534

36-
group("samples") {
37-
deps = [ "samples/embedder:all" ]
38-
}
39-
4035
group("runtime") {
4136
import("runtime/runtime_args.gni")
4237

@@ -69,7 +64,8 @@ group("runtime") {
6964

7065
# AOT samples use dlopen to load AOT snapshots and it works only on
7166
# 64-bit Linux right now.
72-
if (is_linux && (dart_target_arch == "x64" || dart_target_arch == "arm64")) {
67+
if ((is_linux || is_mac) &&
68+
(dart_target_arch == "x64" || dart_target_arch == "arm64")) {
7369
deps += [ "samples/embedder:aot" ]
7470
}
7571

samples/embedder/BUILD.gn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ template("snapshots") {
9999
# AOT snapshot
100100
aot_snapshot("${target_name}_aot") {
101101
main_dart = invoker.main_dart
102+
103+
# AOT snapshots as shared libraries on Windows are not
104+
# supported, and in fact we don't build AOT samples on
105+
# Windows. However, GN evaluation model will still
106+
# evaluate the `aot_snapshot` template on Windows,
107+
# and it will fail the assert if as_shared_library is
108+
# true, and the current platform is Windows.
109+
as_shared_library = !is_win
102110
}
103111
}
104112

tests/standalone/embedder_samples_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void checkSample(
3232
}
3333

3434
void main() {
35-
final executable = Platform.executable;
35+
final executable = File(Platform.executable).absolute.path;
3636
final out = executable.substring(0, executable.lastIndexOf('dart') - 1);
3737

3838
checkSample('$out/run_main_kernel', ['$out/gen/hello_kernel.dart.snapshot']);

utils/aot_snapshot.gni

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,101 @@ template("aot_snapshot") {
100100
}
101101
}
102102

103-
# Create a snapshot from kernel built above.
104-
gen_snapshot_action(target_name) {
105-
if (defined(invoker.pool)) {
106-
pool = invoker.pool
103+
# Whether to build an AOT snapshot, which can be opened by dlopen.
104+
# Ignore this option on Linux, as the default app-aot-elf AOT
105+
# snapshot already can be used with dlopen on Linux.
106+
as_shared_library = defined(invoker.as_shared_library) &&
107+
invoker.as_shared_library && !is_linux
108+
109+
assert(!(as_shared_library && is_win),
110+
"AOT Snapshots as shared libraries are not supported on Windows")
111+
112+
if (!as_shared_library) {
113+
# Create a snapshot from kernel built above.
114+
gen_snapshot_action(target_name) {
115+
if (defined(invoker.pool)) {
116+
pool = invoker.pool
117+
}
118+
deps = extra_deps + [ ":${target_name}_dill" ]
119+
120+
inputs = extra_inputs
121+
122+
outputs = [ output ]
123+
124+
abs_output = rebase_path(output)
125+
126+
vm_args = [
127+
"--deterministic",
128+
"--snapshot-kind=app-aot-elf",
129+
"--elf=$abs_output",
130+
] + gen_snapshot_args
131+
132+
args = [ rebase_path(dill) ]
133+
134+
force_product_mode = product_mode
107135
}
108-
deps = extra_deps + [ ":${target_name}_dill" ]
136+
} else {
137+
assembly = "$target_gen_dir/$name.S"
138+
dill_target_name = ":${target_name}_dill"
109139

110-
inputs = extra_inputs
140+
# Create an assembly snapshot from kernel built above.
141+
assembly_target_name = target_name + "_assembly"
142+
gen_snapshot_action(assembly_target_name) {
143+
if (defined(invoker.pool)) {
144+
pool = invoker.pool
145+
}
146+
deps = extra_deps + [ dill_target_name ]
111147

112-
outputs = [ output ]
148+
inputs = extra_inputs
113149

114-
abs_output = rebase_path(output)
150+
outputs = [ assembly ]
115151

116-
vm_args = [
117-
"--deterministic",
118-
"--snapshot-kind=app-aot-elf",
119-
"--elf=$abs_output",
120-
] + gen_snapshot_args
152+
abs_output = rebase_path(assembly)
153+
vm_args = [
154+
"--deterministic",
155+
"--snapshot-kind=app-aot-assembly",
156+
"--assembly=$abs_output",
157+
] + gen_snapshot_args
158+
159+
args = [ rebase_path(dill) ]
160+
161+
force_product_mode = product_mode
162+
}
121163

122-
args = [ rebase_path(dill) ]
164+
# build a shared library from assembly.
165+
shared_library_target_name = target_name + "_shared_library"
166+
shared_library(shared_library_target_name) {
167+
sources = [ assembly ]
168+
deps = [ ":${assembly_target_name}" ]
169+
}
123170

124-
force_product_mode = product_mode
171+
output_prefix = "lib"
172+
output_extension = ""
173+
174+
if (current_os == "mac" || current_os == "ios") {
175+
output_extension = "dylib"
176+
} else if (current_os == "win") {
177+
output_extension = "dll"
178+
output_prefix = ""
179+
} else if (current_os == "unknown" && current_cpu == "wasm32") {
180+
output_extension = "wasm"
181+
} else {
182+
output_extension = "so"
183+
}
184+
185+
shared_library_output_file_name =
186+
"$output_prefix$shared_library_target_name"
187+
if (output_extension != "") {
188+
shared_library_output_file_name += ".$output_extension"
189+
}
190+
191+
# copy shared library to the same output, as when
192+
# `as_shared_library` is False.
193+
copy(target_name) {
194+
sources = [ "$root_out_dir/$shared_library_output_file_name" ]
195+
outputs = [ output ]
196+
197+
deps = [ ":${shared_library_target_name}" ]
198+
}
125199
}
126200
}

0 commit comments

Comments
 (0)