Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not hard code libgo.a and libgo.so #28

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
24 changes: 15 additions & 9 deletions rust2go/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<GOSRC, GOC> Builder<GOSRC, GOC> {
self
}

/// Copy libgo.so to target dir.
/// Copy DLL to target dir.
pub fn with_copy_lib(mut self, copy_lib: CopyLib) -> Self {
self.copy_lib = copy_lib;
self
Expand All @@ -121,15 +121,12 @@ pub trait GoCompiler {

fn build(&self, go_src: &Path, binding_name: &str, link: LinkType, copy_lib: &CopyLib) {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let output = out_dir.join(if link == LinkType::Static {
"libgo.a"
} else {
"libgo.so"
});
let out_filename = filename(link);
let output = out_dir.join(&out_filename);

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

// Copy .so file to target dir.
// Copy the DLL file to target dir.
if link == LinkType::Dynamic {
// A workaround to get target dir.
// From https://github.com/rust-lang/cargo/issues/9661#issuecomment-1722358176
Expand All @@ -154,11 +151,11 @@ pub trait GoCompiler {
CopyLib::DefaultPath => {
let target_dir = get_cargo_target_dir().unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
std::fs::copy(out_dir.join("libgo.so"), target_dir.join("libgo.so"))
std::fs::copy(out_dir.join(&out_filename), target_dir.join(&out_filename))
.expect("unable to copy dynamic library");
}
CopyLib::CustomPath(p) => {
std::fs::copy(out_dir.join("libgo.so"), p.join("libgo.so"))
std::fs::copy(out_dir.join(&out_filename), p.join(&out_filename))
.expect("unable to copy dynamic library");
}
}
Expand Down Expand Up @@ -226,3 +223,12 @@ impl<GOC: GoCompiler> Builder<PathBuf, GOC> {
.build(&self.go_src, binding_name, self.link, &self.copy_lib);
}
}

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

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