Skip to content

Commit 2e55936

Browse files
committed
[native_assets_cli] Add example for downloading assets
1 parent 598c969 commit 2e55936

File tree

16 files changed

+480
-2
lines changed

16 files changed

+480
-2
lines changed

.github/workflows/download_asset.yaml

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: download_asset
2+
3+
permissions:
4+
contents: write
5+
6+
on:
7+
pull_request:
8+
branches: [ main ]
9+
paths:
10+
- .github/workflows/download_asset.yaml
11+
- pkgs/native_assets_cli/example/build/download_asset/
12+
push:
13+
tags:
14+
- 'download_asset-prebuild-assets-*'
15+
workflow_dispatch:
16+
17+
jobs:
18+
build:
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
os: [ubuntu, macos, windows]
23+
24+
runs-on: ${{ matrix.os }}-latest
25+
26+
defaults:
27+
run:
28+
working-directory: pkgs/native_assets_cli/example/build/download_asset/
29+
30+
steps:
31+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
32+
33+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
34+
with:
35+
sdk: stable
36+
37+
- uses: nttld/setup-ndk@afb4c9964b521afb97c864b7d40b11e6911bd410
38+
with:
39+
ndk-version: r27
40+
if: ${{ matrix.os == 'ubuntu' }} # Only build on one host.
41+
42+
- name: Install native toolchains
43+
run: sudo apt-get update && sudo apt-get install clang-15 gcc-i686-linux-gnu gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf gcc-riscv64-linux-gnu
44+
if: ${{ matrix.os == 'ubuntu' }}
45+
46+
- run: dart pub get
47+
48+
- name: Build Linux host
49+
if: matrix.os == 'ubuntu'
50+
run: |
51+
dart tool/build.dart -oandroid -aarm
52+
dart tool/build.dart -oandroid -aarm64
53+
dart tool/build.dart -oandroid -aia32
54+
dart tool/build.dart -oandroid -ariscv64
55+
dart tool/build.dart -oandroid -ax64
56+
dart tool/build.dart -olinux -aarm
57+
dart tool/build.dart -olinux -aarm64
58+
dart tool/build.dart -olinux -aia32
59+
dart tool/build.dart -olinux -ariscv64
60+
dart tool/build.dart -olinux -ax64
61+
62+
- name: Build MacOS host
63+
if: matrix.os == 'macos'
64+
run: |
65+
dart tool/build.dart -omacos -aarm64
66+
dart tool/build.dart -omacos -ax64
67+
dart tool/build.dart -oios -iiphoneos -aarm64
68+
dart tool/build.dart -oios -iiphonesimulator -aarm64
69+
dart tool/build.dart -oios -iiphonesimulator -ax64
70+
71+
- name: Build Windows host
72+
if: matrix.os == 'windows'
73+
run: |
74+
dart tool/build.dart -owindows -aarm
75+
dart tool/build.dart -owindows -aarm64
76+
dart tool/build.dart -owindows -aia32
77+
dart tool/build.dart -owindows -ax64
78+
79+
- name: Upload artifacts
80+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
81+
with:
82+
name: ${{ matrix.os }}-host
83+
path: |
84+
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.dll
85+
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.dylib
86+
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.so
87+
if-no-files-found: error
88+
89+
check:
90+
needs: build
91+
runs-on: ubuntu-latest
92+
93+
defaults:
94+
run:
95+
working-directory: pkgs/native_assets_cli/example/build/download_asset/
96+
97+
steps:
98+
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
99+
with:
100+
submodules: true
101+
102+
- name: Download assets
103+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
104+
with:
105+
merge-multiple: true
106+
path: pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/
107+
108+
- name: Display structure of downloaded assets
109+
run: ls -R .dart_tool/download_asset/
110+
111+
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
112+
with:
113+
sdk: stable
114+
115+
- run: dart pub get
116+
117+
- name: Check hashes of released artifacts
118+
run: |
119+
dart tool/generate_asset_hashes.dart
120+
git diff --exit-code
121+
122+
- name: Release
123+
uses: softprops/action-gh-release@e7a8f85e1c67a31e6ed99a94b41bd0b71bbee6b8
124+
if: startsWith(github.ref, 'refs/tags/download_asset-prebuild-assets')
125+
with:
126+
files: '.dart_tool/download_asset/**'
127+
fail_on_unmatched_files: true

.github/workflows/native.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ jobs:
105105
- run: dart pub get -C test_data/native_dynamic_linking/
106106
if: ${{ matrix.package == 'native_assets_builder' }}
107107

