Skip to content

Commit b2e1011

Browse files
committed
Generate bindings to SCDynamicStore
1 parent 06eeed8 commit b2e1011

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

system-configuration-sys/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ license = "MIT/Apache-2.0"
1010
build = "build.rs"
1111

1212
[dependencies]
13+
core-foundation = "0.4"
14+
core-foundation-sys = "0.4"
15+
16+
[build-dependencies]
17+
bindgen = "0.31"

system-configuration-sys/build.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,49 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
extern crate bindgen;
10+
11+
use std::env;
12+
use std::path::Path;
13+
use std::process::Command;
14+
use std::str;
915

1016
fn main() {
1117
if std::env::var("TARGET").unwrap().contains("-apple") {
1218
println!("cargo:rustc-link-lib=framework=SystemConfiguration");
1319
}
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()
1454
}

system-configuration-sys/src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
1+
// Copyright 2017 Amagicom AB.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
#![allow(non_camel_case_types)]
10+
#![allow(non_upper_case_globals)]
11+
#![allow(non_snake_case)]
12+
13+
extern crate core_foundation_sys;
14+
15+
pub mod dynamic_store {
16+
include!(concat!(env!("OUT_DIR"), "/SCDynamicStore.rs"));
717
}

0 commit comments

Comments
 (0)