diff --git a/examples/platform-switching/rust-platform/build.rs b/examples/platform-switching/rust-platform/build.rs index 47763b34c3..bc33139862 100644 --- a/examples/platform-switching/rust-platform/build.rs +++ b/examples/platform-switching/rust-platform/build.rs @@ -1,3 +1,5 @@ +use std::path::{Path, PathBuf}; + fn main() { #[cfg(not(windows))] println!("cargo:rustc-link-lib=dylib=app"); @@ -5,5 +7,34 @@ fn main() { #[cfg(windows)] 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) } diff --git a/examples/platform-switching/rust-platform/src/lib.rs b/examples/platform-switching/rust-platform/src/lib.rs index a9d1cc7b02..94dacefe6d 100644 --- a/examples/platform-switching/rust-platform/src/lib.rs +++ b/examples/platform-switching/rust-platform/src/lib.rs @@ -2,20 +2,24 @@ use core::ffi::c_void; use roc_std::RocStr; -use std::ffi::CStr; use std::io::Write; -use std::os::raw::c_char; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] fn roc_main(_: &mut RocStr); } +/// # Safety +/// +/// TODO #[no_mangle] 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] pub unsafe extern "C" fn roc_realloc( c_ptr: *mut c_void, @@ -23,14 +27,20 @@ pub unsafe extern "C" fn roc_realloc( _old_size: usize, _alignment: u32, ) -> *mut c_void { - return libc::realloc(c_ptr, new_size); + libc::realloc(c_ptr, new_size) } +/// # Safety +/// +/// TODO #[no_mangle] 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] pub unsafe extern "C" fn roc_panic(msg: *mut RocStr, tag_id: u32) { match tag_id { @@ -45,22 +55,34 @@ pub unsafe extern "C" fn roc_panic(msg: *mut RocStr, tag_id: u32) { std::process::exit(1); } +/// # Safety +/// +/// TODO #[no_mangle] pub unsafe extern "C" fn roc_dbg(loc: *mut RocStr, msg: *mut RocStr, src: *mut RocStr) { eprintln!("[{}] {} = {}", &*loc, &*src, &*msg); } +/// # Safety +/// +/// TODO #[no_mangle] pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut c_void { libc::memset(dst, c, n) } +/// # Safety +/// +/// TODO #[cfg(unix)] #[no_mangle] pub unsafe extern "C" fn roc_getppid() -> libc::pid_t { libc::getppid() } +/// # Safety +/// +/// TODO #[cfg(unix)] #[no_mangle] 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) } +/// # Safety +/// +/// TODO #[cfg(unix)] #[no_mangle] 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) } +/// # Safety +/// +/// TODO #[no_mangle] pub extern "C" fn rust_main() -> i32 { let mut roc_str = RocStr::default();