Skip to content

Commit e35a4f1

Browse files
Add option for native exe
1 parent 9237e57 commit e35a4f1

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

doc/standalone.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ Depends on: [`pkg-compile-snapshot`][]
3939

4040
## `pkg-compile-native`
4141

42-
Uses configuration: [`pkg.executables`][], [`pkg.version`][]
42+
Uses configuration: [`pkg.executables`][], [`pkg.version`][], [`pkg.useExe`][],
43+
44+
[`pkg.useExe`]: https://pub.dev/documentation/cli_pkg/latest/cli_pkg/useExe.html
4345

4446
Output: `build/$executable.native`
4547

4648
Compiles each executable in the package to an
47-
[AOT module (aot-snapshot)][aot-snapshot] with asserts disabled.
49+
[AOT module (aot-snapshot)][aot-snapshot] or a
50+
[self-contained executable][exe] (depending on the current platform and
51+
[`pkg.useExe`][]) with asserts disabled.
52+
53+
[exe]: https://dart.dev/tools/dart-compile#exe
4854

4955
[`String.fromEnvironment()`]: https://api.dartlang.org/stable/dart-core/String/String.fromEnvironment.html
5056

@@ -104,8 +110,9 @@ that can be used to invoke them.
104110

105111
Any OS's packages can be built regardless of the OS running the task, but if the
106112
host OS matches the target OS *and* the architecture is 64-bit, executables will
107-
be built as [AOT modules (aot-snapshot)][aot-snapshot], which are substantially
108-
faster and smaller than the kernel snapshots that are generated otherwise.
113+
be built as [AOT modules (aot-snapshot)][aot-snapshot] or ["exe"][exe], which
114+
are substantially faster and smaller than the kernel snapshots that are
115+
generated otherwise.
109116

110117
The target for the current OS and architecture is always available. However, for
111118
any OS or architecture under experimental Dart SDK support, such task is only

lib/src/standalone.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import 'utils.dart';
3333
/// This defaults to [name].
3434
final standaloneName = InternalConfigVariable.fn<String>(() => name.value);
3535

36+
/// Whether to build a native executable for the current platform instead of an
37+
/// AOT snapshot.
38+
/// Defined as a function of a [CliPlatform] to allow different behavior on
39+
/// different platforms.
40+
///
41+
/// This defaults to `CliPlatform::useExe`.
42+
final useExe = InternalConfigVariable.value<bool Function(CliPlatform)>((CliPlatform platform) => platform.useExe);
43+
3644
/// For each executable entrypoint in [executables], builds a portable module
3745
/// (kernel) to `build/${executable}.snapshot`.
3846
void _compileSnapshot() {
@@ -66,7 +74,8 @@ void _compileSnapshot() {
6674
}
6775

6876
/// For each executable entrypoint in [executables], builds an AOT module
69-
/// (aot-snapshot) to `build/${executable}.native`.
77+
/// (aot-snapshot) or standalone executable (exe) to
78+
/// `build/${executable}.native`.
7079
///
7180
/// If [enableAsserts] is `true`, this compiles with `--enable-asserts`.
7281
void _compileNative({bool enableAsserts = false}) {
@@ -85,7 +94,7 @@ void _compileNative({bool enableAsserts = false}) {
8594
'dart',
8695
arguments: [
8796
'compile',
88-
CliPlatform.current.useExe ? 'exe' : 'aot-snapshot',
97+
useExe.value(CliPlatform.current) ? 'exe' : 'aot-snapshot',
8998
if (enableAsserts) '--enable-asserts',
9099
for (var entry in environmentConstants.value.entries)
91100
'-D${entry.key}=${entry.value}',
@@ -109,6 +118,7 @@ void addStandaloneTasks() {
109118

110119
freezeSharedVariables();
111120
standaloneName.freeze();
121+
useExe.freeze();
112122

113123
addTask(
114124
GrinderTask(
@@ -211,7 +221,7 @@ Future<void> _buildPackage(CliPlatform platform) async {
211221
var archive = Archive()
212222
..addFile(fileFromString("$standaloneName/src/LICENSE", await license));
213223

214-
if (!platform.useExe) {
224+
if (!(platform.useNative && useExe.value(platform))) {
215225
archive.addFile(
216226
fileFromBytes(
217227
"$standaloneName/src/dart${platform.binaryExtension}",
@@ -222,7 +232,7 @@ Future<void> _buildPackage(CliPlatform platform) async {
222232
}
223233

224234
for (var name in executables.value.keys) {
225-
if (platform.useExe) {
235+
if (platform.useNative && useExe.value(platform)) {
226236
archive.addFile(
227237
file(
228238
"$standaloneName/$name${platform.binaryExtension}",
@@ -240,7 +250,7 @@ Future<void> _buildPackage(CliPlatform platform) async {
240250
}
241251
}
242252

243-
if (!platform.useExe) {
253+
if (!(platform.useNative && useExe.value(platform))) {
244254
// Do this separately from adding entrypoints because multiple executables
245255
// may have the same entrypoint.
246256
for (var name in executables.value.keys) {

0 commit comments

Comments
 (0)