|
| 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 | +import 'dart:io'; |
| 6 | +import 'dart:ffi'; |
| 7 | + |
| 8 | +final repoRoot = File.fromUri(Platform.script).parent.parent.parent.path; |
| 9 | +final runtimeRoot = '$repoRoot/runtime'; |
| 10 | +final buildtoolsRoot = '$repoRoot/buildtools'; |
| 11 | +final clangBinDir = |
| 12 | + '$buildtoolsRoot/$currentPlatformBuildtoolsSubdir/clang/bin'; |
| 13 | +final clangFormatBin = '$clangBinDir/clang-format'; |
| 14 | + |
| 15 | +final currentPlatformBuildtoolsSubdir = switch (Abi.current()) { |
| 16 | + Abi.macosX64 => 'mac-x64', |
| 17 | + Abi.macosArm64 => 'mac-arm64', |
| 18 | + Abi.linuxX64 => 'linux-x64', |
| 19 | + Abi.linuxArm64 => 'linux-arm64', |
| 20 | + Abi.windowsX64 => 'win-x64', |
| 21 | + Abi.windowsArm64 => 'win-arm64', |
| 22 | + _ => throw UnimplementedError(), |
| 23 | +}; |
| 24 | + |
| 25 | +void main(List<String> args) { |
| 26 | + final checkUpToDate = args.contains('--check-up-to-date'); |
| 27 | + |
| 28 | + final dartApiHFile = File('$runtimeRoot/include/dart_api.h'); |
| 29 | + final dartNativeApiHFile = File('$runtimeRoot/include/dart_native_api.h'); |
| 30 | + final dartApiWinCFile = File('$runtimeRoot/bin/dart_api_win.c'); |
| 31 | + final dartApiWinCTmpFile = File('$runtimeRoot/bin/dart_api_win_tmp.c'); |
| 32 | + final dartApiHContents = dartApiHFile.readAsStringSync(); |
| 33 | + final dartNativeApiHContents = dartNativeApiHFile.readAsStringSync(); |
| 34 | + |
| 35 | + final procedureRegexp = RegExp( |
| 36 | + r'(DART_\w+\s+)+(?<returnType>[\w\s\*]+)\s+(?<name>\w+)\((?<arguments>[^)]*)\);', |
| 37 | + multiLine: true, |
| 38 | + ); |
| 39 | + |
| 40 | + final matches = [ |
| 41 | + ...procedureRegexp.allMatches(dartApiHContents), |
| 42 | + ...procedureRegexp.allMatches(dartNativeApiHContents), |
| 43 | + ]; |
| 44 | + |
| 45 | + final procedures = <Procedure>[]; |
| 46 | + |
| 47 | + for (final match in matches) { |
| 48 | + final returnType = match.namedGroup('returnType')!; |
| 49 | + final name = match.namedGroup('name')!; |
| 50 | + final argumentsString = match.namedGroup('arguments') ?? ''; |
| 51 | + final argumentList = |
| 52 | + argumentsString.split(',').where((arg) => arg != 'void').map((arg) { |
| 53 | + final parts = arg.trim().split(' '); |
| 54 | + return ( |
| 55 | + type: parts.sublist(0, parts.length - 1).join(' '), |
| 56 | + name: parts[parts.length - 1], |
| 57 | + ); |
| 58 | + }).toList(); |
| 59 | + procedures.add(( |
| 60 | + name: name, |
| 61 | + returnType: returnType, |
| 62 | + arguments: argumentList, |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + final buffer = StringBuffer(); |
| 67 | + |
| 68 | + buffer.writeln(''' |
| 69 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 70 | +// for details. All rights reserved. Use of this source code is governed by a |
| 71 | +// BSD-style license that can be found in the LICENSE file. |
| 72 | +
|
| 73 | +// DO NOT EDIT. This file is generated by runtime/tools/generate_dart_api_win_c.dart. |
| 74 | +
|
| 75 | +#include <windows.h> |
| 76 | +
|
| 77 | +#include <include/dart_api.h> |
| 78 | +#include <include/dart_native_api.h> |
| 79 | +
|
| 80 | +'''); |
| 81 | + |
| 82 | + // Generate typedefs for all procedures. |
| 83 | + for (final procedure in procedures) { |
| 84 | + buffer.write('typedef '); |
| 85 | + buffer.write(procedure.returnType); |
| 86 | + buffer.write(' (*'); |
| 87 | + buffer.write(procedure.typedefName); |
| 88 | + buffer.write(')('); |
| 89 | + for (final (i, argument) in procedure.arguments.indexed) { |
| 90 | + buffer.write(argument.type); |
| 91 | + if (i < procedure.arguments.length - 1) { |
| 92 | + buffer.write(', '); |
| 93 | + } |
| 94 | + } |
| 95 | + buffer.writeln(');'); |
| 96 | + } |
| 97 | + |
| 98 | + buffer.writeln(); |
| 99 | + |
| 100 | + // Generate function pointers for all procedures. |
| 101 | + for (final procedure in procedures) { |
| 102 | + buffer.write('static '); |
| 103 | + buffer.write(procedure.typedefName); |
| 104 | + buffer.write(' '); |
| 105 | + buffer.write(procedure.functionPointerName); |
| 106 | + buffer.writeln(' = NULL;'); |
| 107 | + } |
| 108 | + |
| 109 | + buffer.writeln(); |
| 110 | + |
| 111 | + // Generate the DllMain function that initializes all function pointers. |
| 112 | + buffer.writeln(''' |
| 113 | +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { |
| 114 | + if (fdwReason == DLL_PROCESS_ATTACH) { |
| 115 | + HMODULE process = GetModuleHandle(NULL); |
| 116 | +'''); |
| 117 | + |
| 118 | + for (final procedure in procedures) { |
| 119 | + buffer.write(' '); |
| 120 | + buffer.write(procedure.functionPointerName); |
| 121 | + buffer.write(' = ('); |
| 122 | + buffer.write(procedure.typedefName); |
| 123 | + buffer.write(') GetProcAddress(process, "'); |
| 124 | + buffer.write(procedure.name); |
| 125 | + buffer.writeln('");'); |
| 126 | + } |
| 127 | + |
| 128 | + buffer.writeln(''' |
| 129 | + } |
| 130 | +
|
| 131 | + return TRUE; |
| 132 | +}'''); |
| 133 | + |
| 134 | + buffer.writeln(); |
| 135 | + |
| 136 | + // Generate redirecting implementations for all procedures. |
| 137 | + for (final procedure in procedures) { |
| 138 | + final (:name, :returnType, :arguments) = procedure; |
| 139 | + buffer.write(returnType); |
| 140 | + buffer.write(' '); |
| 141 | + buffer.write(name); |
| 142 | + buffer.write('('); |
| 143 | + for (final (i, (:name, :type)) in arguments.indexed) { |
| 144 | + buffer.write(type); |
| 145 | + buffer.write(' '); |
| 146 | + buffer.write(name); |
| 147 | + if (i < arguments.length - 1) { |
| 148 | + buffer.write(', '); |
| 149 | + } |
| 150 | + } |
| 151 | + buffer.writeln(') {'); |
| 152 | + buffer.write(' '); |
| 153 | + if (procedure.returnType != 'void') { |
| 154 | + buffer.write('return '); |
| 155 | + } |
| 156 | + buffer.write(procedure.functionPointerName); |
| 157 | + buffer.write('('); |
| 158 | + for (final (i, argument) in arguments.indexed) { |
| 159 | + buffer.write(argument.name); |
| 160 | + if (i < arguments.length - 1) { |
| 161 | + buffer.write(', '); |
| 162 | + } |
| 163 | + } |
| 164 | + buffer.writeln(');'); |
| 165 | + buffer.writeln('}'); |
| 166 | + buffer.writeln(); |
| 167 | + } |
| 168 | + |
| 169 | + buffer.writeln(); |
| 170 | + |
| 171 | + dartApiWinCTmpFile.writeAsStringSync(buffer.toString()); |
| 172 | + |
| 173 | + try { |
| 174 | + // Run clang-format on the generated file. |
| 175 | + final clangFormatResult = Process.runSync( |
| 176 | + clangFormatBin, |
| 177 | + ['-i', dartApiWinCTmpFile.path], |
| 178 | + // Allows us to specify the path to the clang-format binary without the |
| 179 | + // .exe extension on Windows. |
| 180 | + runInShell: Platform.isWindows, |
| 181 | + ); |
| 182 | + if (clangFormatResult.exitCode != 0) { |
| 183 | + print(clangFormatResult.stdout); |
| 184 | + print(clangFormatResult.stderr); |
| 185 | + exitCode = 1; |
| 186 | + } else { |
| 187 | + final changed = |
| 188 | + !dartApiWinCFile.existsSync() || |
| 189 | + dartApiWinCTmpFile.readAsStringSync() != |
| 190 | + dartApiWinCFile.readAsStringSync(); |
| 191 | + if (changed) { |
| 192 | + if (checkUpToDate) { |
| 193 | + exitCode = 1; |
| 194 | + } else { |
| 195 | + dartApiWinCTmpFile.copySync(dartApiWinCFile.path); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } finally { |
| 200 | + dartApiWinCTmpFile.deleteSync(); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +typedef Procedure = |
| 205 | + ({ |
| 206 | + String name, |
| 207 | + String returnType, |
| 208 | + List<({String name, String type})> arguments, |
| 209 | + }); |
| 210 | + |
| 211 | +extension on Procedure { |
| 212 | + String get typedefName => '${name}Type'; |
| 213 | + String get functionPointerName => '${name}Fn'; |
| 214 | +} |
0 commit comments