robo9k / rust-magic-sys

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

Linking on macOS is still troublesome #34

Open vrmiguel opened 1 year ago

vrmiguel commented 1 year ago

Hey there! I'm trying to use magic-sys on an app of mine but I'm having some issues compiling on macOS

Versions

$ uname -mv 
Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:46 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T8101 arm64
$ brew link libmagic
Warning: Already linked: /opt/homebrew/Cellar/libmagic/5.44
$ rustc --version
rustc 1.68.0-nightly (92c1937a9 2022-12-27)

and magic-sys 0.3.0

Build outputs

When running cargo build

ld: library not found for -lmagic

When running MAGIC_DIR=/opt/homebrew/Cellar/libmagic/5.44/lib MAGIC_STATIC=false cargo run

  thread 'main' panicked at 'No libmagic.so found in "/opt/homebrew/Cellar/libmagic/5.44/lib"', /Users/viniciusmiguel-mba/.cargo/registry/src/github.com-1ecc6299db9ec823/magic-sys-0.3.0/build.rs:30:21

It seems build.rs still only looks for libmagic.so instead of libmagic.dylib on macOS, which causes the build to fail, despite #32

If I go to /opt/homebrew/Cellar/libmagic/5.44/lib and symlink libmagic.dylib to libmagic.so then it works correctly

When running MAGIC_DIR=/opt/homebrew/Cellar/libmagic/5.44/lib MAGIC_STATIC=true cargo run

  = note: Undefined symbols for architecture arm64:
            "_BZ2_bzDecompress", referenced from:
                _uncompressbzlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_BZ2_bzDecompressEnd", referenced from:
                _uncompressbzlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_BZ2_bzDecompressInit", referenced from:
                _uncompressbzlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_inflate", referenced from:
                _uncompresszlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_inflateEnd", referenced from:
                _uncompresszlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_inflateInit2_", referenced from:
                _uncompresszlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_inflateInit_", referenced from:
                _uncompresszlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
            "_zError", referenced from:
                _uncompresszlib in libmagic_sys-1608e149fbcfc5d0.rlib(compress.o)
          ld: symbol(s) not found for architecture arm64
robo9k commented 1 year ago

For libmagic.dylib there's #30 which I didn't get to merge yet.

As for why libmagic is not found with just cargo build I'm not sure. I don't have a Mac, so can't really test how Brew behaves, especially on arm64. I can experiment by using GitHub Actions for this (see .github/workflows/mac.yml) but this is a bit cumbersome. Can you check if the Brew directories are included in the default ld library paths?

The undefined symbols might be solved with using pkg-config, see #1 Could you check whether Brew installed a libmagic.pc or such and post it? This might be a similar case as https://github.com/robo9k/rust-magic-sys/pull/16#issuecomment-949094327 and https://github.com/robo9k/rust-magic-sys/pull/30#issuecomment-1337537973

robo9k commented 1 year ago

Could you elaborate why you got your own wizardry binding instead of using the/my magic crate?