|
| 1 | +// Copyright (c) 2023, 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 'package:logging/logging.dart'; |
| 6 | +import 'package:native_assets_cli/native_assets_cli.dart'; |
| 7 | +import 'package:native_toolchain_c/native_toolchain_c.dart'; |
| 8 | + |
| 9 | +const packageName = 'native_split_library'; |
| 10 | + |
| 11 | +/// Implements the protocol from `package:native_assets_cli` by building |
| 12 | +/// the C code in `src/` and reporting what native assets it built. |
| 13 | +void main(List<String> args) async { |
| 14 | + // Parse the build configuration passed to this CLI from Dart or Flutter. |
| 15 | + final buildConfig = await BuildConfig.fromArgs(args); |
| 16 | + final buildOutput = BuildOutput(); |
| 17 | + |
| 18 | + // Configure `package:native_toolchain_c` to build the C code for us. |
| 19 | + final addCBuilder = CBuilder.library( |
| 20 | + name: '${packageName}_add', |
| 21 | + assetId: 'package:$packageName/native_add.dart', |
| 22 | + sources: [ |
| 23 | + 'src/native_add.c', |
| 24 | + ], |
| 25 | + ); |
| 26 | + await addCBuilder.run( |
| 27 | + buildConfig: buildConfig, |
| 28 | + // `package:native_toolchain_c` will output the dynamic or static libraries it built, |
| 29 | + // what files it accessed (for caching the build), etc. |
| 30 | + buildOutput: buildOutput, |
| 31 | + logger: Logger('')..onRecord.listen((record) => print(record.message)), |
| 32 | + ); |
| 33 | + |
| 34 | + final addLibraryAsset = buildOutput.assets.firstWhere( |
| 35 | + (asset) => asset.id == 'package:$packageName/native_add.dart', |
| 36 | + ); |
| 37 | + |
| 38 | + final multiplyCBuilder = CBuilder.library( |
| 39 | + name: '${packageName}_multiply', |
| 40 | + assetId: 'package:$packageName/native_multiply.dart', |
| 41 | + includes: [ |
| 42 | + 'src', |
| 43 | + ], |
| 44 | + sources: [ |
| 45 | + 'src/native_multiply.c', |
| 46 | + ], |
| 47 | + flags: [ |
| 48 | + (addLibraryAsset.path as AssetAbsolutePath).uri.toFilePath(), |
| 49 | + ], |
| 50 | + ); |
| 51 | + await multiplyCBuilder.run( |
| 52 | + buildConfig: buildConfig, |
| 53 | + // `package:native_toolchain_c` will output the dynamic or static libraries it built, |
| 54 | + // what files it accessed (for caching the build), etc. |
| 55 | + buildOutput: buildOutput, |
| 56 | + logger: Logger('')..onRecord.listen((record) => print(record.message)), |
| 57 | + ); |
| 58 | + |
| 59 | + // Write the output according to the native assets protocol so that Dart or |
| 60 | + // Flutter can find the native assets produced by this script. |
| 61 | + await buildOutput.writeToFile(outDir: buildConfig.outDir); |
| 62 | +} |
0 commit comments