|
| 1 | +//! Build script for `rpi-led-matrix-sys` |
| 2 | +//! |
| 3 | +//! This build script: |
| 4 | +//! 0. checks if we're on a raspberry pi to make sure compilation has a chance of success |
| 5 | +//! 1. copies our git submodule checkout of the C++ library to build artifacts |
| 6 | +//! 2. builds the C++ library from there |
| 7 | +//! 3. statically links against it |
| 8 | +
|
| 9 | +fn main() { |
| 10 | + // Early out if we're stubbing the C api ourselves |
| 11 | + if std::env::var("CARGO_FEATURE_C_STUBS").is_ok() { |
| 12 | + std::process::exit(0); |
| 13 | + } |
| 14 | + |
| 15 | + // 0. To guess at if we're on the right platform, look for linux as the system & arm as the architecture |
| 16 | + // Note I'm checking HOST instead of TARGET since the C++ library depends on natively linking to some libraries |
| 17 | + // that are only on rpis |
| 18 | + let host = std::env::var("HOST").unwrap(); |
| 19 | + if !(host.contains("arm") || host.contains("aarch")) || !host.contains("linux") { |
| 20 | + eprintln!("rpi-led-matrix-sys detected you're likely not compiling on a raspberry pi"); |
| 21 | + std::process::exit(-1); |
| 22 | + } |
| 23 | + |
| 24 | + // 1. copy our git submodule over to build artifacts so things like `cargo clean` work properly |
| 25 | + let target_dir = std::env::var("OUT_DIR").unwrap(); |
| 26 | + let repo_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 27 | + let cpp_lib_dir: std::path::PathBuf = [&repo_dir, "cpp-library"].iter().collect(); |
| 28 | + let cpp_lib_out_dir: std::path::PathBuf = [&target_dir, "cpp-library"].iter().collect(); |
| 29 | + // Make sure our git submodule is checked out and up to date |
| 30 | + let status = std::process::Command::new("git") |
| 31 | + .arg("submodule") |
| 32 | + .arg("init") |
| 33 | + .status() |
| 34 | + .expect("process failed to execute"); |
| 35 | + if !status.success() { |
| 36 | + panic!("failed to checkout the C++ library git submodule"); |
| 37 | + } |
| 38 | + let status = std::process::Command::new("git") |
| 39 | + .arg("submodule") |
| 40 | + .arg("update") |
| 41 | + .status() |
| 42 | + .expect("process failed to execute"); |
| 43 | + if !status.success() { |
| 44 | + panic!("failed to checkout the C++ library git submodule"); |
| 45 | + } |
| 46 | + // delete our output git directory, if it exists, then copy the git repo over |
| 47 | + std::fs::remove_dir_all(&cpp_lib_out_dir).ok(); |
| 48 | + copy_dir::copy_dir(&cpp_lib_dir, &cpp_lib_out_dir).unwrap(); |
| 49 | + println!("cargo:rerun-if-changed={}", cpp_lib_dir.display()); |
| 50 | + |
| 51 | + // 2. build the library. We assume you have the tools necessary to build the library, |
| 52 | + // which I think are available by default on all pis |
| 53 | + let cpp_lib_lib_out_dir: std::path::PathBuf = |
| 54 | + [cpp_lib_out_dir.to_str().unwrap(), "lib"].iter().collect(); |
| 55 | + std::env::set_current_dir(&cpp_lib_lib_out_dir).unwrap(); |
| 56 | + println!("building from {}", cpp_lib_out_dir.display()); |
| 57 | + let status = std::process::Command::new("make") |
| 58 | + .status() |
| 59 | + .expect("process failed to execute"); |
| 60 | + if !status.success() { |
| 61 | + panic!("failed to compile the C++ library"); |
| 62 | + } |
| 63 | + |
| 64 | + // 2.1 rename the library produced to avoid ambiguity with global variants. |
| 65 | + let cpp_lib_lib_out_file: std::path::PathBuf = |
| 66 | + [cpp_lib_out_dir.to_str().unwrap(), "lib", "librgbmatrix.a"] |
| 67 | + .iter() |
| 68 | + .collect(); |
| 69 | + let cpp_lib_lib_out_file_rename: std::path::PathBuf = [ |
| 70 | + cpp_lib_out_dir.to_str().unwrap(), |
| 71 | + "lib", |
| 72 | + "librgbmatrixsys.a", |
| 73 | + ] |
| 74 | + .iter() |
| 75 | + .collect(); |
| 76 | + println!( |
| 77 | + "renaming library from {:?} to {:?}", |
| 78 | + &cpp_lib_lib_out_file, &cpp_lib_lib_out_file_rename |
| 79 | + ); |
| 80 | + std::fs::rename(&cpp_lib_lib_out_file, &cpp_lib_lib_out_file_rename).unwrap(); |
| 81 | + |
| 82 | + // 3. link! |
| 83 | + println!( |
| 84 | + "cargo:rustc-link-search=native={}", |
| 85 | + cpp_lib_lib_out_dir.display() |
| 86 | + ); |
| 87 | + println!("cargo:rustc-link-lib=static=rgbmatrixsys"); |
| 88 | +} |
0 commit comments