Skip to content

Commit d360edf

Browse files
dcharkesCommit Queue
authored and
Commit Queue
committed
[pkg][vm] Native Assets builder
This package contains the logic for building native assets. This package is the backend that invokes toplevel `build.dart` scripts. For more info on these scripts see https://github.com/dart-lang/native. This is a separate package so that dartdev and flutter_tools can reuse the same logic without flutter_tools having to import dartdev. Some design decisions: * We don't yet have `build_dependencies`, so we use the ordinary dependency graph for ordering of native assets builds. (If there is a cycle we refuse to run.) Bug: dart-lang/pub#3794 * Builds are cached based on all the configuration provided by the caller. Environment variables are ignored in caching. This CL also contains a unit test that invokes the build by not passing through environment variables. However, for Windows we need to pass through at least `SYSTEMROOT` for MSVC to run correctly. So we might need to further explore if we can/want to lock env variables down. Bug: dart-lang/native#32 Bug: dart-lang/native#33 Run tests: ``` dart tools/generate_package_config.dart && \ tools/test.py -n unittest-asserts-release-linux pkg/native_assets_builder ``` Bug: #50565 Change-Id: I133052d7195373e87d20924d61e1e96e3d34ce8f Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-mac-release-try,pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300203 Reviewed-by: Liam Appelbe <[email protected]> Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Hossein Yousefi <[email protected]>
1 parent 05e77a9 commit d360edf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2080
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ client/tests/drt
7575
packages
7676
pubspec.lock
7777

