Skip to content

Commit

Permalink
Add C bindings (#6)
Browse files Browse the repository at this point in the history
* First working prototype for exposing functions to C

* crunch64_decompress_yaz0_get_dst_buffer_size

* Bindings for compressing yaz0

* cleanup cod

* fix lib and bin

* makefile for building the C tests

* c bindings for yay0

* Tests for Yay0 bindings

* Update the yaz0 test

* _bound

* CI for testing C bindings

* More Result

* u8_vec_from_pointer_array and set_pointer_array_from_u8_array

* Cleanup yaz0

* yay0 cleanup

* fix

* Testing packaging C libs

* maybe fix ci

* ¿¿

* idk

* hopefully fix

* install target

* more fixes

* does this work?

* maybe?

* sad

* redundant path?

* suffering

* asd

* CI: Enable building cdylib and compress format based on target arch

* more customization

* oops

* fix

* fix errors on some archs

* Fix

* Package readme and license in C libs

* Try to build cdylib on other targets

* Add x86_64-pc-windows-msvc

* disable cdylib for windows msvc

* a

* Add x86_64-pc-windows-gnullvm

* yeet x86_64-pc-windows-gnullvm

* Fix msvc

* Disable cdylib

* #ifdef __cplusplus extern "C"

* add windows weird stuff before i forget

* Propagate errors to C

* Run C tests against all test cases

* Enable Rust tests to run on other branches

* Fix clippy warnings

* fmt

* reorganize stuff

* remove

* Put C bindings behind a feature

* Move common test code to utils.c

* publish release ci

* instructions on readme

* size_for_compressed_buffer
  • Loading branch information
AngheloAlf authored Dec 11, 2023
1 parent 2f2ba45 commit fe1780a
Show file tree
Hide file tree
Showing 26 changed files with 1,416 additions and 86 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/c_bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: C bindings

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
test-c-bindings:
name: Test C bindings
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Make C test programs
run: make -C c_bindings BUILD_MODE=release

- name: Test Yay0
run: ./c_bindings/tests/test_yay0.sh

- name: Test Yaz0
run: ./c_bindings/tests/test_yaz0.sh

release:
name: Release ${{ matrix.crate-type }} for ${{ matrix.target }}
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
target: [x86_64-pc-windows-gnu, x86_64-pc-windows-msvc, x86_64-apple-darwin, x86_64-unknown-linux-musl, x86_64-unknown-linux-gnu]
crate-type: [staticlib]
include:
- target: x86_64-pc-windows-gnu
archive: zip
- target: x86_64-pc-windows-msvc
archive: zip
- target: x86_64-apple-darwin
archive: zip
- target: x86_64-unknown-linux-musl
archive: tar.gz
- target: x86_64-unknown-linux-gnu
archive: tar.gz

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}

- name: Build lib
run: cargo rustc --manifest-path lib/Cargo.toml --lib --features c_bindings --release --crate-type ${{ matrix.crate-type }} --target ${{ matrix.target }}

