robo9k / rust-magic-sys

Rust declarations crate for the `libmagic` C library
Apache License 2.0
10 stars 6 forks source link

Allow overriding the link search path with `<TARGET>_MAGIC_DIR` #11

Closed bossmc closed 4 years ago

bossmc commented 4 years ago

In a way similar to openssl-sys this allows the user to pass a MAGIC_DIR environment variable that tells rustc where to find libmagic.so/libmagic.a. For finer grained control, the user can pass an environment variable like x86_64_UNKNOWN_LINUX_MUSL_MAGIC_DIR to only set the custom search path for a specific target.

Static linking is also now possible, detected either by only a libmagic.a found in the (provided) search path, or by explicitly passing MAGIC_STATIC=true (or <TARGET>_MAGIC_STATIC=true). Similarly MAGIC_STATIC=false can be passed to choose to link dynamically. If neither is passed, but both libraries are available, the build.rs bails out and asks the user to choose an implementation, an alternative would be to pick one (openssl-sys picks "shared library" in this case).

Tested with a local, static-only, musl-gcc-compiled libmagic:

$ X86_64_UNKNOWN_LINUX_MUSL_MAGIC_STATIC=true X86_64_UNKNOWN_LINUX_MUSL_MAGIC_DIR=../libmagic/src/.libs cargo c -vv --target x86_64-unknown-linux-musl
...
     Running `/home/andy/src/rust-magic-sys/target/debug/build/magic-sys-3a925b5dcd0ba125/build-script-build`
[magic-sys 0.2.0] cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_MAGIC_DIR
[magic-sys 0.2.0] cargo:rustc-link-search=native=../libmagic/src/.libs
[magic-sys 0.2.0] cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_MAGIC_STATIC
[magic-sys 0.2.0] cargo:rustc-link-lib=static=magic
     Running `CARGO=/home/andy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_MANIFEST_DIR=/home/andy/src/rust-magic-sys CARGO_PKG_AUTHORS='robo9k <robo@9k.lv>' CARGO_PKG_DESCRIPTION='Declarations for `libmagic`' CARGO_PKG_HOMEPAGE='https://github.com/robo9k/rust-magic-sys' CARGO_PKG_NAME=magic-sys CARGO_PKG_REPOSITORY='https://github.com/robo9k/rust-magic-sys.git' CARGO_PKG_VERSION=0.2.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE= LD_LIBRARY_PATH='/home/andy/src/rust-magic-sys/target/debug/deps:/home/andy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/andy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/andy/src/rust-magic-sys/target/x86_64-unknown-linux-musl/debug/build/magic-sys-e078dc43334cb961/out sccache-msw rustc --crate-name magic_sys src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata -Cembed-bitcode=no -C debuginfo=2 -C metadata=2a7a520a13672ee4 -C extra-filename=-2a7a520a13672ee4 --out-dir /home/andy/src/rust-magic-sys/target/x86_64-unknown-linux-musl/debug/deps --target x86_64-unknown-linux-musl -C incremental=/home/andy/src/rust-magic-sys/target/x86_64-unknown-linux-musl/debug/incremental -L dependency=/home/andy/src/rust-magic-sys/target/x86_64-unknown-linux-musl/debug/deps -L dependency=/home/andy/src/rust-magic-sys/target/debug/deps --extern libc=/home/andy/src/rust-magic-sys/target/x86_64-unknown-linux-musl/debug/deps/liblibc-df81d5c25afc76a4.rmeta -L native=../libmagic/src/.libs -l static=magic`
    Finished dev [unoptimized + debuginfo] target(s) in 0.92s

If neither environment variable is set, the behaviour is completely unchanged (we link dynamically with the system-wide library).

robo9k commented 4 years ago

Hey, thanks for that pull request! It implements #5 quite nicely.

I've only shallowly tested that plain cargo build && cargo test still works in Ubuntu 20.04 LTS in WSLv2, will see how Travis CI likes this.

@bossmc Are you using rust-magic-sys by itself or would you also like me to update robo9k/rust-magic?

bossmc commented 4 years ago

I'm using rust-magic-sys through rust-magic, but I don't think I need any changes to rust-magic to be able to use this (assuming this is released in a version of rust-magic-sys that's semver-compatible with the previous one, which I think should be possible since the fallback behaviour is unchanged?).

The only thing I'm not sure about is where the documentation for this should live, maybe leave the docs in this repo's README and link to them from rust-magic's README with something like:

By default rust-magic will search your system library paths for a version of libmagic.so, if you're cross-compiling, or need more control over which library is selected, see [here]().

robo9k commented 4 years ago

@bossmc I've updated this repo's README and reference it in rust-magic's README. I've also published a new 0.2.1 version of rust-magic-sys, which should be semver compatible with the 0.2.0 from rust-magic's requirements. CI build of rust-magic is failing, but that's unrelated and should be fixed with the next commit. Please give it a try

bossmc commented 4 years ago

Thanks, that's working for me!

$ X86_64_UNKNOWN_LINUX_MUSL_MAGIC_STATIC=true X86_64_UNKNOWN_LINUX_MUSL_MAGIC_DIR=$PWD/../libmagic/src/.libs cargo b --target x86_64-unknown-linux-musl
   Compiling magic-sys v0.2.1
   Compiling magic v0.12.2
   Compiling magic-test v0.1.0 (/home/andy/src/magic-test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.78s
$ ldd target/x86_64-unknown-linux-musl/debug/magic-test
    not a dynamic executable
$ target/x86_64-unknown-linux-musl/debug/magic-test
mime: text/x-python; charset=us-ascii