Skip to content

Commit

Permalink
add dart support (#130)
Browse files Browse the repository at this point in the history
* add powershell build file
* add dart ci test
* add github action dart env
  • Loading branch information
dreamcmi authored Oct 14, 2024
1 parent 0edcf26 commit 95ee6a7
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ jobs:
cd go
go build -o ruapu-go
./ruapu-go
- uses: dart-lang/setup-dart@v1
- name: build-test-dart
run: |
cd dart
bash ./build.sh
macos:
runs-on: macos-latest
Expand Down Expand Up @@ -92,6 +97,11 @@ jobs:
cd go
go build -o ruapu-go
./ruapu-go
- uses: dart-lang/setup-dart@v1
- name: build-test-dart
run: |
cd dart
bash ./build.sh
macos-arm64:
runs-on: macos-14
Expand Down Expand Up @@ -127,6 +137,11 @@ jobs:
cd go
go build -o ruapu-go
./ruapu-go
- uses: dart-lang/setup-dart@v1
- name: build-test-dart
run: |
cd dart
bash ./build.sh
windows:
runs-on: windows-latest
Expand Down Expand Up @@ -174,6 +189,11 @@ jobs:
cd go
go build -o ruapu-go.exe
./ruapu-go.exe
- uses: dart-lang/setup-dart@v1
- name: build-test-dart
run: |
cd dart
.\build.ps1
qemu:
runs-on: ubuntu-latest
Expand Down
66 changes: 59 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ int main()
## Features

* Detect **CPU ISA with single-file**   
_`sse2`, `avx`, `avx512f`, `neon`, etc._
_`sse2`, `avx`, `avx512f`, `neon`, etc._
* Detect **vendor extended ISA**    
_apple `amx`, risc-v vendor ISA, etc._
_apple `amx`, risc-v vendor ISA, etc._
* Detect **richer ISA on Windows ARM**  
_`IsProcessorFeaturePresent()` returns little ISA information_
_`IsProcessorFeaturePresent()` returns little ISA information_
* Detect **`x86-avx512` on macOS correctly** 
_macOS hides it in `cpuid`_
_macOS hides it in `cpuid`_
* Detect **new CPU's ISA on old systems** 
_they are usually not exposed in `auxv` or `MISA`_
_they are usually not exposed in `auxv` or `MISA`_
* Detect **CPU hidden ISA**       
_`fma4` on zen1, ISA in hypervisor, etc._
_`fma4` on zen1, ISA in hypervisor, etc._

## Supported ISA _ (more is comming ... :)_

Expand Down Expand Up @@ -544,6 +544,58 @@ main(): Int64 {
</td></tr>
</table>


### ruapu with Dart

<table>

<tr><td>

Compile ruapu library

```bash
cd dart
bash build.sh
```

</td>
<td>
Use ruapu in dart

```dart
void main() {
var libraryPath =
path.join(Directory.current.path, 'build', 'libruapu.so');
if (Platform.isMacOS) {
libraryPath =
path.join(Directory.current.path, 'build', 'libruapu.dylib');
}
if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path, 'build', 'Debug', 'ruapu.dll');
}
Ruapu ruapu = Ruapu(libraryPath);
ruapu.init();
List<String> isas = ruapu.rua();
print("This CPU Support:");
for (String isa in isas) {
print(isa);
}
print("=================");
String isaToCheck = 'aes';
bool isSupported = ruapu.supports(isaToCheck);
print('Does the system support $isaToCheck? $isSupported');
}
```
</td></tr>
</table>

<details>
<summary>Github-hosted runner result (Linux)</summary>

Expand Down Expand Up @@ -787,4 +839,4 @@ ruapu determines whether the CPU supports certain instruction sets by trying to
* [@whyb](https://github.com/whyb) &emsp;_Detect more x86 AMX*, SHA*, AVX512*, SM*_

## License
MIT License
MIT License
20 changes: 20 additions & 0 deletions dart/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$PROJECT_DIR = Get-Location
$RUAPU_API_DIR = Join-Path -Path $PROJECT_DIR -ChildPath "ruapu-api"
$BUILD_DIR = Join-Path -Path $PROJECT_DIR -ChildPath "build"

New-Item -ItemType Directory -Force -Path $BUILD_DIR
Set-Location $BUILD_DIR

cmake $RUAPU_API_DIR
cmake --build $BUILD_DIR

if ($LASTEXITCODE -ne 0) {
Write-Host "Build Error!!! Plz Check!!!"
exit 1
}

Set-Location $PROJECT_DIR

dart pub get

dart run main.dart
22 changes: 22 additions & 0 deletions dart/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

PROJECT_DIR=$(pwd)
RUAPU_API_DIR="$PROJECT_DIR/ruapu-api"
BUILD_DIR="$PROJECT_DIR/build"

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR" || exit

cmake "$RUAPU_API_DIR"
cmake --build "$BUILD_DIR"

if [ $? -ne 0 ]; then
echo "Build Error!!! Plz Check!!!"
exit 1
fi

cd "$PROJECT_DIR" || exit

dart pub get

dart run main.dart
33 changes: 33 additions & 0 deletions dart/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:io';
import 'ruapu_api.dart';
import 'package:path/path.dart' as path;

void main() {
var libraryPath =
path.join(Directory.current.path, 'build', 'libruapu.so');

if (Platform.isMacOS) {
libraryPath =
path.join(Directory.current.path, 'build', 'libruapu.dylib');
}

if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path, 'build', 'Debug', 'ruapu.dll');
}

Ruapu ruapu = Ruapu(libraryPath);

ruapu.init();

List<String> isas = ruapu.rua();
print("This CPU Support:");
for (String isa in isas) {
print(isa);
}
print("=================");

String isaToCheck = 'aes';
bool isSupported = ruapu.supports(isaToCheck);
print('Does the system support $isaToCheck? $isSupported');
}
17 changes: 17 additions & 0 deletions dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: dartRuapu
version: 1.0.0
description: >-
Use ruapu with Dart.
publish_to: none

environment:
sdk: ^3.4.0

dependencies:
path: ^1.9.0
ffi: ^1.0.0

dev_dependencies:
lints: ^4.0.0
test: ^1.25.0
12 changes: 12 additions & 0 deletions dart/ruapu-api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(ruapu_library VERSION 1.0.0 LANGUAGES C)
add_library(ruapu_library SHARED ruapu.c ruapu.def)
include_directories(../../)

set_target_properties(ruapu_library PROPERTIES
PUBLIC_HEADER ../../ruapu.h
VERSION ${PROJECT_VERSION}
SOVERSION 1
OUTPUT_NAME "ruapu"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Hex_Identity_ID_Goes_Here"
)
3 changes: 3 additions & 0 deletions dart/ruapu-api/ruapu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define RUAPU_IMPLEMENTATION
#include "ruapu.h"

5 changes: 5 additions & 0 deletions dart/ruapu-api/ruapu.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LIBRARY ruapu
EXPORTS
ruapu_rua
ruapu_supports
ruapu_init
63 changes: 63 additions & 0 deletions dart/ruapu_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';


typedef RuapuRUA_C = Pointer<Pointer<Utf8>> Function();
typedef RuapuRUA_Dart = Pointer<Pointer<Utf8>> Function();

typedef RuapuInit_C = Void Function();
typedef RuapuInit_Dart = void Function();

typedef RuapuSupports_C = Int32 Function(Pointer<Utf8>);
typedef RuapuSupports_Dart = int Function(Pointer<Utf8>);

class Ruapu {
final DynamicLibrary _nativeLib;

Ruapu(String libraryPath) : _nativeLib = DynamicLibrary.open(libraryPath);

/// Init the ruapu environment
void init() {
final RuapuInit_Dart ruapu_init = _nativeLib
.lookup<NativeFunction<RuapuInit_C>>('ruapu_init')
.asFunction();
ruapu_init();
}

/// Get the ISA support list of the CPU
List<String> rua() {
final RuapuRUA_Dart ruapu_rua = _nativeLib
.lookup<NativeFunction<RuapuRUA_C>>('ruapu_rua')
.asFunction();

Pointer<Pointer<Utf8>> result = ruapu_rua();

List<String> isas = [];
int index = 0;
while (true) {
Pointer<Utf8> strPtr = result.elementAt(index).value;
if (strPtr.address == 0) break;
String str = strPtr.toDartString();
isas.add(str);
index++;
}

return isas;
}

/// Check the support status of a given isa
bool supports(String isa) {
final Pointer<Utf8> cIsa = isa.toNativeUtf8();

final RuapuSupports_Dart ruapu_supports = _nativeLib
.lookup<NativeFunction<RuapuSupports_C>>('ruapu_supports')
.asFunction();

int result = ruapu_supports(cIsa);

malloc.free(cIsa);

return result != 0;
}

}

0 comments on commit 95ee6a7

Please sign in to comment.