Skip to content

Commit d1b1fce

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 this enables support for parsing an environment.json file like: { "FFMPEG_DIR": "/path/to/ffmpeg-build/" } Addresses: rust-lang/cargo#4121
1 parent 5d4e2fe commit d1b1fce

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Cargo.toml

+1
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 = { version = "0.54", default-features = false, features = ["runtime"] }
26+
serde_json = "1.0"
2627

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

build.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ 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};
89
use std::io::{self, BufRead, BufReader, Write};
9-
use std::path::PathBuf;
10+
use std::path::{Path, PathBuf};
1011
use std::process::Command;
1112
use std::str;
1213

1314
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,
@@ -629,6 +632,23 @@ fn link_to_libraries(statik: bool) {
629632
fn main() {
630633
let statik = env::var("CARGO_FEATURE_STATIC").is_ok();
631634

635+
// It's often impractical to use an environment variable to specify
636+
// FFMPEG_DIR so we also support parsing an environment.json file
637+
// Ref: rust-lang/cargo#4121
638+
let manifest_dir_env = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR evironment variable unset");
639+
let env_path = Path::new(&manifest_dir_env).join("environment.json");
640+
if let Ok(env) = fs::read_to_string(env_path) {
641+
let env: Value = serde_json::from_str(&env).expect("Failed to parse environment.json");
642+
if let Some(env_map) = env.as_object() {
643+
for key in env_map.keys() {
644+
if let Some(ffmpeg_dir) = env[key].as_str() {
645+
// Simply re-export as an actual environment variable for consistent handling below...
646+
env::set_var("FFMPEG_DIR", ffmpeg_dir);
647+
}
648+
}
649+
}
650+
}
651+
632652
let include_paths: Vec<PathBuf> = if env::var("CARGO_FEATURE_BUILD").is_ok() {
633653
println!(
634654
"cargo:rustc-link-search=native={}",

0 commit comments

Comments
 (0)