chore: reorg
This commit is contained in:
5
__ffi/live-reload-rust/README.md
Normal file
5
__ffi/live-reload-rust/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
reference: https://fasterthanli.me/articles/so-you-want-to-live-reload-rust
|
||||
|
||||
PDF: https://playsecurity.org/getdoc/3958_635BCC52F59BF7DB3EC161313629B923/So%20you%20want%20to%20live-reload%20Rust.pdf
|
||||
|
||||
5
__ffi/live-reload-rust/greet-rs-2/Cargo.lock
generated
Normal file
5
__ffi/live-reload-rust/greet-rs-2/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "greet-rs-2"
|
||||
version = "0.1.0"
|
||||
9
__ffi/live-reload-rust/greet-rs-2/Cargo.toml
Normal file
9
__ffi/live-reload-rust/greet-rs-2/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "greet-rs-2"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
36
__ffi/live-reload-rust/greet-rs-2/src/main.rs
Normal file
36
__ffi/live-reload-rust/greet-rs-2/src/main.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::{ffi::c_void, ffi::CString, os::raw::c_char, os::raw::c_int};
|
||||
|
||||
#[link(name = "dl")]
|
||||
extern "C" {
|
||||
fn dlopen(path: *const c_char, flags: c_int) -> *const c_void;
|
||||
fn dlsym(handle: *const c_void, name: *const c_char) -> *const c_void;
|
||||
fn dlclose(handle: *const c_void);
|
||||
}
|
||||
|
||||
// had to look that one up in `dlfcn.h`
|
||||
// in C, it's a #define. in Rust, it's a proper constant
|
||||
pub const RTLD_LAZY: c_int = 0x00001;
|
||||
|
||||
fn main() {
|
||||
let lib_name = CString::new("../libgreet.so").unwrap();
|
||||
let lib = unsafe { dlopen(lib_name.as_ptr(), RTLD_LAZY) };
|
||||
if lib.is_null() {
|
||||
panic!("could not open library");
|
||||
}
|
||||
|
||||
let greet_name = CString::new("greet").unwrap();
|
||||
let greet = unsafe { dlsym(lib, greet_name.as_ptr()) };
|
||||
|
||||
type Greet = unsafe extern "C" fn(name: *const c_char);
|
||||
use std::mem::transmute;
|
||||
let greet: Greet = unsafe { transmute(greet) };
|
||||
|
||||
let name = CString::new("fresh coffee").unwrap();
|
||||
unsafe {
|
||||
greet(name.as_ptr());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
dlclose(lib);
|
||||
}
|
||||
}
|
||||
81
__ffi/live-reload-rust/greet-rs-3/Cargo.lock
generated
Normal file
81
__ffi/live-reload-rust/greet-rs-3/Cargo.lock
generated
Normal file
@@ -0,0 +1,81 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "cstr"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d59a053faf8d8062404e09ef74417a6ac0d337b6729e26ee5f0b4a839f8ff61d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "greet-rs-3"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cstr",
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2443d8f0478b16759158b2f66d525991a05491138bc05814ef52a250148ef4f9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51ef7cd2518ead700af67bf9d1a658d90b6037d77110fd9c0445429d0ba1c6c9"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
11
__ffi/live-reload-rust/greet-rs-3/Cargo.toml
Normal file
11
__ffi/live-reload-rust/greet-rs-3/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "greet-rs-3"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cstr = "*"
|
||||
libloading = "*"
|
||||
14
__ffi/live-reload-rust/greet-rs-3/src/main.rs
Normal file
14
__ffi/live-reload-rust/greet-rs-3/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use cstr::cstr;
|
||||
use std::{error::Error, os::raw::c_char};
|
||||
|
||||
use libloading::{Library, Symbol};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let lib = Library::new("../libgreet.so")?;
|
||||
unsafe {
|
||||
let greet: Symbol<unsafe extern "C" fn(name: *const c_char)> = lib.get(b"greet")?;
|
||||
greet(cstr!("rust macros").as_ptr());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
5
__ffi/live-reload-rust/greet-rs/Cargo.lock
generated
Normal file
5
__ffi/live-reload-rust/greet-rs/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "greet-rs"
|
||||
version = "0.1.0"
|
||||
9
__ffi/live-reload-rust/greet-rs/Cargo.toml
Normal file
9
__ffi/live-reload-rust/greet-rs/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "greet-rs"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
11
__ffi/live-reload-rust/greet-rs/README.md
Normal file
11
__ffi/live-reload-rust/greet-rs/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
build libgreet.so
|
||||
|
||||
|
||||
direct build or use build.rs:
|
||||
RUSTFLAGS="-L ${PWD}/.." cargo build
|
||||
|
||||
|
||||
run:
|
||||
LD_LIBRARY_PATH="${PWD}/.." ./target/debug/greet-rs
|
||||
|
||||
|
||||
10
__ffi/live-reload-rust/greet-rs/build.rs
Normal file
10
__ffi/live-reload-rust/greet-rs/build.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let manifest_dir =
|
||||
PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("manifest dir should be set"));
|
||||
let lib_dir = manifest_dir
|
||||
.parent()
|
||||
.expect("manifest dir should have a parent");
|
||||
println!("cargo:rustc-link-search={}", lib_dir.display());
|
||||
}
|
||||
14
__ffi/live-reload-rust/greet-rs/src/main.rs
Normal file
14
__ffi/live-reload-rust/greet-rs/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use std::{ffi::CString, os::raw::c_char};
|
||||
|
||||
#[link(name = "greet")]
|
||||
extern "C" {
|
||||
fn greet(name: *const c_char);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let name = CString::new("fresh coffee").unwrap();
|
||||
unsafe {
|
||||
greet(name.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
5
__ffi/live-reload-rust/greet.c
Normal file
5
__ffi/live-reload-rust/greet.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void greet(const char *name) {
|
||||
printf("Hello, %s!\n", name);
|
||||
}
|
||||
1
__ffi/live-reload-rust/greet.c-compile-shared.sh
Executable file
1
__ffi/live-reload-rust/greet.c-compile-shared.sh
Executable file
@@ -0,0 +1 @@
|
||||
gcc -Wall -shared greet.c -o libgreet.so
|
||||
1
__ffi/live-reload-rust/greet.c-compile.sh
Executable file
1
__ffi/live-reload-rust/greet.c-compile.sh
Executable file
@@ -0,0 +1 @@
|
||||
gcc -Wall -c greet.c
|
||||
6
__ffi/live-reload-rust/libgreet-rs/Cargo.lock
generated
Normal file
6
__ffi/live-reload-rust/libgreet-rs/Cargo.lock
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "greet"
|
||||
version = "0.1.0"
|
||||
|
||||
12
__ffi/live-reload-rust/libgreet-rs/Cargo.toml
Normal file
12
__ffi/live-reload-rust/libgreet-rs/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "greet"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies]
|
||||
10
__ffi/live-reload-rust/libgreet-rs/src/lib.rs
Normal file
10
__ffi/live-reload-rust/libgreet-rs/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use std::{ffi::CStr, os::raw::c_char};
|
||||
|
||||
/// # Safety
|
||||
/// Pointer must be valid, and point to a null-terminated
|
||||
/// string. What happens otherwise is UB.
|
||||
pub unsafe extern "C" fn greet(name: *const c_char) {
|
||||
let cstr = CStr::from_ptr(name);
|
||||
println!("Hello, {}!", cstr.to_str().unwrap());
|
||||
}
|
||||
|
||||
21
__ffi/live-reload-rust/load-shared.c
Normal file
21
__ffi/live-reload-rust/load-shared.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef void (*greet_t)(const char *name);
|
||||
|
||||
int main(void) {
|
||||
// this was `./libmain.so`
|
||||
void *lib = dlopen("./libgreet.so", RTLD_LAZY);
|
||||
if (!lib) {
|
||||
fprintf(stderr, "failed to load library\n");
|
||||
return 1;
|
||||
}
|
||||
greet_t greet = (greet_t) dlsym(lib, "greet");
|
||||
if (!lib) {
|
||||
fprintf(stderr, "could not look up symbol 'greet'\n");
|
||||
return 1;
|
||||
}
|
||||
greet("venus");
|
||||
dlclose(lib);
|
||||
return 0;
|
||||
}
|
||||
24
__ffi/live-reload-rust/load.c
Normal file
24
__ffi/live-reload-rust/load.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// C function pointer syntax is... something.
|
||||
// Let's typedef our way out of this one.
|
||||
typedef void (*greet_t)(const char *name);
|
||||
|
||||
int main(void) {
|
||||
// what do we want? symbols!
|
||||
// when do we want them? at an implementation-defined time!
|
||||
void *lib = dlopen("./main-greet", RTLD_LAZY);
|
||||
if (!lib) {
|
||||
fprintf(stderr, "failed to load library\n");
|
||||
return 1;
|
||||
}
|
||||
greet_t greet = (greet_t) dlsym(lib, "greet");
|
||||
if (!greet) {
|
||||
fprintf(stderr, "could not look up symbol 'greet'\n");
|
||||
return 1;
|
||||
}
|
||||
greet("venus");
|
||||
dlclose(lib);
|
||||
return 0;
|
||||
}
|
||||
1
__ffi/live-reload-rust/load.c-compile.sh
Executable file
1
__ffi/live-reload-rust/load.c-compile.sh
Executable file
@@ -0,0 +1 @@
|
||||
gcc -Wall load.c -o load
|
||||
6
__ffi/live-reload-rust/main-greet.c
Normal file
6
__ffi/live-reload-rust/main-greet.c
Normal file
@@ -0,0 +1,6 @@
|
||||
extern void greet(const char *name);
|
||||
|
||||
int main(void) {
|
||||
greet("stars");
|
||||
return 0;
|
||||
}
|
||||
2
__ffi/live-reload-rust/main-greet.c-compile-link.sh
Executable file
2
__ffi/live-reload-rust/main-greet.c-compile-link.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
gcc -Wall -c main-greet.c
|
||||
gcc main-greet.o greet.o -o main-greet
|
||||
10
__ffi/live-reload-rust/main.c
Normal file
10
__ffi/live-reload-rust/main.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void greet(const char *name) {
|
||||
printf("Hello, %s!\n", name);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
greet("moon");
|
||||
return 0;
|
||||
}
|
||||
1
__ffi/live-reload-rust/main.c-compile.sh
Executable file
1
__ffi/live-reload-rust/main.c-compile.sh
Executable file
@@ -0,0 +1 @@
|
||||
gcc -Wall main.c -o main
|
||||
Reference in New Issue
Block a user