From 4f9b473da26a98818983bbd01d4c4a75617c6e0e Mon Sep 17 00:00:00 2001 From: Moritz Date: Fri, 16 Aug 2024 16:29:07 +0200 Subject: [PATCH 1/2] Add to method --- pkgs/intl4x/hook/build.dart | 171 ++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/pkgs/intl4x/hook/build.dart b/pkgs/intl4x/hook/build.dart index 5b838380..04f378ab 100644 --- a/pkgs/intl4x/hook/build.dart +++ b/pkgs/intl4x/hook/build.dart @@ -153,104 +153,105 @@ final class CheckoutMode extends BuildMode { List get dependencies => [ Uri.directory(workingDirectory!).resolve('Cargo.lock'), ]; -} -Future buildLib(BuildConfig config, String workingDirectory) async { - final dylibFileName = - config.targetOS.dylibFileName(crateName.replaceAll('-', '_')); - final dylibFileUri = config.outputDirectory.resolve(dylibFileName); - if (!config.dryRun) { - final rustTarget = _asRustTarget( - config.targetOS, - config.dryRun ? null : config.targetArchitecture!, - config.targetOS == OS.iOS && - config.targetIOSSdk == IOSSdk.iPhoneSimulator, - ); - final isNoStd = - _isNoStdTarget((config.targetOS, config.targetArchitecture)); - - if (!isNoStd) { - final rustArguments = ['target', 'add', rustTarget]; - final rustup = await Process.run( - 'rustup', - rustArguments, - workingDirectory: workingDirectory, + Future buildLib(BuildConfig config, String workingDirectory) async { + final dylibFileName = + config.targetOS.dylibFileName(crateName.replaceAll('-', '_')); + final dylibFileUri = config.outputDirectory.resolve(dylibFileName); + if (!config.dryRun) { + final rustTarget = _asRustTarget( + config.targetOS, + config.dryRun ? null : config.targetArchitecture!, + config.targetOS == OS.iOS && + config.targetIOSSdk == IOSSdk.iPhoneSimulator, ); + final isNoStd = + _isNoStdTarget((config.targetOS, config.targetArchitecture)); - if (rustup.exitCode != 0) { - throw ProcessException( + if (!isNoStd) { + final rustArguments = ['target', 'add', rustTarget]; + final rustup = await Process.run( 'rustup', rustArguments, - rustup.stderr.toString(), - rustup.exitCode, + workingDirectory: workingDirectory, ); - } - } - final tempDir = await Directory.systemTemp.createTemp(); - - final stdFeatures = [ - 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', - 'compiled_data', - 'buffer_provider', - 'logging', - 'simple_logger', - 'experimental_components', - ]; - final noStdFeatures = [ - 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', - 'compiled_data', - 'buffer_provider', - 'libc-alloc', - 'panic-handler', - 'experimental_components', - ]; - final linkModeType = config.linkModePreference == LinkModePreference.static - ? 'staticlib' - : 'cdylib'; - final arguments = [ - if (isNoStd) '+nightly', - 'rustc', - '-p=$crateName', - '--crate-type=$linkModeType', - '--release', - '--config=profile.release.panic="abort"', - '--config=profile.release.codegen-units=1', - '--no-default-features', - if (!isNoStd) '--features=${stdFeatures.join(',')}', - if (isNoStd) '--features=${noStdFeatures.join(',')}', - if (isNoStd) '-Zbuild-std=core,alloc', - if (isNoStd) '-Zbuild-std-features=panic_immediate_abort', - '--target=$rustTarget', - '--target-dir=${tempDir.path}' - ]; - final cargo = await Process.run( - 'cargo', - arguments, - workingDirectory: workingDirectory, - ); - if (cargo.exitCode != 0) { - throw ProcessException( + if (rustup.exitCode != 0) { + throw ProcessException( + 'rustup', + rustArguments, + rustup.stderr.toString(), + rustup.exitCode, + ); + } + } + final tempDir = await Directory.systemTemp.createTemp(); + + final stdFeatures = [ + 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', + 'compiled_data', + 'buffer_provider', + 'logging', + 'simple_logger', + 'experimental_components', + ]; + final noStdFeatures = [ + 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', + 'compiled_data', + 'buffer_provider', + 'libc-alloc', + 'panic-handler', + 'experimental_components', + ]; + final linkModeType = + config.linkModePreference == LinkModePreference.static + ? 'staticlib' + : 'cdylib'; + final arguments = [ + if (isNoStd) '+nightly', + 'rustc', + '-p=$crateName', + '--crate-type=$linkModeType', + '--release', + '--config=profile.release.panic="abort"', + '--config=profile.release.codegen-units=1', + '--no-default-features', + if (!isNoStd) '--features=${stdFeatures.join(',')}', + if (isNoStd) '--features=${noStdFeatures.join(',')}', + if (isNoStd) '-Zbuild-std=core,alloc', + if (isNoStd) '-Zbuild-std-features=panic_immediate_abort', + '--target=$rustTarget', + '--target-dir=${tempDir.path}' + ]; + final cargo = await Process.run( 'cargo', arguments, - cargo.stderr.toString(), - cargo.exitCode, + workingDirectory: workingDirectory, ); - } - final builtPath = path.join( - tempDir.path, - rustTarget, - 'release', - dylibFileName, - ); - final file = File(builtPath); - if (!(await file.exists())) { - throw FileSystemException('Building the dylib failed', builtPath); + if (cargo.exitCode != 0) { + throw ProcessException( + 'cargo', + arguments, + cargo.stderr.toString(), + cargo.exitCode, + ); + } + + final builtPath = path.join( + tempDir.path, + rustTarget, + 'release', + dylibFileName, + ); + final file = File(builtPath); + if (!(await file.exists())) { + throw FileSystemException('Building the dylib failed', builtPath); + } + await file.copy(dylibFileUri.toFilePath(windows: Platform.isWindows)); } - await file.copy(dylibFileUri.toFilePath(windows: Platform.isWindows)); + return dylibFileUri; } - return dylibFileUri; } String _asRustTarget(OS os, Architecture? architecture, bool isSimulator) { From 04fabde9e5bfe026bf0d394ea817515335c9ddc4 Mon Sep 17 00:00:00 2001 From: Moritz Date: Fri, 16 Aug 2024 17:47:04 +0200 Subject: [PATCH 2/2] Use `RustBuilder` --- pkgs/intl4x/hook/build.dart | 130 +++++++++++++----------------------- pkgs/intl4x/pubspec.yaml | 5 +- 2 files changed, 49 insertions(+), 86 deletions(-) diff --git a/pkgs/intl4x/hook/build.dart b/pkgs/intl4x/hook/build.dart index 04f378ab..591f16ed 100644 --- a/pkgs/intl4x/hook/build.dart +++ b/pkgs/intl4x/hook/build.dart @@ -6,8 +6,8 @@ import 'dart:io'; import 'package:crypto/crypto.dart' show sha256; import 'package:native_assets_cli/native_assets_cli.dart'; -import 'package:path/path.dart' as path; - +import 'package:native_toolchain_rust/native_toolchain_rust.dart'; +import 'package:logging/logging.dart'; import 'hashes.dart'; import 'version.dart'; @@ -22,7 +22,7 @@ void main(List args) async { final environmentBuildMode = Platform.environment[env]; final buildMode = switch (environmentBuildMode) { 'local' => LocalMode(config), - 'checkout' => CheckoutMode(config), + 'checkout' => CheckoutMode(config, output), 'fetch' || null => FetchMode(config), String() => throw ArgumentError(''' @@ -39,14 +39,16 @@ Unknown build mode for icu4x. Set the `ICU4X_BUILD_MODE` environment variable wi // For debugging purposes output.addMetadatum(env, environmentBuildMode ?? 'fetch'); - output.addAsset(NativeCodeAsset( - package: package, - name: assetId, - linkMode: DynamicLoadingBundled(), - architecture: config.targetArchitecture, - os: config.targetOS, - file: builtLibrary, - )); + if (builtLibrary != null) { + output.addAsset(NativeCodeAsset( + package: package, + name: assetId, + linkMode: DynamicLoadingBundled(), + architecture: config.targetArchitecture, + os: config.targetOS, + file: builtLibrary, + )); + } output.addDependencies( [ @@ -64,7 +66,7 @@ sealed class BuildMode { List get dependencies; - Future build(); + Future build(); } final class FetchMode extends BuildMode { @@ -136,12 +138,13 @@ final class LocalMode extends BuildMode { } final class CheckoutMode extends BuildMode { - CheckoutMode(super.config); + final BuildOutput output; + CheckoutMode(super.config, this.output); String? get workingDirectory => Platform.environment['LOCAL_ICU4X_CHECKOUT']; @override - Future build() async { + Future build() async { if (workingDirectory == null) { throw ArgumentError('Specify the ICU4X checkout folder' 'with the LOCAL_ICU4X_CHECKOUT variable'); @@ -154,10 +157,7 @@ final class CheckoutMode extends BuildMode { Uri.directory(workingDirectory!).resolve('Cargo.lock'), ]; - Future buildLib(BuildConfig config, String workingDirectory) async { - final dylibFileName = - config.targetOS.dylibFileName(crateName.replaceAll('-', '_')); - final dylibFileUri = config.outputDirectory.resolve(dylibFileName); + Future buildLib(BuildConfig config, String workingDirectory) async { if (!config.dryRun) { final rustTarget = _asRustTarget( config.targetOS, @@ -170,87 +170,49 @@ final class CheckoutMode extends BuildMode { if (!isNoStd) { final rustArguments = ['target', 'add', rustTarget]; - final rustup = await Process.run( - 'rustup', + await Rustup.systemRustup()?.runCommand( rustArguments, - workingDirectory: workingDirectory, + logger: Logger(''), ); - - if (rustup.exitCode != 0) { - throw ProcessException( - 'rustup', - rustArguments, - rustup.stderr.toString(), - rustup.exitCode, - ); - } } - final tempDir = await Directory.systemTemp.createTemp(); - - final stdFeatures = [ - 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', - 'compiled_data', + final features = [ + 'icu_collator', + 'icu_datetime', + 'icu_list', + 'icu_decimal', + 'icu_plurals', + if (config.linkingEnabled) 'compiled_data', 'buffer_provider', - 'logging', - 'simple_logger', + if (!isNoStd) ...['logging', 'simple_logger'], + if (isNoStd) ...['libc-alloc', 'panic-handler'], 'experimental_components', ]; - final noStdFeatures = [ - 'icu_collator,icu_datetime,icu_list,icu_decimal,icu_plurals', - 'compiled_data', - 'buffer_provider', - 'libc-alloc', - 'panic-handler', - 'experimental_components', - ]; - final linkModeType = - config.linkModePreference == LinkModePreference.static - ? 'staticlib' - : 'cdylib'; final arguments = [ if (isNoStd) '+nightly', - 'rustc', - '-p=$crateName', - '--crate-type=$linkModeType', - '--release', '--config=profile.release.panic="abort"', '--config=profile.release.codegen-units=1', '--no-default-features', - if (!isNoStd) '--features=${stdFeatures.join(',')}', - if (isNoStd) '--features=${noStdFeatures.join(',')}', - if (isNoStd) '-Zbuild-std=core,alloc', - if (isNoStd) '-Zbuild-std-features=panic_immediate_abort', - '--target=$rustTarget', - '--target-dir=${tempDir.path}' + '--features=${features.join(',')}', + if (isNoStd) ...[ + '-Zbuild-std=core,alloc', + '-Zbuild-std-features=panic_immediate_abort' + ], ]; - final cargo = await Process.run( - 'cargo', - arguments, - workingDirectory: workingDirectory, - ); - if (cargo.exitCode != 0) { - throw ProcessException( - 'cargo', - arguments, - cargo.stderr.toString(), - cargo.exitCode, - ); - } - - final builtPath = path.join( - tempDir.path, - rustTarget, - 'release', - dylibFileName, + final rustBuilder = RustBuilder( + package: config.packageName, + assetName: assetId, + cratePath: crateName, + buildConfig: config, + extraCargoArgs: arguments, + buildMode: CargoBuildMode.rustc, + crateType: + config.linkingEnabled ? CrateType.staticlib : CrateType.cdylib, + release: true, ); - final file = File(builtPath); - if (!(await file.exists())) { - throw FileSystemException('Building the dylib failed', builtPath); - } - await file.copy(dylibFileUri.toFilePath(windows: Platform.isWindows)); + await rustBuilder.run(output: output); } - return dylibFileUri; + return null; } } diff --git a/pkgs/intl4x/pubspec.yaml b/pkgs/intl4x/pubspec.yaml index a1a444c2..1d557b12 100644 --- a/pkgs/intl4x/pubspec.yaml +++ b/pkgs/intl4x/pubspec.yaml @@ -20,9 +20,10 @@ dependencies: crypto: ^3.0.3 ffi: ^2.1.0 js: ^0.7.1 + logging: ^1.2.0 meta: ^1.12.0 - native_assets_cli: ^0.6.0 - path: ^1.9.0 + native_assets_cli: ^0.7.2 + native_toolchain_rust: ^0.1.0-dev.7 dev_dependencies: args: ^2.4.2