rust-pcap / pcap

Rust language pcap library
Apache License 2.0
610 stars 138 forks source link

Document how to link against a local, static/dynamic libpcap #183

Closed Stargateur closed 2 years ago

Stargateur commented 3 years ago

Would be nice to have a vendored feature that statically link the pcaplib. Like openssl crate for example https://docs.rs/openssl/0.10.36/openssl/#vendored

Wojtek242 commented 3 years ago

Sounds convenient. PRs are welcome :)

Wojtek242 commented 2 years ago

See my latest comment on the PR:https://github.com/rust-pcap/pcap/pull/195#issuecomment-1100212171

Wojtek242 commented 2 years ago

See https://github.com/rust-pcap/pcap/pull/195#issuecomment-1100223874

Wojtek242 commented 2 years ago

Would be nice to have a vendored feature that statically link the pcaplib. Like openssl crate for example https://docs.rs/openssl/0.10.36/openssl/#vendored

I looked into what openssl does and when it's "vendored" it compiles openssl using the openssl-src crate. We won't do that. But, having thought about it, better support/documentation on how to compile against a locally compiled libpcap is worthwhile. Therefore, this issue is actually broader than #195.

Wojtek242 commented 2 years ago

As per the discussion in #195, pcap can be compiled against a static libpcap without modifying any pcap code. Therefore, there is no need to expose a new feature as we won't compile libpcap from scratch like openssl does (too much work, too little gain for a small project like this).

However, there should be some documentation in the README as to how static compiling can be achieved so this issue is getting renamed t reflect this.

Stargateur commented 2 years ago

Do you think we should use something like https://crates.io/crates/pcap-sys ? we make binding by manually in pcap, but don't use a more "raw" level lib. We could talk to maintainer if they agree to come to pcap rust org

Wojtek242 commented 2 years ago

Do you think we should use something like https://crates.io/crates/pcap-sys ? we make binding by manually in pcap, but don't use a more "raw" level lib. We could talk to maintainer if they agree to come to pcap rust org

Sounds like a good idea, but pcap-sys is already taken so I'm not particularly motivated to spend my time resolving that.

Wojtek242 commented 2 years ago

With https://github.com/rust-pcap/pcap/pull/253, the README was sufficiently updated to provide enough information to anybody who wants to build statically. Ultimately, it's a rust compilation issue, not a pcap issue so it's up to the user to consult https://doc.rust-lang.org/cargo/reference/build-scripts.html for static linking. From pcap's point of view, they may have to set LIBPCAP_VER if the static library is not the latest version (which is assumed by default).

Wojtek242 commented 2 years ago

Having said that. If somebody is willing to put the effort into it, PRs along the line of https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages are welcome.

Stargateur commented 2 years ago

it's impossible to statically link for windows, cause the .lib is a fake thing that just say to windows to find a dll.

ilya-epifanov commented 1 year ago

here's a build.rs:

fn main() {
    #[cfg(all(unix, feature = "static"))]
    {
        let linux_kernel_headers = std::env::var("LINUX_KERNEL_HEADERS").ok();

        let mut cmake_config = cmake::Config::new("libpcap");

        if let Some(linux_kernel_headers) = linux_kernel_headers {
            cmake_config.define("DISABLE_USB", "ON");
            cmake_config.define("DISABLE_DBUS", "ON");
            cmake_config.define("DISABLE_BLUETOOTH", "ON");
            cmake_config.define("DISABLE_RDMA", "ON");
            cmake_config.define("ENABLE_REMOTE", "OFF");
            cmake_config.define("USE_STATIC_RT", "ON");
            cmake_config.define("BUILD_SHARED_LIBS", "OFF");
            cmake_config.cflag(format!("-I{}", linux_kernel_headers));
        }

        let mut dst = cmake_config.build();
        dst.push("lib");

        println!("cargo:rustc-link-search=native={}", dst.display());
        println!("cargo:rustc-link-lib=static=pcap");
    }
}

Cargo.toml:

[build-dependencies]
pkg-config = { version = "0.3", optional = true }
cc = { version = "1.0", optional = true }
cmake = { version = "0.1", optional = true }

[features]
static = ["pkg-config", "cc", "cmake"]

also git submodule add https://github.com/the-tcpdump-group/libpcap.git to put libpcap source in the root as build.rs expects this.

stappersg commented 1 year ago

here's a build.rs:

fn main() {
    #[cfg(all(unix, feature = "static"))]
    {
        let linux_kernel_headers = std::env::var("LINUX_KERNEL_HEADERS").ok();
     ....
        println!("cargo:rustc-link-search=native={}", dst.display());
        println!("cargo:rustc-link-lib=static=pcap");
    }
 }

Cargo.toml:

    ....

also git submodule add https://github.com/the-tcpdump-group/libpcap.git to put libpcap source in the root as build.rs expects this.

And what the idea about it? What is the why?

Please consider to put the answer in a merge request.

Regards Geert Stappers

P.S.

For those who want to click on https://github.com/the-tcpdump-group/libpcap, here is the link https://github.com/the-tcpdump-group/libpcap :smiley: