Skip to content

Commit

Permalink
Move package:native_assets_builder (dart-lang#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Jun 27, 2023
1 parent c80be90 commit e12d3e6
Show file tree
Hide file tree
Showing 66 changed files with 2,119 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# This configures the .github/workflows/pull_request_label.yml workflow.
# This configures the .github/workflows/pull_request_label.yml workflow.

'type-infra':
- '.github/**'

'package:c_compiler':
- pkgs/c_compiler/**/*

'package:native_assets_builder':
- pkgs/native_assets_builder/**/*

'package:native_assets_cli':
- pkgs/native_assets_cli/**/*
23 changes: 20 additions & 3 deletions .github/workflows/dart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
matrix:
os: [ubuntu, macos, windows]
sdk: [stable, dev]
package: [c_compiler, native_assets_cli]
package: [c_compiler, native_assets_builder, native_assets_cli]
exclude:
# Only run analyze against dev on one host.
- os: macos
Expand Down Expand Up @@ -49,8 +49,25 @@ jobs:

- run: dart pub get

- run: dart pub get
working-directory: pkgs/native_assets_cli/example/native_add/
- run: dart pub get -C test/data/dart_app/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test/data/native_add/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test/data/native_add_add_source/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test/data/native_subtract/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test/data/package_reading_metadata/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test/data/package_with_metadata/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C example/native_add/
if: ${{ matrix.package == 'native_assets_cli' }}

- run: dart analyze --fatal-infos
Expand Down
6 changes: 6 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Dart project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google LLC
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bundling.
| -------------------------------------------- | ------------------------------------------------------------------------------------------- | ------- |
| [c_compiler](pkgs/c_compiler/) | A library to invoke the native C compiler installed on the host machine. | |
| [native_assets_cli](pkgs/native_assets_cli/) | A library that contains the argument and file formats for implementing a native assets CLI. | |
| [native_assets_builder](pkgs/native_assets_builder/) | A library that contains the logic for building native assets. This should not be used by users, and is used as shared implementation between dartdev and flutter_tools. | |

<!-- ## Publishing automation
Expand Down
9 changes: 9 additions & 0 deletions pkgs/native_assets_builder/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

coverage/
27 changes: 27 additions & 0 deletions pkgs/native_assets_builder/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2023, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7 changes: 7 additions & 0 deletions pkgs/native_assets_builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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 with flutter_tools having to import dartdev.
22 changes: 22 additions & 0 deletions pkgs/native_assets_builder/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
include: package:dart_flutter_team_lints/analysis_options.yaml

analyzer:
language:
strict-raw-types: true
strict-inference: true

linter:
rules:
- always_declare_return_types
- avoid_dynamic_calls
- camel_case_types
- depend_on_referenced_packages
- directives_ordering
- prefer_const_declarations
- prefer_expression_function_bodies
- prefer_final_in_for_each
- prefer_final_locals
- prefer_relative_imports
- prefer_single_quotes
- sort_pub_dependencies
- unawaited_futures
2 changes: 2 additions & 0 deletions pkgs/native_assets_builder/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
paths:
- test/build_runner/
6 changes: 6 additions & 0 deletions pkgs/native_assets_builder/lib/native_assets_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

export 'package:native_assets_builder/src/build_runner/build_runner.dart';
export 'package:native_assets_builder/src/package_layout/package_layout.dart';
101 changes: 101 additions & 0 deletions pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:graphs/graphs.dart' as graphs;
import 'package:package_config/package_config.dart';

class NativeAssetsBuildPlanner {
final PackageGraph packageGraph;
final List<Package> packagesWithNativeAssets;
final Uri dartExecutable;

NativeAssetsBuildPlanner({
required this.packageGraph,
required this.packagesWithNativeAssets,
required this.dartExecutable,
});

static Future<NativeAssetsBuildPlanner> fromRootPackageRoot({
required Uri rootPackageRoot,
required List<Package> packagesWithNativeAssets,
required Uri dartExecutable,
}) async {
final result = await Process.run(
dartExecutable.toFilePath(),
[
'pub',
'deps',
'--json',
],
workingDirectory: rootPackageRoot.toFilePath(),
);
final packageGraph =
PackageGraph.fromPubDepsJsonString(result.stdout as String);
return NativeAssetsBuildPlanner(
packageGraph: packageGraph,
packagesWithNativeAssets: packagesWithNativeAssets,
dartExecutable: dartExecutable,
);
}

List<Package> plan() {
final packageMap = {
for (final package in packagesWithNativeAssets) package.name: package
};
final packagesToBuild = packageMap.keys.toSet();
final stronglyConnectedComponents = packageGraph.computeStrongComponents();
final result = <Package>[];
for (final stronglyConnectedComponent in stronglyConnectedComponents) {
final stronglyConnectedComponentWithNativeAssets = [
for (final packageName in stronglyConnectedComponent)
if (packagesToBuild.contains(packageName)) packageName
];
if (stronglyConnectedComponentWithNativeAssets.length > 1) {
throw Exception(
'Cyclic dependency for native asset builds in the following '
'packages: $stronglyConnectedComponent.',
);
} else if (stronglyConnectedComponentWithNativeAssets.length == 1) {
result.add(
packageMap[stronglyConnectedComponentWithNativeAssets.single]!);
}
}
return result;
}
}

class PackageGraph {
final Map<String, List<String>> map;

PackageGraph(this.map);

/// Construct a graph from the JSON produced by `dart pub deps --json`.
factory PackageGraph.fromPubDepsJsonString(String json) =>
PackageGraph.fromPubDepsJson(jsonDecode(json) as Map<dynamic, dynamic>);

/// Construct a graph from the JSON produced by `dart pub deps --json`.
factory PackageGraph.fromPubDepsJson(Map<dynamic, dynamic> map) {
final result = <String, List<String>>{};
final packages = map['packages'] as List<dynamic>;
for (final package in packages) {
final package_ = package as Map<dynamic, dynamic>;
final name = package_['name'] as String;
final dependencies = (package_['dependencies'] as List<dynamic>)
.whereType<String>()
.toList();
result[name] = dependencies;
}
return PackageGraph(result);
}

Iterable<String> neighborsOf(String vertex) => map[vertex] ?? [];

Iterable<String> get vertices => map.keys;

List<List<String>> computeStrongComponents() =>
graphs.stronglyConnectedComponents(vertices, neighborsOf);
}
Loading

0 comments on commit e12d3e6

Please sign in to comment.