- name: Print built files
run: |
tree target/${{ matrix.target }}/release
- name: Move files for packaging
run: |
mkdir -p package/lib package/include
cp target/${{ matrix.target }}/release/libcrunch64.a package/lib/ || cp target/${{ matrix.target }}/release/crunch64.lib package/lib/
cp -r c_bindings/include/* package/include/
cp LICENSE package/crunch64.LICENSE
cp README.md package/crunch64.README.md
tree package
- name: Package .tar.gz
if: matrix.archive == 'tar.gz'
run: |
cd package && tar -czf ../crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.tar.gz *
- name: Package .zip
if: matrix.archive == 'zip'
run: |
cd package && zip -r ../crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.zip *
- name: Upload .tar.gz archive
if: matrix.archive == 'tar.gz'
uses: actions/upload-artifact@v3
with:
name: crunch64-${{ matrix.crate-type }}-${{ matrix.target }}
path: |
crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.tar.gz
if-no-files-found: error

- name: Upload .zip archive
if: matrix.archive == 'zip'
uses: actions/upload-artifact@v3
with:
name: crunch64-${{ matrix.crate-type }}-${{ matrix.target }}
path: |
crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.zip
if-no-files-found: error

- name: Publish .tar.gz release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') && matrix.archive == 'tar.gz'
with:
files: crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.tar.gz

- name: Publish .zip release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') && matrix.archive == 'zip'
with:
files: crunch64-${{ matrix.crate-type }}-${{ matrix.target }}.zip
71 changes: 38 additions & 33 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@ on:
release:
types: [created]
jobs:
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-pc-windows-gnu
archive: zip
- target: x86_64-unknown-linux-musl
archive: tar.gz
- target: x86_64-apple-darwin
archive: zip
steps:
- uses: actions/checkout@master
- name: Compile and release
uses: rust-build/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
RUSTTARGET: ${{ matrix.target }}
ARCHIVE_TYPES: ${{ matrix.archive }}
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: katyo/publish-crates@v2
with:
registry-token: ${{ secrets.CRATE_AUTH_TOKEN }}
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-pc-windows-gnu
archive: zip
- target: x86_64-pc-windows-msvc
archive: zip
- target: x86_64-apple-darwin
archive: zip
- target: x86_64-unknown-linux-musl
archive: tar.gz
- target: x86_64-unknown-linux-gnu
archive: tar.gz
steps:
- uses: actions/checkout@master
- name: Compile and release
uses: rust-build/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
RUSTTARGET: ${{ matrix.target }}
ARCHIVE_TYPES: ${{ matrix.archive }}

publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: katyo/publish-crates@v2
with:
registry-token: ${{ secrets.CRATE_AUTH_TOKEN }}
19 changes: 9 additions & 10 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Rust
name: Test Rust code

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
Expand All @@ -13,26 +9,29 @@ jobs:
rustfmt-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Run cargo fmt
run: cargo fmt --all -- --check
- name: Run cargo clippy
run: cargo clippy --all -- -D warnings

macos-check:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Test
run: cargo test --all-features

ubuntu-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Test
run: cargo test --all-features

windows-check:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Test
run: cargo test --all-features
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ target/
*.bin
*.Yay0
*.Yaz0
*.elf

.vscode/settings.json
15 changes: 15 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/c_bindings/include"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "test_yay0.elf",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/c_bindings/tests/test_yay0.elf",
"args": ["${workspaceFolder}/test_data/ground.png", "${workspaceFolder}/test_data/ground.png.Yay0"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[workspace]
members = [
"lib",
"cli",
]
members = ["lib", "cli"]
resolver = "2"
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# crunch64

## C bindings

This library provides bindings to call this library from C code. They are available on the [releases](https://github.com/decompals/crunch64/releases) tab.

To build said bindings from source, enable the `c_bindings` Rust feature:

```bash
cargo build --lib --features c_bindings
```

Headers are located at [c_bindings/include](c_bindings/include).

### Windows executables

Due to Rust requirements, linking the C bindings of this library when building a C program adds extra library dependencies. Those libraries are the following:

```plain_text
-lws2_32 -lntdll -lbcrypt -ladvapi32 -luserenv
```
39 changes: 39 additions & 0 deletions c_bindings/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Either `debug` or `release`
BUILD_MODE ?= debug

CC := gcc
LIB := ../target/$(BUILD_MODE)/libcrunch64.a

CSTD := -std=c11
ifeq ($(BUILD_MODE), debug)
CFLAGS ?= -O0 -g3
else
CFLAGS ?= -Os
endif
IINC := -I include
WARNINGS := -Wall -Wextra -Wshadow -Werror


C_BINDINGS_TESTS := $(wildcard tests/test_*.c)
C_BINDINGS_ELFS := $(C_BINDINGS_TESTS:.c=.elf)

all: $(C_BINDINGS_ELFS)

clean:
$(RM) -rf $(C_BINDINGS_ELFS) tests/*.elf

.PHONY: all clean
.DEFAULT_GOAL := all

CARGO_FLAGS ?=
ifneq ($(BUILD_MODE), debug)
CARGO_FLAGS += --release
endif
$(shell cargo build --lib --features c_bindings $(CARGO_FLAGS))


%.elf: %.c $(LIB)
$(CC) $(CSTD) $(CFLAGS) $(IINC) $(WARNINGS) -o $@ tests/utils.c $< -L ../target/$(BUILD_MODE) -Wl,-Bstatic -l crunch64 -Wl,-Bdynamic

# Print target for debugging
print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true
9 changes: 9 additions & 0 deletions c_bindings/include/crunch64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef CRUNCH64_H
#define CRUNCH64_H
#pragma once

#include "crunch64/error.h"
#include "crunch64/yay0.h"
#include "crunch64/yaz0.h"

#endif
Loading

0 comments on commit fe1780a

Please sign in to comment.