Skip to content

Commit 6a2c865

Browse files
authored
Merge branch 'main' into ffigen_cleanup_3
2 parents efd5fbf + d754fff commit 6a2c865

19 files changed

+103
-145
lines changed

pkgs/ffigen/lib/src/code_generator/library.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:yaml_edit/yaml_edit.dart';
88

99
import '../code_generator.dart';
1010
import '../code_generator/utils.dart';
11-
import '../config_provider/config_types.dart';
11+
import '../config_provider/config.dart';
1212
import '../context.dart';
1313

1414
import 'writer.dart';
@@ -27,15 +27,19 @@ class Library {
2727
required List<Binding> bindings,
2828
required Context context,
2929
}) => Library(
30-
description: context.config.wrapperDocComment,
30+
description: switch (context.config.output.style) {
31+
final DynamicLibraryBindings e => e.wrapperDocComment,
32+
_ => null,
33+
},
3134
bindings: bindings,
32-
header: context.config.preamble,
35+
header: context.config.output.preamble,
3336
generateForPackageObjectiveC:
3437
// ignore: deprecated_member_use_from_same_package
3538
context.config.objectiveC?.generateForPackageObjectiveC ?? false,
36-
libraryImports: context.config.libraryImports.values.toList(),
39+
// ignore: deprecated_member_use_from_same_package
40+
libraryImports: context.config.libraryImports,
3741
silenceEnumWarning: context.config.enums.silenceWarning,
38-
nativeEntryPoints: context.config.entryPoints
42+
nativeEntryPoints: context.config.headers.entryPoints
3943
.map((uri) => uri.toFilePath())
4044
.toList(),
4145
context: context,
@@ -54,13 +58,18 @@ class Library {
5458
// Seperate bindings which require lookup.
5559
final lookupBindings = <LookUpBinding>[];
5660
final nativeBindings = <LookUpBinding>[];
57-
FfiNativeConfig? nativeConfig;
61+
String? nativeAssetId;
62+
63+
final outputStyle = context.config.output.style;
64+
final outputStyleAssetId = outputStyle is NativeExternalBindings
65+
? outputStyle.assetId
66+
: null;
5867

5968
for (final binding in bindings.whereType<LookUpBinding>()) {
6069
final loadFromNativeAsset = binding.loadFromNativeAsset;
6170

6271
// At the moment, all bindings share their native config.
63-
if (loadFromNativeAsset) nativeConfig = context.config.ffiNativeConfig;
72+
if (loadFromNativeAsset) nativeAssetId = outputStyleAssetId;
6473

6574
(loadFromNativeAsset ? nativeBindings : lookupBindings).add(binding);
6675
}
@@ -69,7 +78,7 @@ class Library {
6978
final writer = Writer(
7079
lookUpBindings: lookupBindings,
7180
ffiNativeBindings: nativeBindings,
72-
nativeAssetId: nativeConfig?.assetId,
81+
nativeAssetId: nativeAssetId,
7382
noLookUpBindings: noLookUpBindings,
7483
classDocComment: description,
7584
header: header,

pkgs/ffigen/lib/src/code_generator/objc_built_in_functions.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ class ObjCBuiltInFunctions {
220220
// a hash of parts of the config.
221221
static String _libraryIdFromConfigHash(Config config) => fnvHash32(
222222
[
223-
...config.entryPoints,
224-
config.output,
225-
config.outputObjC,
223+
...config.headers.entryPoints,
224+
config.output.dartFile,
225+
config.output.objCFile,
226226
].map((uri) => path.basename(uri.toFilePath())).join('\n'),
227227
).toRadixString(36);
228228
}

pkgs/ffigen/lib/src/config_provider/config.dart

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,7 @@ final class Output {
647647
/// The output Objective-C file for the generated Objective-C bindings.
648648
final Uri? objectiveCFile;
649649

650-
Uri get _objectiveCFile =>
651-
objectiveCFile ?? Uri.file('${dartFile.toFilePath()}.m');
650+
Uri get objCFile => objectiveCFile ?? Uri.file('${dartFile.toFilePath()}.m');
652651

653652
/// The config for the symbol file.
654653
final SymbolFile? symbolFile;
@@ -717,74 +716,8 @@ final class DynamicLibraryBindings implements BindingStyle {
717716
}
718717

719718
extension type Config(FfiGenerator ffiGen) implements FfiGenerator {
720-
ObjectiveC get _objectiveC => ffiGen.objectiveC ?? const ObjectiveC();
721-
bool get includeTransitiveObjCInterfaces =>
722-
_objectiveC.interfaces.includeTransitive;
723-
bool get includeTransitiveObjCProtocols =>
724-
_objectiveC.protocols.includeTransitive;
725-
bool get includeTransitiveObjCCategories =>
726-
_objectiveC.categories.includeTransitive;
727-
String? Function(Declaration declaration) get interfaceModule =>
728-
(declaration) => _objectiveC.interfaces.module(declaration);
729-
String? Function(Declaration declaration) get protocolModule =>
730-
(declaration) => _objectiveC.protocols.module(declaration);
731-
bool get generateForPackageObjectiveC =>
732-
// ignore: deprecated_member_use_from_same_package
733-
_objectiveC.generateForPackageObjectiveC;
734-
Categories get objcCategories => _objectiveC.categories;
735-
Interfaces get objcInterfaces => _objectiveC.interfaces;
736-
Protocols get objcProtocols => _objectiveC.protocols;
737-
ExternalVersions get externalVersions => _objectiveC.externalVersions;
738719
// ignore: deprecated_member_use_from_same_package
739720
Map<String, ImportedType> get importedTypesByUsr => ffiGen.importedTypesByUsr;
740-
String get wrapperName =>
741-
(ffiGen.output.style as DynamicLibraryBindings).wrapperName;
742-
743-
String? get wrapperDocComment => switch (ffiGen.output.style) {
744-
final DynamicLibraryBindings e => e.wrapperDocComment,
745-
_ => null,
746-
};
747-
748-
FfiNativeConfig get ffiNativeConfig => FfiNativeConfig(
749-
enabled: ffiGen.output.style is NativeExternalBindings,
750-
assetId: switch (ffiGen.output.style) {
751-
final NativeExternalBindings e => e.assetId,
752-
_ => null,
753-
},
754-
);
755-
756-
bool shouldIncludeHeader(Uri header) => ffiGen.headers.include(header);
757-
758-
bool get ignoreSourceErrors => ffiGen.headers.ignoreSourceErrors;
759-
760-
List<String>? get compilerOpts => ffiGen.headers.compilerOptions;
761-
762-
List<Uri> get entryPoints => ffiGen.headers.entryPoints;
763-
764-
Uri get output => ffiGen.output.dartFile;
765-
766-
Uri get outputObjC => ffiGen.output._objectiveCFile;
767-
768-
BindingStyle get outputStyle => ffiGen.output.style;
769-
770-
SymbolFile? get symbolFile => ffiGen.output.symbolFile;
771-
772-
bool get sort => ffiGen.output.sort;
773-
774-
CommentType get commentType => ffiGen.output.commentType;
775-
776-
String? get preamble => ffiGen.output.preamble;
777-
778-
bool get formatOutput => ffiGen.output.format;
779-
780-
// Override declarative user spec with what FFIgen internals expect.
781-
Map<String, LibraryImport> get libraryImports =>
782-
Map<String, LibraryImport>.fromEntries(
783-
// ignore: deprecated_member_use_from_same_package
784-
ffiGen.libraryImports.map(
785-
(import) => MapEntry<String, LibraryImport>(import.name, import),
786-
),
787-
);
788721

789722
// Override declarative user spec with what FFIgen internals expect.
790723
Map<String, ImportedType> get typedefTypeMappings =>
@@ -820,6 +753,4 @@ extension type Config(FfiGenerator ffiGen) implements FfiGenerator {
820753
(import) => MapEntry<String, ImportedType>(import.nativeType, import),
821754
),
822755
);
823-
824-
Language get language => objectiveC != null ? Language.objc : Language.c;
825756
}

pkgs/ffigen/lib/src/context.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Context {
2626
bool hasSourceErrors = false;
2727
final reportedCommentRanges = <((String, int), (String, int))>{};
2828
final libs = LibraryImports();
29-
late final compilerOpts = config.compilerOpts ?? defaultCompilerOpts(logger);
29+
late final compilerOpts =
30+
config.headers.compilerOptions ?? defaultCompilerOpts(logger);
3031
final Scope rootScope = Scope.createRoot('root');
3132
final Scope rootObjCScope = Scope.createRoot('objc_root');
3233
late final ExtraSymbols extraSymbols;

pkgs/ffigen/lib/src/ffigen.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ extension FfiGenGenerator on FfiGenerator {
2828
final library = parse(context);
2929

3030
// Generate files for the parsed bindings.
31-
final gen = File(config.output.toFilePath());
32-
library.generateFile(gen, format: config.formatOutput);
31+
final gen = File(config.output.dartFile.toFilePath());
32+
library.generateFile(gen, format: config.ffiGen.output.format);
3333
logger.info(
3434
_successPen('Finished, Bindings generated in ${gen.absolute.path}'),
3535
);
3636

37-
final objCGen = File(config.outputObjC.toFilePath());
37+
final objCGen = File(config.output.objCFile.toFilePath());
3838
if (library.generateObjCFile(objCGen)) {
3939
logger.info(
4040
_successPen(
@@ -44,11 +44,12 @@ extension FfiGenGenerator on FfiGenerator {
4444
);
4545
}
4646

47-
if (config.symbolFile != null) {
48-
final symbolFileGen = File(config.symbolFile!.output.toFilePath());
47+
final symbolFile = config.output.symbolFile;
48+
if (symbolFile != null) {
49+
final symbolFileGen = File(symbolFile.output.toFilePath());
4950
library.generateSymbolOutputFile(
5051
symbolFileGen,
51-
config.symbolFile!.importPath.toString(),
52+
symbolFile.importPath.toString(),
5253
);
5354
logger.info(
5455
_successPen(

pkgs/ffigen/lib/src/header_parser/parser.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ List<Binding> parseToBindings(Context context) {
5050
Pointer<Pointer<Utf8>> clangCmdArgs = nullptr;
5151
final compilerOpts = <String>[
5252
// Add compiler opt for comment parsing for clang based on config.
53-
if (config.commentType.length != CommentLength.none &&
54-
config.commentType.style == CommentStyle.any)
53+
if (config.output.commentType.length != CommentLength.none &&
54+
config.output.commentType.style == CommentStyle.any)
5555
strings.fparseAllComments,
5656

5757
// If the config targets Objective C, add a compiler opt for it.
58-
if (config.language == Language.objc) ...[
58+
if (config.objectiveC != null) ...[
5959
...strings.clangLangObjC,
6060
..._findObjectiveCSysroot(),
6161
],
@@ -72,12 +72,12 @@ List<Binding> parseToBindings(Context context) {
7272
final bindings = <Binding>{};
7373

7474
// Log all headers for user.
75-
context.logger.info('Input Headers: ${config.entryPoints}');
75+
context.logger.info('Input Headers: ${config.headers.entryPoints}');
7676

7777
final tuList = <Pointer<clang_types.CXTranslationUnitImpl>>[];
7878

7979
// Parse all translation units from entry points.
80-
for (final headerLocationUri in config.entryPoints) {
80+
for (final headerLocationUri in config.headers.entryPoints) {
8181
final headerLocation = headerLocationUri.toFilePath();
8282
context.logger.fine('Creating TranslationUnit for header: $headerLocation');
8383

@@ -114,11 +114,11 @@ List<Binding> parseToBindings(Context context) {
114114
'The compiler found warnings/errors in source files.',
115115
);
116116
context.logger.warning('This will likely generate invalid bindings.');
117-
if (config.ignoreSourceErrors) {
117+
if (config.headers.ignoreSourceErrors) {
118118
context.logger.warning(
119119
'Ignored source errors. (User supplied --ignore-source-errors)',
120120
);
121-
} else if (config.language == Language.objc) {
121+
} else if (config.objectiveC != null) {
122122
context.logger.warning('Ignored source errors. (ObjC)');
123123
} else {
124124
context.logger.severe(
@@ -219,7 +219,7 @@ List<Binding> transformBindings(List<Binding> bindings, Context context) {
219219

220220
/// Sort bindings.
221221
var finalBindingsList = finalBindings.toList();
222-
if (config.sort) {
222+
if (config.output.sort) {
223223
finalBindingsList = visit(
224224
context,
225225
SorterVisitation(finalBindings, SorterVisitation.nameSortKey),
@@ -283,7 +283,7 @@ void _nameAllSymbols(Context context, Set<Binding> bindings) {
283283
}
284284

285285
ExtraSymbols _createExtraSymbols(Context context) {
286-
final bindingStyle = context.config.outputStyle;
286+
final bindingStyle = context.config.output.style;
287287
Symbol? wrapperClassName;
288288
Symbol? lookupFuncName;
289289
if (bindingStyle is DynamicLibraryBindings) {

pkgs/ffigen/lib/src/header_parser/sub_parsers/api_availability.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ApiAvailability {
2727
this.alwaysUnavailable = false,
2828
this.ios,
2929
this.macos,
30-
required ExternalVersions externalVersions,
30+
required ExternalVersions? externalVersions,
3131
}) {
3232
availability = _getAvailability(externalVersions);
3333
}
@@ -88,7 +88,7 @@ class ApiAvailability {
8888
alwaysUnavailable: alwaysUnavailable.value != 0,
8989
ios: ios,
9090
macos: macos,
91-
externalVersions: context.config.externalVersions,
91+
externalVersions: context.config.objectiveC?.externalVersions,
9292
);
9393

9494
for (var i = 0; i < platformsLength; ++i) {
@@ -101,9 +101,9 @@ class ApiAvailability {
101101
return api;
102102
}
103103

104-
Availability _getAvailability(ExternalVersions externalVersions) {
105-
final macosVer = _normalizeVersions(externalVersions.macos);
106-
final iosVer = _normalizeVersions(externalVersions.ios);
104+
Availability _getAvailability(ExternalVersions? externalVersions) {
105+
final macosVer = _normalizeVersions(externalVersions?.macos);
106+
final iosVer = _normalizeVersions(externalVersions?.ios);
107107

108108
// If no versions are specified, everything is available.
109109
if (iosVer == null && macosVer == null) {

pkgs/ffigen/lib/src/header_parser/sub_parsers/functiondecl_parser.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../../code_generator.dart';
6+
import '../../config_provider/config.dart';
67
import '../../config_provider/config_types.dart';
78
import '../../context.dart';
89
import '../../strings.dart';
@@ -154,7 +155,7 @@ List<Func> parseFunctionDeclaration(
154155
exposeFunctionTypedefs: config.functions.includeTypedef(decl),
155156
isLeaf: config.functions.isLeaf(decl),
156157
objCReturnsRetained: objCReturnsRetained,
157-
loadFromNativeAsset: config.ffiNativeConfig.enabled,
158+
loadFromNativeAsset: config.output.style is NativeExternalBindings,
158159
),
159160
);
160161
}

pkgs/ffigen/lib/src/header_parser/sub_parsers/macro_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ File createFileForMacros(Context context) {
198198

199199
// Write file contents.
200200
final sb = StringBuffer();
201-
for (final h in context.config.entryPoints) {
201+
for (final h in context.config.headers.entryPoints) {
202202
final fullHeaderPath = File(h.toFilePath()).absolute.path;
203203
sb.writeln('#include "$fullHeaderPath"');
204204
}

pkgs/ffigen/lib/src/header_parser/sub_parsers/objccategorydecl_parser.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ ObjCCategory? parseObjCCategoryDeclaration(
1515
Context context,
1616
clang_types.CXCursor cursor,
1717
) {
18-
final config = context.config;
18+
final objcCategories = context.config.objectiveC?.categories;
19+
if (objcCategories == null) {
20+
return null;
21+
}
22+
1923
final logger = context.logger;
2024
final usr = cursor.usr();
2125
final name = cursor.spelling();
@@ -58,7 +62,7 @@ ObjCCategory? parseObjCCategoryDeclaration(
5862
final category = ObjCCategory(
5963
usr: usr,
6064
originalName: name,
61-
name: config.objcCategories.rename(decl),
65+
name: objcCategories.rename(decl),
6266
parent: parentInterface,
6367
dartDoc: getCursorDocComment(
6468
context,
@@ -84,15 +88,15 @@ ObjCCategory? parseObjCCategoryDeclaration(
8488
context,
8589
child,
8690
decl,
87-
config.objcCategories,
91+
objcCategories,
8892
);
8993
category.addMethod(getter);
9094
category.addMethod(setter);
9195
break;
9296
case clang_types.CXCursorKind.CXCursor_ObjCInstanceMethodDecl:
9397
case clang_types.CXCursorKind.CXCursor_ObjCClassMethodDecl:
9498
category.addMethod(
95-
parseObjCMethod(context, child, decl, config.objcCategories),
99+
parseObjCMethod(context, child, decl, objcCategories),
96100
);
97101
break;
98102
}

0 commit comments

Comments
 (0)