|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:native_assets_builder/native_assets_builder.dart' show BuildResult; |
| 6 | +import 'package:native_assets_cli/native_assets_cli.dart' hide BuildMode; |
| 7 | +import 'package:native_assets_cli/native_assets_cli.dart' as native_assets_cli; |
| 8 | + |
| 9 | +import '../base/common.dart'; |
| 10 | +import '../base/file_system.dart'; |
| 11 | +import '../base/io.dart'; |
| 12 | +import '../build_info.dart'; |
| 13 | +import '../globals.dart' as globals; |
| 14 | +import '../native_assets.dart'; |
| 15 | + |
| 16 | +/// Dry run the native builds. |
| 17 | +/// |
| 18 | +/// This does not build native assets, it only simulates what the final paths |
| 19 | +/// of all assets will be so that this can be embedded in the kernel file. |
| 20 | +Future<Uri?> dryRunNativeAssetsLinux({ |
| 21 | + required NativeAssetsBuildRunner buildRunner, |
| 22 | + required Uri projectUri, |
| 23 | + bool flutterTester = false, |
| 24 | + required FileSystem fileSystem, |
| 25 | +}) async { |
| 26 | + if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.linux); |
| 31 | + final Iterable<Asset> nativeAssetPaths = await dryRunNativeAssetsLinuxInternal( |
| 32 | + fileSystem, |
| 33 | + projectUri, |
| 34 | + flutterTester, |
| 35 | + buildRunner, |
| 36 | + ); |
| 37 | + final Uri nativeAssetsUri = await writeNativeAssetsYaml( |
| 38 | + nativeAssetPaths, |
| 39 | + buildUri_, |
| 40 | + fileSystem, |
| 41 | + ); |
| 42 | + return nativeAssetsUri; |
| 43 | +} |
| 44 | + |
| 45 | +Future<Iterable<Asset>> dryRunNativeAssetsLinuxInternal( |
| 46 | + FileSystem fileSystem, |
| 47 | + Uri projectUri, |
| 48 | + bool flutterTester, |
| 49 | + NativeAssetsBuildRunner buildRunner, |
| 50 | +) async { |
| 51 | + const OS targetOs = OS.linux; |
| 52 | + final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs); |
| 53 | + |
| 54 | + globals.logger.printTrace('Dry running native assets for $targetOs.'); |
| 55 | + final List<Asset> nativeAssets = (await buildRunner.dryRun( |
| 56 | + linkModePreference: LinkModePreference.dynamic, |
| 57 | + targetOs: targetOs, |
| 58 | + workingDirectory: projectUri, |
| 59 | + includeParentEnvironment: true, |
| 60 | + )) |
| 61 | + .assets; |
| 62 | + ensureNoLinkModeStatic(nativeAssets); |
| 63 | + globals.logger.printTrace('Dry running native assets for $targetOs done.'); |
| 64 | + final Uri? absolutePath = flutterTester ? buildUri_ : null; |
| 65 | + final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath); |
| 66 | + final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values; |
| 67 | + return nativeAssetPaths; |
| 68 | +} |
| 69 | + |
| 70 | +/// Builds native assets. |
| 71 | +/// |
| 72 | +/// If [targetPlatform] is omitted, the current target architecture is used. |
| 73 | +/// |
| 74 | +/// If [flutterTester] is true, absolute paths are emitted in the native |
| 75 | +/// assets mapping. This can be used for JIT mode without sandbox on the host. |
| 76 | +/// This is used in `flutter test` and `flutter run -d flutter-tester`. |
| 77 | +Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsLinux({ |
| 78 | + required NativeAssetsBuildRunner buildRunner, |
| 79 | + TargetPlatform? targetPlatform, |
| 80 | + required Uri projectUri, |
| 81 | + required BuildMode buildMode, |
| 82 | + bool flutterTester = false, |
| 83 | + Uri? yamlParentDirectory, |
| 84 | + required FileSystem fileSystem, |
| 85 | +}) async { |
| 86 | + const OS targetOs = OS.linux; |
| 87 | + final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs); |
| 88 | + final Directory buildDir = fileSystem.directory(buildUri_); |
| 89 | + if (!await buildDir.exists()) { |
| 90 | + // CMake requires the folder to exist to do copying. |
| 91 | + await buildDir.create(recursive: true); |
| 92 | + } |
| 93 | + if (await hasNoPackageConfig(buildRunner) || await isDisabledAndNoNativeAssets(buildRunner)) { |
| 94 | + final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem); |
| 95 | + return (nativeAssetsYaml, <Uri>[]); |
| 96 | + } |
| 97 | + |
| 98 | + final Target target = targetPlatform != null ? _getNativeTarget(targetPlatform) : Target.current; |
| 99 | + final native_assets_cli.BuildMode buildModeCli = nativeAssetsBuildMode(buildMode); |
| 100 | + |
| 101 | + globals.logger.printTrace('Building native assets for $target $buildModeCli.'); |
| 102 | + final BuildResult result = await buildRunner.build( |
| 103 | + linkModePreference: LinkModePreference.dynamic, |
| 104 | + target: target, |
| 105 | + buildMode: buildModeCli, |
| 106 | + workingDirectory: projectUri, |
| 107 | + includeParentEnvironment: true, |
| 108 | + cCompilerConfig: await buildRunner.cCompilerConfig, |
| 109 | + ); |
| 110 | + final List<Asset> nativeAssets = result.assets; |
| 111 | + final Set<Uri> dependencies = result.dependencies.toSet(); |
| 112 | + ensureNoLinkModeStatic(nativeAssets); |
| 113 | + globals.logger.printTrace('Building native assets for $target done.'); |
| 114 | + final Uri? absolutePath = flutterTester ? buildUri_ : null; |
| 115 | + final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath); |
| 116 | + await _copyNativeAssetsLinux( |
| 117 | + buildUri_, |
| 118 | + assetTargetLocations, |
| 119 | + buildMode, |
| 120 | + fileSystem, |
| 121 | + ); |
| 122 | + final Uri nativeAssetsUri = await writeNativeAssetsYaml( |
| 123 | + assetTargetLocations.values, |
| 124 | + yamlParentDirectory ?? buildUri_, |
| 125 | + fileSystem, |
| 126 | + ); |
| 127 | + return (nativeAssetsUri, dependencies.toList()); |
| 128 | +} |
| 129 | + |
| 130 | +Map<Asset, Asset> _assetTargetLocations( |
| 131 | + List<Asset> nativeAssets, |
| 132 | + Uri? absolutePath, |
| 133 | +) => |
| 134 | + <Asset, Asset>{ |
| 135 | + for (final Asset asset in nativeAssets) asset: _targetLocationLinux(asset, absolutePath), |
| 136 | + }; |
| 137 | + |
| 138 | +Asset _targetLocationLinux(Asset asset, Uri? absolutePath) { |
| 139 | + final AssetPath path = asset.path; |
| 140 | + switch (path) { |
| 141 | + case AssetSystemPath _: |
| 142 | + case AssetInExecutable _: |
| 143 | + case AssetInProcess _: |
| 144 | + return asset; |
| 145 | + case AssetAbsolutePath _: |
| 146 | + final String fileName = path.uri.pathSegments.last; |
| 147 | + Uri uri; |
| 148 | + if (absolutePath != null) { |
| 149 | + // Flutter tester needs full host paths. |
| 150 | + uri = absolutePath.resolve(fileName); |
| 151 | + } else { |
| 152 | + // Flutter Desktop needs "absolute" paths inside the app. |
| 153 | + // "relative" in the context of native assets would be relative to the |
| 154 | + // kernel or aot snapshot. |
| 155 | + uri = Uri(path: fileName); |
| 156 | + } |
| 157 | + return asset.copyWith(path: AssetAbsolutePath(uri)); |
| 158 | + } |
| 159 | + throw Exception('Unsupported asset path type ${path.runtimeType} in asset $asset'); |
| 160 | +} |
| 161 | + |
| 162 | +/// Extract the [Target] from a [TargetPlatform]. |
| 163 | +Target _getNativeTarget(TargetPlatform targetPlatform) { |
| 164 | + switch (targetPlatform) { |
| 165 | + case TargetPlatform.linux_x64: |
| 166 | + return Target.linuxX64; |
| 167 | + case TargetPlatform.linux_arm64: |
| 168 | + return Target.linuxArm64; |
| 169 | + case TargetPlatform.android: |
| 170 | + case TargetPlatform.ios: |
| 171 | + case TargetPlatform.darwin: |
| 172 | + case TargetPlatform.windows_x64: |
| 173 | + case TargetPlatform.fuchsia_arm64: |
| 174 | + case TargetPlatform.fuchsia_x64: |
| 175 | + case TargetPlatform.tester: |
| 176 | + case TargetPlatform.web_javascript: |
| 177 | + case TargetPlatform.android_arm: |
| 178 | + case TargetPlatform.android_arm64: |
| 179 | + case TargetPlatform.android_x64: |
| 180 | + case TargetPlatform.android_x86: |
| 181 | + throw Exception('Unknown targetPlatform: $targetPlatform.'); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +Future<void> _copyNativeAssetsLinux( |
| 186 | + Uri buildUri, |
| 187 | + Map<Asset, Asset> assetTargetLocations, |
| 188 | + BuildMode buildMode, |
| 189 | + FileSystem fileSystem, |
| 190 | +) async { |
| 191 | + if (assetTargetLocations.isNotEmpty) { |
| 192 | + globals.logger.printTrace('Copying native assets to ${buildUri.toFilePath()}.'); |
| 193 | + final Directory buildDir = fileSystem.directory(buildUri.toFilePath()); |
| 194 | + if (!buildDir.existsSync()) { |
| 195 | + buildDir.createSync(recursive: true); |
| 196 | + } |
| 197 | + for (final MapEntry<Asset, Asset> assetMapping in assetTargetLocations.entries) { |
| 198 | + final Uri source = (assetMapping.key.path as AssetAbsolutePath).uri; |
| 199 | + final Uri target = (assetMapping.value.path as AssetAbsolutePath).uri; |
| 200 | + final Uri targetUri = buildUri.resolveUri(target); |
| 201 | + final String targetFullPath = targetUri.toFilePath(); |
| 202 | + await fileSystem.file(source).copy(targetFullPath); |
| 203 | + } |
| 204 | + globals.logger.printTrace('Copying native assets done.'); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +/// Flutter expects `clang++` to be on the path on Linux hosts. |
| 209 | +/// |
| 210 | +/// Search for the accompanying `clang`, `ar`, and `ld`. |
| 211 | +Future<CCompilerConfig> cCompilerConfigLinux() async { |
| 212 | + const String kClangPlusPlusBinary = 'clang++'; |
| 213 | + const String kClangBinary = 'clang'; |
| 214 | + const String kArBinary = 'llvm-ar'; |
| 215 | + const String kLdBinary = 'ld.lld'; |
| 216 | + |
| 217 | + final ProcessResult whichResult = await globals.processManager.run(<String>['which', kClangPlusPlusBinary]); |
| 218 | + if (whichResult.exitCode != 0) { |
| 219 | + throwToolExit('Failed to find $kClangPlusPlusBinary on PATH.'); |
| 220 | + } |
| 221 | + File clangPpFile = globals.fs.file((whichResult.stdout as String).trim()); |
| 222 | + clangPpFile = globals.fs.file(await clangPpFile.resolveSymbolicLinks()); |
| 223 | + |
| 224 | + final Directory clangDir = clangPpFile.parent; |
| 225 | + final Map<String, Uri> binaryPaths = <String, Uri>{}; |
| 226 | + for (final String binary in <String>[kClangBinary, kArBinary, kLdBinary]) { |
| 227 | + final File binaryFile = clangDir.childFile(binary); |
| 228 | + if (!await binaryFile.exists()) { |
| 229 | + throwToolExit("Failed to find $binary relative to $clangPpFile: $binaryFile doesn't exist."); |
| 230 | + } |
| 231 | + binaryPaths[binary] = binaryFile.uri; |
| 232 | + } |
| 233 | + return CCompilerConfig( |
| 234 | + ar: binaryPaths[kArBinary], |
| 235 | + cc: binaryPaths[kClangBinary], |
| 236 | + ld: binaryPaths[kLdBinary], |
| 237 | + ); |
| 238 | +} |
0 commit comments