1 // Copyright © 2018 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license.  See the
4 // accompanying file LICENSE for details.
5 
6 #[cfg(not(feature = "gecko-in-tree"))]
7 extern crate cmake;
8 #[cfg(not(feature = "gecko-in-tree"))]
9 extern crate pkg_config;
10 
11 use std::env;
12 use std::fs;
13 use std::path::Path;
14 use std::process::Command;
15 
16 macro_rules! t {
17     ($e:expr) => {
18         match $e {
19             Ok(e) => e,
20             Err(e) => panic!("{} failed with {}", stringify!($e), e),
21         }
22     };
23 }
24 
25 #[cfg(feature = "gecko-in-tree")]
main()26 fn main() {}
27 
28 #[cfg(not(feature = "gecko-in-tree"))]
main()29 fn main() {
30     if env::var("LIBCUBEB_SYS_USE_PKG_CONFIG").is_ok()
31         && pkg_config::find_library("libcubeb").is_ok()
32     {
33         return;
34     }
35 
36     if !Path::new("libcubeb/.git").exists() {
37         let _ = Command::new("git")
38             .args(&["submodule", "update", "--init", "--recursive"])
39             .status();
40     }
41 
42     let target = env::var("TARGET").unwrap();
43     let windows = target.contains("windows");
44     let darwin = target.contains("darwin");
45     let freebsd = target.contains("freebsd");
46     let android = target.contains("android");
47     let mut cfg = cmake::Config::new("libcubeb");
48 
49     let _ = fs::remove_dir_all(env::var("OUT_DIR").unwrap());
50     t!(fs::create_dir_all(env::var("OUT_DIR").unwrap()));
51 
52     env::remove_var("DESTDIR");
53     let dst = cfg
54         .define("BUILD_SHARED_LIBS", "OFF")
55         .define("BUILD_TESTS", "OFF")
56         .define("BUILD_TOOLS", "OFF")
57         .build();
58 
59     println!("cargo:rustc-link-lib=static=cubeb");
60     if windows {
61         println!("cargo:rustc-link-lib=dylib=avrt");
62         println!("cargo:rustc-link-lib=dylib=ole32");
63         println!("cargo:rustc-link-lib=dylib=user32");
64         println!("cargo:rustc-link-lib=dylib=winmm");
65         println!("cargo:rustc-link-search=native={}/lib", dst.display());
66     } else if darwin {
67         println!("cargo:rustc-link-lib=framework=AudioUnit");
68         println!("cargo:rustc-link-lib=framework=CoreAudio");
69         println!("cargo:rustc-link-lib=framework=CoreServices");
70         println!("cargo:rustc-link-lib=dylib=c++");
71         println!("cargo:rustc-link-search=native={}/lib", dst.display());
72     } else {
73         if freebsd || android {
74             println!("cargo:rustc-link-lib=dylib=c++");
75         } else {
76             println!("cargo:rustc-link-lib=dylib=stdc++");
77         }
78         println!("cargo:rustc-link-search=native={}/lib", dst.display());
79         println!("cargo:rustc-link-search=native={}/lib64", dst.display());
80 
81         // Ignore the result of find_library. We don't care if the
82         // libraries are missing.
83         let _ = pkg_config::find_library("alsa");
84         let _ = pkg_config::find_library("libpulse");
85         let _ = pkg_config::find_library("jack");
86         if android {
87             println!("cargo:rustc-link-lib=dylib=OpenSLES");
88         }
89     }
90 }
91