Skip to content

Commit 8d48f6f

Browse files
committed
[ffigen] ffigen only regenerates if input mtimes > output mtimes (dart-lang#874)
1 parent 7eefc74 commit 8d48f6f

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 12.1.0
2+
3+
- ffigen now only regenerates the dart bindings only if input (the `config.yaml`
4+
and header files) mtimes is greater than the output mtimes.
5+
16
## 12.0.0-wip
27

38
- Global variables are now compatible with the `ffi-native` option.
@@ -6,7 +11,7 @@
611

712
## 11.0.0
813

9-
- Any compiler errors/warnings in source header files will now result in
14+
- Any compilier errors/warnings in source header files will now result in
1015
bindings to **not** be generated by default, since it may result in invalid
1116
bindings if the compiler makes a wrong guess. A flag `--ignore-source-errors` (or yaml config `ignore-source-errors: true`)
1217
must be passed to change this behaviour.

pkgs/ffigen/lib/src/executables/ffigen.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ void main(List<String> args) async {
5656
final library = parse(config);
5757

5858
// Generate file for the parsed bindings.
59-
final gen = File(config.output);
60-
library.generateFile(gen);
61-
_logger
62-
.info(successPen('Finished, Bindings generated in ${gen.absolute.path}'));
59+
final fileToBeGenerated = File(config.output);
60+
61+
// Consider regenif config has been updated
62+
final bool inputHasBeenUpdated = _inputHasBeenUpdated(
63+
fileToBeGenerated, [config.filename!, ...config.headers.entryPoints]);
64+
if (!inputHasBeenUpdated) {
65+
_logger.info('Bindings are up-to-date. No changes to headers detected.');
66+
return;
67+
}
68+
69+
library.generateFile(fileToBeGenerated);
70+
_logger.info(successPen(
71+
'Finished, Bindings generated in ${fileToBeGenerated.absolute.path}'));
6372

6473
if (config.symbolFile != null) {
6574
final symbolFileGen = File(config.symbolFile!.output);
@@ -70,6 +79,25 @@ void main(List<String> args) async {
7079
}
7180
}
7281

82+
bool _inputHasBeenUpdated(
83+
File fileToBeGenerated, List<String> configAndHeaders) {
84+
// if file does not exist, consider it needs to be generated.
85+
if (!fileToBeGenerated.existsSync()) {
86+
_logger.info('Bindings file does not exist, generating bindings.');
87+
return true;
88+
}
89+
90+
for (final configOrHeader in configAndHeaders) {
91+
final headerMTime = File(configOrHeader).lastModifiedSync();
92+
93+
if (fileToBeGenerated.existsSync() &&
94+
headerMTime.isAfter(fileToBeGenerated.lastModifiedSync())) {
95+
return true; // file needs to be regenerated.
96+
}
97+
}
98+
return false;
99+
}
100+
73101
Config getConfig(ArgResults result, PackageConfig? packageConfig) {
74102
_logger.info('Running in ${Directory.current}');
75103
Config config;

0 commit comments

Comments
 (0)