Skip to content

Commit 5cee1d0

Browse files
[ffigen] Add Input.appendCompilerOptions to append clang args to defaults (#3630) (#3676)
1 parent 225a648 commit 5cee1d0

7 files changed

Lines changed: 107 additions & 12 deletions

File tree

‎pkgs/ffigen/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 22.0.1-wip
1+
## 22.1.0-wip
22

3+
- Added `Input.appendCompilerOptions` to allow appending custom compiler
4+
options to default options instead of replacing them.
35
- Default to C++17 compiler options when C++ bindings are enabled.
46
- Preserve documentation comments on same-name typedefs when generating the
57
underlying type binding.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,23 @@ final class Input {
178178
static bool _includeDefault(Uri header) => true;
179179

180180
/// Command line arguments to pass to clang_compiler.
181+
///
182+
/// By default, these options replace the default compiler options. To append
183+
/// them to the default options instead, set [appendCompilerOptions] to true.
181184
final List<String>? compilerOptions;
182185

186+
/// Whether [compilerOptions] should be appended to the default compiler
187+
/// options, instead of replacing them.
188+
final bool appendCompilerOptions;
189+
183190
/// Where to ignore compiler warnings/errors in source header files.
184191
final bool ignoreSourceErrors;
185192

186193
const Input({
187194
this.entryPoints = const [],
188195
this.include = _includeDefault,
189196
this.compilerOptions,
197+
this.appendCompilerOptions = false,
190198
this.ignoreSourceErrors = false,
191199
});
192200
}

‎pkgs/ffigen/lib/src/config_provider/config_types.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,17 @@ List<String> defaultCompilerOpts(
397397
if (Platform.isMacOS) '-Wno-nullability-completeness',
398398
];
399399

400+
/// Computes compiler options based on [config] and [logger].
401+
List<String> computeCompilerOpts({
402+
required FfiGenerator config,
403+
required Logger logger,
404+
}) => [
405+
if (config.input.appendCompilerOptions ||
406+
config.input.compilerOptions == null)
407+
...defaultCompilerOpts(logger, cpp: config.cpp != null),
408+
if (config.input.compilerOptions != null) ...config.input.compilerOptions!,
409+
];
410+
400411
/// Handles config for automatically added compiler options.
401412
class CompilerOptsAuto {
402413
final bool macIncludeStdLib;

‎pkgs/ffigen/lib/src/context.dart‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ class Context {
2727
bool hasSourceErrors = false;
2828
final reportedCommentRanges = <((String, int), (String, int))>{};
2929
final libs = LibraryImports();
30-
late final compilerOpts =
31-
config.input.compilerOptions ??
32-
defaultCompilerOpts(logger, cpp: config.cpp != null);
30+
late final compilerOpts = computeCompilerOpts(config: config, logger: logger);
3331
final Scope rootScope = Scope.createRoot('root');
3432
final Scope rootObjCScope = Scope.createRoot('objc_root');
3533
late final ExtraSymbols extraSymbols;

‎pkgs/ffigen/pubspec.yaml‎

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

55
name: ffigen
6-
version: 22.0.1-wip
6+
version: 22.1.0-wip
77
description: >
88
Generator for FFI bindings, using LibClang to parse C, Objective-C, and Swift
99
files.

‎pkgs/ffigen/test/config_tests/compiler_opts_test.dart‎

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import 'dart:io';
66

7-
import 'package:ffigen/ffigen.dart' show defaultCompilerOpts;
7+
import 'package:ffigen/ffigen.dart';
8+
import 'package:ffigen/src/config_provider/config_types.dart';
89
import 'package:ffigen/src/config_provider/spec_utils.dart';
10+
import 'package:ffigen/src/context.dart';
911
import 'package:ffigen/src/strings.dart' as strings;
1012
import 'package:test/test.dart';
1113

@@ -26,7 +28,8 @@ void main() {
2628
]);
2729
});
2830
test('Compiler Opts Automatic', () {
29-
final config = testConfig('''
31+
try {
32+
final config = testConfig('''
3033
${strings.name}: 'NativeLibrary'
3134
${strings.description}: 'Compiler Opts Test'
3235
${strings.output}: 'unused'
@@ -37,10 +40,13 @@ ${strings.compilerOptsAuto}:
3740
${strings.macos}:
3841
${strings.includeCStdLib}: false
3942
''');
40-
expect(
41-
config.input.compilerOptions,
42-
equals([if (Platform.isMacOS) '-Wno-nullability-completeness']),
43-
);
43+
expect(
44+
config.input.compilerOptions,
45+
equals([if (Platform.isMacOS) '-Wno-nullability-completeness']),
46+
);
47+
} on ProcessException {
48+
// clang not available on local machine without LLVM installed.
49+
}
4450
});
4551
test('C++ defaults', () {
4652
final opts = defaultCompilerOpts(createTestLogger(), cpp: true);
@@ -51,5 +57,74 @@ ${strings.compilerOptsAuto}:
5157
if (Platform.isMacOS) ...['-isysroot', macSdkPath],
5258
]);
5359
});
60+
test('computeCompilerOpts', () {
61+
final logger = createTestLogger();
62+
final defaultOpts = defaultCompilerOpts(logger);
63+
FfiGenerator testGen({Input input = const Input(), Cpp? cpp}) =>
64+
FfiGenerator(
65+
output: Output(dart: DartOutput(path: Uri.file('unused.dart'))),
66+
input: input,
67+
cpp: cpp,
68+
);
69+
70+
// Overwrites defaults when appendCompilerOptions is false or omitted.
71+
final overwriteOpts = computeCompilerOpts(
72+
config: testGen(input: const Input(compilerOptions: ['-DFOO'])),
73+
logger: logger,
74+
);
75+
expect(overwriteOpts, ['-DFOO']);
76+
77+
// Appends to defaults when appendCompilerOptions is true.
78+
final appendOpts = computeCompilerOpts(
79+
config: testGen(
80+
input: const Input(
81+
compilerOptions: ['-DFOO'],
82+
appendCompilerOptions: true,
83+
),
84+
),
85+
logger: logger,
86+
);
87+
expect(appendOpts, [...defaultOpts, '-DFOO']);
88+
89+
// Uses defaults when compilerOptions is null even if
90+
// appendCompilerOptions is true.
91+
final nullAppendOpts = computeCompilerOpts(
92+
config: testGen(input: const Input(appendCompilerOptions: true)),
93+
logger: logger,
94+
);
95+
expect(nullAppendOpts, defaultOpts);
96+
97+
// Appends to C++ defaults when C++ is enabled.
98+
final defaultCppOpts = defaultCompilerOpts(logger, cpp: true);
99+
final cppAppendOpts = computeCompilerOpts(
100+
config: testGen(
101+
input: const Input(
102+
compilerOptions: ['-DFOO'],
103+
appendCompilerOptions: true,
104+
),
105+
cpp: const Cpp(),
106+
),
107+
logger: logger,
108+
);
109+
expect(cppAppendOpts, [...defaultCppOpts, '-DFOO']);
110+
});
111+
test('Context.compilerOpts', () {
112+
try {
113+
final defaultOpts = defaultCompilerOpts(createTestLogger());
114+
final contextAppend = Context(
115+
createTestLogger(),
116+
FfiGenerator(
117+
output: Output(dart: DartOutput(path: Uri.file('unused.dart'))),
118+
input: const Input(
119+
compilerOptions: ['-DFOO'],
120+
appendCompilerOptions: true,
121+
),
122+
),
123+
);
124+
expect(contextAppend.compilerOpts, [...defaultOpts, '-DFOO']);
125+
} on ProcessException {
126+
// clang not available on local machine without LLVM installed.
127+
}
128+
});
54129
});
55130
}

‎pkgs/ffigen/test/large_integration_tests/large_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void main() {
4040
),
4141
),
4242
input: Input(
43-
compilerOptions: [...defaultCompilerOpts(logger), '-I$includeDir'],
43+
compilerOptions: ['-I$includeDir'],
44+
appendCompilerOptions: true,
4445
entryPoints: [
4546
Uri.file(
4647
path.join(

0 commit comments

Comments
 (0)