|
6 | 6 | // option. This file may not be copied, modified, or distributed
|
7 | 7 | // except according to those terms.
|
8 | 8 |
|
| 9 | +extern crate bindgen; |
| 10 | + |
| 11 | +use std::env; |
| 12 | +use std::path::Path; |
| 13 | +use std::process::Command; |
| 14 | +use std::str; |
9 | 15 |
|
10 | 16 | fn main() {
|
11 | 17 | if std::env::var("TARGET").unwrap().contains("-apple") {
|
12 | 18 | println!("cargo:rustc-link-lib=framework=SystemConfiguration");
|
13 | 19 | }
|
| 20 | + |
| 21 | + let out_dir = env::var("OUT_DIR").expect("OUT_DIR missing from environment"); |
| 22 | + let sdk_path = get_macos_sdk_path(); |
| 23 | + let framework_path = format!("{}/System/Library/Frameworks/", sdk_path); |
| 24 | + let sc_header_dir = format!("{}/SystemConfiguration.framework/Headers/", framework_path); |
| 25 | + |
| 26 | + let sc_header_path = format!("{}/SCDynamicStore.h", sc_header_dir); |
| 27 | + let _ = bindgen::builder() |
| 28 | + .header(sc_header_path) |
| 29 | + .clang_arg(format!("-I{}/usr/include", sdk_path)) |
| 30 | + .clang_arg(format!("-F{}", framework_path)) |
| 31 | + .whitelist_function("SCDynamicStore.*") |
| 32 | + .whitelist_var("kSCDynamicStore.*") |
| 33 | + .blacklist_type("(__)?CF.*") |
| 34 | + .blacklist_type("Boolean") |
| 35 | + .raw_line("use core_foundation_sys::array::CFArrayRef;") |
| 36 | + .raw_line("use core_foundation_sys::base::{Boolean, CFIndex, CFAllocatorRef, CFTypeID};") |
| 37 | + .raw_line("use core_foundation_sys::string::CFStringRef;") |
| 38 | + .raw_line("use core_foundation_sys::dictionary::CFDictionaryRef;") |
| 39 | + .raw_line("use core_foundation_sys::propertylist::CFPropertyListRef;") |
| 40 | + .raw_line("use core_foundation_sys::runloop::CFRunLoopSourceRef;") |
| 41 | + .generate() |
| 42 | + .expect("Unable to generate bindings") |
| 43 | + .write_to_file(Path::new(&out_dir).join("SCDynamicStore.rs")) |
| 44 | + .expect("Unable to write SCDynamicStore.rs"); |
| 45 | +} |
| 46 | + |
| 47 | +fn get_macos_sdk_path() -> String { |
| 48 | + let output = Command::new("xcodebuild") |
| 49 | + .args(&["-sdk", "macosx", "Path", "-version"]) |
| 50 | + .output() |
| 51 | + .expect("Unable to get macOS SDK path with \"xcodebuild\""); |
| 52 | + let stdout = str::from_utf8(&output.stdout).expect("xcodebuild did not print valid utf-8"); |
| 53 | + stdout.trim().to_owned() |
14 | 54 | }
|
0 commit comments