| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'dart:io'; | |
| import 'package:cli_util/cli_logging.dart' show Ansi; | |
| import 'package:logging/logging.dart'; | |
| import 'config_provider/config.dart'; | |
| import 'context.dart'; | |
| import 'header_parser.dart' show parse; | |
| import 'logger.dart'; | |
| final _ansi = Ansi(Ansi.terminalSupportsAnsi); | |
| extension FfiGenGenerator on FfiGenerator { | |
| /// Runs the entire generation pipeline for the given config. | |
| /// | |
| /// If provided, uses [logger] to output logs. Otherwise, uses a default | |
| /// logger that streams [Level.WARNING] to stdout and higher levels to stderr. | |
| Future<void> generate({Logger? logger, Uri? libclangDylib}) async { | |
| logger ??= createDefaultLogger(); | |
| final context = Context(logger, this, libclangDylib: libclangDylib); | |
| // Parse the bindings according to config object provided. | |
| final library = parse(context); | |
| // Generate files for the parsed bindings. | |
| final gen = File(output.dart.path.toFilePath()); | |
| await library.generateFile(gen, format: output.format); | |
| logger.info( | |
| _successPen('Finished, Bindings generated in ${gen.absolute.path}'), | |
| ); | |
| final objCGen = File(output.objCFile.toFilePath()); | |
| if (library.generateObjCFile(objCGen)) { | |
| logger.info( | |
| _successPen( | |
| 'Finished, Objective C bindings generated ' | |
| 'in ${objCGen.absolute.path}', | |
| ), | |
| ); | |
| } | |
| final cppGen = File(output.cppBindingsFile.toFilePath()); | |
| if (library.generateCppFile(cppGen)) { | |
| logger.info( | |
| _successPen( | |
| 'Finished, Cpp bindings generated in ${cppGen.absolute.path}', | |
| ), | |
| ); | |
| } | |
| final recordUseMappingFile = output.recordUseMapping; | |
| if (recordUseMappingFile != null) { | |
| final recordUseMappingGen = File(recordUseMappingFile.toFilePath()); | |
| if (await library.generateRecordUseMappingFile( | |
| recordUseMappingGen, | |
| format: output.format, | |
| )) { | |
| logger.info( | |
| _successPen( | |
| 'Finished, RecordUse Mapping generated ' | |
| 'in ${recordUseMappingGen.absolute.path}', | |
| ), | |
| ); | |
| } | |
| } | |
| final symbolFile = output.symbolFile; | |
| if (symbolFile != null) { | |
| final symbolFileGen = File(symbolFile.output.toFilePath()); | |
| library.generateSymbolOutputFile( | |
| symbolFileGen, | |
| symbolFile.importPath.toString(), | |
| ); | |
| logger.info( | |
| _successPen( | |
| 'Finished, Symbol Output generated in ' | |
| '${symbolFileGen.absolute.path}', | |
| ), | |
| ); | |
| } | |
| } | |
| static String _successPen(String str) => '${_ansi.green}$str${_ansi.none}'; | |
| } |