Skip to content

Commit 837a3e7

Browse files
committed
feat: extend build hook to support Windows (tc_helper.dll) auto-compilation
1 parent 38d8193 commit 837a3e7

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

hook/build.dart

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ import 'package:native_assets_cli/code_assets.dart';
44

55
void main(List<String> args) async {
66
await build(args, (input, output) async {
7-
// Check if code assets are supported/requested
8-
// Note: hook/build.dart is only called if the package has native assets.
9-
107
final targetOS = input.config.code.targetOS;
11-
if (targetOS != OS.linux) return;
8+
9+
// Only support Linux and Windows for now.
10+
// macOS support will be added once tested on a Mac environment.
11+
if (targetOS != OS.linux && targetOS != OS.windows) return;
1212

1313
final rustDir = input.packageRoot.resolve('rust/');
14-
14+
15+
// Determine the correct library filename for the target OS
16+
final String libName;
17+
if (targetOS == OS.windows) {
18+
libName = 'tc_helper.dll';
19+
} else {
20+
libName = 'libtc_helper.so';
21+
}
22+
1523
// 1. Run cargo build --release
1624
final result = await Process.run(
1725
'cargo',
@@ -22,21 +30,23 @@ void main(List<String> args) async {
2230
if (result.exitCode != 0) {
2331
stdout.write(result.stdout);
2432
stderr.write(result.stderr);
25-
throw Exception('Rust build failed');
33+
throw Exception('Rust build failed on $targetOS');
2634
}
2735

28-
// 2. Locate the library
29-
final libName = 'libtc_helper.so';
36+
// 2. Locate the compiled library
3037
final libPath = rustDir.resolve('target/release/$libName');
3138

3239
if (!await File.fromUri(libPath).exists()) {
33-
throw Exception('Built library not found at $libPath');
40+
throw Exception(
41+
'Built library not found at $libPath.\n'
42+
'Make sure Rust and Cargo are installed and the build succeeded.',
43+
);
3444
}
3545

36-
// 3. Add the asset to the output
46+
// 3. Register the native asset with Flutter
3747
output.assets.code.add(CodeAsset(
3848
package: input.packageName,
39-
name: 'main.dart',
49+
name: 'main.dart',
4050
linkMode: DynamicLoadingBundled(),
4151
file: libPath,
4252
));

0 commit comments

Comments
 (0)