Skip to content

Commit d3b1463

Browse files
fix: handle tmpdir null issue in ffigen context
1 parent d6a32a6 commit d3b1463

28 files changed

+97
-36
lines changed

pkgs/ffigen/lib/src/context.dart

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,31 @@ class Context {
3131
final Scope rootScope = Scope.createRoot('root');
3232
final Scope rootObjCScope = Scope.createRoot('objc_root');
3333
late final ExtraSymbols extraSymbols;
34-
35-
Context(this.logger, FfiGenerator generator, {Uri? libclangDylib})
36-
: config = Config(generator),
37-
cursorIndex = CursorIndex(logger) {
38-
objCBuiltInFunctions = ObjCBuiltInFunctions(
39-
this,
40-
// ignore: deprecated_member_use_from_same_package
41-
generator.objectiveC?.generateForPackageObjectiveC ?? false,
42-
);
43-
final libclangDylibPath =
44-
// ignore: deprecated_member_use_from_same_package
45-
generator.libclangDylib?.toFilePath() ??
46-
libclangDylib?.toFilePath() ??
47-
findDylibAtDefaultLocations(logger);
48-
_clang ??= Clang(DynamicLibrary.open(libclangDylibPath));
49-
}
34+
final String tmpDir;
35+
36+
Context(
37+
this.logger,
38+
FfiGenerator generator, {
39+
Uri? libclangDylib,
40+
String? tmpDir, // optional parameter
41+
}) : config = Config(generator),
42+
cursorIndex = CursorIndex(logger),
43+
tmpDir = tmpDir ?? Directory.systemTemp.createTempSync('ffigen temp dir ').path // default temp dir with spaces
44+
{
45+
// Initialize compiler options
46+
compilerOpts = config.headers.compilerOptions ?? defaultCompilerOpts(logger);
47+
48+
objCBuiltInFunctions = ObjCBuiltInFunctions(
49+
this,
50+
generator.objectiveC?.generateForPackageObjectiveC ?? false,
51+
);
52+
53+
final libclangDylibPath =
54+
generator.libclangDylib?.toFilePath() ??
55+
libclangDylib?.toFilePath() ??
56+
findDylibAtDefaultLocations(logger);
57+
58+
_clang ??= Clang(DynamicLibrary.open(libclangDylibPath));
5059
}
5160

5261
/// The clang bindings.
@@ -123,3 +132,4 @@ typedef ExtraSymbols = ({
123132
// TODO(https://github.com/dart-lang/native/issues/1259): Make this nullable.
124133
Symbol symbolAddressVariableName,
125134
});
135+
}

pkgs/ffigen/test/code_generator_tests/code_generator_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,8 @@ void main() {
789789

790790
/// Utility to match expected bindings to the generated bindings.
791791
void _matchLib(Library lib, String testName) {
792-
matchLibraryWithExpected(lib, 'code_generator_test_${testName}_output.dart', [
792+
final context = testContext();
793+
matchLibraryWithExpected(context, lib, 'code_generator_test_${testName}_output.dart', [
793794
'test',
794795
'code_generator_tests',
795796
'expected_bindings',

pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ void main() {
8282
),
8383
], context),
8484
);
85+
final context = testContext();
8586
matchLibraryWithExpected(
87+
context,
8688
library,
8789
'decl_decl_collision_test_output.dart',
8890
[

pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ void main() {
4949
);
5050
});
5151
test('declaration and symbol address conflict', () {
52+
final context = testContext();
5253
matchLibraryWithExpected(
54+
context,
5355
actual,
5456
'collision_test_decl_symbol_address_collision_output.dart',
5557
[

pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ ${strings.headers}:
2828
});
2929

3030
test('Expected bindings', () {
31+
final context = testContext();
3132
matchLibraryWithExpected(
33+
context,
3234
actual,
3335
'decl_type_name_collision_test_output.dart',
3436
[

pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../test_utils.dart';
1212
void main() {
1313
group('reserved_keyword_collision_test', () {
1414
test('reserved keyword collision', () {
15+
final context = testContext();
1516
final library = parser.parse(
1617
testContext(
1718
FfiGenerator(
@@ -45,6 +46,7 @@ void main() {
4546
),
4647
);
4748
matchLibraryWithExpected(
49+
context,
4850
library,
4951
'reserved_keyword_collision_test_output.dart',
5052
[

pkgs/ffigen/test/example_tests/cjson_example_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ void main() {
1414
final config = testConfigFromPath(
1515
path.join(packagePathForTests, 'example', 'c_json', 'config.yaml'),
1616
);
17+
final context = testContext(config);
1718
final library = parse(testContext(config));
1819

19-
matchLibraryWithExpected(library, 'example_c_json.dart', [
20+
matchLibraryWithExpected(context, library, 'example_c_json.dart', [
2021
config.output.dartFile.toFilePath(),
2122
]);
2223
});

pkgs/ffigen/test/example_tests/ffinative_example_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ void main() {
1414
final config = testConfigFromPath(
1515
path.join(packagePathForTests, 'example', 'ffinative', 'config.yaml'),
1616
);
17-
final library = parse(testContext(config));
17+
final context = testContext(config);
18+
final library = parse(context);
1819

19-
matchLibraryWithExpected(library, 'example_ffinative.dart', [
20+
matchLibraryWithExpected(context, library, 'example_ffinative.dart', [
2021
config.output.dartFile.toFilePath(),
2122
]);
2223
});

pkgs/ffigen/test/example_tests/libclang_example_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main() {
3535
final context = testContext(generator);
3636
final library = parse(context);
3737

38-
matchLibraryWithExpected(library, 'example_libclang.dart', [
38+
matchLibraryWithExpected(context, library, 'example_libclang.dart', [
3939
generator.output.dartFile.toFilePath(),
4040
]);
4141
});

pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void main() {
2020
'a_shared_base.yaml',
2121
),
2222
);
23-
final library = parse(testContext(config));
24-
25-
matchLibraryWithExpected(library, 'example_shared_bindings.dart', [
23+
final context = testContext(config);
24+
final library = parse(context);
25+
matchLibraryWithExpected(context, library, 'example_shared_bindings.dart', [
2626
config.output.dartFile.toFilePath(),
2727
]);
2828
});
@@ -37,8 +37,10 @@ void main() {
3737
'base.yaml',
3838
),
3939
);
40-
final library = parse(testContext(config));
40+
final context = testContext(config);
41+
final library = parse(context);
4142
matchLibrarySymbolFileWithExpected(
43+
context,
4244
library,
4345
'example_shared_bindings.yaml',
4446
[config.output.symbolFile!.output.toFilePath()],

0 commit comments

Comments
 (0)