108+
- run: dart pub get -C example/build/download_asset/
109+
if: ${{ matrix.package == 'native_assets_cli' }}
110+
108111
- run: dart pub get -C example/build/native_dynamic_linking/
109112
if: ${{ matrix.package == 'native_assets_cli' }}
110113

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
An example of a library depending on prebuilt assets which are downloaded in
2+
the build hook.
3+
4+
## Usage
5+
6+
Run tests with `dart --enable-experiment=native-assets test`.
7+
8+
## Code organization
9+
10+
A typical layout of a package which downloads assets:
11+
12+
* `hook/build.dart` downloads the prebuilt assets.
13+
* `lib/` contains Dart code which uses the assets.
14+
* `tool/build.dart` prebuilts assets and is exercised from the GitHub CI.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `src/native_add.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
7+
output: 'lib/native_add.dart'
8+
headers:
9+
entry-points:
10+
- 'src/native_add.h'
11+
include-directives:
12+
- 'src/native_add.h'
13+
preamble: |
14+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
15+
// for details. All rights reserved. Use of this source code is governed by a
16+
// BSD-style license that can be found in the LICENSE file.
17+
comments:
18+
style: any
19+
length: full
20+
ffi-native:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 'package:download_asset/src/hook_helpers/c_build.dart';
6+
import 'package:native_assets_cli/native_assets_cli.dart';
7+
8+
void main(List<String> args) async {
9+
const localBuild = false;
10+
11+
await build(args, (config, output) async {
12+
// ignore: dead_code
13+
if (localBuild) {
14+
await runBuild(config, output);
15+
} else {
16+
// TODO(dcharkes): Download from GitHub.
17+
}
18+
});
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
12+
external int add(
13+
int a,
14+
int b,
15+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 'package:logging/logging.dart';
6+
import 'package:native_assets_cli/code_assets_builder.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
Future<void> runBuild(BuildConfig config, BuildOutputBuilder output) async {
10+
final cbuilder = CBuilder.library(
11+
name: 'native_add',
12+
assetName: 'native_add.dart',
13+
sources: [
14+
'src/native_add.c',
15+
],
16+
);
17+
await cbuilder.run(
18+
config: config,
19+
output: output,
20+
logger: Logger('')
21+
..level = Level.ALL
22+
..onRecord.listen((record) => print(record.message)),
23+
);
24+
}
25+
26+
String createTargetName(String osString, String architecture, String? iOSSdk) {
27+
var targetName = '${osString}_$architecture';
28+
if (iOSSdk != null) {
29+
targetName += '_$iOSSdk';
30+
}
31+
return targetName;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// THIS FILE IS AUTOGENERATED. TO UPDATE, RUN
6+
//
7+
// dart --enable-experiment=native-assets tool/generate_asset_hashes.dart
8+
//
9+
10+
const assetHashes = <String, String>{
11+
'android_arm': '2c38f3edc805a399dad866d619f9157d',
12+
'android_arm64': 'c4f0d8c4c50d1e83592e499e7434b967',
13+
'android_ia32': 'e3277144d97bd2c54beee581ed7e6665',
14+
'android_riscv64': '8c2576cbe75c9a23f2532ff895f94f76',
15+
'android_x64': '9a7bec53e1591091669ecd2bd20911d1',
16+
'ios_arm64_iphoneos': 'e9a4d8b4d47efc05da36c7b05991d174',
17+
'ios_arm64_iphonesimulator': 'a745567274206e879a267c4f6ab998db',
18+
'ios_x64_iphonesimulator': '6568ce774690893512c108bb4e2d1974',
19+
'linux_arm': '1a5b9e4b459e13ee85c148582b9b2252',
20+
'linux_arm64': '2b3d736e0c0ac1e1537bc43bd9b82cad',
21+
'linux_ia32': 'ed6e130e53fa18eab5572ed106cdaab1',
22+
'linux_riscv64': '7fa82325ba7803a0443ca27e3300e7f9',
23+
'linux_x64': 'f7af8d1547cdfb150a73d513a7957999',
24+
'macos_arm64': '075398c86f0a13f0ee166bc8aaf1e41a',
25+
'macos_x64': 'ed56424be52561da5a57d3f5af837ce2',
26+
'windows_arm64': 'c0a52c089afa52f6532717c4c91e904f',
27+
'windows_ia32': '70b7c31e4546764618600f380a0db869',
28+
'windows_x64': '73d2f8f4daf35111370a8a6fa380ac69',
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
const androidTargetNdkApi = 30;
6+
const int macOSTargetVersion = 13;
7+
const iOSTargetVersion = 16;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
publish_to: none
2+
3+
name: download_asset
4+
description: Sums two numbers with native code, prebuilt assets.
5+
version: 0.1.0
6+
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/build/download_asset
7+
8+
environment:
9+
sdk: '>=3.3.0 <4.0.0'
10+
11+
dependencies:
12+
logging: ^1.1.1
13+
native_assets_cli: ^0.10.0
14+
# native_assets_cli:
15+
# path: ../../../../native_assets_cli/
16+
native_toolchain_c: ^0.7.0
17+
# native_toolchain_c:
18+
# path: ../../../../native_toolchain_c/
19+
20+
dev_dependencies:
21+
args: ^2.6.0
22+
crypto: ^3.0.3
23+
ffigen: ^8.0.2
24+
lints: ^3.0.0
25+
test: ^1.21.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
#include "native_add.h"
6+
7+
int32_t add(int32_t a, int32_t b) { return a + b; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
#include <stdint.h>
6+
7+
#if _WIN32
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#else
10+
#define MYLIB_EXPORT
11+
#endif
12+
13+
MYLIB_EXPORT int32_t add(int32_t a, int32_t b);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 'package:download_asset/native_add.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
test('invoke native function', () {
10+
expect(add(24, 18), 42);
11+
});
12+
}

0 commit comments

Comments
 (0)