mini_c_ares_src/
lib.rs

1use std::env;
2use std::fs;
3use std::path::{Path, PathBuf};
4
5pub fn build() {
6    // Rerun if the c-ares source code has changed.
7    println!("cargo:rerun-if-changed=c-ares");
8
9    // We'll compile from source.  Clean up previous build, if any.
10    let outdir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
11    let build = outdir.join("build");
12    let _ = fs::remove_dir_all(&build);
13    fs::create_dir(&build).unwrap();
14
15    let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("c-ares");
16
17    // Export the include path for crates dependending on c-ares
18    println!("cargo:include={}", src.join("include").display());
19
20    // Need libresolv on macos.
21    if cfg!(target_os = "macos") {
22        println!("cargo:rustc-link-lib=resolv");
23    }
24
25    compile(&src)
26}
27
28fn compile(src_dir: &Path) {
29    let dst = cmake::Config::new(src_dir)
30        .define("CARES_STATIC", "ON")
31        .define("CARES_SHARED", "OFF")
32        .define("CARES_BUILD_TOOLS", "OFF")
33        .define("CMAKE_INSTALL_LIBDIR", "lib")
34        .build();
35
36    println!("cargo:rustc-link-search={}/lib", dst.display());
37    println!("cargo:rustc-link-lib=static=cares");
38}