Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
feat: add ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Aug 2, 2024
1 parent aa7de1a commit b3eb45f
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = [ "rivet-cli",
"rivet-toolchain",
"rivet-toolchain", "rivet-toolchain-ffi",
]

# Config for 'cargo dist'
Expand Down
13 changes: 13 additions & 0 deletions rivet-toolchain-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rivet-toolchain-ffi"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
serde_json = "1.0"
tokio = { version = "1.27", default-features = false, features = ["rt"] }
toolchain = { path = "../rivet-toolchain/", package = "rivet-toolchain" }

36 changes: 36 additions & 0 deletions rivet-toolchain-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod util;

use std::{
ffi::{CStr, CString},
os::raw::c_char,
};

#[no_mangle]
pub extern "C" fn run_task(
run_config: *const c_char,
name: *const c_char,
input_json: *const c_char,
) -> *mut c_char {
let run_config = unsafe { CStr::from_ptr(run_config).to_str().unwrap() };
let name = unsafe { CStr::from_ptr(name).to_str().unwrap() };
let input_json = unsafe { CStr::from_ptr(input_json).to_str().unwrap() };

let result = util::run_task(
run_config.to_string(),
name.to_string(),
input_json.to_string(),
);

let c_result = CString::new(result).unwrap();
c_result.into_raw()
}

#[no_mangle]
pub extern "C" fn free_rust_string(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
let _ = CString::from_raw(s);
};
}
43 changes: 43 additions & 0 deletions rivet-toolchain-ffi/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde_json;
use std::future::Future;
use tokio::time::Duration;
use toolchain::tasks::RunConfig;

pub fn run_task(run_config: String, name: String, input_json: String) -> String {
let run_config = serde_json::from_str::<RunConfig>(&run_config).unwrap();
let task_config = toolchain::tasks::get_task_config(&name);
let name_inner = name.clone();
let output_json = block_on(
async move { toolchain::tasks::run_task_json(run_config, &name_inner, &input_json).await },
BlockOnOpts {
multithreaded: task_config.prefer_multithreaded,
},
);
output_json
}

const FORCE_MULTI_THREAD: bool = true;

struct BlockOnOpts {
multithreaded: bool,
}

/// Create a temporary Tokio runtime to run the given future.
fn block_on<Output>(fut: impl Future<Output = Output>, opts: BlockOnOpts) -> Output {
// Build temporary runtime
let mut builder = if opts.multithreaded || FORCE_MULTI_THREAD {
tokio::runtime::Builder::new_multi_thread()
} else {
tokio::runtime::Builder::new_current_thread()
};
let rt = builder.enable_all().build().unwrap();

// Run future
let output = rt.block_on(fut);

// Give tasks time to shut down
// rt.shutdown_background();
rt.shutdown_timeout(Duration::from_secs(1));

output
}
100 changes: 100 additions & 0 deletions scripts/build_ffi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/sh
set -e

docker build -t rust-cross-compiler - << 'EOF'
FROM rust:1.80
RUN apt-get update && apt-get install -y \
gcc-mingw-w64-x86-64 \
gcc-x86-64-linux-gnu \
libc6-dev-amd64-cross \
clang \
libssl-dev \
wget \
xz-utils \
cmake \
patch \
libxml2-dev \
llvm-dev \
uuid-dev \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
# Install osxcross
RUN git clone https://github.com/tpoechtrager/osxcross /root/osxcross
WORKDIR /root/osxcross
RUN wget -nc https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz
RUN mv MacOSX11.3.sdk.tar.xz tarballs/
RUN UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh
ENV PATH="/root/osxcross/target/bin:$PATH"
# Install targets
RUN rustup target add x86_64-unknown-linux-gnu \
x86_64-pc-windows-gnu \
x86_64-apple-darwin \
aarch64-apple-darwin
WORKDIR /app
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
ENV CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=x86_64-apple-darwin20.4-clang
ENV CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=aarch64-apple-darwin20.4-clang
ENV CC_x86_64_apple_darwin=x86_64-apple-darwin20.4-clang
ENV CXX_x86_64_apple_darwin=x86_64-apple-darwin20.4-clang++
ENV CC_aarch64_apple_darwin=aarch64-apple-darwin20.4-clang
ENV CXX_aarch64_apple_darwin=aarch64-apple-darwin20.4-clang++
RUN mkdir -p /root/.cargo && \
echo '\
[target.x86_64-unknown-linux-gnu]\n\
linker = "x86_64-linux-gnu-gcc"\n\
\n\
[target.x86_64-pc-windows-gnu]\n\
linker = "x86_64-w64-mingw32-gcc"\n\
\n\
[target.x86_64-apple-darwin]\n\
linker = "x86_64-apple-darwin20.4-clang"\n\
ar = "x86_64-apple-darwin20.4-ar"\n\
\n\
[target.aarch64-apple-darwin]\n\
linker = "aarch64-apple-darwin20.4-clang"\n\
ar = "aarch64-apple-darwin20.4-ar"\n\
' > /root/.cargo/config.toml
EOF

docker run -it --rm -v "$(pwd)":/app rust-cross-compiler /bin/sh -c '
set -e
# echo "which"
# ls /root/osxcross/target/bin
# exit 1
echo "Building for x86 Linux..."
# cargo build --target x86_64-unknown-linux-gnu
# echo "Building for x86 Windows..."
# cargo build --target x86_64-pc-windows-gnu
echo "Building for x86 macOS..."
cargo build --target x86_64-apple-darwin
# echo "Building for ARM macOS..."
# cargo build --target aarch64-apple-darwin
'

# docker run -it --rm -v "$(pwd)":/app rust-cross-compiler /bin/sh -c '
# set -e
# echo "Building for x86 Linux..."
# cargo build --target x86_64-unknown-linux-gnu --release
# echo "Building for x86 Windows..."
# cargo build --target x86_64-pc-windows-gnu --release
# echo "Building for x86 macOS..."
# cargo build --target x86_64-apple-darwin --release
# echo "Building for ARM macOS..."
# cargo build --target aarch64-apple-darwin --release
# '

echo "Build process completed."
echo "Built libraries can be found at:"
# echo "x86 Linux: target/x86_64-unknown-linux-gnu/release/"
# echo "x86 Windows: target/x86_64-pc-windows-gnu/release/"
# echo "x86 macOS: target/x86_64-apple-darwin/release/"
# echo "ARM macOS: target/aarch64-apple-darwin/release/"
echo "x86 Linux: target/x86_64-unknown-linux-gnu/debug/"
echo "x86 Windows: target/x86_64-pc-windows-gnu/debug/"
echo "x86 macOS: target/x86_64-apple-darwin/debug/"
echo "ARM macOS: target/aarch64-apple-darwin/debug/"
12 changes: 10 additions & 2 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ in
buildInputs = with pkgs; [
cacert

pkgs.latest.rustChannels.stable.rust
rust-script
# pkgs.latest.rustChannels.stable.rust
(pkgs.latest.rustChannels.stable.rust.override {
extensions = [ "rust-src" "rust-std" ];
targets = [
"x86_64-unknown-linux-gnu"
"x86_64-pc-windows-msvc"
"x86_64-apple-darwin"
"aarch64-apple-darwin"
];
})
pkg-config
perl

Expand Down

0 comments on commit b3eb45f

Please sign in to comment.