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

Using pre-built library? #26

Open
Fuuzetsu opened this issue Sep 29, 2022 · 7 comments
Open

Using pre-built library? #26

Fuuzetsu opened this issue Sep 29, 2022 · 7 comments

Comments

@Fuuzetsu
Copy link

It appears glfw-sys will always compile glfw for whatever the target system is.

However we control the build system precisely and have the ability to provide it with pre-built glfw. It seems a shame/pointless to compile anyway: instead of giving it pre-built library, we instead give it all the tools to build one...

A lot of other *-sys crates allow specifying existing libraries with things like pkg-config which can also ensure all the libraries are properly presented when linking binaries later on.

Is there a reason why glfw needs to be pre-built? I'm a little confused if this is actually a requirement: the README mentions that Windows users can download pre-built glfw binaries. If so, that should imply that it's possible to use pre-compiled libraries, somehow.

@Fuuzetsu
Copy link
Author

After some experimentation, I've come to realise that it makes no sense for this ticket to exist on glfw-sys: all glfw-sys does is build library and spit out some flags.

Instead, we should add a feature flag to glfw-rs that uses pkg-config to find the library instead of getting it via glfw-sys. I'll make a ticket there and link here.

@Fuuzetsu
Copy link
Author

Fuuzetsu commented Oct 4, 2022

I was asked to reopen this at PistonDevelopers/glfw-rs#506 .

@Fuuzetsu Fuuzetsu reopened this Oct 4, 2022
@Diegovsky
Copy link

Is this being worked on still?

@Fuuzetsu
Copy link
Author

I haven't done anything but it's still desirable. I think the way to do it is you just add pkg_config to build deps and something like pkg_config::probe_library("glfw3") at start of build.rs main and in case it fails, fall back to building (current code). I guess I can try it later today if I don't forget.

Fuuzetsu added a commit to Fuuzetsu/glfw-sys that referenced this issue Feb 28, 2024
Fuuzetsu added a commit to Fuuzetsu/glfw-sys that referenced this issue Feb 28, 2024
@Fuuzetsu
Copy link
Author

So, I tried a simple patch like the one at the bottom. It works in-as-so-far as getting glfw-sys to find the library and include it later. However there's an issue: once we come to try and use this in glfw-rs via glfw-sys feature flag, we get:

rust_glfw> Building src/lib.rs (glfw)
rust_glfw> Running rustc --crate-name glfw src/lib.rs --out-dir target/lib -L dependency=target/deps --cap-lints allow -L native=/nix/store/5msqd90ci50307ffs5i1avb6k78z6a41-glfw-3.3.8/lib -C opt-level=3 -C codegen-units=1 --remap-path-prefix=/build=/ --extern bitflags=/nix/store/a6bgm020s04nynwnzsbqn10ghwm5gpsf-rust_bitflags-1.3.2-lib/lib/libbitflags-25edeaf30b.rlib --extern glfw_sys=/nix/store/k4sljpxxy6c074kh5l9ajpzlw44ngzz9-rust_glfw-sys-5.0.0+3.3.9-lib/lib/libglfw_sys-29d93d1f86.rlib --extern raw_window_handle_0_5=/nix/store/9fz8iq16hmrv39qg5rjjwh874ld8xbcm-rust_raw-window-handle-0.5.2-lib/lib/libraw_window_handle-2212bb0514.rlib --cfg feature="glfw-sys" --edition 2021 -C metadata=44075dcdb8 -C extra-filename=-44075dcdb8 --crate-type lib -L native=/nix/store/5msqd90ci50307ffs5i1avb6k78z6a41-glfw-3.3.8/lib --color always
rust_glfw> error: could not find native static library `glfw3`, perhaps an -L flag is missing?
rust_glfw> error: aborting due to 1 previous error
error: builder for '/nix/store/5ijyddy0abr8cz7yswk2kj8xhvwqs8yx-rust_glfw-0.55.0.drv' failed with exit code 1;
       last 10 log lines:
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Running phase: configurePhase
       > Running cd .
       > Running phase: buildPhase
       > Building src/lib.rs (glfw)
       > Running rustc --crate-name glfw src/lib.rs --out-dir target/lib -L dependency=target/deps --cap-lints allow -L native=/nix/store/5msqd90ci50307ffs5i1avb6k78z6a41-glfw-3.3.8/lib -C opt-level=3 -C codegen-units=1 --remap-path-prefix=/build=/ --extern bitflags=/nix/store/a6bgm020s04nynwnzsbqn10ghwm5gpsf-rust_bitflags-1.3.2-lib/lib/libbitflags-25edeaf30b.rlib --extern glfw_sys=/nix/store/k4sljpxxy6c074kh5l9ajpzlw44ngzz9-rust_glfw-sys-5.0.0+3.3.9-lib/lib/libglfw_sys-29d93d1f86.rlib --extern raw_window_handle_0_5=/nix/store/9fz8iq16hmrv39qg5rjjwh874ld8xbcm-rust_raw-window-handle-0.5.2-lib/lib/libraw_window_handle-2212bb0514.rlib --cfg feature="glfw-sys" --edition 2021 -C metadata=44075dcdb8 -C extra-filename=-44075dcdb8 --crate-type lib -L native=/nix/store/5msqd90ci50307ffs5i1avb6k78z6a41-glfw-3.3.8/lib --color always
       > error: could not find native static library `glfw3`, perhaps an -L flag is missing?
       >
       > error: aborting due to 1 previous error
       >
       For full logs, run 'nix log /nix/store/5ijyddy0abr8cz7yswk2kj8xhvwqs8yx-rust_glfw-0.55.0.drv'.
error: 1 dependencies of derivation '/nix/store/8i5x0s9pmfp1q5pip7ak0p1njc94zpi2-rust_isim-0.1.0.drv' failed to build

We can see the -L native=/nix/store/5msqd90ci50307ffs5i1avb6k78z6a41-glfw-3.3.8/lib which is my system install that glfw-sys helpfully found. However the error message is talking about a static library! Presumably this is because of this code in glfw-rs: https://github.com/PistonDevelopers/glfw-rs/blob/b8845df2142c868557b1dc6687c0b71540203627/src/ffi/link.rs#L17 . It always assumes that glfw-sys produces a static library, which is not true anymore.

My original patch (which we are using since I made the ticket) didn't have this issue because it bypassed glfw-sys all together.

I'm not sure what the right way forward is. Should we just take off kind = "static" in link.rs? I'm not sure what the consequences are for the people using glfw-sys currently.

diff --git a/Cargo.toml b/Cargo.toml
index 720802e0..b2287c9f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,6 +17,7 @@ path = "lib.rs"
 
 [build-dependencies]
 cmake = "0.1"
+pkg-config = "0.3"
 
 [features]
 wayland = []
diff --git a/build.rs b/build.rs
index d0a2bde9..53aaeded 100644
--- a/build.rs
+++ b/build.rs
@@ -1,7 +1,15 @@
 extern crate cmake;
+extern crate pkg_config;
+
 use cmake::Config;
 
 fn main() {
+    if pkg_config::probe_library("glfw3").is_ok() {
+        // We got the library via pkg_config, we don't need to try and build it
+        // below.
+        return;
+    }
+
     let mut cfg = Config::new("glfw");
 
     cfg.define("GLFW_BUILD_EXAMPLES", "OFF")

@Fuuzetsu
Copy link
Author

PS: In the end we also want X11 to be found by pkgconfig so we end up patching glfw-rs either way... I guess there should be x11-sys dependency that already handles this.

@Diegovsky
Copy link

Wow, thanks for working on this! It's a shame it can't be done right now tho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants