$npx -y skills add flutter/agent-plugins --skill dart-setup-ffi-assetsGuides agents in compiling and packaging C/C++ source code into dynamic or static libraries (Code Assets) using Dart's Native Assets hook system (via hook/build.dart and hook/link.dart utilizing package:hooks and package:native_toolchain_c). Use when a user asks to: 'setup native
| 1 | # Compiling C Code into Code Assets with Native Assets Hooks |
| 2 | |
| 3 | Integrate and automate the compilation and packaging of native C/C++ source code into **Code Assets** under Dart's overarching **Native Assets** feature using build and link hooks. |
| 4 | |
| 5 | ## Contents |
| 6 | - [Introduction](#introduction) |
| 7 | - [Constraints](#constraints) |
| 8 | - [Native Interop Packages](#native-interop-packages) |
| 9 | - [Step-by-Step Workflow](#step-by-step-workflow) |
| 10 | - [Choosing an Integration Approach](#choosing-an-integration-approach) |
| 11 | - [Method 1: Local Compilation with Linker Tree-Shaking (Recommended)](#method-1-local-compilation-with-linker-tree-shaking-recommended) |
| 12 | - [Prerequisite Host Compiler Toolchains](#prerequisite-host-compiler-toolchains) |
| 13 | - [C Source and Bindings Setup](#c-source-and-bindings-setup) |
| 14 | - [Defining the C Library Build Spec](#defining-the-c-library-build-spec) |
| 15 | - [Implementing hook/build.dart](#implementing-hookbuilddart) |
| 16 | - [Implementing hook/link.dart](#implementing-hooklinkdart) |
| 17 | - [Method 2: Downloading Precompiled Dynamic Libraries](#method-2-downloading-precompiled-dynamic-libraries) |
| 18 | - [Why Download Precompiled Binaries?](#why-download-precompiled-binaries) |
| 19 | - [Implementing Precompiled Dynamic Downloads](#implementing-precompiled-dynamic-downloads) |
| 20 | - [Verification Checklist](#verification-checklist) |
| 21 | - [1. Local Execution Sandbox](#1-local-execution-sandbox) |
| 22 | - [2. Verify Target Outputs](#2-verify-target-outputs) |
| 23 | - [3. Verify Tree-Shaking Stripping](#3-verify-tree-shaking-stripping) |
| 24 | - [4. Verify Offline Compliance (User Defines)](#4-verify-offline-compliance-user-defines) |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Introduction |
| 29 | |
| 30 | Under Dart's **Native Assets** feature, packages can package native code (like C/C++ libraries) as **Code Assets** and bundle them automatically during standard development cycles (e.g., `dart run`, `dart test`, `dart build`, and `flutter run`). The packaging of **Code Assets** is driven by two programmatic hook scripts placed inside a package's `hook/` folder: |
| 31 | |
| 32 | 1. `hook/build.dart`: Compiles local C sources to machine code or bundles prebuilt native binaries as code assets for a specific host/target architecture. |
| 33 | 2. `hook/link.dart`: Links built code assets, applying advanced tree-shaking optimizations to strip unused native symbols and compress the runtime binary size. |
| 34 | |
| 35 | --- |
| 36 | |
| 37 | ## Constraints |
| 38 | |
| 39 | > [!IMPORTANT] |
| 40 | > Keep all file resolving platform-independent. Never hardcode absolute target paths, shell scripts, or system command variables. Always use `Platform.script.resolve()` or `Uri`-based resolution to ensure scripts are fully portable. |
| 41 | |
| 42 | * **Hook Locations**: Compiling and packaging hooks must reside strictly inside the `hook/` directory at the package's root: |
| 43 | * `hook/build.dart` (Build execution phase) |
| 44 | * `hook/link.dart` (Optional packaging/linking/tree-shaking phase) |
| 45 | * **Compile Toolchain Standard**: Use the programmatic APIs from `package:native_toolchain_c` (e.g. `CBuilder` and `CLibrary`) to run compile toolchains. Never invoke raw `gcc`, `clang`, or `msvc` via shell commands. |
| 46 | * **Preamble & License Headers**: Every handcrafted and generated source file (including bindings, helpers, and hooks) must strictly contain the target package's copyright and licensing header. |
| 47 | * **Tree Shaking Mapping**: If utilizing compiler tree-shaking, you must map the target Dart method names (e.g. `Method.name`) back to their raw native C symbol names using a record use mapping generated by FFIgen. The mapping file must reside under `lib/src/third_party/` and strictly use the `.g.dart` extension (e.g., `sqlite3.record_use_mapping.g.dart`). |
| 48 | * **Integrity Safeguards for Precompiled Libraries**: If adopting the dynamic download pattern: |
| 49 | * **Cryptographic Verification**: Downloaded prebuilt binaries must be checked against preconfigured lookup tables containing MD5 or SHA-256 hashes to guarantee binary integrity and prevent tampering. |
| 50 | * **Graceful Recovery**: Support offline developers by providing fallbacks (such as local compiler execution via flags like `local_build`). |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | ## Native Interop Packages |
| 55 | |
| 56 | Programmatic build and link hooks for **Code Assets** leverage three specialized native interop packages: |
| 57 | |
| 58 | | Dependency |