Skip to content

Commit

Permalink
Check for FFMPEG_DIR in environment.json
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rib committed Nov 7, 2020
1 parent 70f711a commit 449a882
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -14,6 +15,8 @@ use bindgen::callbacks::{
EnumVariantCustomBehavior, EnumVariantValue, IntKind, MacroParsingBehavior, ParseCallbacks,
};

use serde_json::Value;

#[derive(Debug)]
struct Library {
name: &'static str,
Expand Down Expand Up @@ -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<PathBuf> = if env::var("CARGO_FEATURE_BUILD").is_ok() {
println!(
"cargo:rustc-link-search=native={}",
Expand Down

0 comments on commit 449a882

Please sign in to comment.