| // 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:async'; |
| import 'dart:io'; |
| |
| import 'package:dartdev/src/sdk.dart'; |
| import 'package:logging/logging.dart'; |
| import 'package:native_assets_builder/native_assets_builder.dart'; |
| import 'package:native_assets_cli/native_assets_cli.dart'; |
| |
| import 'core.dart'; |
| |
| final logger = Logger('') |
| ..onRecord.listen((LogRecord record) { |
| final levelValue = record.level.value; |
| if (levelValue >= Level.SEVERE.value) { |
| log.stderr(record.message); |
| } else if (levelValue >= Level.INFO.value) { |
| log.stdout(record.message); |
| } else { |
| log.trace(record.message); |
| } |
| }); |
| |
| /// Compiles all native assets for host OS in JIT mode. |
| /// |
| /// Returns `null` on missing package_config.json, failing gracefully. |
| Future<List<Asset>?> compileNativeAssetsJit() async { |
| final workingDirectory = Directory.current.uri; |
| // TODO(https://github.com/dart-lang/package_config/issues/126): Use |
| // package config resolution from package:package_config. |
| if (!await File.fromUri( |
| workingDirectory.resolve('.dart_tool/package_config.json')) |
| .exists()) { |
| return null; |
| } |
| final assets = await NativeAssetsBuildRunner( |
| // This always runs in JIT mode. |
| dartExecutable: Uri.file(sdk.dart), |
| logger: logger, |
| ).build( |
| workingDirectory: workingDirectory, |
| // When running in JIT mode, only the host OS needs to be build. |
| target: Target.current, |
| // When running in JIT mode, only dynamic libraries are supported. |
| linkModePreference: LinkModePreference.dynamic, |
| // Dart has no concept of release vs debug, default to release. |
| buildMode: BuildMode.release, |
| includeParentEnvironment: true, |
| ); |
| return assets; |
| } |
| |
| /// Compiles all native assets for host OS in JIT mode, and creates the |
| /// native assets yaml file. |
| /// |
| /// Used in `dart run` and `dart test`. |
| /// |
| /// Returns `null` on missing package_config.json, failing gracefully. |
| Future<Uri?> compileNativeAssetsJitYamlFile() async { |
| final assets = await compileNativeAssetsJit(); |
| if (assets == null) return null; |
| |
| final workingDirectory = Directory.current.uri; |
| final assetsUri = workingDirectory.resolve('.dart_tool/native_assets.yaml'); |
| final nativeAssetsYaml = '''# Native assets mapping for host OS in JIT mode. |
| # Generated by dartdev and package:native. |
| ${assets.toNativeAssetsFile()}'''; |
| final assetFile = File(assetsUri.toFilePath()); |
| await assetFile.writeAsString(nativeAssetsYaml); |
| return assetsUri; |
| } |