Unverified Commit ff4a0f67 authored by Daco Harkes's avatar Daco Harkes Committed by GitHub

Native assets support for Windows (#134203)

Support for FFI calls with `@Native external` functions through Native assets on Windows. This enables bundling native code without any build-system boilerplate code.

For more info see:

* https://github.com/flutter/flutter/issues/129757

### Implementation details for Windows.

Mainly follows the design of https://github.com/flutter/flutter/pull/134031.

Specifically for Windows in this PR is the logic for finding the compiler `cl.exe` and environment variables that contain the paths to the Windows headers `vcvars.bat` based on `vswhere.exe`.
parent 3e7c388e
......@@ -16,6 +16,7 @@ import '../../linux/native_assets.dart';
import '../../macos/native_assets.dart';
import '../../macos/xcode.dart';
import '../../native_assets.dart';
import '../../windows/native_assets.dart';
import '../build_system.dart';
import '../depfile.dart';
import '../exceptions.dart';
......@@ -134,6 +135,20 @@ class NativeAssets extends Target {
fileSystem: fileSystem,
buildRunner: buildRunner,
);
case TargetPlatform.windows_x64:
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsWindows(
targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
);
case TargetPlatform.tester:
if (const LocalPlatform().isMacOS) {
(_, dependencies) = await buildNativeAssetsMacOS(
......@@ -154,6 +169,15 @@ class NativeAssets extends Target {
buildRunner: buildRunner,
flutterTester: true,
);
} else if (const LocalPlatform().isWindows) {
(_, dependencies) = await buildNativeAssetsWindows(
buildMode: BuildMode.debug,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
flutterTester: true,
);
} else {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
......@@ -168,7 +192,6 @@ class NativeAssets extends Target {
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.web_javascript:
case TargetPlatform.windows_x64:
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
......
......@@ -27,7 +27,7 @@ Future<Uri?> dryRunNativeAssetsIOS({
return null;
}
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.iOS);
final Uri buildUri = nativeAssetsBuildUri(projectUri, OS.iOS);
final Iterable<Asset> assetTargetLocations = await dryRunNativeAssetsIOSInternal(
fileSystem,
projectUri,
......@@ -35,7 +35,7 @@ Future<Uri?> dryRunNativeAssetsIOS({
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations,
buildUri_,
buildUri,
fileSystem,
);
return nativeAssetsUri;
......@@ -46,17 +46,17 @@ Future<Iterable<Asset>> dryRunNativeAssetsIOSInternal(
Uri projectUri,
NativeAssetsBuildRunner buildRunner,
) async {
const OS targetOs = OS.iOS;
globals.logger.printTrace('Dry running native assets for $targetOs.');
const OS targetOS = OS.iOS;
globals.logger.printTrace('Dry running native assets for $targetOS.');
final List<Asset> nativeAssets = (await buildRunner.dryRun(
linkModePreference: LinkModePreference.dynamic,
targetOs: targetOs,
targetOS: targetOS,
workingDirectory: projectUri,
includeParentEnvironment: true,
))
.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOs done.');
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Iterable<Asset> assetTargetLocations = _assetTargetLocations(nativeAssets).values;
return assetTargetLocations;
}
......@@ -80,8 +80,8 @@ Future<List<Uri>> buildNativeAssetsIOS({
final List<Target> targets = darwinArchs.map(_getNativeTarget).toList();
final native_assets_cli.BuildMode buildModeCli = nativeAssetsBuildMode(buildMode);
const OS targetOs = OS.iOS;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
const OS targetOS = OS.iOS;
final Uri buildUri = nativeAssetsBuildUri(projectUri, targetOS);
final IOSSdk iosSdk = _getIOSSdk(environmentType);
globals.logger.printTrace('Building native assets for $targets $buildModeCli.');
......@@ -104,7 +104,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
globals.logger.printTrace('Building native assets for $targets done.');
final Map<AssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets);
await copyNativeAssetsMacOSHost(
buildUri_,
buildUri,
fatAssetTargetLocations,
codesignIdentity,
buildMode,
......
......@@ -2,9 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart' show BuildResult;
import 'package:native_assets_cli/native_assets_cli.dart' hide BuildMode;
import 'package:native_assets_cli/native_assets_cli.dart' as native_assets_cli;
import '../base/common.dart';
import '../base/file_system.dart';
......@@ -22,24 +20,14 @@ Future<Uri?> dryRunNativeAssetsLinux({
required Uri projectUri,
bool flutterTester = false,
required FileSystem fileSystem,
}) async {
if (!await nativeBuildRequired(buildRunner)) {
return null;
}
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.linux);
final Iterable<Asset> nativeAssetPaths = await dryRunNativeAssetsLinuxInternal(
fileSystem,
projectUri,
flutterTester,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
nativeAssetPaths,
buildUri_,
fileSystem,
}) {
return dryRunNativeAssetsSingleArchitecture(
buildRunner: buildRunner,
projectUri: projectUri,
flutterTester: flutterTester,
fileSystem: fileSystem,
os: OS.linux,
);
return nativeAssetsUri;
}
Future<Iterable<Asset>> dryRunNativeAssetsLinuxInternal(
......@@ -47,33 +35,16 @@ Future<Iterable<Asset>> dryRunNativeAssetsLinuxInternal(
Uri projectUri,
bool flutterTester,
NativeAssetsBuildRunner buildRunner,
) async {
const OS targetOs = OS.linux;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
globals.logger.printTrace('Dry running native assets for $targetOs.');
final List<Asset> nativeAssets = (await buildRunner.dryRun(
linkModePreference: LinkModePreference.dynamic,
targetOs: targetOs,
workingDirectory: projectUri,
includeParentEnvironment: true,
))
.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOs done.');
final Uri? absolutePath = flutterTester ? buildUri_ : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
) {
return dryRunNativeAssetsSingleArchitectureInternal(
fileSystem,
projectUri,
flutterTester,
buildRunner,
OS.linux,
);
}
/// Builds native assets.
///
/// If [targetPlatform] is omitted, the current target architecture is used.
///
/// If [flutterTester] is true, absolute paths are emitted in the native
/// assets mapping. This can be used for JIT mode without sandbox on the host.
/// This is used in `flutter test` and `flutter run -d flutter-tester`.
Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsLinux({
required NativeAssetsBuildRunner buildRunner,
TargetPlatform? targetPlatform,
......@@ -82,127 +53,16 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsLinux({
bool flutterTester = false,
Uri? yamlParentDirectory,
required FileSystem fileSystem,
}) async {
const OS targetOs = OS.linux;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
final Directory buildDir = fileSystem.directory(buildUri_);
if (!await buildDir.exists()) {
// CMake requires the folder to exist to do copying.
await buildDir.create(recursive: true);
}
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem);
return (nativeAssetsYaml, <Uri>[]);
}
final Target target = targetPlatform != null ? _getNativeTarget(targetPlatform) : Target.current;
final native_assets_cli.BuildMode buildModeCli = nativeAssetsBuildMode(buildMode);
globals.logger.printTrace('Building native assets for $target $buildModeCli.');
final BuildResult result = await buildRunner.build(
linkModePreference: LinkModePreference.dynamic,
target: target,
buildMode: buildModeCli,
workingDirectory: projectUri,
includeParentEnvironment: true,
cCompilerConfig: await buildRunner.cCompilerConfig,
);
final List<Asset> nativeAssets = result.assets;
final Set<Uri> dependencies = result.dependencies.toSet();
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $target done.');
final Uri? absolutePath = flutterTester ? buildUri_ : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
await _copyNativeAssetsLinux(
buildUri_,
assetTargetLocations,
buildMode,
fileSystem,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations.values,
yamlParentDirectory ?? buildUri_,
fileSystem,
}) {
return buildNativeAssetsSingleArchitecture(
buildRunner: buildRunner,
targetPlatform: targetPlatform,
projectUri: projectUri,
buildMode: buildMode,
flutterTester: flutterTester,
yamlParentDirectory: yamlParentDirectory,
fileSystem: fileSystem,
);
return (nativeAssetsUri, dependencies.toList());
}
Map<Asset, Asset> _assetTargetLocations(
List<Asset> nativeAssets,
Uri? absolutePath,
) =>
<Asset, Asset>{
for (final Asset asset in nativeAssets) asset: _targetLocationLinux(asset, absolutePath),
};
Asset _targetLocationLinux(Asset asset, Uri? absolutePath) {
final AssetPath path = asset.path;
switch (path) {
case AssetSystemPath _:
case AssetInExecutable _:
case AssetInProcess _:
return asset;
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
Uri uri;
if (absolutePath != null) {
// Flutter tester needs full host paths.
uri = absolutePath.resolve(fileName);
} else {
// Flutter Desktop needs "absolute" paths inside the app.
// "relative" in the context of native assets would be relative to the
// kernel or aot snapshot.
uri = Uri(path: fileName);
}
return asset.copyWith(path: AssetAbsolutePath(uri));
}
throw Exception('Unsupported asset path type ${path.runtimeType} in asset $asset');
}
/// Extract the [Target] from a [TargetPlatform].
Target _getNativeTarget(TargetPlatform targetPlatform) {
switch (targetPlatform) {
case TargetPlatform.linux_x64:
return Target.linuxX64;
case TargetPlatform.linux_arm64:
return Target.linuxArm64;
case TargetPlatform.android:
case TargetPlatform.ios:
case TargetPlatform.darwin:
case TargetPlatform.windows_x64:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
throw Exception('Unknown targetPlatform: $targetPlatform.');
}
}
Future<void> _copyNativeAssetsLinux(
Uri buildUri,
Map<Asset, Asset> assetTargetLocations,
BuildMode buildMode,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
globals.logger.printTrace('Copying native assets to ${buildUri.toFilePath()}.');
final Directory buildDir = fileSystem.directory(buildUri.toFilePath());
if (!buildDir.existsSync()) {
buildDir.createSync(recursive: true);
}
for (final MapEntry<Asset, Asset> assetMapping in assetTargetLocations.entries) {
final Uri source = (assetMapping.key.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as AssetAbsolutePath).uri;
final Uri targetUri = buildUri.resolveUri(target);
final String targetFullPath = targetUri.toFilePath();
await fileSystem.file(source).copy(targetFullPath);
}
globals.logger.printTrace('Copying native assets done.');
}
}
/// Flutter expects `clang++` to be on the path on Linux hosts.
......
......@@ -27,9 +27,9 @@ Future<Uri?> dryRunNativeAssetsMacOS({
return null;
}
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.macOS);
final Uri buildUri = nativeAssetsBuildUri(projectUri, OS.macOS);
final Iterable<Asset> nativeAssetPaths = await dryRunNativeAssetsMacOSInternal(fileSystem, projectUri, flutterTester, buildRunner);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(nativeAssetPaths, buildUri_, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(nativeAssetPaths, buildUri, fileSystem);
return nativeAssetsUri;
}
......@@ -39,20 +39,20 @@ Future<Iterable<Asset>> dryRunNativeAssetsMacOSInternal(
bool flutterTester,
NativeAssetsBuildRunner buildRunner,
) async {
const OS targetOs = OS.macOS;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
const OS targetOS = OS.macOS;
final Uri buildUri = nativeAssetsBuildUri(projectUri, targetOS);
globals.logger.printTrace('Dry running native assets for $targetOs.');
globals.logger.printTrace('Dry running native assets for $targetOS.');
final List<Asset> nativeAssets = (await buildRunner.dryRun(
linkModePreference: LinkModePreference.dynamic,
targetOs: targetOs,
targetOS: targetOS,
workingDirectory: projectUri,
includeParentEnvironment: true,
))
.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOs done.');
final Uri? absolutePath = flutterTester ? buildUri_ : null;
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
......@@ -75,10 +75,10 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
Uri? yamlParentDirectory,
required FileSystem fileSystem,
}) async {
const OS targetOs = OS.macOS;
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOs);
const OS targetOS = OS.macOS;
final Uri buildUri = nativeAssetsBuildUri(projectUri, targetOS);
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri_, fileSystem);
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri, fileSystem);
return (nativeAssetsYaml, <Uri>[]);
}
......@@ -102,11 +102,11 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
}
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Uri? absolutePath = flutterTester ? buildUri_ : null;
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
final Map<AssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets, absolutePath);
await copyNativeAssetsMacOSHost(buildUri_, fatAssetTargetLocations, codesignIdentity, buildMode, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(assetTargetLocations.values, yamlParentDirectory ?? buildUri_, fileSystem);
await copyNativeAssetsMacOSHost(buildUri, fatAssetTargetLocations, codesignIdentity, buildMode, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(assetTargetLocations.values, yamlParentDirectory ?? buildUri, fileSystem);
return (nativeAssetsUri, dependencies.toList());
}
......
......@@ -20,6 +20,7 @@ import '../linux/native_assets.dart';
import '../macos/native_assets.dart';
import '../native_assets.dart';
import '../project.dart';
import '../windows/native_assets.dart';
import 'test_time_recorder.dart';
/// A request to the [TestCompiler] for recompilation.
......@@ -195,6 +196,14 @@ class TestCompiler {
fileSystem: globals.fs,
buildRunner: buildRunner,
);
} else if (globals.platform.isWindows) {
(nativeAssetsYaml, _) = await buildNativeAssetsWindows(
buildMode: buildInfo.mode,
projectUri: projectUri,
flutterTester: true,
fileSystem: globals.fs,
buildRunner: buildRunner,
);
} else {
await ensureNoNativeAssetsOrOsIsSupported(
projectUri,
......
......@@ -18,6 +18,7 @@ import '../convert.dart';
import '../flutter_plugins.dart';
import '../globals.dart' as globals;
import '../migrations/cmake_custom_command_migration.dart';
import '../migrations/cmake_native_assets_migration.dart';
import 'migrations/build_architecture_migration.dart';
import 'migrations/show_window_migration.dart';
import 'migrations/version_migration.dart';
......@@ -61,6 +62,7 @@ Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {
final List<ProjectMigrator> migrators = <ProjectMigrator>[
CmakeCustomCommandMigration(windowsProject, globals.logger),
CmakeNativeAssetsMigration(windowsProject, 'windows', globals.logger),
VersionMigration(windowsProject, globals.logger),
ShowWindowMigration(windowsProject, globals.logger),
BuildArchitectureMigration(windowsProject, buildDirectory, globals.logger),
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:native_assets_cli/native_assets_cli.dart' hide BuildMode;
import '../base/file_system.dart';
import '../build_info.dart';
import '../globals.dart' as globals;
import '../native_assets.dart';
import 'visual_studio.dart';
/// Dry run the native builds.
///
/// This does not build native assets, it only simulates what the final paths
/// of all assets will be so that this can be embedded in the kernel file.
Future<Uri?> dryRunNativeAssetsWindows({
required NativeAssetsBuildRunner buildRunner,
required Uri projectUri,
bool flutterTester = false,
required FileSystem fileSystem,
}) {
return dryRunNativeAssetsSingleArchitecture(
buildRunner: buildRunner,
projectUri: projectUri,
flutterTester: flutterTester,
fileSystem: fileSystem,
os: OS.windows,
);
}
Future<Iterable<Asset>> dryRunNativeAssetsWindowsInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,
NativeAssetsBuildRunner buildRunner,
) {
return dryRunNativeAssetsSingleArchitectureInternal(
fileSystem,
projectUri,
flutterTester,
buildRunner,
OS.windows,
);
}
Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
buildNativeAssetsWindows({
required NativeAssetsBuildRunner buildRunner,
TargetPlatform? targetPlatform,
required Uri projectUri,
required BuildMode buildMode,
bool flutterTester = false,
Uri? yamlParentDirectory,
required FileSystem fileSystem,
}) {
return buildNativeAssetsSingleArchitecture(
buildRunner: buildRunner,
targetPlatform: targetPlatform,
projectUri: projectUri,
buildMode: buildMode,
flutterTester: flutterTester,
yamlParentDirectory: yamlParentDirectory,
fileSystem: fileSystem,
);
}
Future<CCompilerConfig> cCompilerConfigWindows() async {
final VisualStudio visualStudio = VisualStudio(
fileSystem: globals.fs,
platform: globals.platform,
logger: globals.logger,
processManager: globals.processManager,
);
return CCompilerConfig(
cc: _toOptionalFileUri(visualStudio.clPath),
ld: _toOptionalFileUri(visualStudio.linkPath),
ar: _toOptionalFileUri(visualStudio.libPath),
envScript: _toOptionalFileUri(visualStudio.vcvarsPath),
envScriptArgs: <String>[],
);
}
Uri? _toOptionalFileUri(String? string) {
if (string == null) {
return null;
}
return Uri.file(string);
}
......@@ -184,6 +184,60 @@ class VisualStudio {
}
}
/// The path to cl.exe, or null if no Visual Studio installation has
/// the components necessary to build.
String? get clPath {
return _getMsvcBinPath('cl.exe');
}
/// The path to lib.exe, or null if no Visual Studio installation has
/// the components necessary to build.
String? get libPath {
return _getMsvcBinPath('lib.exe');
}
/// The path to link.exe, or null if no Visual Studio installation has
/// the components necessary to build.
String? get linkPath {
return _getMsvcBinPath('link.exe');
}
String? _getMsvcBinPath(String executable) {
final VswhereDetails? details = _bestVisualStudioDetails;
if (details == null || !details.isUsable || details.installationPath == null || details.msvcVersion == null) {
return null;
}
return _fileSystem.path.joinAll(<String>[
details.installationPath!,
'VC',
'Tools',
'MSVC',
details.msvcVersion!,
'bin',
'Hostx64',
'x64',
executable,
]);
}
/// The path to vcvars64.exe, or null if no Visual Studio installation has
/// the components necessary to build.
String? get vcvarsPath {
final VswhereDetails? details = _bestVisualStudioDetails;
if (details == null || !details.isUsable || details.installationPath == null) {
return null;
}
return _fileSystem.path.joinAll(<String>[
details.installationPath!,
'VC',
'Auxiliary',
'Build',
'vcvars64.bat',
]);
}
/// The major version of the Visual Studio install, as an integer.
int? get _majorVersion => fullVersion != null ? int.tryParse(fullVersion!.split('.')[0]) : null;
......@@ -301,7 +355,12 @@ class VisualStudio {
if (whereResult.exitCode == 0) {
final List<Map<String, dynamic>>? installations = _tryDecodeVswhereJson(whereResult.stdout);
if (installations != null && installations.isNotEmpty) {
return VswhereDetails.fromJson(validateRequirements, installations[0]);
final String? msvcVersion = _findMsvcVersion(installations);
return VswhereDetails.fromJson(
validateRequirements,
installations[0],
msvcVersion,
);
}
}
} on ArgumentError {
......@@ -312,6 +371,28 @@ class VisualStudio {
return null;
}
String? _findMsvcVersion(List<Map<String, dynamic>> installations) {
final String? installationPath = installations[0]['installationPath'] as String?;
String? msvcVersion;
if (installationPath != null) {
final Directory installationDir = _fileSystem.directory(installationPath);
final Directory msvcDir = installationDir
.childDirectory('VC')
.childDirectory('Tools')
.childDirectory('MSVC');
if (msvcDir.existsSync()) {
final Iterable<Directory> msvcVersionDirs = msvcDir.listSync().whereType<Directory>();
if (msvcVersionDirs.isEmpty) {
return null;
}
msvcVersion = msvcVersionDirs.last.uri.pathSegments
.where((String e) => e.isNotEmpty)
.last;
}
}
return msvcVersion;
}
List<Map<String, dynamic>>? _tryDecodeVswhereJson(String vswhereJson) {
List<dynamic>? result;
FormatException? originalError;
......@@ -443,12 +524,14 @@ class VswhereDetails {
required this.isRebootRequired,
required this.isPrerelease,
required this.catalogDisplayVersion,
required this.msvcVersion,
});
/// Create a `VswhereDetails` from the JSON output of vswhere.exe.
factory VswhereDetails.fromJson(
bool meetsRequirements,
Map<String, dynamic> details
Map<String, dynamic> details,
String? msvcVersion,
) {
final Map<String, dynamic>? catalog = details['catalog'] as Map<String, dynamic>?;
......@@ -467,6 +550,8 @@ class VswhereDetails {
// contain replacement characters.
displayName: details['displayName'] as String?,
catalogDisplayVersion: catalog == null ? null : catalog['productDisplayVersion'] as String?,
msvcVersion: msvcVersion,
);
}
......@@ -511,6 +596,9 @@ class VswhereDetails {
/// The user-friendly version.
final String? catalogDisplayVersion;
/// The MSVC versions.
final String? msvcVersion;
/// Checks if the Visual Studio installation can be used by Flutter.
///
/// Returns false if the installation has issues the user must resolve.
......
......@@ -91,6 +91,12 @@ if(PLUGIN_BUNDLED_LIBRARIES)
COMPONENT Runtime)
endif()
# Copy the native assets provided by the build.dart from all packages.
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/")
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
......
......@@ -10,7 +10,7 @@ dependencies:
cli_config: ^0.1.1
logging: ^1.1.1
native_assets_cli: ^0.2.0
native_toolchain_c: ^0.2.0
native_toolchain_c: ^0.2.3
dev_dependencies:
ffi: ^2.0.2
......
......@@ -49,7 +49,7 @@ class FakeNativeAssetsBuildRunner implements NativeAssetsBuildRunner {
Future<native_assets_builder.DryRunResult> dryRun({
required bool includeParentEnvironment,
required LinkModePreference linkModePreference,
required OS targetOs,
required OS targetOS,
required Uri workingDirectory,
}) async {
dryRunInvocations++;
......
......@@ -142,6 +142,13 @@ void main() {
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Dry running native assets for ios.',
'Dry running native assets for ios done.',
]),
);
expect(
nativeAssetsYaml,
projectUri.resolve('build/native_assets/ios/native_assets.yaml'),
......@@ -259,13 +266,20 @@ void main() {
Asset(
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSArm64,
target: native_assets_cli.Target.iOSArm64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
),
],
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Building native assets for [ios_arm64] debug.',
'Building native assets for [ios_arm64] done.',
]),
);
expect(
environment.buildDir.childFile('native_assets.yaml'),
exists,
......
......@@ -180,6 +180,13 @@ void main() {
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Dry running native assets for linux.',
'Dry running native assets for linux done.',
]),
);
expect(
nativeAssetsYaml,
projectUri.resolve('build/native_assets/linux/native_assets.yaml'),
......@@ -222,6 +229,7 @@ void main() {
await packageConfig.parent.create();
await packageConfig.create();
final (Uri? nativeAssetsYaml, _) = await buildNativeAssetsLinux(
targetPlatform: TargetPlatform.linux_x64,
projectUri: projectUri,
buildMode: BuildMode.debug,
fileSystem: fileSystem,
......@@ -257,16 +265,14 @@ void main() {
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
ProcessManager: () => FakeProcessManager.empty(),
}, () async {
if (const LocalPlatform().isWindows) {
return; // Backslashes in commands, but we will never run these commands on Windows.
}
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
final File packageConfig = environment.projectDir.childDirectory('.dart_tool').childFile('package_config.json');
await packageConfig.parent.create();
await packageConfig.create();
final File dylibAfterCompiling = fileSystem.file('libbar.so');
// The mock doesn't create the file, so create it here.
await dylibAfterCompiling.create();
final (Uri? nativeAssetsYaml, _) = await buildNativeAssetsLinux(
targetPlatform: TargetPlatform.linux_x64,
projectUri: projectUri,
buildMode: BuildMode.debug,
fileSystem: fileSystem,
......@@ -287,6 +293,13 @@ void main() {
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Building native assets for linux_x64 debug.',
'Building native assets for linux_x64 done.',
]),
);
expect(
nativeAssetsYaml,
projectUri.resolve('build/native_assets/linux/native_assets.yaml'),
......@@ -297,7 +310,7 @@ void main() {
'package:bar/bar.dart',
if (flutterTester)
// Tests run on host system, so the have the full path on the system.
'- ${projectUri.resolve('/build/native_assets/linux/libbar.so').toFilePath()}'
'- ${projectUri.resolve('build/native_assets/linux/libbar.so').toFilePath()}'
else
// Apps are a bundle with the dylibs on their dlopen path.
'- libbar.so',
......@@ -365,7 +378,6 @@ void main() {
FileSystem: () => fileSystem,
}, () async {
if (!const LocalPlatform().isLinux) {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
return;
}
......
......@@ -161,6 +161,13 @@ void main() {
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Dry running native assets for macos.',
'Dry running native assets for macos done.',
]),
);
expect(
nativeAssetsYaml,
projectUri.resolve('build/native_assets/macos/native_assets.yaml'),
......@@ -291,6 +298,13 @@ void main() {
),
),
);
expect(
(globals.logger as BufferLogger).traceText,
stringContainsInOrder(<String>[
'Building native assets for [macos_arm64] debug.',
'Building native assets for [macos_arm64] done.',
]),
);
expect(
nativeAssetsYaml,
projectUri.resolve('build/native_assets/macos/native_assets.yaml'),
......@@ -301,7 +315,7 @@ void main() {
'package:bar/bar.dart',
if (flutterTester)
// Tests run on host system, so the have the full path on the system.
'- ${projectUri.resolve('/build/native_assets/macos/bar.dylib').toFilePath()}'
'- ${projectUri.resolve('build/native_assets/macos/bar.dylib').toFilePath()}'
else
// Apps are a bundle with the dylibs on their dlopen path.
'- bar.dylib',
......@@ -370,7 +384,6 @@ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault
),
}, () async {
if (!const LocalPlatform().isMacOS) {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
return;
}
......
......@@ -230,7 +230,7 @@ install(DIRECTORY "${NATIVE_ASSETS_DIR}"
expect(testLogger.statusText, isEmpty);
});
// TODO(dacoharkes): Add test for Windows when adding Windows support. https://github.com/flutter/flutter/issues/129757
for (final String os in <String>['linux', 'windows']) {
testWithoutContext('is migrated to copy native assets', () {
managedCmakeFile.writeAsStringSync(r'''
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
......@@ -251,36 +251,38 @@ install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
final CmakeNativeAssetsMigration cmakeProjectMigration = CmakeNativeAssetsMigration(
mockCmakeProject,
'linux',
os,
testLogger,
);
cmakeProjectMigration.migrate();
expect(managedCmakeFile.readAsStringSync(), r'''
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
install(FILES "${bundled_library}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
expect(managedCmakeFile.readAsStringSync(), '''
foreach(bundled_library \${PLUGIN_BUNDLED_LIBRARIES})
install(FILES "\${bundled_library}"
DESTINATION "\${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endforeach(bundled_library)
# Copy the native assets provided by the build.dart from all packages.
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/")
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
set(NATIVE_ASSETS_DIR "\${PROJECT_BUILD_DIR}native_assets/$os/")
install(DIRECTORY "\${NATIVE_ASSETS_DIR}"
DESTINATION "\${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
install(CODE "
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
file(REMOVE_RECURSE \\"\${INSTALL_BUNDLE_DATA_DIR}/\${FLUTTER_ASSET_DIR_NAME}\\")
" COMPONENT Runtime)
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
install(DIRECTORY "\${PROJECT_BUILD_DIR}/\${FLUTTER_ASSET_DIR_NAME}"
DESTINATION "\${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
''');
expect(testLogger.statusText, contains('CMake missing install() NATIVE_ASSETS_DIR command, updating.'));
expect(testLogger.statusText,
contains('CMake missing install() NATIVE_ASSETS_DIR command, updating.'));
});
}
});
});
}
......
......@@ -11,12 +11,16 @@ import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/windows/visual_studio.dart';
import '../../src/common.dart';
import '../../src/fake_process_manager.dart';
import '../../src/context.dart';
const String programFilesPath = r'C:\Program Files (x86)';
const String visualStudioPath = programFilesPath + r'\Microsoft Visual Studio\2017\Community';
const String cmakePath = visualStudioPath + r'\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe';
const String vswherePath = programFilesPath + r'\Microsoft Visual Studio\Installer\vswhere.exe';
const String clPath = visualStudioPath + r'\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64\cl.exe';
const String libPath = visualStudioPath + r'\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64\lib.exe';
const String linkPath = visualStudioPath + r'\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64\link.exe';
const String vcvarsPath = visualStudioPath + r'\VC\Auxiliary\Build\vcvars64.bat';
final Platform windowsPlatform = FakePlatform(
operatingSystem: 'windows',
......@@ -140,6 +144,7 @@ void setMockVswhereResponse(
]) {
fileSystem.file(vswherePath).createSync(recursive: true);
fileSystem.file(cmakePath).createSync(recursive: true);
fileSystem.file(clPath).createSync(recursive: true);
final String finalResponse = responseOverride
?? (response != null ? json.encode(<Map<String, dynamic>>[response]) : '[]');
final List<String> requirementArguments = requiredComponents == null
......@@ -776,6 +781,10 @@ void main() {
expect(visualStudio.hasNecessaryComponents, true);
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
});
testWithoutContext('Everything returns good values when Build Tools is present with all components', () {
......@@ -814,6 +823,10 @@ void main() {
expect(visualStudio.hasNecessaryComponents, true);
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 17 2022'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
});
testWithoutContext('Metadata is for compatible version when latest is missing components', () {
......@@ -905,8 +918,7 @@ void main() {
});
testWithoutContext('Ignores unicode replacement char in unused properties', () {
final Map<String, dynamic> response = Map<String, dynamic>.of(_defaultResponse)
..['unused'] = 'Bad UTF8 \u{FFFD}';
final Map<String, dynamic> response = Map<String, dynamic>.of(_defaultResponse)..['unused'] = 'Bad UTF8 \u{FFFD}';
setMockCompatibleVisualStudioInstallation(
response,
......@@ -919,6 +931,10 @@ void main() {
expect(visualStudio.hasNecessaryComponents, true);
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
});
testWithoutContext('Throws ToolExit on bad UTF-8 in installationPath', () {
......@@ -953,13 +969,16 @@ void main() {
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
expect(visualStudio.displayName, equals('\u{FFFD}'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
});
testWithoutContext("Ignores bad UTF-8 in catalog's productDisplayVersion", () {
final Map<String, dynamic> catalog = Map<String, dynamic>.of(_defaultResponse['catalog'] as Map<String, dynamic>)
..['productDisplayVersion'] = '\u{FFFD}';
final Map<String, dynamic> response = Map<String, dynamic>.of(_defaultResponse)
..['catalog'] = catalog;
final Map<String, dynamic> response = Map<String, dynamic>.of(_defaultResponse)..['catalog'] = catalog;
setMockCompatibleVisualStudioInstallation(response, fixture.fileSystem, fixture.processManager);
......@@ -969,6 +988,10 @@ void main() {
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
expect(visualStudio.displayVersion, equals('\u{FFFD}'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
});
testWithoutContext('Ignores malformed JSON in description property', () {
......@@ -987,6 +1010,10 @@ void main() {
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
expect(visualStudio.displayVersion, equals('16.2.5'));
expect(visualStudio.clPath, equals(clPath));
expect(visualStudio.libPath, equals(libPath));
expect(visualStudio.linkPath, equals(linkPath));
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
expect(fixture.logger.warningText, isEmpty);
});
......@@ -996,8 +1023,9 @@ void main() {
test('Accepts empty JSON', () {
const bool meetsRequirements = true;
final Map<String, dynamic> json = <String, dynamic>{};
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json, msvcVersion);
expect(result.installationPath, null);
expect(result.displayName, null);
......@@ -1015,8 +1043,9 @@ void main() {
final Map<String, dynamic> json = <String, dynamic>{
'hello': 'world',
};
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json, msvcVersion);
expect(result.installationPath, null);
expect(result.displayName, null);
......@@ -1031,8 +1060,9 @@ void main() {
test('Accepts JSON', () {
const bool meetsRequirements = true;
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, _defaultResponse);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, _defaultResponse, msvcVersion);
expect(result.installationPath, visualStudioPath);
expect(result.displayName, 'Visual Studio Community 2019');
......@@ -1047,38 +1077,39 @@ void main() {
test('Installation that does not satisfy requirements is not usable', () {
const bool meetsRequirements = false;
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, _defaultResponse);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, _defaultResponse, msvcVersion);
expect(result.isUsable, isFalse);
});
test('Incomplete installation is not usable', () {
const bool meetsRequirements = true;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)
..['isComplete'] = false;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)..['isComplete'] = false;
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json, msvcVersion);
expect(result.isUsable, isFalse);
});
test('Unlaunchable installation is not usable', () {
const bool meetsRequirements = true;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)
..['isLaunchable'] = false;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)..['isLaunchable'] = false;
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json, msvcVersion);
expect(result.isUsable, isFalse);
});
test('Installation that requires reboot is not usable', () {
const bool meetsRequirements = true;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)
..['isRebootRequired'] = true;
final Map<String, dynamic> json = Map<String, dynamic>.of(_defaultResponse)..['isRebootRequired'] = true;
const String msvcVersion = '';
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json);
final VswhereDetails result = VswhereDetails.fromJson(meetsRequirements, json, msvcVersion);
expect(result.isUsable, isFalse);
});
......
......@@ -58,8 +58,8 @@ const String packageName = 'package_with_native_assets';
const String exampleAppName = '${packageName}_example';
void main() {
if (!platform.isMacOS && !platform.isLinux) {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
if (!platform.isMacOS && !platform.isLinux && !platform.isWindows) {
// TODO(dacoharkes): Implement Fuchsia. https://github.com/flutter/flutter/issues/129757
return;
}
......@@ -146,9 +146,10 @@ void main() {
if (device == 'macos') {
expectDylibIsBundledMacOS(exampleDirectory, buildMode);
}
if (device == 'linux') {
} else if (device == 'linux') {
expectDylibIsBundledLinux(exampleDirectory, buildMode);
} else if (device == 'windows') {
expectDylibIsBundledWindows(exampleDirectory, buildMode);
}
if (device == hostOs) {
expectCCompilerIsConfigured(exampleDirectory);
......@@ -205,6 +206,8 @@ void main() {
expectDylibIsBundledIos(exampleDirectory, buildMode);
} else if (buildSubcommand == 'linux') {
expectDylibIsBundledLinux(exampleDirectory, buildMode);
} else if (buildSubcommand == 'windows') {
expectDylibIsBundledWindows(exampleDirectory, buildMode);
}
expectCCompilerIsConfigured(exampleDirectory);
});
......@@ -239,11 +242,15 @@ void main() {
'build',
buildSubcommand,
if (buildSubcommand == 'ios') '--no-codesign',
if (buildSubcommand == 'windows') '-v' // Requires verbose mode for error.
],
workingDirectory: exampleDirectory.path,
);
expect(result.exitCode, isNot(0));
expect(result.stderr, contains('link mode set to static, but this is not yet supported'));
expect(
(result.stdout as String) + (result.stderr as String),
contains('link mode set to static, but this is not yet supported'),
);
});
});
}
......@@ -301,14 +308,36 @@ void expectDylibIsBundledIos(Directory appDirectory, String buildMode) {
void expectDylibIsBundledLinux(Directory appDirectory, String buildMode) {
// Linux does not support cross compilation, so always only check current architecture.
final String architecture = Architecture.current.dartPlatform;
final Directory appBundle = appDirectory.childDirectory('build/$hostOs/$architecture/$buildMode/bundle/');
final Directory appBundle = appDirectory
.childDirectory('build')
.childDirectory(hostOs)
.childDirectory(architecture)
.childDirectory(buildMode)
.childDirectory('bundle');
expect(appBundle, exists);
final Directory dylibsFolder = appBundle.childDirectory('lib/');
final Directory dylibsFolder = appBundle.childDirectory('lib');
expect(dylibsFolder, exists);
final File dylib = dylibsFolder.childFile(OS.linux.dylibFileName(packageName));
expect(dylib, exists);
}
/// Checks that dylibs are bundled.
///
/// Sample path: build\windows\x64\runner\Debug\my_package_example.exe
void expectDylibIsBundledWindows(Directory appDirectory, String buildMode) {
// Linux does not support cross compilation, so always only check current architecture.
final String architecture = Architecture.current.dartPlatform;
final Directory appBundle = appDirectory
.childDirectory('build')
.childDirectory(hostOs)
.childDirectory(architecture)
.childDirectory('runner')
.childDirectory(buildMode.upperCaseFirst());
expect(appBundle, exists);
final File dylib = appBundle.childFile(OS.windows.dylibFileName(packageName));
expect(dylib, exists);
}
/// For `flutter build` we can't easily test whether running the app works.
/// Check that we have the dylibs in the app.
void expectDylibIsBundledWithFrameworks(Directory appDirectory, String buildMode, String os) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment