cleaup platform-switching Rust platform

This commit is contained in:
Luke Boswell 2024-10-24 12:30:34 +11:00
parent 157c0d2aad
commit bc60d623f2
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
2 changed files with 65 additions and 6 deletions

View file

@ -1,3 +1,5 @@
use std::path::{Path, PathBuf};
fn main() { fn main() {
#[cfg(not(windows))] #[cfg(not(windows))]
println!("cargo:rustc-link-lib=dylib=app"); println!("cargo:rustc-link-lib=dylib=app");
@ -5,5 +7,34 @@ fn main() {
#[cfg(windows)] #[cfg(windows)]
println!("cargo:rustc-link-lib=dylib=libapp"); println!("cargo:rustc-link-lib=dylib=libapp");
println!("cargo:rustc-link-search=."); #[cfg(target_os = "macos")]
let dylib_file_name = "libapp.dylib";
#[cfg(target_os = "linux")]
let dylib_file_name = "libapp.so";
#[cfg(target_os = "windows")]
let dylib_file_name = "libapp.dll";
// Get the build cache directory (OUT_DIR)
let out_dir = std::env::var("OUT_DIR").unwrap();
let lib_app = workspace_root()
.join("examples")
.join("platform-switching")
.join("rust-platform")
.join(dylib_file_name);
let out_path = Path::new(&out_dir).join(dylib_file_name);
// copy the dylib to the output build cache
std::fs::copy(lib_app, out_path).unwrap();
// Search for static libraries in the cache directory
println!("cargo:rustc-link-search=native={out_dir}");
}
pub fn workspace_root() -> PathBuf {
let root = std::env::var("ROC_WORKSPACE_DIR").expect("Can't find the ROC_WORKSPACE_DIR variable expected to be set in .cargo/config.toml. Are you running tests outside of cargo?");
PathBuf::from(root)
} }

View file

@ -2,20 +2,24 @@
use core::ffi::c_void; use core::ffi::c_void;
use roc_std::RocStr; use roc_std::RocStr;
use std::ffi::CStr;
use std::io::Write; use std::io::Write;
use std::os::raw::c_char;
extern "C" { extern "C" {
#[link_name = "roc__mainForHost_1_exposed_generic"] #[link_name = "roc__mainForHost_1_exposed_generic"]
fn roc_main(_: &mut RocStr); fn roc_main(_: &mut RocStr);
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { pub unsafe extern "C" fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void {
return libc::malloc(size); libc::malloc(size)
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_realloc( pub unsafe extern "C" fn roc_realloc(
c_ptr: *mut c_void, c_ptr: *mut c_void,
@ -23,14 +27,20 @@ pub unsafe extern "C" fn roc_realloc(
_old_size: usize, _old_size: usize,
_alignment: u32, _alignment: u32,
) -> *mut c_void { ) -> *mut c_void {
return libc::realloc(c_ptr, new_size); libc::realloc(c_ptr, new_size)
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { pub unsafe extern "C" fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
return libc::free(c_ptr); libc::free(c_ptr);
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_panic(msg: *mut RocStr, tag_id: u32) { pub unsafe extern "C" fn roc_panic(msg: *mut RocStr, tag_id: u32) {
match tag_id { match tag_id {
@ -45,22 +55,34 @@ pub unsafe extern "C" fn roc_panic(msg: *mut RocStr, tag_id: u32) {
std::process::exit(1); std::process::exit(1);
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_dbg(loc: *mut RocStr, msg: *mut RocStr, src: *mut RocStr) { pub unsafe extern "C" fn roc_dbg(loc: *mut RocStr, msg: *mut RocStr, src: *mut RocStr) {
eprintln!("[{}] {} = {}", &*loc, &*src, &*msg); eprintln!("[{}] {} = {}", &*loc, &*src, &*msg);
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut c_void { pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut c_void {
libc::memset(dst, c, n) libc::memset(dst, c, n)
} }
/// # Safety
///
/// TODO
#[cfg(unix)] #[cfg(unix)]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_getppid() -> libc::pid_t { pub unsafe extern "C" fn roc_getppid() -> libc::pid_t {
libc::getppid() libc::getppid()
} }
/// # Safety
///
/// TODO
#[cfg(unix)] #[cfg(unix)]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_mmap( pub unsafe extern "C" fn roc_mmap(
@ -74,6 +96,9 @@ pub unsafe extern "C" fn roc_mmap(
libc::mmap(addr, len, prot, flags, fd, offset) libc::mmap(addr, len, prot, flags, fd, offset)
} }
/// # Safety
///
/// TODO
#[cfg(unix)] #[cfg(unix)]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn roc_shm_open( pub unsafe extern "C" fn roc_shm_open(
@ -84,6 +109,9 @@ pub unsafe extern "C" fn roc_shm_open(
libc::shm_open(name, oflag, mode as libc::c_uint) libc::shm_open(name, oflag, mode as libc::c_uint)
} }
/// # Safety
///
/// TODO
#[no_mangle] #[no_mangle]
pub extern "C" fn rust_main() -> i32 { pub extern "C" fn rust_main() -> i32 {
let mut roc_str = RocStr::default(); let mut roc_str = RocStr::default();