Skip to content

Commit a796a45

Browse files
authored
[ffigen] Changes to support downstream clone (#2197)
1 parent 735250a commit a796a45

36 files changed

+142
-93
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 18.1.1-wip
2+
3+
- Make it easier for a downstream clone to change behavior of certain utils.
4+
15
## 18.1.0
26

37
- Fix a clang warning in ObjC protocol generated bindings.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// This file contains utils that are intended to be overridden by downstream
6+
// clones. They're gathered into one file to make it easy to swap them out.
7+
8+
// This file is exclusively imported by utils.dart, so that there's only one
9+
// line we have to patch in the downstream clones.
10+
@Deprecated('Import config_provider/utils.dart instead')
11+
library;
12+
13+
import 'dart:io';
14+
15+
import 'package:path/path.dart' as p;
16+
17+
// Replaces the path separators according to current platform.
18+
String _replaceSeparators(String path) {
19+
if (Platform.isWindows) {
20+
return path.replaceAll(p.posix.separator, p.windows.separator);
21+
} else {
22+
return path.replaceAll(p.windows.separator, p.posix.separator);
23+
}
24+
}
25+
26+
/// Replaces the path separators according to current platform, and normalizes .
27+
/// and .. in the path. If a relative path is passed in, it is resolved relative
28+
/// to the config path, and the absolute path is returned.
29+
String normalizePath(String path, String? configFilename) {
30+
final resolveInConfigDir =
31+
(configFilename == null) || p.isAbsolute(path) || path.startsWith('**');
32+
return _replaceSeparators(p.normalize(resolveInConfigDir
33+
? path
34+
: p.absolute(p.join(p.dirname(configFilename), path))));
35+
}
36+
37+
/// These locations are searched for clang dylibs before any others. Downstream
38+
/// clones can use a non-null value for this path to search here first.
39+
final libclangOverridePaths = const <String>[];
40+
41+
/// Returns the root path of the package, for use during tests.
42+
///
43+
/// Note that `dart test` sets the current directory to the package root.
44+
final packagePathForTests = p.current;
45+
46+
/// Returns a path to a config yaml in a unit test.
47+
String configPathForTest(String directory, String file) =>
48+
p.join(directory, file);

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,19 @@ String? _findLibInConda() {
308308
/// Returns location of dynamic library by searching default locations. Logs
309309
/// error and throws an Exception if not found.
310310
String findDylibAtDefaultLocations() {
311+
for (final libclangPath in libclangOverridePaths) {
312+
final overridableLib = findLibclangDylib(libclangPath);
313+
if (overridableLib != null) return overridableLib;
314+
}
315+
311316
// Assume clang in conda has a higher priority.
312-
var k = _findLibInConda();
313-
if (k != null) return k;
317+
final condaLib = _findLibInConda();
318+
if (condaLib != null) return condaLib;
319+
314320
if (Platform.isLinux) {
315321
for (final l in strings.linuxDylibLocations) {
316-
k = findLibclangDylib(l);
317-
if (k != null) return k;
322+
final linuxLib = findLibclangDylib(l);
323+
if (linuxLib != null) return linuxLib;
318324
}
319325
Process.runSync('ldconfig', ['-p']);
320326
final ldConfigResult = Process.runSync('ldconfig', ['-p']);
@@ -338,13 +344,13 @@ String findDylibAtDefaultLocations() {
338344
.add(p.join(userHome, 'scoop', 'apps', 'llvm', 'current', 'bin'));
339345
}
340346
for (final l in dylibLocations) {
341-
k = findLibclangDylib(l);
342-
if (k != null) return k;
347+
final winLib = findLibclangDylib(l);
348+
if (winLib != null) return winLib;
343349
}
344350
} else if (Platform.isMacOS) {
345351
for (final l in strings.macOsDylibLocations) {
346-
k = findLibclangDylib(l);
347-
if (k != null) return k;
352+
final macLib = findLibclangDylib(l);
353+
if (macLib != null) return macLib;
348354
}
349355
final findLibraryResult =
350356
Process.runSync('xcodebuild', ['-find-library', 'libclang.dylib']);

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,8 @@
44

55
import 'dart:io';
66

7-
import 'package:path/path.dart' as p;
8-
9-
// Replaces the path separators according to current platform.
10-
String _replaceSeparators(String path) {
11-
if (Platform.isWindows) {
12-
return path.replaceAll(p.posix.separator, p.windows.separator);
13-
} else {
14-
return path.replaceAll(p.windows.separator, p.posix.separator);
15-
}
16-
}
17-
18-
/// Replaces the path separators according to current platform, and normalizes .
19-
/// and .. in the path. If a relative path is passed in, it is resolved relative
20-
/// to the config path, and the absolute path is returned.
21-
String normalizePath(String path, String? configFilename) {
22-
final resolveInConfigDir =
23-
(configFilename == null) || p.isAbsolute(path) || path.startsWith('**');
24-
return _replaceSeparators(p.normalize(resolveInConfigDir
25-
? path
26-
: p.absolute(p.join(p.dirname(configFilename), path))));
27-
}
7+
// ignore: deprecated_member_use_from_same_package
8+
export 'overrideable_utils.dart';
289

2910
/// Replaces any variable names in the path with the corresponding value.
3011
String substituteVars(String path) {

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: 18.1.0
6+
version: 18.1.1-wip
77
description: >
88
Generator for FFI bindings, using LibClang to parse C, Objective-C, and Swift
99
files.

pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ${strings.description}: 'Decl type name collision test'
2222
${strings.output}: 'unused'
2323
${strings.headers}:
2424
${strings.entryPoints}:
25-
- 'test/collision_tests/decl_type_name_collision.h'
25+
- '${absPath('test/collision_tests/decl_type_name_collision.h')}'
2626
${strings.preamble}: |
2727
// ignore_for_file: non_constant_identifier_names,
2828
'''),

pkgs/ffigen/test/config_tests/compiler_opts_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ${strings.description}: 'Compiler Opts Test'
3434
${strings.output}: 'unused'
3535
${strings.headers}:
3636
${strings.entryPoints}:
37-
- 'test/header_parser_tests/comment_markup.h'
37+
- '${absPath('test/header_parser_tests/comment_markup.h')}'
3838
${strings.compilerOptsAuto}:
3939
${strings.macos}:
4040
${strings.includeCStdLib}: false

pkgs/ffigen/test/config_tests/deprecate_assetid_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ${strings.ffiNative}:
2121
assetId: 'myasset'
2222
${strings.headers}:
2323
${strings.entryPoints}:
24-
- 'test/header_parser_tests/comment_markup.h'
24+
- '${absPath('test/header_parser_tests/comment_markup.h')}'
2525
''');
2626
parse(config);
2727

pkgs/ffigen/test/config_tests/exclude_all_by_default_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ${strings.output}: 'unused'
1919
${strings.excludeAllByDefault}: false
2020
${strings.headers}:
2121
${strings.entryPoints}:
22-
- 'test/config_tests/exclude_all_by_default.h'
22+
- '${absPath('test/config_tests/exclude_all_by_default.h')}'
2323
''');
2424

2525
final library = parse(config);
@@ -40,7 +40,7 @@ ${strings.output}: 'unused'
4040
${strings.excludeAllByDefault}: true
4141
${strings.headers}:
4242
${strings.entryPoints}:
43-
- 'test/config_tests/exclude_all_by_default.h'
43+
- '${absPath('test/config_tests/exclude_all_by_default.h')}'
4444
''');
4545

4646
final library = parse(config);

pkgs/ffigen/test/config_tests/include_exclude_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ${strings.description}: 'include_exclude test'
4949
${strings.output}: 'unused'
5050
${strings.headers}:
5151
${strings.entryPoints}:
52-
- 'test/config_tests/include_exclude.h'
52+
- '${absPath('test/config_tests/include_exclude.h')}'
5353
''';
5454
if (include != null || exclude != null) {
5555
templateString += '''

pkgs/ffigen/test/config_tests/no_cursor_definition_warn_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ${strings.description}: 'Warn for no cursor definition.'
2121
${strings.output}: 'unused'
2222
${strings.headers}:
2323
${strings.entryPoints}:
24-
- 'test/header_parser_tests/opaque_dependencies.h'
24+
- '${absPath('test/header_parser_tests/opaque_dependencies.h')}'
2525
${strings.structs}:
2626
${strings.dependencyOnly}: ${strings.opaqueCompoundDependencies}
2727
${strings.include}:

pkgs/ffigen/test/config_tests/packed_struct_override_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import '../test_utils.dart';
1212
void main() {
1313
group('packed_struct_override_test', () {
1414
test('Invalid Packed Config values', () {
15-
const baseYaml = '''${strings.name}: 'NativeLibrary'
15+
final baseYaml = '''${strings.name}: 'NativeLibrary'
1616
${strings.description}: 'Packed Struct Override Test'
1717
${strings.output}: 'unused'
1818
${strings.headers}:
1919
${strings.entryPoints}:
20-
- 'test/header_parser_tests/packed_structs.h'
20+
- '${absPath('test/header_parser_tests/packed_structs.h')}'
2121
${strings.structs}:
2222
${strings.structPack}:
2323
''';
@@ -35,7 +35,7 @@ ${strings.description}: 'Packed Struct Override Test'
3535
${strings.output}: 'unused'
3636
${strings.headers}:
3737
${strings.entryPoints}:
38-
- 'test/header_parser_tests/packed_structs.h'
38+
- '${absPath('test/header_parser_tests/packed_structs.h')}'
3939
${strings.structs}:
4040
${strings.structPack}:
4141
'Normal.*': 1

pkgs/ffigen/test/config_tests/unknown_keys_warn_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ${strings.description}: 'Warn for unknown keys.'
2020
${strings.output}: 'unused'
2121
${strings.headers}:
2222
${strings.entryPoints}:
23-
- 'test/header_parser_tests/packed_structs.h'
23+
- '${absPath('test/header_parser_tests/packed_structs.h')}'
2424
'warn-1': 'warn'
2525
${strings.typeMap}:
2626
'warn-2': 'warn'

pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ${strings.description}: 'Comment Markup Test'
2222
${strings.output}: 'unused'
2323
${strings.headers}:
2424
${strings.entryPoints}:
25-
- 'test/header_parser_tests/comment_markup.h'
25+
- '${absPath('test/header_parser_tests/comment_markup.h')}'
2626
${strings.comments}:
2727
${strings.style}: ${strings.any}
2828
${strings.length}: ${strings.full}

pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ${strings.compilerOpts}: '-I${path.join(sdkPath, "include")}'
2626
2727
${strings.headers}:
2828
${strings.entryPoints}:
29-
- 'test/header_parser_tests/dart_handle.h'
29+
- '${absPath('test/header_parser_tests/dart_handle.h')}'
3030
${strings.includeDirectives}:
3131
- '**dart_handle.h'
3232
'''),

pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ${strings.description}: 'Enum int mimic test'
2222
${strings.output}: 'unused'
2323
${strings.headers}:
2424
${strings.entryPoints}:
25-
- 'test/header_parser_tests/enum_int_mimic.h'
25+
- '${absPath('test/header_parser_tests/enum_int_mimic.h')}'
2626
${strings.includeDirectives}:
2727
- '**enum_int_mimic.h'
2828
${strings.ignoreSourceErrors}: true

pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ${strings.description}: 'Forward Declaration Test'
2222
${strings.output}: 'unused'
2323
${strings.headers}:
2424
${strings.entryPoints}:
25-
- 'test/header_parser_tests/forward_decl.h'
25+
- '${absPath('test/header_parser_tests/forward_decl.h')}'
2626
${strings.ignoreSourceErrors}: true
2727
'''),
2828
);

pkgs/ffigen/test/header_parser_tests/function_n_struct_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ${strings.output}: 'unused'
2525
2626
${strings.headers}:
2727
${strings.entryPoints}:
28-
- 'test/header_parser_tests/function_n_struct.h'
28+
- '${absPath('test/header_parser_tests/function_n_struct.h')}'
2929
'''),
3030
);
3131
});

pkgs/ffigen/test/header_parser_tests/functions_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ${strings.output}: 'unused'
2323
2424
${strings.headers}:
2525
${strings.entryPoints}:
26-
- 'test/header_parser_tests/functions.h'
26+
- '${absPath('test/header_parser_tests/functions.h')}'
2727
${strings.includeDirectives}:
2828
- '**functions.h'
2929
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'NativeLibrary'
2+
description: 'Globals Test'
3+
output: 'unused'
4+
headers:
5+
entry-points:
6+
- globals.h
7+
include-directives:
8+
- '**globals.h'
9+
globals:
10+
exclude:
11+
- GlobalIgnore
12+
symbol-address:
13+
include:
14+
- myInt
15+
- pointerToLongDouble
16+
- globalStruct
17+
compiler-opts: '-Wno-nullability-completeness'
18+
ignore-source-errors: true

pkgs/ffigen/test/header_parser_tests/globals_test.dart

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:ffigen/src/code_generator.dart';
66
import 'package:ffigen/src/header_parser.dart' as parser;
7-
import 'package:ffigen/src/strings.dart' as strings;
7+
import 'package:path/path.dart' as path;
88
import 'package:test/test.dart';
99

1010
import '../test_utils.dart';
@@ -17,26 +17,8 @@ void main() {
1717
logWarnings();
1818
expected = expectedLibrary();
1919
actual = parser.parse(
20-
testConfig('''
21-
${strings.name}: 'NativeLibrary'
22-
${strings.description}: 'Globals Test'
23-
${strings.output}: 'unused'
24-
${strings.headers}:
25-
${strings.entryPoints}:
26-
- 'test/header_parser_tests/globals.h'
27-
${strings.includeDirectives}:
28-
- '**globals.h'
29-
${strings.globals}:
30-
${strings.exclude}:
31-
- GlobalIgnore
32-
${strings.symbolAddress}:
33-
${strings.include}:
34-
- myInt
35-
- pointerToLongDouble
36-
- globalStruct
37-
${strings.compilerOpts}: '-Wno-nullability-completeness'
38-
${strings.ignoreSourceErrors}: true
39-
'''),
20+
testConfigFromPath(configPath(
21+
path.join('test', 'header_parser_tests'), 'globals_config.yaml')),
4022
);
4123
});
4224

pkgs/ffigen/test/header_parser_tests/imported_types_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ${strings.output}: 'unused'
2323
2424
${strings.headers}:
2525
${strings.entryPoints}:
26-
- 'test/header_parser_tests/imported_types.h'
26+
- '${absPath('test/header_parser_tests/imported_types.h')}'
2727
${strings.includeDirectives}:
2828
- '**imported_types.h'
2929
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: 'NativeLibrary'
2+
description: 'Macros Test'
3+
output: 'unused'
4+
headers:
5+
entry-points:
6+
- macros.h
7+
include-directives:
8+
- '**macros.h'

pkgs/ffigen/test/header_parser_tests/macros_test.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:ffigen/src/code_generator.dart';
66
import 'package:ffigen/src/header_parser.dart' as parser;
77
import 'package:ffigen/src/strings.dart' as strings;
88
import 'package:logging/logging.dart';
9+
import 'package:path/path.dart' as path;
910
import 'package:test/test.dart';
1011

1112
import '../test_utils.dart';
@@ -18,16 +19,8 @@ void main() {
1819
logWarnings(Level.WARNING);
1920
expected = expectedLibrary();
2021
actual = parser.parse(
21-
testConfig('''
22-
${strings.name}: 'NativeLibrary'
23-
${strings.description}: 'Macros Test'
24-
${strings.output}: 'unused'
25-
${strings.headers}:
26-
${strings.entryPoints}:
27-
- 'test/header_parser_tests/macros.h'
28-
${strings.includeDirectives}:
29-
- '**macros.h'
30-
'''),
22+
testConfigFromPath(configPath(
23+
path.join('test', 'header_parser_tests'), 'macros_config.yaml')),
3124
);
3225
});
3326
test('Total bindings count', () {

pkgs/ffigen/test/header_parser_tests/native_func_typedef_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ${strings.description}: 'Native Func Typedef Test.'
2222
${strings.output}: 'unused'
2323
${strings.headers}:
2424
${strings.entryPoints}:
25-
- 'test/header_parser_tests/native_func_typedef.h'
25+
- '${absPath('test/header_parser_tests/native_func_typedef.h')}'
2626
'''),
2727
);
2828
});

0 commit comments

Comments
 (0)