Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 227 additions & 127 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/example-monoio-mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ monoio = { workspace = true, features = ['sync'] }
rust2go = { path = "../../rust2go", features = ["build"] }

[[bin]]
name = "example-mem"
name = "example-monoio-mem"
path = "src/main.rs"
2 changes: 1 addition & 1 deletion examples/example-monoio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ monoio = { workspace = true, features = ['sync'] }
rust2go = { path = "../../rust2go", features = ["build"] }

[[bin]]
name = "example-cgo"
name = "example-monoio-cgo"
path = "src/main.rs"
2 changes: 1 addition & 1 deletion examples/example-tokio-mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ tokio = { version = "1", features = ["full"] }
rust2go = { path = "../../rust2go", features = ["build"] }

[[bin]]
name = "example"
name = "example-tokio-mem"
path = "src/main.rs"
2 changes: 1 addition & 1 deletion examples/example-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ tokio = { version = "1", features = ["full"] }
rust2go = { path = "../../rust2go", features = ["build"] }

[[bin]]
name = "example"
name = "example-tokio-cgo"
path = "src/main.rs"
5 changes: 3 additions & 2 deletions rust2go/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust2go"
version = "0.4.1"
version = "0.4.2"

description = "Rust2go main shared library."

Expand All @@ -18,7 +18,8 @@ rust2go-cli = { version = "0.4.1", path = "../rust2go-cli", optional = true }

bindgen = { version = "0.71", optional = true }
syn = { version = "2", features = ["full"], optional = true }
fs-set-times = { version = "0.20", optional = true }

[features]
default = []
build = ["syn", "bindgen", "rust2go-cli"]
build = ["syn", "bindgen", "rust2go-cli", "fs-set-times"]
57 changes: 51 additions & 6 deletions rust2go/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,32 @@ pub trait GoCompiler {
fn go_build(&self, go_src: &Path, link: LinkType, output: &Path);

fn build(&self, go_src: &Path, binding_name: &str, link: LinkType, copy_lib: &CopyLib) {
use std::env::consts::DLL_PREFIX;

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_filename = filename(link);
let out_filename = lib_filename(link);
let output = out_dir.join(&out_filename);

self.go_build(go_src, link, output.as_path());
let header_filename = header_filename();
let header = out_dir.join(&header_filename);

{
// Get header file content and metadata to reset .h atime/mtime for compile speed.
let (header_content, header_time) = read_header_file(&header);

self.go_build(go_src, link, output.as_path());

let (header_content_after, _) = read_header_file(&header);
if header_content == header_content_after && !header_content_after.is_empty() {
// If the header file is not changed, reset atime/mtime to avoid recompilation.
if let Some((atime, mtime)) = header_time {
if let Some(atime) = atime {
let _ = fs_set_times::set_atime(&header, atime.into());
}
if let Some(mtime) = mtime {
let _ = fs_set_times::set_mtime(&header, mtime.into());
}
}
}
}

// Copy the DLL file to target dir.
if link == LinkType::Dynamic {
Expand Down Expand Up @@ -200,7 +219,7 @@ pub trait GoCompiler {
}

let bindings = bindgen::Builder::default()
.header(out_dir.join(format!("{DLL_PREFIX}go.h")).to_str().unwrap())
.header(header.to_str().unwrap())
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
Expand Down Expand Up @@ -331,11 +350,37 @@ impl<GOC: GoCompiler> Builder<PathBuf, GOC> {
}
}

fn filename(link_type: LinkType) -> String {
fn lib_filename(link_type: LinkType) -> String {
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};

match link_type {
LinkType::Static => format!("{DLL_PREFIX}go{LIB_EXT}"),
LinkType::Dynamic => format!("{DLL_PREFIX}go{DLL_SUFFIX}"),
}
}

fn header_filename() -> String {
use std::env::consts::DLL_PREFIX;

format!("{DLL_PREFIX}go.h")
}

// Helper function to read header file content, size, and times.
#[allow(clippy::type_complexity)]
fn read_header_file(
path: &Path,
) -> (
Vec<u8>,
Option<(Option<std::time::SystemTime>, Option<std::time::SystemTime>)>,
) {
use std::io::Read;

let mut file = std::fs::File::open(path).ok();
let mut content = Vec::new();
file.as_mut().and_then(|h| h.read_to_end(&mut content).ok());
let time = file
.as_ref()
.and_then(|f| f.metadata().ok())
.map(|m| (m.accessed().ok(), m.modified().ok()));
(content, time)
}
16 changes: 16 additions & 0 deletions test/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type TestCall interface {
add_friends(req *FriendsListRequest) FriendsListResponse
delete_friends(req *FriendsListRequest) FriendsListResponse
pm_friend(req *PMFriendRequest) PMFriendResponse
multi_param_test(user *User, message *string, token *[]uint8) LoginResponse
}

//export CTestCall_ping
Expand Down Expand Up @@ -147,6 +148,21 @@ func CTestCall_pm_friend(req C.PMFriendRequestRef, slot *C.void, cb *C.void) {
}()
}

//export CTestCall_multi_param_test
func CTestCall_multi_param_test(user C.UserRef, message C.StringRef, token C.ListRef, slot *C.void, cb *C.void) {
_new_user := newUser(user)
_new_message := newString(message)
_new_token := new_list_mapper_primitive(newC_uint8_t)(token)
go func() {
resp := TestCallImpl.multi_param_test(&_new_user, &_new_message, &_new_token)
resp_ref, buffer := cvt_ref(cntLoginResponse, refLoginResponse)(&resp)
asmcall.CallFuncG0P2(unsafe.Pointer(cb), unsafe.Pointer(&resp_ref), unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}()
}

// An alternative impl of unsafe.String for go1.18
func unsafeString(ptr *byte, length int) string {
sliceHeader := &reflect.SliceHeader{
Expand Down
Loading