ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
165 stars 79 forks source link

Add support for NetBSD. Avoid needless repetition of targetnames, use compile_unix() as last step. #119

Closed nikkicoon closed 3 months ago

nikkicoon commented 1 year ago

A minimal working example with libhidapi 0.14.0 from pkgsrc gets me as far as this (while building the example in the post below), this seems to be picking up libusb1 as well, is this intended?

hex$ cargo build
warning: unused manifest key: build
warning: use of deprecated method `HidApi::check_error`: use the return values from the other methods
   --> /home/nikita/src/hidapi-rs/src/lib.rs:235:24
    |
235 |             match self.check_error() {
    |                        ^^^^^^^^^^^
    |
    = note: `#[warn(deprecated)]` on by default

warning: `hidapi` (lib) generated 1 warning
   Compiling hello_world v0.1.0 (/home/nikita/src/sandboxes/rustc/hidapi)
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/bin:/home/nikita/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R7/bin:/usr/pkg/bin:/usr/pkg/sbin:/usr/games:/home/nikita/.cache/rebar3/bin:/home/nikita/.npm-packages/bin:/home/nikita/.nimble/bin:/home/nikita/go/bin:/home/nikita/prefix/bin:/home/nikita/.local/bin" VSLANG="1033" "cc" "-Wl,-L/usr/pkg/lib/libatomic" "-m64" "/tmp/rustc1YgO3B/symbols.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.1wok6383z9yby6go.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.27yoh8cqqb4vq39x.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.342lfc2yiucw6j.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.356zv6nuu0teek8p.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.3mag8xn8ez88wqrn.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.3ozpni7844vj3g50.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.3r54koval0psuhc6.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.3tx8ebqranjyimtu.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.45et1z0hr6hc5gco.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.47brcqv16lsgjomx.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.4ambqz524g237vxy.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.4nyblz9sskv80j7x.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.4pj7g3a5oq17p7v9.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.4xmoj2jqvvlo0r3f.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.4y45j8so8p2wfp1g.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.561c9j0m5vex2cv0.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.58yn3g07ol9cj5dk.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.c1fqaqqfwf87pc7.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.qeie366jqpaa90b.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.r5l2hlkpa7l4m6j.rcgu.o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c.1kfuaidr2iww102z.rcgu.o" "-Wl,--as-needed" "-L" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps" "-L" "/usr/pkg/lib" "-L" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib" "-Wl,-Bstatic" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/libhidapi-439f34f3715cbe06.rlib" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/liblibc-848588b6868145b8.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libstd-c7aeaa1fd852d75e.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libpanic_unwind-375051c05a03a5df.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libobject-9a81d30ab65373d2.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libmemchr-cff9f67d90a02abd.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libaddr2line-a8aa1e065b5e03ae.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libgimli-634d0905ede6b27e.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/librustc_demangle-f4d0042764f2d533.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libstd_detect-ac976742d0d5d9bf.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libhashbrown-cb5e76daa14cf945.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libminiz_oxide-d4328da0bee48f45.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libadler-df8f39a3ea14537c.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/librustc_std_workspace_alloc-b7fd7c4c69befd13.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libunwind-251ec1e4fe116345.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libcfg_if-ac657ad1fa2e0560.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/liblibc-9d24e1dda46a6492.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/liballoc-aec36ccfb18eda3d.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/librustc_std_workspace_core-5e1dc4af234fcf9f.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libcore-621b635d001bb138.rlib" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib/libcompiler_builtins-f294b30eb2328b54.rlib" "-Wl,-Bdynamic" "-lhidapi-libusb" "-lutil" "-lrt" "-lutil" "-lexecinfo" "-lpthread" "-lrt" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lrt" "-lutil" "-lexecinfo" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/usr/pkg/lib/rustlib/x86_64-unknown-netbsd/lib" "-o" "/home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/hello_world-8c806a40f1bb268c" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now"
  = note: ld: /home/nikita/src/sandboxes/rustc/hidapi/target/debug/deps/libhidapi-439f34f3715cbe06.rlib(hidapi-439f34f3715cbe06.nv2t9ti3yfxfo1f.rcgu.o): undefined reference to symbol 'libusb_set_option'
          ld: /usr/pkg/lib/libusb-1.0.so.0: error adding symbols: DSO missing from command line

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `hello_world` due to previous error
nikkicoon commented 1 year ago

minimal example:

cargo.toml

[package]
name = "hello_world" # the name of the package
version = "0.1.0"    # the current version, obeying semver
authors = ["Alice <a@example.com>", "Bob <b@example.com>"]
edition = '2021'

[dependencies]
hidapi = { version = "2.3.3", default-features = false, features = ["unix-shared-libusb"], path = "/home/nikita/src/hidapi-rs" }

[build]
rustflags = ["-Clink-args=-lhidapi-libusb"]

src/main.rs:

/****************************************************************************
Copyright (c) 2015 Osspial All Rights Reserved.

This file is part of hidapi-rs, based on hidapi_rust by Roland Ruckerbauer.
****************************************************************************/

//! This example shows the added possibility (after version 0.4.1),
//! to move devices into a function / or closure with static lifetime bounds.

extern crate hidapi;

use hidapi::{HidApi, HidDevice};
use std::rc::Rc;

fn main() {
    let _dev = test_lt();
}

fn requires_static_lt_bound<F: Fn() + 'static>(f: F) {
    f();
}

fn test_lt() -> Rc<HidDevice> {
    let api = HidApi::new().expect("Hidapi init failed");

    let mut devices = api.device_list();

    let dev_info = devices
        .next()
        .expect("There is not a single hid device available");

    let dev = Rc::new(
        api.open(dev_info.vendor_id(), dev_info.product_id())
            .expect("Can not open device"),
    );

    let dev_1 = dev.clone();
    requires_static_lt_bound(move || {
        println!("{:?}", dev_1.get_device_info().unwrap()); //<! Can be captured by closure with static lt
    });

    dev //<! Can be returned from a function, which exceeds the lifetime of the API context
}
ruabmbua commented 1 year ago

I do not think it makes sense to merge the different unix os into one in the build script since they all seem to require different libraries / build flags. Also it will be easier to make quick fixes to one of them if they stop working because of upstream changes.

nikkicoon commented 6 months ago

I do not think it makes sense to merge the different unix os into one in the build script since they all seem to require different libraries / build flags. Also it will be easier to make quick fixes to one of them if they stop working because of upstream changes.

From a multi-systems package manager perspective the way I've proposed makes more sense, since you will end up with oppinionated defaults otherwise which might or might not be true in different contexts (often we have to override such oppinionated defaults when we package something due to false assumptions).

Since libusb and hidraw seem to be mutually exclusive, from what I remember from updating the package many months ago, it makes sense to have it as an either/or decision supported by pkg-config. For every deriviation from this, local patches to the build process can be applied.

ruabmbua commented 6 months ago

Sry for the late review, I forgot to actually submit it O_o

ruabmbua commented 3 months ago

Closed because of inactivity