44
55import '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' ;
89import 'package:ffigen/src/config_provider/spec_utils.dart' ;
10+ import 'package:ffigen/src/context.dart' ;
911import 'package:ffigen/src/strings.dart' as strings;
1012import '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}
0 commit comments