Skip to content

Commit 449a882

Browse files
committed
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
1 parent 70f711a commit 449a882

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ num_cpus = "1.11"
2323
cc = "1.0"
2424
pkg-config = "0.3"
2525
bindgen = "0.55"
26+
serde_json = "1.0"
2627

2728
[target.'cfg(target_env = "msvc")'.build-dependencies]
2829
vcpkg = "0.2"

build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate bindgen;
22
extern crate cc;
33
extern crate num_cpus;
44
extern crate pkg_config;
5+
extern crate serde_json;
56

67
use std::env;
78
use std::fs::{self, File};
@@ -14,6 +15,8 @@ use bindgen::callbacks::{
1415
EnumVariantCustomBehavior, EnumVariantValue, IntKind, MacroParsingBehavior, ParseCallbacks,
1516
};
1617

18+
use serde_json::Value;
19+
1720
#[derive(Debug)]
1821
struct Library {
1922
name: &'static str,
@@ -579,6 +582,23 @@ fn link_to_libraries(statik: bool) {
579582
fn main() {
580583
let statik = env::var("CARGO_FEATURE_STATIC").is_ok();
581584

585+
// It's often impractical to use an environment variable to specify
586+
// FFMPEG_DIR so we also support parsing an environment.json file
587+
// Ref: rust-lang/cargo#4121
588+
let manifest_dir_env = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR evironment variable unset");
589+
let env_path = Path::new(&manifest_dir_env).join("environment.json");
590+
if let Ok(env) = fs::read_to_string(env_path) {
591+
let env: Value = serde_json::from_str(&env).expect("Failed to parse environment.json");
592+
if let Some(env_map) = env.as_object() {
593+
for key in env_map.keys() {
594+
if let Some(ffmpeg_dir) = env[key].as_str() {
595+
// Simply re-export as an actual environment variable for consistent handling below...
596+
env::set_var("FFMPEG_DIR", ffmpeg_dir);
597+
}
598+
}
599+
}
600+
}
601+
582602
let include_paths: Vec<PathBuf> = if env::var("CARGO_FEATURE_BUILD").is_ok() {
583603
println!(
584604
"cargo:rustc-link-search=native={}",

0 commit comments

Comments
 (0)