forked from deviceplug/btleplug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
116 lines (107 loc) · 3.51 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
fn main() {
#[cfg(target_os = "linux")]
build::build();
#[cfg(target_os = "windows")]
windows::build!(
windows::devices::bluetooth::generic_attribute_profile::{
GattCharacteristic,
GattCharacteristicProperties,
GattClientCharacteristicConfigurationDescriptorValue,
GattCommunicationStatus,
GattDeviceService,
GattDeviceServicesResult,
GattValueChangedEventArgs,
},
windows::devices::bluetooth::advertisement::*,
windows::devices::bluetooth::{
BluetoothConnectionStatus,
BluetoothLEDevice,
},
windows::devices::radios::{
Radio,
RadioKind
},
windows::storage::streams::{
DataReader,
DataWriter,
},
);
}
#[cfg(target_os = "linux")]
mod build {
use std::{
env,
path::{Path, PathBuf},
};
pub fn build() {
// Only rebuild if the script, or one of the XML files is changed.
println!("cargo:rerun-if-changed=build.rs");
let options = dbus_codegen::GenOpts {
methodtype: None,
genericvariant: true,
propnewtype: true,
..dbus_codegen::GenOpts::default()
};
let output_path = Path::new(env::var("OUT_DIR").unwrap().as_str()).join("bluez_dbus/");
let input_path = Path::new("src/bluez/bluez_dbus/");
std::fs::create_dir_all(&output_path).unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-manager.xml"),
output_path.join("manager.rs"),
&options,
)
.unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-adapter.xml"),
output_path.join("adapter.rs"),
&options,
)
.unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-device.xml"),
output_path.join("device.rs"),
&options,
)
.unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-gatt-service.xml"),
output_path.join("gatt_service.rs"),
&options,
)
.unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-gatt-characteristic.xml"),
output_path.join("gatt_characteristic.rs"),
&options,
)
.unwrap();
generate_dbus_interfaces(
input_path.join("bluez-dbus-introspect-gatt-descriptor.xml"),
output_path.join("gatt_descriptor.rs"),
&options,
)
.unwrap();
}
fn generate_dbus_interfaces(
input_file: PathBuf,
output_file: PathBuf,
options: &dbus_codegen::GenOpts,
) -> Result<(), Box<dyn std::error::Error>> {
// Only rerun this build script if the input file has changed
println!("cargo:rerun-if-changed={}", input_file.display());
let contents = std::fs::read_to_string(&input_file)?;
let output = dbus_codegen::generate(
&contents,
&dbus_codegen::GenOpts {
command_line: format!(
"--generic-variant --methodtype None --file {} --output {}",
input_file.display(),
output_file.display()
),
..options.clone()
},
)?;
std::fs::write(output_file, output)?;
Ok(())
}
}