Fix native assets failing to build with AGP 8.4 and multiple flavors (#155039)
The nativeAssetDir is not flavor specific and it should only be added to the jniLibs.scrDir once. Fixes https://github.com/flutter/flutter/issues/155038 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
@@ -1425,10 +1425,13 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copy the native assets created by build.dart and placed here by flutter assemble.
|
||||
String nativeAssetsDir = "${project.layout.buildDirectory.dir("../native_assets/android/jniLibs/lib").get()}/"
|
||||
project.android.sourceSets.main.jniLibs.srcDir(nativeAssetsDir)
|
||||
}
|
||||
// Copy the native assets created by build.dart and placed here by flutter assemble.
|
||||
// This path is not flavor specific and must only be added once.
|
||||
// If support for flavors is added to native assets, then they must only be added
|
||||
// once per flavor; see https://github.com/dart-lang/native/issues/1359.
|
||||
String nativeAssetsDir = "${project.buildDir}/../native_assets/android/jniLibs/lib/"
|
||||
project.android.sourceSets.main.jniLibs.srcDir(nativeAssetsDir)
|
||||
configurePlugins(project)
|
||||
detectLowCompileSdkVersionOrNdkVersion()
|
||||
return
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
// 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.
|
||||
|
||||
@Timeout(Duration(minutes: 10))
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file_testing/file_testing.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../test_utils.dart' show platform;
|
||||
import '../transition_test_utils.dart';
|
||||
import 'native_assets_test_utils.dart';
|
||||
|
||||
const String packageName = 'package_with_native_assets';
|
||||
|
||||
/// The AGP versions to run these tests against.
|
||||
final List<String> agpVersions = <String>[
|
||||
'8.4'
|
||||
];
|
||||
|
||||
/// The build modes to target for each flutter command that supports passing
|
||||
/// a build mode.
|
||||
///
|
||||
/// The flow of compiling kernel as well as bundling dylibs can differ based on
|
||||
/// build mode, so we should cover this.
|
||||
const List<String> buildModes = <String>[
|
||||
'debug',
|
||||
'profile',
|
||||
'release',
|
||||
];
|
||||
|
||||
void main() {
|
||||
if (!platform.isMacOS && !platform.isLinux && !platform.isWindows) {
|
||||
// TODO(dacoharkes): Implement Fuchsia. https://github.com/flutter/flutter/issues/129757
|
||||
return;
|
||||
}
|
||||
|
||||
setUpAll(() {
|
||||
processManager.runSync(<String>[
|
||||
flutterBin,
|
||||
'config',
|
||||
'--enable-native-assets',
|
||||
]);
|
||||
});
|
||||
|
||||
for (final String agpVersion in agpVersions) {
|
||||
for (final String buildMode in buildModes) {
|
||||
testWithoutContext('flutter build apk with native assets with build mode $buildMode and multiple flavors on AGP $agpVersion', () async {
|
||||
await inTempDir((Directory tempDirectory) async {
|
||||
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
||||
final Directory exampleDirectory = packageDirectory.childDirectory('example');
|
||||
|
||||
final File buildGradleFile = exampleDirectory.childDirectory('android').childFile('build.gradle');
|
||||
final File appBuildGradleFile = exampleDirectory.childDirectory('android').childDirectory('app').childFile('build.gradle');
|
||||
|
||||
expect(buildGradleFile, exists);
|
||||
expect(appBuildGradleFile, exists);
|
||||
|
||||
// Use expected AGP version.
|
||||
final String buildGradle = buildGradleFile.readAsStringSync();
|
||||
final RegExp androidPluginRegExp =
|
||||
RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
|
||||
final String newBuildGradle = buildGradle.replaceAll(
|
||||
androidPluginRegExp, 'com.android.tools.build:gradle:$agpVersion');
|
||||
buildGradleFile.writeAsStringSync(newBuildGradle);
|
||||
|
||||
// Use Android app with multiple flavors.
|
||||
final String appBuildGradle = appBuildGradleFile.readAsStringSync().replaceAll('\r\n', '\n');
|
||||
final RegExp buildTypesBlockRegExp = RegExp(r'buildTypes {\n[ \t]+release {((.|\n)*)\n[ \t]+}\n[ \t]+}');
|
||||
final String buildTypesBlock = buildTypesBlockRegExp.firstMatch(appBuildGradle)!.toString();
|
||||
final String appBuildGradleSegmentDefiningFlavors = '''
|
||||
$buildTypesBlock
|
||||
|
||||
flavorDimensions "mode"
|
||||
|
||||
productFlavors {
|
||||
flavorOne {}
|
||||
flavorTwo {}
|
||||
flavorThree {}
|
||||
}
|
||||
''';
|
||||
appBuildGradle.replaceFirst(
|
||||
buildTypesBlockRegExp, appBuildGradleSegmentDefiningFlavors);
|
||||
|
||||
final ProcessResult result = processManager.runSync(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'build',
|
||||
'apk',
|
||||
'--$buildMode',
|
||||
],
|
||||
workingDirectory: exampleDirectory.path,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
throw Exception('flutter build failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
||||
}
|
||||
|
||||
// Test that the native libraries are included as expected.
|
||||
final Directory nativeAssetsDir = exampleDirectory
|
||||
.childDirectory('build')
|
||||
.childDirectory('native_assets')
|
||||
.childDirectory('android')
|
||||
.childDirectory('jniLibs')
|
||||
.childDirectory('lib');
|
||||
expect(nativeAssetsDir, exists);
|
||||
|
||||
// We expect one subdirectory for each Android architecture.
|
||||
expect(nativeAssetsDir.listSync().length, 4);
|
||||
expect(nativeAssetsDir..childDirectory('armeabi-v7a'), exists);
|
||||
expect(nativeAssetsDir..childDirectory('x86'), exists);
|
||||
expect(nativeAssetsDir..childDirectory('arm64-v8a'), exists);
|
||||
expect(nativeAssetsDir..childDirectory('x86_64'), exists);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,9 @@ import 'package:flutter_tools/src/base/os.dart';
|
||||
import 'package:native_assets_cli/native_assets_cli_internal.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../test_utils.dart' show ProcessResultMatcher, fileSystem, platform;
|
||||
import '../test_utils.dart' show fileSystem, platform;
|
||||
import '../transition_test_utils.dart';
|
||||
import 'native_assets_test_utils.dart';
|
||||
|
||||
final String hostOs = platform.operatingSystem;
|
||||
|
||||
@@ -539,269 +540,3 @@ extension on String {
|
||||
return replaceFirst(this[0], this[0].toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
Future<Directory> createTestProject(String packageName, Directory tempDirectory) async {
|
||||
final ProcessResult result = processManager.runSync(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'create',
|
||||
'--no-pub',
|
||||
'--template=package_ffi',
|
||||
packageName,
|
||||
],
|
||||
workingDirectory: tempDirectory.path,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
throw Exception(
|
||||
'flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
|
||||
);
|
||||
}
|
||||
|
||||
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
|
||||
|
||||
// No platform-specific boilerplate files.
|
||||
expect(packageDirectory.childDirectory('android/'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('ios/'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('linux/'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('macos/'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('windows/'), isNot(exists));
|
||||
|
||||
await pinDependencies(packageDirectory.childFile('pubspec.yaml'));
|
||||
await pinDependencies(
|
||||
packageDirectory.childDirectory('example').childFile('pubspec.yaml'));
|
||||
|
||||
await addLinkHookDependency(packageDirectory);
|
||||
await addDynamicallyLinkedNativeLibrary(packageDirectory);
|
||||
|
||||
final ProcessResult result2 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'get',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result2, const ProcessResultMatcher());
|
||||
|
||||
return packageDirectory;
|
||||
}
|
||||
|
||||
Future<Directory> createTestProjectWithNoCBuild(String packageName, Directory tempDirectory) async {
|
||||
final ProcessResult result = processManager.runSync(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'create',
|
||||
'--no-pub',
|
||||
packageName,
|
||||
],
|
||||
workingDirectory: tempDirectory.path,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
throw Exception(
|
||||
'flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
|
||||
);
|
||||
}
|
||||
|
||||
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
|
||||
|
||||
final ProcessResult result2 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'add',
|
||||
'native_assets_cli',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result2, const ProcessResultMatcher());
|
||||
|
||||
await pinDependencies(packageDirectory.childFile('pubspec.yaml'));
|
||||
|
||||
final ProcessResult result3 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'get',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result3, const ProcessResultMatcher());
|
||||
|
||||
// Add build hook that does nothing to the package.
|
||||
final File buildHook = packageDirectory.childDirectory('hook').childFile('build.dart');
|
||||
buildHook.createSync(recursive: true);
|
||||
buildHook.writeAsStringSync('''
|
||||
import 'package:native_assets_cli/native_assets_cli.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
await build(args, (config, output) async {});
|
||||
}
|
||||
''');
|
||||
|
||||
return packageDirectory;
|
||||
}
|
||||
|
||||
Future<void> addLinkHookDependency(Directory packageDirectory) async {
|
||||
final Directory flutterDirectory = fileSystem.currentDirectory.parent.parent;
|
||||
final Directory linkHookDirectory = flutterDirectory
|
||||
.childDirectory('dev')
|
||||
.childDirectory('integration_tests')
|
||||
.childDirectory('link_hook');
|
||||
expect(linkHookDirectory, exists);
|
||||
|
||||
final File pubspecFile = packageDirectory.childFile('pubspec.yaml');
|
||||
final String pubspecOld =
|
||||
(await pubspecFile.readAsString()).replaceAll('\r\n', '\n');
|
||||
final String pubspecNew = pubspecOld.replaceFirst('''
|
||||
dependencies:
|
||||
''', '''
|
||||
dependencies:
|
||||
link_hook:
|
||||
path: ${linkHookDirectory.path}
|
||||
''');
|
||||
expect(pubspecNew, isNot(pubspecOld));
|
||||
await pubspecFile.writeAsString(pubspecNew);
|
||||
|
||||
final File dartFile =
|
||||
packageDirectory.childDirectory('lib').childFile('$packageName.dart');
|
||||
final String dartFileOld =
|
||||
(await dartFile.readAsString()).replaceAll('\r\n', '\n');
|
||||
// Replace with something that results in the same resulting int, so that the
|
||||
// tests don't have to be updated.
|
||||
final String dartFileNew = dartFileOld.replaceFirst(
|
||||
'''
|
||||
import '${packageName}_bindings_generated.dart' as bindings;
|
||||
''',
|
||||
'''
|
||||
import 'package:link_hook/link_hook.dart' as l;
|
||||
|
||||
import '${packageName}_bindings_generated.dart' as bindings;
|
||||
''',
|
||||
);
|
||||
expect(dartFileNew, isNot(dartFileOld));
|
||||
final String dartFileNew2 = dartFileNew.replaceFirst(
|
||||
'int sum(int a, int b) => bindings.sum(a, b);',
|
||||
'int sum(int a, int b) => bindings.sum(a, b) + l.difference(2, 1) - 1;',
|
||||
);
|
||||
expect(dartFileNew2, isNot(dartFileNew));
|
||||
await dartFile.writeAsString(dartFileNew2);
|
||||
}
|
||||
|
||||
/// Adds a native library to be built by the builder and dynamically link it to
|
||||
/// the main library.
|
||||
Future<void> addDynamicallyLinkedNativeLibrary(Directory packageDirectory) async {
|
||||
// Add linked library source files.
|
||||
final Directory srcDirectory = packageDirectory.childDirectory('src');
|
||||
final File linkedLibraryHeaderFile = srcDirectory.childFile('add.h');
|
||||
await linkedLibraryHeaderFile.writeAsString('''
|
||||
#include <stdint.h>
|
||||
|
||||
#if _WIN32
|
||||
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define FFI_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
FFI_PLUGIN_EXPORT intptr_t add(intptr_t a, intptr_t b);
|
||||
'''
|
||||
);
|
||||
final File linkedLibrarySourceFile = srcDirectory.childFile('add.c');
|
||||
await linkedLibrarySourceFile.writeAsString('''
|
||||
#include "add.h"
|
||||
|
||||
FFI_PLUGIN_EXPORT intptr_t add(intptr_t a, intptr_t b) {
|
||||
return a + b;
|
||||
}
|
||||
''');
|
||||
|
||||
// Update main library to include call to linked library.
|
||||
final File mainLibrarySourceFile = srcDirectory.childFile('$packageName.c');
|
||||
String mainLibrarySource = await mainLibrarySourceFile.readAsString();
|
||||
mainLibrarySource = mainLibrarySource.replaceFirst(
|
||||
'#include "$packageName.h"',
|
||||
'''
|
||||
#include "$packageName.h"
|
||||
#include "add.h"
|
||||
''',
|
||||
);
|
||||
mainLibrarySource = mainLibrarySource.replaceAll('a + b', 'add(a, b)');
|
||||
await mainLibrarySourceFile.writeAsString(mainLibrarySource);
|
||||
|
||||
// Update builder to build the native library and link it into the main library.
|
||||
const String builderSource = r'''
|
||||
import 'package:native_toolchain_c/native_toolchain_c.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:native_assets_cli/native_assets_cli.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
await build(args, (config, output) async {
|
||||
final packageName = config.packageName;
|
||||
|
||||
final builders = [
|
||||
CBuilder.library(
|
||||
name: 'add',
|
||||
assetName: 'add',
|
||||
sources: ['src/add.c'],
|
||||
),
|
||||
CBuilder.library(
|
||||
name: packageName,
|
||||
assetName: '${packageName}_bindings_generated.dart',
|
||||
sources: ['src/$packageName.c'],
|
||||
flags: config.dynamicLinkingFlags('add'),
|
||||
),
|
||||
];
|
||||
|
||||
final logger = Logger('')
|
||||
..level = Level.ALL
|
||||
..onRecord.listen((record) => print(record.message));
|
||||
|
||||
for (final builder in builders) {
|
||||
await builder.run(
|
||||
config: config,
|
||||
output: output,
|
||||
logger: logger,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
extension on BuildConfig {
|
||||
List<String> dynamicLinkingFlags(String libraryName) => switch (targetOS) {
|
||||
OS.macOS || OS.iOS => [
|
||||
'-L${outputDirectory.toFilePath()}',
|
||||
'-l$libraryName',
|
||||
],
|
||||
OS.linux || OS.android => [
|
||||
'-Wl,-rpath=\$ORIGIN/.',
|
||||
'-L${outputDirectory.toFilePath()}',
|
||||
'-l$libraryName',
|
||||
],
|
||||
OS.windows => [
|
||||
outputDirectory.resolve('$libraryName.lib').toFilePath()
|
||||
],
|
||||
_ => throw UnimplementedError('Unsupported OS: $targetOS'),
|
||||
};
|
||||
}
|
||||
''';
|
||||
|
||||
final Directory hookDirectory = packageDirectory.childDirectory('hook');
|
||||
final File builderFile = hookDirectory.childFile('build.dart');
|
||||
await builderFile.writeAsString(builderSource);
|
||||
}
|
||||
|
||||
Future<void> pinDependencies(File pubspecFile) async {
|
||||
expect(pubspecFile, exists);
|
||||
final String oldPubspec = await pubspecFile.readAsString();
|
||||
final String newPubspec = oldPubspec.replaceAll(RegExp(r':\s*\^'), ': ');
|
||||
expect(newPubspec, isNot(oldPubspec));
|
||||
await pubspecFile.writeAsString(newPubspec);
|
||||
}
|
||||
|
||||
Future<void> inTempDir(Future<void> Function(Directory tempDirectory) fun) async {
|
||||
final Directory tempDirectory = fileSystem.directory(fileSystem.systemTempDirectory.createTempSync().resolveSymbolicLinksSync());
|
||||
try {
|
||||
await fun(tempDirectory);
|
||||
} finally {
|
||||
tryToDelete(tempDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
// 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 'dart:io';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file_testing/file_testing.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../test_utils.dart' show ProcessResultMatcher, fileSystem;
|
||||
import '../transition_test_utils.dart';
|
||||
|
||||
Future<Directory> createTestProject(String packageName, Directory tempDirectory) async {
|
||||
final ProcessResult result = processManager.runSync(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'create',
|
||||
'--no-pub',
|
||||
'--template=package_ffi',
|
||||
packageName,
|
||||
],
|
||||
workingDirectory: tempDirectory.path,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
throw Exception(
|
||||
'flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
|
||||
);
|
||||
}
|
||||
|
||||
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
|
||||
|
||||
// No platform-specific boilerplate files.
|
||||
expect(packageDirectory.childDirectory('android'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('ios'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('linux'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('macos'), isNot(exists));
|
||||
expect(packageDirectory.childDirectory('windows'), isNot(exists));
|
||||
|
||||
await pinDependencies(packageDirectory.childFile('pubspec.yaml'));
|
||||
await pinDependencies(
|
||||
packageDirectory.childDirectory('example').childFile('pubspec.yaml'));
|
||||
|
||||
await addLinkHookDependency(packageName, packageDirectory);
|
||||
await addDynamicallyLinkedNativeLibrary(packageName, packageDirectory);
|
||||
|
||||
final ProcessResult result2 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'get',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result2, const ProcessResultMatcher());
|
||||
|
||||
return packageDirectory;
|
||||
}
|
||||
|
||||
Future<Directory> createTestProjectWithNoCBuild(String packageName, Directory tempDirectory) async {
|
||||
final ProcessResult result = processManager.runSync(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'create',
|
||||
'--no-pub',
|
||||
packageName,
|
||||
],
|
||||
workingDirectory: tempDirectory.path,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
throw Exception(
|
||||
'flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
|
||||
);
|
||||
}
|
||||
|
||||
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
|
||||
|
||||
final ProcessResult result2 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'add',
|
||||
'native_assets_cli',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result2, const ProcessResultMatcher());
|
||||
|
||||
await pinDependencies(packageDirectory.childFile('pubspec.yaml'));
|
||||
|
||||
final ProcessResult result3 = await processManager.run(
|
||||
<String>[
|
||||
flutterBin,
|
||||
'pub',
|
||||
'get',
|
||||
],
|
||||
workingDirectory: packageDirectory.path,
|
||||
);
|
||||
expect(result3, const ProcessResultMatcher());
|
||||
|
||||
// Add build hook that does nothing to the package.
|
||||
final File buildHook = packageDirectory.childDirectory('hook').childFile('build.dart');
|
||||
buildHook.createSync(recursive: true);
|
||||
buildHook.writeAsStringSync('''
|
||||
import 'package:native_assets_cli/native_assets_cli.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
await build(args, (config, output) async {});
|
||||
}
|
||||
''');
|
||||
|
||||
return packageDirectory;
|
||||
}
|
||||
|
||||
Future<void> addLinkHookDependency(String packageName, Directory packageDirectory) async {
|
||||
final Directory flutterDirectory = fileSystem.currentDirectory.parent.parent;
|
||||
final Directory linkHookDirectory = flutterDirectory
|
||||
.childDirectory('dev')
|
||||
.childDirectory('integration_tests')
|
||||
.childDirectory('link_hook');
|
||||
expect(linkHookDirectory, exists);
|
||||
|
||||
final File pubspecFile = packageDirectory.childFile('pubspec.yaml');
|
||||
final String pubspecOld =
|
||||
(await pubspecFile.readAsString()).replaceAll('\r\n', '\n');
|
||||
final String pubspecNew = pubspecOld.replaceFirst('''
|
||||
dependencies:
|
||||
''', '''
|
||||
dependencies:
|
||||
link_hook:
|
||||
path: ${linkHookDirectory.path}
|
||||
''');
|
||||
expect(pubspecNew, isNot(pubspecOld));
|
||||
await pubspecFile.writeAsString(pubspecNew);
|
||||
|
||||
final File dartFile =
|
||||
packageDirectory.childDirectory('lib').childFile('$packageName.dart');
|
||||
final String dartFileOld =
|
||||
(await dartFile.readAsString()).replaceAll('\r\n', '\n');
|
||||
// Replace with something that results in the same resulting int, so that the
|
||||
// tests don't have to be updated.
|
||||
final String dartFileNew = dartFileOld.replaceFirst(
|
||||
'''
|
||||
import '${packageName}_bindings_generated.dart' as bindings;
|
||||
''',
|
||||
'''
|
||||
import 'package:link_hook/link_hook.dart' as l;
|
||||
|
||||
import '${packageName}_bindings_generated.dart' as bindings;
|
||||
''',
|
||||
);
|
||||
expect(dartFileNew, isNot(dartFileOld));
|
||||
final String dartFileNew2 = dartFileNew.replaceFirst(
|
||||
'int sum(int a, int b) => bindings.sum(a, b);',
|
||||
'int sum(int a, int b) => bindings.sum(a, b) + l.difference(2, 1) - 1;',
|
||||
);
|
||||
expect(dartFileNew2, isNot(dartFileNew));
|
||||
await dartFile.writeAsString(dartFileNew2);
|
||||
}
|
||||
|
||||
/// Adds a native library to be built by the builder and dynamically link it to
|
||||
/// the main library.
|
||||
Future<void> addDynamicallyLinkedNativeLibrary(String packageName, Directory packageDirectory) async {
|
||||
// Add linked library source files.
|
||||
final Directory srcDirectory = packageDirectory.childDirectory('src');
|
||||
final File linkedLibraryHeaderFile = srcDirectory.childFile('add.h');
|
||||
await linkedLibraryHeaderFile.writeAsString('''
|
||||
#include <stdint.h>
|
||||
|
||||
#if _WIN32
|
||||
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define FFI_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
FFI_PLUGIN_EXPORT intptr_t add(intptr_t a, intptr_t b);
|
||||
'''
|
||||
);
|
||||
final File linkedLibrarySourceFile = srcDirectory.childFile('add.c');
|
||||
await linkedLibrarySourceFile.writeAsString('''
|
||||
#include "add.h"
|
||||
|
||||
FFI_PLUGIN_EXPORT intptr_t add(intptr_t a, intptr_t b) {
|
||||
return a + b;
|
||||
}
|
||||
''');
|
||||
|
||||
// Update main library to include call to linked library.
|
||||
final File mainLibrarySourceFile = srcDirectory.childFile('$packageName.c');
|
||||
String mainLibrarySource = await mainLibrarySourceFile.readAsString();
|
||||
mainLibrarySource = mainLibrarySource.replaceFirst(
|
||||
'#include "$packageName.h"',
|
||||
'''
|
||||
#include "$packageName.h"
|
||||
#include "add.h"
|
||||
''',
|
||||
);
|
||||
mainLibrarySource = mainLibrarySource.replaceAll('a + b', 'add(a, b)');
|
||||
await mainLibrarySourceFile.writeAsString(mainLibrarySource);
|
||||
|
||||
// Update builder to build the native library and link it into the main library.
|
||||
const String builderSource = r'''
|
||||
import 'package:native_toolchain_c/native_toolchain_c.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:native_assets_cli/native_assets_cli.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
await build(args, (config, output) async {
|
||||
final packageName = config.packageName;
|
||||
|
||||
final builders = [
|
||||
CBuilder.library(
|
||||
name: 'add',
|
||||
assetName: 'add',
|
||||
sources: ['src/add.c'],
|
||||
),
|
||||
CBuilder.library(
|
||||
name: packageName,
|
||||
assetName: '${packageName}_bindings_generated.dart',
|
||||
sources: ['src/$packageName.c'],
|
||||
flags: config.dynamicLinkingFlags('add'),
|
||||
),
|
||||
];
|
||||
|
||||
final logger = Logger('')
|
||||
..level = Level.ALL
|
||||
..onRecord.listen((record) => print(record.message));
|
||||
|
||||
for (final builder in builders) {
|
||||
await builder.run(
|
||||
config: config,
|
||||
output: output,
|
||||
logger: logger,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
extension on BuildConfig {
|
||||
List<String> dynamicLinkingFlags(String libraryName) => switch (targetOS) {
|
||||
OS.macOS || OS.iOS => [
|
||||
'-L${outputDirectory.toFilePath()}',
|
||||
'-l$libraryName',
|
||||
],
|
||||
OS.linux || OS.android => [
|
||||
'-Wl,-rpath=\$ORIGIN/.',
|
||||
'-L${outputDirectory.toFilePath()}',
|
||||
'-l$libraryName',
|
||||
],
|
||||
OS.windows => [
|
||||
outputDirectory.resolve('$libraryName.lib').toFilePath()
|
||||
],
|
||||
_ => throw UnimplementedError('Unsupported OS: $targetOS'),
|
||||
};
|
||||
}
|
||||
''';
|
||||
|
||||
final Directory hookDirectory = packageDirectory.childDirectory('hook');
|
||||
final File builderFile = hookDirectory.childFile('build.dart');
|
||||
await builderFile.writeAsString(builderSource);
|
||||
}
|
||||
|
||||
Future<void> pinDependencies(File pubspecFile) async {
|
||||
expect(pubspecFile, exists);
|
||||
final String oldPubspec = await pubspecFile.readAsString();
|
||||
final String newPubspec = oldPubspec.replaceAll(RegExp(r':\s*\^'), ': ');
|
||||
expect(newPubspec, isNot(oldPubspec));
|
||||
await pubspecFile.writeAsString(newPubspec);
|
||||
}
|
||||
|
||||
|
||||
Future<void> inTempDir(Future<void> Function(Directory tempDirectory) fun) async {
|
||||
final Directory tempDirectory = fileSystem.directory(fileSystem.systemTempDirectory.createTempSync().resolveSymbolicLinksSync());
|
||||
try {
|
||||
await fun(tempDirectory);
|
||||
} finally {
|
||||
tryToDelete(tempDirectory);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user