Skip to content

Commit

Permalink
Add hashes for datagen
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Aug 23, 2024
1 parent bf4cf65 commit 455a7db
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkgs/intl4x/hook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
1. Create PR.
2. Run `bash tools/regenerate_bindings.sh`, and fix resulting errors.
3. Update `const version` in `version.dart` to tag.
4. Regenerate hashes using `cd pkgs/intl4x/; dart --enable-experiment=native-assets run tool/regenerate_hashes.dart`.
4. Regenerate hashes using `cd pkgs/intl4x/; dart --enable-experiment=native-assets tool/regenerate_hashes.dart`.
5. Land PR.
48 changes: 30 additions & 18 deletions pkgs/intl4x/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,47 @@ final class FetchMode extends BuildMode {
config.outputDirectory.resolve(config.filename('icu4x')),
);

final bytes = await library.readAsBytes();
final fileHash = sha256.convert(bytes).toString();
final expectedFileHash =
fileHashes[(config.targetOS, config.targetArchitecture, libraryType)];
if (fileHash != expectedFileHash) {
throw Exception(
'The pre-built binary for the target $target at $dylibRemoteUri has a'
' hash of $fileHash, which does not match $expectedFileHash fixed in'
' the build hook of package:intl4x.');
}

final datagenName = config.targetOS.executableFileName('$target-datagen');
final datagenUri = Uri.parse(
'https://github.com/dart-lang/i18n/releases/download/$version/$datagenName');
final datagen = await fetchToFile(
Uri.parse(
'https://github.com/dart-lang/i18n/releases/download/$version/$datagenName'),
datagenUri,
config.outputDirectory.resolve('datagen'),
);

final datagenBytes = await datagen.readAsBytes();
final datagenHash = sha256.convert(datagenBytes).toString();
final expectedDatagenHash =
datagenHashes[(config.targetOS, config.targetArchitecture)];
if (datagenHash != expectedDatagenHash) {
throw Exception(
'The pre-built binary for the target $target at $datagenUri has a'
' hash of $datagenHash, which does not match $expectedDatagenHash '
'fixed in the build hook of package:intl4x.');
}

final postcard = await fetchToFile(
Uri.parse(
'https://github.com/dart-lang/i18n/releases/download/$version/full.postcard'),
config.outputDirectory.resolve('full.postcard'),
);

final bytes = await library.readAsBytes();
final fileHash = sha256.convert(bytes).toString();
final expectedFileHash =
fileHashes[(config.targetOS, config.targetArchitecture, libraryType)];
if (fileHash == expectedFileHash) {
return BuildResult(
library: library.uri,
datagen: datagen.uri,
postcard: postcard.uri,
);
} else {
throw Exception(
'The pre-built binary for the target $target at $dylibRemoteUri has a'
' hash of $fileHash, which does not match $expectedFileHash fixed in'
' the build hook of package:intl4x.');
}
return BuildResult(
library: library.uri,
datagen: datagen.uri,
postcard: postcard.uri,
);
}

