Skip to content

Commit

Permalink
Revert license header change in test
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 8, 2024
1 parent ba586af commit 3c5bb80
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 71 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ffigen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: dev #3.2.0
sdk: dev #3.3.0
- name: Install dependencies
run: dart pub get
- name: Install libclang-14-dev
Expand All @@ -78,7 +78,7 @@ jobs:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: dev #3.2.0
sdk: dev #3.3.0
- name: Install dependencies
run: dart pub get
- name: Build test dylib and bindings
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: dev #3.2.0
sdk: dev #3.3.0
- name: Install dependencies
run: dart pub get
- name: Build test dylib and bindings
Expand Down
27 changes: 11 additions & 16 deletions pkgs/ffigen/example/ffinative/lib/generated_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,56 @@
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
@ffi.DefaultAsset('package:ffinative_example/generated_bindings.dart')
library;

import 'dart:ffi' as ffi;
import '' as self;

/// Adds 2 integers.
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>()
external int sum(
int a,
int b,
);

/// Subtracts 2 integers.
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>()
external int subtract(
int a,
int b,
);

/// Multiplies 2 integers, returns pointer to an integer,.
@ffi.Native<ffi.Pointer<ffi.Int> Function(ffi.Int, ffi.Int)>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Pointer<ffi.Int> Function(ffi.Int, ffi.Int)>()
external ffi.Pointer<ffi.Int> multiply(
int a,
int b,
);

/// Divides 2 integers, returns pointer to a float.
@ffi.Native<ffi.Pointer<ffi.Float> Function(ffi.Int, ffi.Int)>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Pointer<ffi.Float> Function(ffi.Int, ffi.Int)>()
external ffi.Pointer<ffi.Float> divide(
int a,
int b,
);

/// Divides 2 floats, returns a pointer to double.
@ffi.Native<ffi.Pointer<ffi.Double> Function(ffi.Float, ffi.Float)>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Pointer<ffi.Double> Function(ffi.Float, ffi.Float)>()
external ffi.Pointer<ffi.Double> dividePrecision(
double a,
double b,
);

@ffi.Native<ffi.Int>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Int>()
external int log_level;

@ffi.Array.multi([5])
@ffi.Native<ffi.Array<ffi.Int>>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Array<ffi.Int>>()
external ffi.Array<ffi.Int> array;

/// Version of the native C library
@ffi.Native<ffi.Pointer<ffi.Char>>(
assetId: 'package:ffinative_example/generated_bindings.dart')
@ffi.Native<ffi.Pointer<ffi.Char>>()
external final ffi.Pointer<ffi.Char> library_version;

