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 this enables support for
parsing an environment.json file like:

{
    "FFMPEG_DIR": "/path/to/ffmpeg-build/"
}

Addresses: rust-lang/cargo#4121
  • Loading branch information
rib committed Nov 7, 2020
1 parent 5d4e2fe commit 5fbbdc4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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 = { version = "0.54", default-features = false, features = ["runtime"] }
serde_json = "1.0"

[target.'cfg(target_env = "msvc")'.build-dependencies]
vcpkg = "0.2"
Expand Down
22 changes: 21 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ 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};
use std::io::{self, BufRead, BufReader, Write};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;

use bindgen::callbacks::{
EnumVariantCustomBehavior, EnumVariantValue, IntKind, MacroParsingBehavior, ParseCallbacks,
};

use serde_json::Value;

#[derive(Debug)]
struct Library {
name: &'static str,
Expand Down Expand Up @@ -629,6 +632,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(env_value) = env[key].as_str() {
// Simply re-export as an actual environment variable for consistent handling below...
env::set_var(key, env_value);
}
}
}
}

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 5fbbdc4

Please sign in to comment.