phidget-sys 0.1.5

Low-level, unsafe, Rust wrapper for the phidget22 library.
Documentation
// phidget-rs/build.rs
//
// Copyright (c) 2023, Frank Pagliughi
//
// Licensed under the MIT license:
//   <LICENSE or http://opensource.org/licenses/MIT>
// This file may not be copied, modified, or distributed except according
// to those terms.
//

use std::env;

// Looks like the latest Phidgets installer puts the framework into
// '/Library/Frameworks'
//
// Earlier versions sometimes used architecture-specific locations.
//
// But it also seems that the dynlib is in /usr/local/lib, so standard
// linking should work, too.
#[cfg(target_os = "macos")]
fn config_macos() {
    println!("cargo:rustc-link-lib=framework=Phidget22");
    println!(r"cargo:rustc-link-search=framework=/Library/Frameworks");

    let fw_path = if cfg!(target_arch = "x86_64") {
        "/usr/local"
    }
    else {
        "/opt/homebrew"
    };
    println!(r"cargo:rustc-link-search=framework={}/Frameworks", fw_path);
}

fn main() {
    // TODO: We should eventually find or regenerate the
    //      bindings file for the specific target.
    let tgt = env::var("TARGET").unwrap();
    println!("debug: Building for target: '{}'", tgt);

    // PHIDGET_ROOT should be set to point to the installation directory
    // of phidgets if placed into a non-standard location.
    // (e.g. C:\Program Files\Phidgets\Phidget22)

    #[cfg(target_os = "windows")]
    let phidget_libs = match env::var("PHIDGET_ROOT") {
        Err(std::env::VarError::NotPresent) => {
            Ok(r"C:\Program Files\Phidgets\Phidget22".to_string())
        }
        res => res,
    };

    #[cfg(not(target_os = "windows"))]
    let phidget_libs = env::var("PHIDGET_ROOT");

    if let Ok(libs) = phidget_libs {
        println!("cargo:rustc-link-search={}", libs);
    }

    #[cfg(target_os = "macos")]
    config_macos();

    #[cfg(not(target_os = "macos"))]
    println!("cargo:rustc-link-lib=phidget22");
}