Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkgs/ffigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 21.0.0

- __Breaking change__: Remove deprecated `wrapperName` field from
`NativeExternalBindings`.

## 20.1.1

- Update tests and examples now that package:objective_c is using native assets.
Expand Down
2 changes: 0 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Library {
required List<Binding> bindings,
required Context context,
}) => Library(
name: context.config.wrapperName,
description: context.config.wrapperDocComment,
bindings: bindings,
header: context.config.preamble,
Expand All @@ -43,7 +42,6 @@ class Library {
);

factory Library({
required String name,
String? description,
required List<Binding> bindings,
String? header,
Expand Down
28 changes: 19 additions & 9 deletions pkgs/ffigen/lib/src/code_generator/objc_built_in_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// 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 'package:path/path.dart' as path;

import '../code_generator.dart';
import '../config_provider.dart';
import '../context.dart';
import '../visitor/ast.dart';

Expand All @@ -14,15 +17,12 @@ import 'writer.dart';

/// Built in functions used by the Objective C bindings.
class ObjCBuiltInFunctions {
ObjCBuiltInFunctions(
this.context,
this.wrapperName,
this.generateForPackageObjectiveC,
);
ObjCBuiltInFunctions(this.context, this.generateForPackageObjectiveC)
: libraryId = _libraryIdFromConfigHash(context.config);

final Context context;
final String wrapperName;
final bool generateForPackageObjectiveC;
final String libraryId;

static const registerName = ObjCImport('registerName');
static const getClass = ObjCImport('getClass');
Expand Down Expand Up @@ -149,9 +149,9 @@ class ObjCBuiltInFunctions {
ObjCBlockWrapperFuncs? getBlockTrampolines(ObjCBlock block) {
final (id, idHash) = _methodSigId(block.returnType, block.params);
return _blockTrampolines[id] ??= ObjCBlockWrapperFuncs(
_blockTrampolineFunc('_${wrapperName}_wrapListenerBlock_$idHash'),
_blockTrampolineFunc('_${libraryId}_wrapListenerBlock_$idHash'),
_blockTrampolineFunc(
'_${wrapperName}_wrapBlockingBlock_$idHash',
'_${libraryId}_wrapBlockingBlock_$idHash',
blocking: true,
),
);
Expand Down Expand Up @@ -191,7 +191,7 @@ class ObjCBuiltInFunctions {
final (id, idHash) = _methodSigId(block.returnType, block.params);
return _protocolTrampolines[id] ??= ObjCProtocolMethodTrampoline(
Func(
name: '_${wrapperName}_protocolTrampoline_$idHash',
name: '_${libraryId}_protocolTrampoline_$idHash',
returnType: block.returnType,
parameters: [
Parameter(
Expand All @@ -215,6 +215,16 @@ class ObjCBuiltInFunctions {
final baseType = type.typealiasType;
return baseType is ObjCNullable && baseType.child is ObjCInstanceType;
}

// A unique (but not human readable) ID for the generated library based on
// a hash of parts of the config.
static String _libraryIdFromConfigHash(Config config) => fnvHash32(
[
...config.entryPoints,
config.output,
config.outputObjC,
].map((uri) => path.basename(uri.toFilePath())).join('\n'),
).toRadixString(36);
}

/// A native trampoline function for a listener block.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/objc_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ ${generateInstanceMethodBindings(w, this)}
BindingString? toObjCBindingString(Writer w) {
if (generateAsStub) return null;

final wrapName = context.objCBuiltInFunctions.wrapperName;
final libraryId = context.objCBuiltInFunctions.libraryId;
final mainString =
'''

Protocol* _${wrapName}_$originalName(void) { return @protocol($originalName); }
Protocol* _${libraryId}_$originalName(void) { return @protocol($originalName); }
''';

return BindingString(
Expand Down
18 changes: 3 additions & 15 deletions pkgs/ffigen/lib/src/config_provider/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,7 @@ final class NativeExternalBindings implements BindingStyle {
/// If omitted, it will not be generated.
final String? assetId;

/// The prefix for the generated Objective-C functions.
// TODO(https://github.com/dart-lang/native/issues/2580): Remove this.
@Deprecated('Will be replaced by a hash.')
final String wrapperName;

const NativeExternalBindings({
this.assetId,
@Deprecated('Will be replaced by a hash.')
this.wrapperName = 'NativeLibrary',
});
const NativeExternalBindings({this.assetId});
}

/// Generate bindings which take a [DynamicLibrary] or [DynamicLibrary.lookup]
Expand Down Expand Up @@ -764,11 +755,8 @@ extension type Config(FfiGenerator ffiGen) implements FfiGenerator {
bool get useDartHandle => ffiGen.useDartHandle;
// ignore: deprecated_member_use_from_same_package
Map<String, ImportedType> get importedTypesByUsr => ffiGen.importedTypesByUsr;
String get wrapperName => switch (ffiGen.output.style) {
final DynamicLibraryBindings e => e.wrapperName,
// ignore: deprecated_member_use_from_same_package
final NativeExternalBindings e => e.wrapperName,
};
String get wrapperName =>
(ffiGen.output.style as DynamicLibraryBindings).wrapperName;

String? get wrapperDocComment => switch (ffiGen.output.style) {
final DynamicLibraryBindings e => e.wrapperDocComment,
Expand Down
6 changes: 1 addition & 5 deletions pkgs/ffigen/lib/src/config_provider/yaml_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1252,11 +1252,7 @@ final class YamlConfig {
preamble: preamble,
format: formatOutput,
style: ffiNativeConfig.enabled
? NativeExternalBindings(
assetId: ffiNativeConfig.assetId,
// ignore: deprecated_member_use_from_same_package
wrapperName: wrapperName,
)
? NativeExternalBindings(assetId: ffiNativeConfig.assetId)
: DynamicLibraryBindings(
wrapperName: wrapperName,
wrapperDocComment: wrapperDocComment,
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Context {
cursorIndex = CursorIndex(logger) {
objCBuiltInFunctions = ObjCBuiltInFunctions(
this,
config.wrapperName,
// ignore: deprecated_member_use_from_same_package
generator.objectiveC?.generateForPackageObjectiveC ?? false,
);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.

name: ffigen
version: 20.1.1
version: 21.0.0-wip
description: >
Generator for FFI bindings, using LibClang to parse C, Objective-C, and Swift
files.
Expand Down
15 changes: 0 additions & 15 deletions pkgs/ffigen/test/code_generator_tests/code_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void main() {
);
final library = Library(
context: nativeContext,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
Func(
Expand Down Expand Up @@ -131,7 +130,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
Struct(
Expand Down Expand Up @@ -218,7 +216,6 @@ void main() {
);
final library = Library(
context: context,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
structSome,
Expand Down Expand Up @@ -259,7 +256,6 @@ void main() {

final library = Library(
context: nativeContext,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
Global(
Expand Down Expand Up @@ -304,7 +300,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
bindings: transformBindings([
MacroConstant(name: 'test1', rawType: 'int', rawValue: '20'),
Expand All @@ -318,7 +313,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
bindings: transformBindings([
EnumClass(
Expand All @@ -339,7 +333,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
bindings: transformBindings([
EnumClass(
Expand Down Expand Up @@ -392,7 +385,6 @@ void main() {
);
final library = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
silenceEnumWarning: true,
bindings: transformBindings([
Expand Down Expand Up @@ -477,7 +469,6 @@ void main() {
);
final lib = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
silenceEnumWarning: true,
bindings: transformBindings([
Expand All @@ -502,7 +493,6 @@ void main() {
);
final library = Library(
context: context,
name: 'init_dylib',
header: '$licenseHeader\n// ignore_for_file: unused_element\n',
bindings: transformBindings([
Func(
Expand Down Expand Up @@ -558,7 +548,6 @@ void main() {
);
final library = Library(
context: context,
name: 'init_dylib',
header: '$licenseHeader\n',
bindings: transformBindings([
Func(
Expand All @@ -583,7 +572,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
Func(
Expand Down Expand Up @@ -612,7 +600,6 @@ void main() {
final context = makeContext();
final library = Library(
context: context,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
Struct(
Expand Down Expand Up @@ -700,7 +687,6 @@ void main() {
);
final library = Library(
context: context,
name: 'Bindings',
header: licenseHeader,
bindings: transformBindings([
struct1,
Expand Down Expand Up @@ -766,7 +752,6 @@ void main() {
final struct3Typealias = Typealias(name: 'Struct3Typealias', type: struct3);
final library = Library(
context: context,
name: 'Bindings',
header: '$licenseHeader\n',
bindings: transformBindings([
Typealias(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void main() {
);
final library = Library(
context: context,
name: 'Bindings',
bindings: transformBindings([
Struct(context: context, name: 'TestStruct'),
Struct(context: context, name: 'TestStruct'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void main() {
);
actual = Library(
context: context,
name: 'Bindings',
header: '// ignore_for_file: unused_element\n',
bindings: transformBindings([
Struct(context: context, name: 'addresses'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ Library expectedLibrary() {
final struct3 = Struct(context: context, name: 'Struct3');
return Library(
context: context,
name: 'Bindings',
bindings: parser.transformBindings([
struct1,
struct2,
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/test/header_parser_tests/globals_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ Library expectedLibrary() {
);
return Library(
context: context,
name: 'Bindings',
bindings: parser.transformBindings([
Global(type: BooleanType(), name: 'coolGlobal'),
Global(
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/test/header_parser_tests/macros_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ void main() {
Library expectedLibrary() {
return Library(
context: testContext(),
name: 'NativeLibrary',
bindings: [
Constant(name: 'TEST1', rawType: 'double', rawValue: '1.1'),
Constant(name: 'TEST2', rawType: 'int', rawValue: '10'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ Library expectedLibrary() {
);
return Library(
context: context,
name: 'Bindings',
bindings: [
unnamedInternalStruct,
unnamedUnion1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ ${strings.unnamedEnums}:
Library expectedLibrary() {
return Library(
context: testContext(),
name: 'Bindings',
bindings: [
Constant(name: 'A', rawType: 'int', rawValue: '1'),
Constant(name: 'C', rawType: 'int', rawValue: '3'),
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/test/rename_tests/rename_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ Library expectedLibrary() {
);
return Library(
context: context,
name: 'Bindings',
bindings: [
Func(
name: '${functionPrefix}func1',
Expand Down
Loading
Loading