const addresses = _SymbolAddresses();
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/lib/src/code_generator/func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ ${makeNativeAnnotation(
nativeType: cType,
dartName: nativeFuncName,
nativeSymbolName: originalName,
assetId: ffiNativeConfig.assetId,
isLeaf: isLeaf,
)}
external $ffiReturnType $nativeFuncName($ffiArgDeclString);
Expand Down
1 change: 0 additions & 1 deletion pkgs/ffigen/lib/src/code_generator/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Global extends LookUpBinding {
..writeln(makeNativeAnnotation(
w,
nativeType: cType,
assetId: nativeConfig.assetId,
dartName: globalVarName,
nativeSymbolName: originalName,
isLeaf: false,
Expand Down
15 changes: 11 additions & 4 deletions pkgs/ffigen/lib/src/code_generator/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,20 @@ class Library {
// Seperate bindings which require lookup.
final lookupBindings = <LookUpBinding>[];
final nativeBindings = <LookUpBinding>[];
FfiNativeConfig? nativeConfig;

for (final binding in this.bindings.whereType<LookUpBinding>()) {
final usesLookup = switch (binding) {
Func() => !binding.ffiNativeConfig.enabled,
Global() => !binding.nativeConfig.enabled,
_ => true,
final nativeConfigForBinding = switch (binding) {
Func() => binding.ffiNativeConfig,
Global() => binding.nativeConfig,
_ => null,
};

// At the moment, all bindings share their native config.
nativeConfig ??= nativeConfigForBinding;

final usesLookup =
nativeConfigForBinding == null || !nativeConfigForBinding.enabled;
(usesLookup ? lookupBindings : nativeBindings).add(binding);
}
final noLookUpBindings =
Expand All @@ -72,6 +78,7 @@ class Library {
_writer = Writer(
lookUpBindings: lookupBindings,
ffiNativeBindings: nativeBindings,
nativeAssetId: nativeConfig?.assetId,
noLookUpBindings: noLookUpBindings,
className: name,
classDocComment: description,
Expand Down
4 changes: 0 additions & 4 deletions pkgs/ffigen/lib/src/code_generator/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,12 @@ String makeNativeAnnotation(
required String? nativeType,
required String dartName,
required String nativeSymbolName,
String? assetId,
bool isLeaf = false,
}) {
final args = <(String, String)>[];
if (dartName != nativeSymbolName) {
args.add(('symbol', '"$nativeSymbolName"'));
}
if (assetId != null) {
args.add(('assetId', "'$assetId'"));
}
if (isLeaf) {
args.add(('isLeaf', 'true'));
}
Expand Down
15 changes: 15 additions & 0 deletions pkgs/ffigen/lib/src/code_generator/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Writer {
/// Holds bindings which don't lookup symbols.
final List<Binding> noLookUpBindings;

/// The default asset id to use for [ffiNativeBindings].
final String? nativeAssetId;

/// Manages the `_SymbolAddress` class.
final symbolAddressWriter = SymbolAddressWriter();

Expand Down Expand Up @@ -99,6 +102,7 @@ class Writer {
required this.ffiNativeBindings,
required this.noLookUpBindings,
required String className,
required this.nativeAssetId,
Set<LibraryImport>? additionalImports,
this.classDocComment,
this.header,
Expand Down Expand Up @@ -224,6 +228,17 @@ class Writer {
result.write(makeDoc('ignore_for_file: type=lint'));
}

// If there are any @Native bindings, the file needs to have an
// `@DefaultAsset` annotation for the symbols to resolve properly. This
// avoids duplicating the asset on every element.
// Since the annotation goes on a `library;` directive, it needs to appear
// before other definitions in the file.
if (ffiNativeBindings.isNotEmpty && nativeAssetId != null) {
result
..writeln("@$ffiLibraryPrefix.DefaultAsset('$nativeAssetId')")
..writeln('library;\n');
}

/// Write [lookUpBindings].
if (lookUpBindings.isNotEmpty) {
// Write doc comment for wrapper class.
Expand Down
30 changes: 16 additions & 14 deletions pkgs/ffigen/test/code_generator_tests/code_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,38 @@ import '../test_utils.dart';

void main() {
const licenseHeader = '''
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
''';

group('code_generator: ', () {
@isTestGroup
void withAndWithoutNative(String description, void Function(bool) runTest) {
void withAndWithoutNative(
String description, void Function(FfiNativeConfig) runTest) {
group(description, () {
test('without Native', () => runTest(false));
test('with Native', () => runTest(true));
test('without Native', () => runTest(FfiNativeConfig(enabled: false)));
test('with Native',
() => runTest(FfiNativeConfig(enabled: true, assetId: 'test')));
});
}

withAndWithoutNative('Function Binding (primitives, pointers)',
(enableFfiNative) {
(nativeConfig) {
final library = Library(
name: 'Bindings',
header: licenseHeader,
bindings: [
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
ffiNativeConfig: nativeConfig,
name: 'noParam',
dartDoc: 'Just a test function\nheres another line',
returnType: NativeType(
SupportedNativeType.Int32,
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
ffiNativeConfig: nativeConfig,
name: 'withPrimitiveParam',
parameters: [
Parameter(
Expand All @@ -61,7 +63,7 @@ void main() {
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
ffiNativeConfig: nativeConfig,
name: 'withPointerParam',
parameters: [
Parameter(
Expand Down Expand Up @@ -90,7 +92,7 @@ void main() {
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
ffiNativeConfig: nativeConfig,
isLeaf: true,
name: 'leafFunc',
dartDoc: 'A function with isLeaf: true',
Expand All @@ -109,7 +111,8 @@ void main() {
],
);

_matchLib(library, enableFfiNative ? 'function_ffiNative' : 'function');
_matchLib(
library, nativeConfig.enabled ? 'function_ffiNative' : 'function');
});

test('Struct Binding (primitives, pointers)', () {
Expand Down Expand Up @@ -254,12 +257,11 @@ void main() {
});

withAndWithoutNative('global (primitives, pointers, pointer to struct)',
(enableNative) {
(nativeConfig) {
final structSome = Struct(
name: 'Some',
);
final emptyGlobalStruct = Struct(name: 'EmptyStruct');
final nativeConfig = FfiNativeConfig(enabled: enableNative);

final library = Library(
name: 'Bindings',
Expand Down Expand Up @@ -290,7 +292,7 @@ void main() {
NativeType(
SupportedNativeType.Float,
),
useArrayType: enableNative,
useArrayType: nativeConfig.enabled,
),
constant: true,
),
Expand All @@ -310,7 +312,7 @@ void main() {
),
],
);
_matchLib(library, enableNative ? 'global_native' : 'global');
_matchLib(library, nativeConfig.enabled ? 'global_native' : 'global');
});

test('constant', () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
@ffi.DefaultAsset('test')
library;

import 'dart:ffi' as ffi;

/// Just a test function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
@ffi.DefaultAsset('test')
library;

import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int32>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
Loading

0 comments on commit 3c5bb80

Please sign in to comment.