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 7557603
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 2 deletions.
134 changes: 134 additions & 0 deletions .github/workflows/download_asset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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:
name: Prebuilt native libraries

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: dart-${{matrix.os}}-${{ matrix.compiletype }}-libs
# path: submodules/icu4x/bin

# check_hashes:
# needs: dart-libs
# runs-on: ubuntu-latest

# env:
# ICU4X_BUILD_MODE: local

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

# - name: Download binaries
# uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
# with:
# merge-multiple: true

# - name: Display structure of downloaded files
# run: ls -R

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

# - name: Check hashes of released artifacts
# run: |
# cd pkgs/intl4x
# dart pub get
# dart --enable-experiment=native-assets tool/regenerate_hashes.dart
# git diff --exit-code

# release:
# needs: dart-libs
# runs-on: ubuntu-latest
# if: github.event_name == 'push'
# steps:
# - name: Download binaries
# uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16

# - name: Display structure of downloaded files
# run: ls -R

# - name: Release
# uses: softprops/action-gh-release@e7a8f85e1c67a31e6ed99a94b41bd0b71bbee6b8
# with:
# files: '**'
# fail_on_unmatched_files: true
# body: "Build artifacts for `package:intl4x`, based on ICU4X at ref ${{ github.ref_name }}"
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,27 @@
// 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/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

void main(List<String> args) async {
await build(args, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: '$packageName.dart',
sources: [
'src/$packageName.c',
],
);
await cbuilder.run(
config: config,
output: output,
logger: Logger('')
..level = Level.ALL
..onRecord.listen((record) => print(record.message)),
);
});
}
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,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;
24 changes: 24 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,24 @@
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
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 7557603

Please sign in to comment.