forked from sapir/esp-idf-sys
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
135 lines (117 loc) · 4.13 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
env,
error::Error,
ffi::OsStr,
fs::read_to_string,
io::{BufReader, BufRead, Write},
os::unix::ffi::OsStrExt,
path::PathBuf,
process::{Command, Stdio},
};
use bindgen::EnumVariation;
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=src/bindings.h");
println!("cargo:rerun-if-changed=src/sdkconfig.h");
let (idf_target, linker) = match env::var("TARGET")?.as_ref() {
"xtensa-esp32-none-elf" => {
println!(r#"cargo:rustc-cfg=target_device="esp32""#);
("esp32".to_string(), env::var("RUSTC_LINKER").unwrap_or("xtensa-esp32-elf-ld".to_string()))
},
"xtensa-esp8266-none-elf" => {
println!(r#"cargo:rustc-cfg=target_device="esp8266""#);
("esp8266".to_string(), env::var("RUSTC_LINKER").unwrap_or("xtensa-lx106-elf-ld".to_string()))
},
target => {
println!("cargo:warning=Generating ESP IDF bindings for target '{}' it not supported. The resulting crate will be empty.", target);
return Ok(())
},
};
let idf_path = PathBuf::from(env::var("IDF_PATH").expect("IDF_PATH not set"));
let sysroot = Command::new(linker)
.arg("--print-sysroot")
.output()
.map(|mut output| {
// Remove newline from end.
output.stdout.pop();
PathBuf::from(OsStr::from_bytes(&output.stdout))
.canonicalize().expect("failed to canonicalize sysroot")
})
.expect("failed getting sysroot");
let component_includes =
globwalk::GlobWalkerBuilder::from_patterns(
&idf_path,
&["components/*/include"],
)
.build()?
.filter_map(Result::ok)
.map(|d| d.into_path());
let component_additional_includes = globwalk::GlobWalkerBuilder::from_patterns(
&idf_path,
&["components/*/component.mk"],
)
.build()?
.filter_map(Result::ok)
.flat_map(|makefile| {
let path = makefile.into_path();
let component_path = path.parent().unwrap();
let mut contents = read_to_string(&path).expect("failed reading component.mk").replace("$(info ", "$(warn ");
// Define these variables since they affect `COMPONENT_ADD_INCLUDEDIRS`.
contents.insert_str(0, r"
CONFIG_SYSVIEW_ENABLE :=
CONFIG_AWS_IOT_SDK :=
CONFIG_BT_ENABLED :=
CONFIG_BLUEDROID_ENABLED :=
");
contents.push_str("\n$(info ${COMPONENT_ADD_INCLUDEDIRS})");
let mut child = Command::new("make")
.current_dir(&component_path)
.arg("-f")
.arg("-")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.env("IDF_TARGET", &idf_target)
.env("SOC_NAME", &idf_target)
.env("COMPONENT_PATH", &component_path)
.spawn()
.expect("make failed");
let mut stdin = child.stdin.take().unwrap();
let stdout = child.stdout.take().unwrap();
writeln!(stdin, "{}", contents).unwrap();
BufReader::new(stdout).lines()
.filter_map(Result::ok)
.map(|s| s.trim_end().to_string())
.filter(|s| !s.is_empty())
.flat_map(|s| {
let s = s.split(' ');
let s = s.map(|s| s.to_string());
s.collect::<Vec<_>>().into_iter()
})
.map(move |s| path.parent().unwrap().join(s))
.filter(|s| s.is_dir())
});
let mut includes = component_includes.chain(component_additional_includes)
.map(|include| format!("-I{}", include.display()))
.collect::<Vec<_>>();
includes.sort();
includes.dedup();
let bindings = bindgen::Builder::default()
.use_core()
.layout_tests(false)
.ctypes_prefix("libc")
.default_enum_style(EnumVariation::Rust { non_exhaustive: false } )
.header("src/bindings.h")
.clang_arg(format!("--sysroot={}", sysroot.display()))
.clang_arg(format!("-I{}/include", sysroot.display()))
.clang_arg("-Isrc")
.clang_arg("-D__bindgen")
.clang_args(&["-target", "xtensa"])
.clang_args(&["-x", "c"])
.clang_args(includes);
eprintln!("{:?}", bindings.command_line_flags());
let out_path = PathBuf::from(env::var("OUT_DIR")?);
bindings.generate()
.expect("Failed to generate bindings")
.write_to_file(out_path.join("bindings.rs"))?;
Ok(())
}