78+
# Files for native assets auto-generated per checkout.
79+
/.dart_tool/native_assets_builder/
80+
7881
# The top level package file (this is auto-generated per checkout).
7982
/.dart_tool/package_config.json
8083

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ vars = {
152152
"matcher_rev": "4dfd9adfdcd3de95e6964fc295075a32635e5b37",
153153
"mime_rev": "eb9d54b8f6fab9442b2b733e19137dc25fb0b868",
154154
"mockito_rev": "51a7728927c505a907384860f4a407f8b7219004",
155-
"native_rev": "64aa5b500fd71b51a34bf4ef0dc05ec24379be00",
155+
"native_rev": "e01aa63e851ce971390bc45fc17d16240b8d164a",
156156
"package_config_rev": "f41f92cd4d2c539d910f2ef041ba6f6bd8469e78",
157157
"path_rev": "f8d15c2775835fd61ea5e06bb0ab99232f5ec446",
158158
"pool_rev": "86b4f4328a32a9e9eb4f1601c8296e22d3e50552",

pkg/front_end/lib/src/api_prototype/experimental_flags_generated.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ const AllowedExperimentalFlags defaultAllowedExperimentalFlags =
858858
"meta": {
859859
ExperimentalFlag.nonNullable,
860860
},
861+
"native_assets_builder": {
862+
ExperimentalFlag.nonNullable,
863+
},
861864
"native_stack_traces": {
862865
ExperimentalFlag.nonNullable,
863866
},

pkg/front_end/test/spell_checking_list_common.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ asserted
204204
assertion
205205
assertions
206206
asserts
207+
asset
208+
assets
207209
assign
208210
assignability
209211
assignable
@@ -3114,6 +3116,7 @@ tokens
31143116
too
31153117
took
31163118
tool
3119+
toolchain
31173120
tools
31183121
top
31193122
topologically

pkg/native_assets_builder/.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Web related
35+
lib/generated_plugin_registrant.dart
36+
37+
# Symbolication related
38+
app.*.symbols
39+
40+
# Obfuscation related
41+
app.*.map.json
42+
43+
# Android Studio will place build artifacts here
44+
/android/app/debug
45+
/android/app/profile
46+
/android/app/release
47+
48+
pubspec.lock

pkg/native_assets_builder/AUTHORS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Dart project. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google LLC

pkg/native_assets_builder/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2022, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkg/native_assets_builder/OWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file:/tools/OWNERS_VM

pkg/native_assets_builder/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This package contains the logic for building native assets.
2+
3+
This package is the backend that invokes toplevel `build.dart` scripts.
4+
For more info on these scripts see https://github.com/dart-lang/native.
5+
6+
This is a separate package so that dartdev and flutter_tools can reuse
7+
the same logic with flutter_tools having to import dartdev.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
include: package:lints/recommended.yaml
2+
3+
analyzer:
4+
language:
5+
strict-raw-types: true
6+
strict-inference: true
7+
# The test projects do _not_ have resolved dependencies through
8+
# `dart tools/package_deps/bin/package_deps.dart`.
9+
# So don't analyze them in the IDE or on the CI.
10+
exclude:
11+
- test/test_projects/**.dart
12+
13+
linter:
14+
rules:
15+
- always_declare_return_types
16+
- avoid_dynamic_calls
17+
- camel_case_types
18+
- depend_on_referenced_packages
19+
- directives_ordering
20+
- prefer_const_declarations
21+
- prefer_expression_function_bodies
22+
- prefer_final_in_for_each
23+
- prefer_final_locals
24+
- prefer_relative_imports
25+
- prefer_single_quotes
26+
- sort_pub_dependencies
27+
- unawaited_futures
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2023, 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+
export 'package:native_assets_builder/src/build_runner/build_runner.dart';
6+
export 'package:native_assets_builder/src/package_layout/package_layout.dart';
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) 2023, 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+
import 'dart:convert';
6+
import 'dart:io';
7+
8+
import 'package:kernel/util/graph.dart' as graph;
9+
import 'package:package_config/package_config.dart';
10+
11+
class NativeAssetsBuildPlanner {
12+
final PackageGraph packageGraph;
13+
final List<Package> packagesWithNativeAssets;
14+
final Uri dartExecutable;
15+
16+
NativeAssetsBuildPlanner({
17+
required this.packageGraph,
18+
required this.packagesWithNativeAssets,
19+
required this.dartExecutable,
20+
});
21+
22+
static Future<NativeAssetsBuildPlanner> fromRootPackageRoot({
23+
required Uri rootPackageRoot,
24+
required List<Package> packagesWithNativeAssets,
25+
required Uri dartExecutable,
26+
}) async {
27+
final result = await Process.run(
28+
dartExecutable.toFilePath(),
29+
[
30+
'pub',
31+
'deps',
32+
'--json',
33+
],
34+
workingDirectory: rootPackageRoot.toFilePath(),
35+
);
36+
final packageGraph = PackageGraph.fromPubDepsJsonString(result.stdout);
37+
return NativeAssetsBuildPlanner(
38+
packageGraph: packageGraph,
39+
packagesWithNativeAssets: packagesWithNativeAssets,
40+
dartExecutable: dartExecutable,
41+
);
42+
}
43+
44+
List<Package> plan() {
45+
final packageMap = {
46+
for (final package in packagesWithNativeAssets) package.name: package
47+
};
48+
final packagesToBuild = packageMap.keys.toSet();
49+
final stronglyConnectedComponents = packageGraph.computeStrongComponents();
50+
final result = <Package>[];
51+
for (final stronglyConnectedComponent in stronglyConnectedComponents) {
52+
final stronglyConnectedComponentWithNativeAssets = [
53+
for (final packageName in stronglyConnectedComponent)
54+
if (packagesToBuild.contains(packageName)) packageName
55+
];
56+
if (stronglyConnectedComponentWithNativeAssets.length > 1) {
57+
throw Exception(
58+
'Cyclic dependency for native asset builds in the following '
59+
'packages: $stronglyConnectedComponent.',
60+
);
61+
} else if (stronglyConnectedComponentWithNativeAssets.length == 1) {
62+
result.add(
63+
packageMap[stronglyConnectedComponentWithNativeAssets.single]!);
64+
}
65+
}
66+
return result;
67+
}
68+
}
69+
70+
class PackageGraph implements graph.Graph<String> {
71+
final Map<String, List<String>> map;
72+
73+
PackageGraph(this.map);
74+
75+
/// Construct a graph from the JSON produced by `dart pub deps --json`.
76+
factory PackageGraph.fromPubDepsJsonString(String json) =>
77+
PackageGraph.fromPubDepsJson(jsonDecode(json) as Map<dynamic, dynamic>);
78+
79+
/// Construct a graph from the JSON produced by `dart pub deps --json`.
80+
factory PackageGraph.fromPubDepsJson(Map<dynamic, dynamic> map) {
81+
final result = <String, List<String>>{};
82+
final packages = map['packages'] as List<dynamic>;
83+
for (final package in packages) {
84+
final package_ = package as Map<dynamic, dynamic>;
85+
final name = package_['name'] as String;
86+
final dependencies = (package_['dependencies'] as List<dynamic>)
87+
.whereType<String>()
88+
.toList();
89+
result[name] = dependencies;
90+
}
91+
return PackageGraph(result);
92+
}
93+
94+
@override
95+
Iterable<String> neighborsOf(String vertex) => map[vertex] ?? [];
96+
97+
@override
98+
Iterable<String> get vertices => map.keys;
99+
100+
List<List<String>> computeStrongComponents() =>
101+
graph.computeStrongComponents(this);
102+
}

0 commit comments

Comments
 (0)