delight_build/
lib.rs

1#![cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
2//! Utility function for crates depending on `lib3delight`.
3//!
4//! Strictly to be used from within `build.rs`.
5//!
6//! ## Cargo Features
7//!
8//! * `download_lib3delight` -- Downloads (an outdated version) of `lib3delight`
9//!    from Dropbox and copies it to the build directory.
10//!
11//! * `link_lib3delight` -- Links against `lib3delight` during build.
12
13/// Finds/downloads `lib3delight` and sets up linking.
14///
15/// To be used from `build.rs`. The version of `lib3delight` this downloads is
16/// guaranteed to be outdated. This feature is there so e.g. CI builds succeed
17/// without the need to install a full 3Delight package on the build host.
18pub fn setup_lib3delight() -> Result<(), Box<dyn std::error::Error>> {
19    #[cfg(feature = "download_lib3delight")]
20    #[allow(unused_variables)]
21    let lib_path = {
22        use std::{
23            fs::File,
24            io::Write,
25            path::{Path, PathBuf},
26        };
27
28        let lib_path = PathBuf::from(&std::env::var("OUT_DIR")?);
29
30        eprintln!("Building against 3Delight 2.9.30");
31
32        #[cfg(target_os = "windows")]
33        let lib = "https://www.dropbox.com/s/9iavkggor0ecc1x/3Delight.dll";
34        #[cfg(target_os = "macos")]
35        let lib = "https://www.dropbox.com/s/7vle92kcqbbyn8o/lib3delight.dylib";
36        #[cfg(target_os = "linux")]
37        let lib = "https://www.dropbox.com/s/wfw6w6p41lqd8ko/lib3delight.so";
38
39        let lib_path = lib_path.join(Path::new(lib).file_name().unwrap());
40
41        eprintln!("lib:     {}", lib_path.display());
42
43        if !lib_path.exists() {
44            // Download the lib to build against.
45            let lib_data = reqwest::blocking::get(lib.to_owned() + "?dl=1")
46                .ok().ok_or("Failed to download lib3delight")?
47                .bytes()
48                .ok().ok_or("Failed to get data for lib3delight")?;
49
50            File::create(lib_path.clone())?
51                .write_all(&lib_data)?;
52        }
53
54        lib_path.parent().unwrap().to_path_buf()
55    };
56
57    #[cfg(not(feature = "download_lib3delight"))]
58    #[allow(unused_variables)]
59    let lib_path = if let Ok(dl_path) = std::env::var("DELIGHT") {
60        eprintln!("Building against locally installed 3Delight @ {}", &dl_path);
61        let lib_path = std::path::PathBuf::from(dl_path);
62
63        #[cfg(target_os = "windows")]
64        let lib_path = lib_path.join("bin");
65
66        #[cfg(any(target_os = "linux", target_os = "macos"))]
67        let lib_path = lib_path.join("lib");
68
69        lib_path
70    } else {
71        std::path::PathBuf::new()
72    };
73
74    #[cfg(feature = "link_lib3delight")]
75    {
76        // Emit linker searchpath.
77        if lib_path.exists() {
78            println!("cargo:rustc-link-search={}", lib_path.display());
79        }
80
81        // Link to lib3delight.
82        println!("cargo:rustc-link-lib=dylib=3delight");
83    }
84
85    Ok(())
86}