Future<File> fetchToFile(Uri uri, Uri fileUri) async {
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl4x/hook/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void main(List<String> arguments) {
final dylib = output.assets.first;

final datagen = datagenTool.file!.toFilePath();

if (OS.current == OS.linux) {
await runProcess('chmod', ['+x', datagen]);
}
Expand Down
15 changes: 14 additions & 1 deletion pkgs/intl4x/lib/src/hook_helpers/hashes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// THIS FILE IS AUTOGENERATED BY `tool/regenerate_hashes.dart`. TO UPDATE, RUN
//
// dart --enable-experiment=native-assets run tool/regenerate_hashes.dart
// dart --enable-experiment=native-assets tool/regenerate_hashes.dart
//

import 'package:native_assets_cli/native_assets_cli.dart';
Expand Down Expand Up @@ -75,3 +75,16 @@ const fileHashes = <(OS, Architecture, String), String>{
(OS.iOS, Architecture.x64, 'static'):
'6e4e4072126b3017c6a532575a165d1a80b2bccb59a24108682483935c98ff19'
};

const datagenHashes = <(OS, Architecture), String>{
(OS.linux, Architecture.arm64):
'b44dd95e9622f84552dd0599028bd867cdf3f14a1c3d807a39a710a3f2e3e491',
(OS.linux, Architecture.riscv64):
'fdee9c86eb6d8abad33c1ec2a343dc3b66ffd54412a448a38be2b17ea1d28241',
(OS.linux, Architecture.x64):
'10add51f8739a7e87907a1453f58c6fc268b06489bd1d51502e142db7e33eb28',
(OS.macOS, Architecture.arm64):
'019fb83b2a59f366afc21dea30b69ebaa65f16026cd55c57075617c6ca89c61e',
(OS.macOS, Architecture.x64):
'31908391cecd0c842bf9489e08eec57ccdaa5b82ad2f887e316303938011f0b7'
};
47 changes: 35 additions & 12 deletions pkgs/intl4x/tool/regenerate_hashes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ final httpClient = HttpClient();
Future<void> main(List<String> args) async {
print('Checking hashes for $version');
final fileHashes = <(String, Architecture, String), String>{};
final dynamicLibrary = File.fromUri(Directory.systemTemp.uri.resolve('lib'));
await dynamicLibrary.create();
final datagenHashes = <(String, Architecture), String>{};
final file = File.fromUri(Directory.systemTemp.uri.resolve('lib'));
await file.create();
for (final os in ['linux', 'windows', 'fuchsia', 'android', 'macOS', 'iOS']) {
for (final architecture in Architecture.values) {
final target = '${os}_$architecture-datagen';
final datagenHash = await getHash(target, file);
if (datagenHash != null) {
datagenHashes[(os, architecture)] = datagenHash;
}
for (final libraryType in ['dynamic', 'static']) {
final target = [os, architecture, libraryType].join('_');
print('Checking hash for $target');
final success = await _fetchLibrary(target, httpClient, dynamicLibrary);
if (success) {
final bytes = await dynamicLibrary.readAsBytes();
final fileHash = sha256.convert(bytes).toString();
final target = '${os}_${architecture}_$libraryType';
final fileHash = await getHash(target, file);
if (fileHash != null) {
fileHashes[(os, architecture, libraryType)] = fileHash;
print('Hash is $fileHash');
} else {
print('Could not fetch library');
}
}
}
Expand All @@ -37,7 +37,7 @@ Future<void> main(List<String> args) async {
// THIS FILE IS AUTOGENERATED BY `tool/regenerate_hashes.dart`. TO UPDATE, RUN
//
// dart --enable-experiment=native-assets run tool/regenerate_hashes.dart
// dart --enable-experiment=native-assets tool/regenerate_hashes.dart
//
import 'package:native_assets_cli/native_assets_cli.dart';
Expand All @@ -50,9 +50,32 @@ ${fileHashes.map((key, value) => MapEntry(
(e) => ' ${e.key}:\n ${e.value}',
).join(',\n')}
};
const datagenHashes = <(OS, Architecture), String>{
${datagenHashes.map((key, value) => MapEntry(
('OS.${key.$1}', 'Architecture.${key.$2}'),
"'$value'",
)).entries.map(
(e) => ' ${e.key}:\n ${e.value}',
).join(',\n')}
};
''');
}

Future<String?> getHash(String target, File dynamicLibrary) async {
print('Checking hash for $target');
final success = await _fetchLibrary(target, httpClient, dynamicLibrary);
if (success) {
final bytes = await dynamicLibrary.readAsBytes();
final fileHash = sha256.convert(bytes).toString();
print('Hash is $fileHash');
return fileHash;
} else {
print('Could not fetch library');
return null;
}
}

Future<bool> _fetchLibrary(
String target, HttpClient httpClient, File dynamicLibrary) async {
final uri = Uri.parse(
Expand Down

0 comments on commit 455a7db

Please sign in to comment.