From 449a882329389d794bab7d24f2a55f2c4b1d21c8 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Sat, 7 Nov 2020 01:01:03 +0000 Subject: [PATCH] Check for FFMPEG_DIR in environment.json It's often impractical to use an environment variable to specify FFMPEG_DIR (such as when builds are run via rust-analyzer/RLE for editor diagnostics during development) so we also support parsing an environment.json file like: { "FFMPEG_DIR": "/path/to/ffmpeg-build/" } and re-export all values into the environment Addresses: rust-lang/cargo#4121 --- Cargo.toml | 1 + build.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 45667f2..12ac81a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ num_cpus = "1.11" cc = "1.0" pkg-config = "0.3" bindgen = "0.55" +serde_json = "1.0" [target.'cfg(target_env = "msvc")'.build-dependencies] vcpkg = "0.2" diff --git a/build.rs b/build.rs index c724950..7df0cf1 100644 --- a/build.rs +++ b/build.rs @@ -2,6 +2,7 @@ extern crate bindgen; extern crate cc; extern crate num_cpus; extern crate pkg_config; +extern crate serde_json; use std::env; use std::fs::{self, File}; @@ -14,6 +15,8 @@ use bindgen::callbacks::{ EnumVariantCustomBehavior, EnumVariantValue, IntKind, MacroParsingBehavior, ParseCallbacks, }; +use serde_json::Value; + #[derive(Debug)] struct Library { name: &'static str, @@ -579,6 +582,23 @@ fn link_to_libraries(statik: bool) { fn main() { let statik = env::var("CARGO_FEATURE_STATIC").is_ok(); + // It's often impractical to use an environment variable to specify + // FFMPEG_DIR so we also support parsing an environment.json file + // Ref: rust-lang/cargo#4121 + let manifest_dir_env = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR evironment variable unset"); + let env_path = Path::new(&manifest_dir_env).join("environment.json"); + if let Ok(env) = fs::read_to_string(env_path) { + let env: Value = serde_json::from_str(&env).expect("Failed to parse environment.json"); + if let Some(env_map) = env.as_object() { + for key in env_map.keys() { + if let Some(ffmpeg_dir) = env[key].as_str() { + // Simply re-export as an actual environment variable for consistent handling below... + env::set_var("FFMPEG_DIR", ffmpeg_dir); + } + } + } + } + let include_paths: Vec = if env::var("CARGO_FEATURE_BUILD").is_ok() { println!( "cargo:rustc-link-search=native={}",