|
| 1 | +// Copyright (c) 2024, 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 | +/// Library defining the `native_assets.yaml` format for embedding in a Dart |
| 6 | +/// kernel file. |
| 7 | +/// |
| 8 | +/// The `native_assets.yaml` embedded in a kernel file has a different format |
| 9 | +/// from the assets passed in the `build_output.yaml` from individual native |
| 10 | +/// assets builds. This library defines the format of the former so that it |
| 11 | +/// can be reused in `package:dartdev` and `package:flutter_tools`. |
| 12 | +/// |
| 13 | +/// The format should be consistent with `pkg/vm/lib/native_assets/` in the |
| 14 | +/// Dart SDK. |
| 15 | +library kernel_native_assets; |
| 16 | + |
| 17 | +import 'package:native_assets_cli/native_assets_cli_internal.dart'; |
| 18 | + |
| 19 | +import '../utils/yaml.dart'; |
| 20 | + |
| 21 | +class KernelAssets { |
| 22 | + final List<KernelAsset> assets; |
| 23 | + |
| 24 | + KernelAssets(this.assets); |
| 25 | + |
| 26 | + String toNativeAssetsFile() { |
| 27 | + final assetsPerTarget = <Target, List<KernelAsset>>{}; |
| 28 | + for (final asset in assets) { |
| 29 | + final assets = assetsPerTarget[asset.target] ?? []; |
| 30 | + assets.add(asset); |
| 31 | + assetsPerTarget[asset.target] = assets; |
| 32 | + } |
| 33 | + |
| 34 | + final yamlContents = { |
| 35 | + 'format-version': [1, 0, 0], |
| 36 | + 'native-assets': { |
| 37 | + for (final entry in assetsPerTarget.entries) |
| 38 | + entry.key.toString(): { |
| 39 | + for (final e in entry.value) e.id: e.path.toYaml(), |
| 40 | + } |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + return yamlEncode(yamlContents); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class KernelAsset { |
| 49 | + final String id; |
| 50 | + final Target target; |
| 51 | + final KernelAssetPath path; |
| 52 | + |
| 53 | + KernelAsset({ |
| 54 | + required this.id, |
| 55 | + required this.target, |
| 56 | + required this.path, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +abstract class KernelAssetPath { |
| 61 | + List<String> toYaml(); |
| 62 | +} |
| 63 | + |
| 64 | +/// Asset at absolute path [uri] on the target device where Dart is run. |
| 65 | +class KernelAssetAbsolutePath implements KernelAssetPath { |
| 66 | + final Uri uri; |
| 67 | + |
| 68 | + KernelAssetAbsolutePath(this.uri); |
| 69 | + |
| 70 | + static const _pathTypeValue = 'absolute'; |
| 71 | + |
| 72 | + @override |
| 73 | + List<String> toYaml() => [_pathTypeValue, uri.toFilePath()]; |
| 74 | +} |
| 75 | + |
| 76 | +/// Asset at relative path [uri], relative to the 'dart file' executed. |
| 77 | +/// |
| 78 | +/// The 'dart file' executed can be one of the following: |
| 79 | +/// |
| 80 | +/// 1. The `.dart` file when executing `dart path/to/script.dart`. |
| 81 | +/// 2. The `.kernel` file when executing from a kernel file. |
| 82 | +/// 3. The `.aotsnapshot` file when executing from an AOT snapshot with the Dart |
| 83 | +/// AOT runtime. |
| 84 | +/// 4. The executable when executing a Dart app compiled with `dart compile exe` |
| 85 | +/// to a single file. |
| 86 | +/// |
| 87 | +/// Note when writing your own embedder, make sure the `Dart_CreateIsolateGroup` |
| 88 | +/// or similar calls set up the `script_uri` parameter correctly to ensure |
| 89 | +/// relative path resolution works. |
| 90 | +class KernelAssetRelativePath implements KernelAssetPath { |
| 91 | + final Uri uri; |
| 92 | + |
| 93 | + KernelAssetRelativePath(this.uri); |
| 94 | + |
| 95 | + static const _pathTypeValue = 'relative'; |
| 96 | + |
| 97 | + @override |
| 98 | + List<String> toYaml() => [_pathTypeValue, uri.toFilePath()]; |
| 99 | +} |
| 100 | + |
| 101 | +/// Asset is avaliable on the system `PATH`. |
| 102 | +/// |
| 103 | +/// [uri] only contains a file name. |
| 104 | +class KernelAssetSystemPath implements KernelAssetPath { |
| 105 | + final Uri uri; |
| 106 | + |
| 107 | + KernelAssetSystemPath(this.uri); |
| 108 | + |
| 109 | + static const _pathTypeValue = 'system'; |
| 110 | + |
| 111 | + @override |
| 112 | + List<String> toYaml() => [_pathTypeValue, uri.toFilePath()]; |
| 113 | +} |
| 114 | + |
| 115 | +/// Asset is loaded in the process and symbols are available through |
| 116 | +/// `DynamicLibrary.process()`. |
| 117 | +class KernelAssetInProcess implements KernelAssetPath { |
| 118 | + KernelAssetInProcess._(); |
| 119 | + |
| 120 | + static final KernelAssetInProcess _singleton = KernelAssetInProcess._(); |
| 121 | + |
| 122 | + factory KernelAssetInProcess() => _singleton; |
| 123 | + |
| 124 | + static const _pathTypeValue = 'process'; |
| 125 | + |
| 126 | + @override |
| 127 | + List<String> toYaml() => [_pathTypeValue]; |
| 128 | +} |
| 129 | + |
| 130 | +/// Asset is embedded in executable and symbols are available through |
| 131 | +/// `DynamicLibrary.executable()`. |
| 132 | +class KernelAssetInExecutable implements KernelAssetPath { |
| 133 | + KernelAssetInExecutable._(); |
| 134 | + |
| 135 | + static final KernelAssetInExecutable _singleton = KernelAssetInExecutable._(); |
| 136 | + |
| 137 | + factory KernelAssetInExecutable() => _singleton; |
| 138 | + |
| 139 | + static const _pathTypeValue = 'executable'; |
| 140 | + |
| 141 | + @override |
| 142 | + List<String> toYaml() => [_pathTypeValue]; |
| 143 | +} |
0 commit comments