Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
[target.aarch64-linux-android]
linker = "aarch64-linux-android-clang"

[target.armv7-linux-androideabi]
linker = "armv7a-linux-androideabi-clang"

[target.i686-linux-android]
linker = "i686-linux-android-clang"

[target.x86_64-linux-android]
linker = "x86_64-linux-android-clang"

[build]
target-dir = "target"
25 changes: 12 additions & 13 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,47 +110,46 @@ This document describes the high-level architecture of the Flutter + Rust hybrid

1. **Dart invocation**
```dart
String result = greet('World');
final versionJson = coreVersion();
final metadata = jsonDecode(versionJson);
```

2. **Bridge conversion**
```dart
final namePtr = name.toNativeUtf8().cast<ffi.Char>();
final resultPtr = _greet(namePtr);
final resultPtr = _coreVersion();
```

3. **FFI boundary**
- Dart string → C char pointer
- Call native function
- C char pointer → Dart string
- Call native function through C ABI
- Convert C char pointer → Dart string

4. **Rust processing**
```rust
#[no_mangle]
pub extern "C" fn greet(name: *const c_char) -> *mut c_char {
// Process and return result
pub extern "C" fn core_version() -> *mut c_char {
// Serialize crate metadata to JSON string
}
```

5. **Memory cleanup**
- Free Rust-allocated memory
- Free Dart-allocated memory
- Free Rust-allocated string via `free_string`
- Dart code releases native memory automatically

## Build Process

### Android Build Flow

1. Flutter builds Dart code to native
2. CMake invokes Cargo for Rust compilation
3. Rust builds for Android targets (ARM64, ARMv7)
4. Shared libraries (.so) copied to APK
3. Rust builds for Android target (arm64-v8a)
4. Shared library (.so) copied to APK
5. APK assembled with all assets

### Windows Build Flow

1. Flutter builds Dart code to native
2. CMake invokes Cargo for Rust compilation
3. Rust builds for Windows target (x86_64-MSVC)
3. Rust builds for Windows target (x86_64-pc-windows-msvc)
4. DLL copied to executable directory
5. Executable packaged with dependencies

Expand Down
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Build automation scripts (build.sh, Makefile)
- Documentation (README, QUICKSTART, CONTRIBUTING)
- GitHub Actions CI workflow for automated testing
- Example greet function demonstrating FFI integration
- **core_version API**: Rust FFI function returning JSON-formatted version metadata (M0-2)
- Exposes crate name and version via C ABI
- Complete Dart FFI bindings for core_version function
- Flutter UI demonstrating version display
- Unit tests for Rust core_version function
- Unit tests for Rust and Flutter components

### Platform Support
- Android (ARM64, ARMv7)
- Windows (x86_64)
- Android (arm64-v8a via NDK)
- Windows (x86_64-MSVC)

[0.1.0]: https://github.com/yourorg/yourrepo/releases/tag/v0.1.0
166 changes: 166 additions & 0 deletions CORE_VERSION_API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Core Version API Documentation (M0-2)

## Overview

The `core_version` API exposes version metadata from the Rust core library via FFI. This API is accessible from Dart/Flutter applications running on Windows (MSVC) and Android (arm64-v8a) platforms.

## Architecture

### Rust Core (cdylib)

The Rust library exposes a C-compatible function that returns JSON-formatted metadata:

```rust
#[no_mangle]
pub extern "C" fn core_version() -> *mut c_char {
// Returns JSON: {"crate_name":"core","crate_version":"0.1.0"}
}
```

**Build Targets:**
- Windows: `x86_64-pc-windows-msvc` → `core.dll`
- Android: `aarch64-linux-android` → `libcore.so` (arm64-v8a)

### Dart FFI Bridge

The bridge package (`/bridge`) provides type-safe Dart bindings:

```dart
String coreVersion() => CoreLib.coreVersion();
```

### Flutter Application

The Flutter app (`/app`) demonstrates the API usage:

```dart
import 'package:bridge/bridge.dart';

final version = coreVersion(); // Returns JSON string
final data = jsonDecode(version);
print(data['crate_name']); // "core"
print(data['crate_version']); // "0.1.0"
```

## Building

### Prerequisites

- Rust toolchain with targets installed:
- `rustup target add x86_64-pc-windows-msvc`
- `rustup target add aarch64-linux-android`
- Android NDK (for Android builds)
- Flutter SDK

### Build Commands

```bash
# Build for Windows
./build.sh windows

# Build for Android
./build.sh android

# Build for both platforms
./build.sh all
```

## Testing

### Rust Unit Tests

```bash
cd core
cargo test
```

Expected output: Test `core_version_returns_metadata` should pass.

### Running the Flutter App

```bash
cd app

# On Windows
flutter run -d windows

# On Android
flutter run -d android
```

The app will display the core version metadata in a card widget.

## API Response Format

The `core_version()` function returns a JSON string with the following schema:

```json
{
"crate_name": "core",
"crate_version": "0.1.0"
}
```

## Platform Support

| Platform | Target Triple | Library Output | Status |
|----------|---------------|----------------|--------|
| Windows | x86_64-pc-windows-msvc | core.dll | ✅ Supported |
| Android (arm64) | aarch64-linux-android | libcore.so | ✅ Supported |

## Memory Management

The Rust function allocates memory for the returned string. The Dart bridge automatically frees this memory using the `free_string` FFI function:

```rust
#[no_mangle]
pub extern "C" fn free_string(s: *mut c_char) {
// Deallocates the string
}
```

The Dart bridge handles this automatically - no manual memory management required from the Flutter side.

## Future Extensions

Future versions may include additional metadata fields:

- `build_timestamp`: When the library was compiled
- `git_commit`: Git commit hash
- `target_triple`: The Rust target triple used for compilation
- `build_profile`: "debug" or "release"

## Troubleshooting

### Library Not Found

If you see "Failed to load dynamic library" errors:

1. **Windows**: Ensure `core.dll` is in the same directory as the Flutter executable
2. **Android**: Check that `libcore.so` is included in the APK under `lib/arm64-v8a/`

### Version Mismatch

If the version displayed doesn't match expectations:

1. Rebuild the Rust library: `./build.sh <platform>`
2. Clean Flutter build: `cd app && flutter clean`
3. Rebuild the Flutter app: `flutter build <platform>`

## Implementation Checklist

- [x] Rust `core_version()` function implemented
- [x] Cargo configured for cdylib output
- [x] Windows (MSVC) build target configured
- [x] Android (arm64-v8a) build target configured
- [x] Dart FFI bindings implemented
- [x] Flutter UI demonstration
- [x] Rust unit tests
- [x] Build scripts updated
- [x] Documentation updated

---

**Milestone:** M0-2
**Status:** Complete
**Last Updated:** 2025-10-18
Loading