Skip to content

Commit

Permalink
[native_assets_cli] Add example for downloading assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Jan 3, 2025
1 parent 598c969 commit 076b4d7
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 2 deletions.
127 changes: 127 additions & 0 deletions .github/workflows/download_asset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: download_asset

permissions:
contents: write

on:
pull_request:
branches: [ main ]
paths:
- .github/workflows/download_asset.yaml
- pkgs/native_assets_cli/example/build/download_asset/
push:
tags:
- 'download_asset-prebuild-assets-*'
workflow_dispatch:

jobs:
build:
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos, windows]

runs-on: ${{ matrix.os }}-latest

defaults:
run:
working-directory: pkgs/native_assets_cli/example/build/download_asset/

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
with:
sdk: stable

- uses: nttld/setup-ndk@afb4c9964b521afb97c864b7d40b11e6911bd410
with:
ndk-version: r27
if: ${{ matrix.os == 'ubuntu' }} # Only build on one host.

- name: Install native toolchains
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
if: ${{ matrix.os == 'ubuntu' }}

- run: dart pub get

- name: Build Linux host
if: matrix.os == 'ubuntu'
run: |
dart tool/build.dart -oandroid -aarm
dart tool/build.dart -oandroid -aarm64
dart tool/build.dart -oandroid -aia32
dart tool/build.dart -oandroid -ariscv64
dart tool/build.dart -oandroid -ax64
dart tool/build.dart -olinux -aarm
dart tool/build.dart -olinux -aarm64
dart tool/build.dart -olinux -aia32
dart tool/build.dart -olinux -ariscv64
dart tool/build.dart -olinux -ax64
- name: Build MacOS host
if: matrix.os == 'macos'
run: |
dart tool/build.dart -omacos -aarm64
dart tool/build.dart -omacos -ax64
dart tool/build.dart -oios -iiphoneos -aarm64
dart tool/build.dart -oios -iiphonesimulator -aarm64
dart tool/build.dart -oios -iiphonesimulator -ax64
- name: Build Windows host
if: matrix.os == 'windows'
run: |
dart tool/build.dart -owindows -aarm
dart tool/build.dart -owindows -aarm64
dart tool/build.dart -owindows -aia32
dart tool/build.dart -owindows -ax64
- name: Upload artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: ${{ matrix.os }}-host
path: |
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.dll
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.dylib
pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/**/*.so
if-no-files-found: error

check:
needs: build
runs-on: ubuntu-latest

defaults:
run:
working-directory: pkgs/native_assets_cli/example/build/download_asset/

steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
with:
submodules: true

- name: Download assets
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
with:
merge-multiple: true
path: pkgs/native_assets_cli/example/build/download_asset/.dart_tool/download_asset/

- name: Display structure of downloaded assets
run: ls -R .dart_tool/download_asset/

- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: stable

- run: dart pub get

- name: Check hashes of released artifacts
run: |
dart tool/generate_asset_hashes.dart
git diff --exit-code
- name: Release
uses: softprops/action-gh-release@e7a8f85e1c67a31e6ed99a94b41bd0b71bbee6b8
if: startsWith(github.ref, 'refs/tags/')
with:
files: '.dart_tool/download_asset/**'
fail_on_unmatched_files: true
3 changes: 3 additions & 0 deletions .github/workflows/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ jobs:
- run: dart pub get -C test_data/native_dynamic_linking/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C example/build/download_asset/
if: ${{ matrix.package == 'native_assets_cli' }}

- run: dart pub get -C example/build/native_dynamic_linking/
if: ${{ matrix.package == 'native_assets_cli' }}

Expand Down
14 changes: 14 additions & 0 deletions pkgs/native_assets_cli/example/build/download_asset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
An example of a library depending on prebuilt assets which are downloaded in
the build hook.

## Usage

Run tests with `dart --enable-experiment=native-assets test`.

## Code organization

A typical layout of a package which downloads assets:

* `hook/build.dart` downloads the prebuilt assets.
* `lib/` contains Dart code which uses the assets.
* `tool/build.dart` prebuilts assets and is exercised from the GitHub CI.
20 changes: 20 additions & 0 deletions pkgs/native_assets_cli/example/build/download_asset/ffigen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Run with `flutter pub run ffigen --config ffigen.yaml`.
name: NativeAddBindings
description: |
Bindings for `src/native_add.h`.
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
output: 'lib/native_add.dart'
headers:
entry-points:
- 'src/native_add.h'
include-directives:
- 'src/native_add.h'
preamble: |
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
comments:
style: any
length: full
ffi-native:
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:download_asset/src/hook_helpers/c_build.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> args) async {
const localBuild = false;

await build(args, (config, output) async {
// ignore: dead_code
if (localBuild) {
await runBuild(config, output);
} else {
// TODO(dcharkes): Download from GitHub.
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
external int add(
int a,
int b,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:logging/logging.dart';
import 'package:native_assets_cli/code_assets_builder.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

Future<void> runBuild(BuildConfig config, BuildOutputBuilder output) async {
final cbuilder = CBuilder.library(
name: 'native_add',
assetName: 'native_add.dart',
sources: [
'src/native_add.c',
],
);
await cbuilder.run(
config: config,
output: output,
logger: Logger('')
..level = Level.ALL
..onRecord.listen((record) => print(record.message)),
);
}

String createTargetName(String osString, String architecture, String? iOSSdk) {
var targetName = '${osString}_$architecture';
if (iOSSdk != null) {
targetName += '_$iOSSdk';
}
return targetName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// THIS FILE IS AUTOGENERATED. TO UPDATE, RUN
//
// dart --enable-experiment=native-assets tool/generate_asset_hashes.dart
//

const assetHashes = <String, String>{
'macos_x64':
'bd4b95e1b62e3a382c301ed3ec7d4fadc163d1cf35218f985ab214dd131f7f51',
'ios_arm64_iphoneos':
'3a4294cd313189c43d8922c9d222c169e01aabdfe73b84c5937ed84bfb8eb10e',
'ios_arm64_iphonesimulator':
'8873f0f6a4584075f06a7b517b959d52993981e433d993503bf769bc9861ba53',
'macos_arm64':
'c9d9bd0dfa15babefb6e875ea50da5ebad2afaa46a4579cdde43aca3614e393a',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

const androidTargetNdkApi = 30;
const int macOSTargetVersion = 13;
const iOSTargetVersion = 16;
25 changes: 25 additions & 0 deletions pkgs/native_assets_cli/example/build/download_asset/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
publish_to: none

name: download_asset
description: Sums two numbers with native code, prebuilt assets.
version: 0.1.0
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/build/download_asset

environment:
sdk: '>=3.3.0 <4.0.0'

dependencies:
logging: ^1.1.1
native_assets_cli: ^0.10.0
# native_assets_cli:
# path: ../../../../native_assets_cli/
native_toolchain_c: ^0.7.0
# native_toolchain_c:
# path: ../../../../native_toolchain_c/

dev_dependencies:
args: ^2.6.0
crypto: ^3.0.3
ffigen: ^8.0.2
lints: ^3.0.0
test: ^1.21.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "native_add.h"

int32_t add(int32_t a, int32_t b) { return a + b; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include <stdint.h>

#if _WIN32
#define MYLIB_EXPORT __declspec(dllexport)
#else
#define MYLIB_EXPORT
#endif

MYLIB_EXPORT int32_t add(int32_t a, int32_t b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:download_asset/native_add.dart';
import 'package:test/test.dart';

void main() {
test('invoke native function', () {
expect(add(24, 18), 42);
});
}
Loading

0 comments on commit 076b4d7

Please sign in to comment.