rust-pcap / pcap

Rust language pcap library
Apache License 2.0
620 stars 144 forks source link

Build failure, handle different libpcap.so filename #173

Closed gkamathe closed 2 years ago

gkamathe commented 3 years ago

Steps to reproduce

Clone repo

$ git clone https://github.com/ebfull/pcap.git

Try building it, errors out saying "No such file or directory" for libpcap.so

$ cargo build
    Updating crates.io index
  Downloaded memchr v2.4.0
  Downloaded aho-corasick v0.7.18
  Downloaded regex v1.5.4
  Downloaded libc v0.2.95
  Downloaded regex-syntax v0.6.25
  Downloaded 5 crates (1.2 MB) in 0.30s
   Compiling memchr v2.4.0
   Compiling regex-syntax v0.6.25
   Compiling cfg-if v1.0.0
   Compiling libc v0.2.95
   Compiling libloading v0.6.7
   Compiling aho-corasick v0.7.18
   Compiling regex v1.5.4
   Compiling pcap v0.8.1 (/root/PCAP/pcap)
error: failed to run custom build command for `pcap v0.8.1 (/root/PCAP/pcap)`

Caused by:
  process didn't exit successfully: `/root/PCAP/pcap/target/debug/build/pcap-83b401a3db1b0b7e/build-script-build` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=LIBPCAP_LIBDIR
  cargo:rerun-if-env-changed=LIBPCAP_VER

  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DlOpen { desc: "libpcap.so: cannot open shared object file: No such file or directory" }', build.rs:131:42
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$

libpcap library is installed however the symlink is "libpcap.so.1" instead of "libpcap.so"

$rpm -qa | grep -i libpcap
libpcap-1.9.1-5.el8.x86_64
$
$rpm -ql libpcap-1.9.1-5.el8.x86_64 | grep libpcap.so
/usr/lib64/libpcap.so.1
/usr/lib64/libpcap.so.1.9.1
$ ls -l /usr/lib64/libpcap.so.1 /usr/lib64/libpcap.so.1.9.1
lrwxrwxrwx. 1 root root     16 Jan 19 04:43 /usr/lib64/libpcap.so.1 -> libpcap.so.1.9.1
-rwxr-xr-x. 1 root root 312232 Jan 19 04:43 /usr/lib64/libpcap.so.1.9.1
$

Creating a new symlink with expected name solves the build issue

$ ln -s /usr/lib64/libpcap.so.1.9.1 /usr/lib64/libpcap.so
$ 
$ ls -l /usr/lib64/libpcap.so
lrwxrwxrwx. 1 root root 27 May 26 04:17 /usr/lib64/libpcap.so -> /usr/lib64/libpcap.so.1.9.1
$

Able to build now

$ cargo build
   Compiling pcap v0.8.1 (/root/PCAP/pcap)
    Finished dev [unoptimized + debuginfo] target(s) in 0.87s
$

However it would be good to consider possible libpcap.so file names (symlinks) by other means instead of depending on hard coding in the source code

$ grep -irn libpcap.so * | head -1
build.rs:52:    let libfile = "libpcap.so";
$

OS version I used where the "expected" symlink was not available

$ cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.4 (Ootpa)
$
stappersg commented 3 years ago

Is this "Install also the development version of the package" ???

Wojtek242 commented 3 years ago

May also possibly be fixed by using pkg-config as in this old PR: https://github.com/ebfull/pcap/pull/106. Though I don't think this would work on windows. But it would potentially at least solve issues like this. I'll have a look in the coming week or so.

saethlin commented 2 years ago

I'll have a look in the coming week or so.

Did you come up with anything? Totally agree that Windows is a thorny problem here, but could we do a little cfg(target_os = "linux") to get around this?

Wojtek242 commented 2 years ago

It's a bit more complicated than I thought, but a good example to follow is here: https://github.com/aldanor/hdf5-rust/blob/master/hdf5-sys/build.rs

Wojtek242 commented 2 years ago

I opted not to go for a full blown pkg-config solution and instead made build.rs assume some sensible defaults instead if it can't find libpcap.so.