Skip to content

Commit 9b13058

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

File tree

13 files changed

+396
-2
lines changed

13 files changed

+396
-2
lines changed

.github/workflows/download_asset.yaml

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
name: Prebuilt native assets for package: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-native-libraries:
19+
name: Prebuilt native libraries
20+
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
os: [ubuntu, macos, windows]
25+
26+
runs-on: ${{ matrix.os }}-latest
27+
28+
defaults:
29+
run:
30+
working-directory: pkgs/native_assets_cli/example/build/download_asset/
31+
32+
steps:
33+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
34+
35+
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
36+
with:
37+
sdk: stable
38+
39+
- uses: nttld/setup-ndk@afb4c9964b521afb97c864b7d40b11e6911bd410
40+
with:
41+
ndk-version: r27
42+
if: ${{ matrix.os == 'linux' }} # Only build on one host.
43+
44+
- run: dart pub get
45+
46+
- name: Build Linux host
47+
if: matrix.os == 'ubuntu'
48+
run: |
49+
dart tool/build.dart -oandroid -aarm
50+
dart tool/build.dart -oandroid -aarm64
51+
dart tool/build.dart -oandroid -aia32
52+
dart tool/build.dart -oandroid -ariscv64
53+
dart tool/build.dart -oandroid -ax64
54+
dart tool/build.dart -olinux -aarm
55+
dart tool/build.dart -olinux -aarm64
56+
dart tool/build.dart -olinux -aia32
57+
dart tool/build.dart -olinux -ax64
58+
59+
- name: Build MacOS host
60+
if: matrix.os == 'macos'
61+
run: |
62+
dart tool/build.dart -omacos -aarm64
63+
dart tool/build.dart -omacos -ax64
64+
dart tool/build.dart -oios -iiphoneos -aarm64
65+
dart tool/build.dart -oios -iiphonesimulator -aarm64
66+
dart tool/build.dart -oios -iiphonesimulator -ax64
67+
68+
- name: Build Windows host
69+
if: matrix.os == 'windows'
70+
run: |
71+
dart tool/build.dart -owindows -aarm
72+
dart tool/build.dart -owindows -aarm64
73+
dart tool/build.dart -owindows -aia32
74+
dart tool/build.dart -owindows -ax64
75+
76+
# - name: Upload artifacts
77+
# uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
78+
# with:
79+
# name: dart-${{matrix.os}}-${{ matrix.compiletype }}-libs
80+
# path: submodules/icu4x/bin
81+
82+
# check_hashes:
83+
# needs: dart-libs
84+
# runs-on: ubuntu-latest
85+
86+
# env:
87+
# ICU4X_BUILD_MODE: local
88+
89+
# steps:
90+
# - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
91+
# with:
92+
# submodules: true
93+
94+
# - name: Download binaries
95+
# uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
96+
# with:
97+
# merge-multiple: true
98+
99+
# - name: Display structure of downloaded files
100+
# run: ls -R
101+
102+
# - uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
103+
# with:
104+
# sdk: dev
105+
106+
# - name: Check hashes of released artifacts
107+
# run: |
108+
# cd pkgs/intl4x
109+
# dart pub get
110+
# dart --enable-experiment=native-assets tool/regenerate_hashes.dart
111+
# git diff --exit-code
112+
113+
# release:
114+
# needs: dart-libs
115+
# runs-on: ubuntu-latest
116+
# if: github.event_name == 'push'
117+
# steps:
118+
# - name: Download binaries
119+
# uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
120+
121+
# - name: Display structure of downloaded files
122+
# run: ls -R
123+
124+
# - name: Release
125+
# uses: softprops/action-gh-release@e7a8f85e1c67a31e6ed99a94b41bd0b71bbee6b8
126+
# with:
127+
# files: '**'
128+
# fail_on_unmatched_files: true
129+
# body: "Build artifacts for `package:intl4x`, based on ICU4X at ref ${{ github.ref_name }}"

.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,27 @@
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/native_assets_cli.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
void main(List<String> args) async {
10+
await build(args, (config, output) async {
11+
final packageName = config.packageName;
12+
final cbuilder = CBuilder.library(
13+
name: packageName,
14+
assetName: '$packageName.dart',
15+
sources: [
16+
'src/$packageName.c',
17+
],
18+
);
19+
await cbuilder.run(
20+
config: config,
21+
output: output,
22+
logger: Logger('')
23+
..level = Level.ALL
24+
..onRecord.listen((record) => print(record.message)),
25+
);
26+
});
27+
}
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,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,24 @@
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+
ffigen: ^8.0.2
23+
lints: ^3.0.0
24+
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)