-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.rs
225 lines (192 loc) · 8.21 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
extern crate bindgen;
use std::env;
use std::fs::{File, read_dir};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug)]
struct CompilerOpts {
compiler_args: Vec<&'static str>,
c_args: Vec<&'static str>,
cpp_args: Vec<&'static str>,
}
static COMMON_COMPILER_ARGS: &'static [&'static str] = &[
"-mthumb",
"-DUSB_SERIAL",
"-DLAYOUT_US_ENGLISH",
"-DTEENSYDUINO=121",
"-g", // TODO:
"-Os", // TODO: Debug/Release split
];
static COMMON_C_ARGS: &'static [&'static str] = &[];
static COMMON_CPP_ARGS: &'static [&'static str] = &["-std=gnu++0x", // TODO: Guarantee this matches the bindgen invokation
"-felide-constructors",
"-fno-exceptions",
"-fno-rtti",
"-fkeep-inline-functions"];
fn flag_sanity_check() -> Result<CompilerOpts, ()> {
let uc_3_0 = cfg!(feature = "teensy_3_0");
let uc_3_1 = cfg!(feature = "teensy_3_1");
let uc_3_2 = cfg!(feature = "teensy_3_2");
let uc_3_5 = cfg!(feature = "teensy_3_5");
let uc_3_6 = cfg!(feature = "teensy_3_6");
let mut base_args = CompilerOpts {
compiler_args: COMMON_COMPILER_ARGS.to_vec(),
c_args: COMMON_C_ARGS.to_vec(),
cpp_args: COMMON_CPP_ARGS.to_vec(),
};
match (uc_3_0, uc_3_1, uc_3_2, uc_3_5, uc_3_6) {
// Teensy 3.0
(true, false, false, false, false) => {
generate_linkerfile(include_bytes!("cores/teensy3/mk20dx128.ld"));
base_args.compiler_args.append(&mut vec!["-mcpu=cortex-m4",
"-D__MK20DX128__",
"-DF_CPU=48000000"]);
Ok(base_args)
}
// Teensy 3.1
(false, true, false, false, false) => {
generate_linkerfile(include_bytes!("cores/teensy3/mk20dx256.ld"));
base_args.compiler_args.append(&mut vec!["-mcpu=cortex-m4",
"-D__MK20DX256__",
"-DF_CPU=48000000"]);
Ok(base_args)
}
// Teensy 3.2
(false, false, true, false, false) => {
generate_linkerfile(include_bytes!("cores/teensy3/mk20dx256.ld"));
base_args.compiler_args.append(&mut vec!["-mcpu=cortex-m4",
"-D__MK20DX256__",
"-DF_CPU=48000000"]);
Ok(base_args)
}
// Teensy 3.5
(false, false, false, true, false) => {
generate_linkerfile(include_bytes!("cores/teensy3/mk64fx512.ld"));
base_args.compiler_args.append(&mut vec![
"-mcpu=cortex-m4",
// Hard float, yo!
// TODO: add a flag for this
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
"-D__MK64FX512__",
"-DF_CPU=120000000",
]);
Ok(base_args)
}
// Teensy 3.6
(false, false, false, false, true) => {
generate_linkerfile(include_bytes!("cores/teensy3/mk66fx1m0.ld"));
base_args.compiler_args.append(&mut vec![
"-mcpu=cortex-m4",
// Hard float, yo!
// TODO: add a flag for this
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
"-D__MK66FX1M0__",
"-DF_CPU=180000000",
]);
Ok(base_args)
}
// Either none or multiple flags were selected
// TODO: more descriptive failures
_ => Err(()),
}
}
fn generate_linkerfile(linker_bytes: &[u8]) {
// Put the linker script somewhere the top crate can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").expect("Failed to read OUT_DIR"));
File::create(out.join("teensy3-sys.ld"))
.expect("Failed to create linkerfile!")
.write_all(linker_bytes)
.expect("Failed to write to linkerfile");
println!("cargo:rustc-link-search={}", out.display());
}
fn main() {
let flags = flag_sanity_check().expect("Bad Feature Flags!");
// TODO: Assert `teensy3-sys.ld` exists
// TODO: Assert `eabi` and `eabihf` targets make sense wrt other flag settings
let source_dirs = ["cores/teensy3", "SPI", "Wire"];
let c_compiler = "arm-none-eabi-gcc";
let cpp_compiler = "arm-none-eabi-g++";
let archive = "arm-none-eabi-ar";
let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("Failed to find Cargo Manifest Dir"));
let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("Failed to read OUT_DIR"));
let includes = source_dirs.iter().flat_map(|dir| vec!["-I", dir]).collect::<Vec<_>>();
let mut objs = Vec::new();
for source_dir in &source_dirs {
for entry in read_dir(crate_dir.join(source_dir)).expect("Failed to read dir") {
let path = entry.expect("Bad Path").path();
let (compiler, extra_args) = match path.extension() {
Some(e) if e == "c" => (c_compiler, &flags.c_args[..]),
Some(e) if e == "cpp" => (cpp_compiler, &flags.cpp_args[..]),
_ => continue,
};
let obj = path.with_extension("o")
.file_name()
.expect("Failed to create file name")
.to_owned();
check(Command::new(compiler)
.args(&flags.compiler_args)
.args(extra_args)
.args(&includes)
.arg("-c")
.arg(Path::new(source_dir)
.join(path.file_name().expect("Failed to get file name")))
.arg("-o")
.arg(out_dir.join(&obj)));
objs.push(obj);
}
}
// TODO: Consider rolling all of the C based deps into one static lib?
// http://stackoverflow.com/questions/3821916/how-to-merge-two-ar-static-libraries-into-one
// "-C", "link-arg=-lm",
// "-C", "link-arg=-lnosys",
// "-C", "link-arg=-lc",
// "-C", "link-arg=-lgcc",
check(Command::new(archive)
.arg("crus")
.arg(out_dir.join("libteensyduino.a"))
.args(objs)
.current_dir(&out_dir));
println!("cargo:rustc-link-search=native={}",
out_dir.to_str().expect("Failed to render out_dir to str"));
println!("cargo:rustc-link-lib=static=teensyduino");
// FIXME (/~https://github.com/jamesmunns/teensy3-rs/issues/17) Remove this hack
let modified_wprogram_h = out_dir.join("WProgram.h");
let mut wprogram_h = String::new();
File::open("cores/teensy3/WProgram.h")
.expect("failed to open header")
.read_to_string(&mut wprogram_h)
.expect("failed to read program header");
File::create(&modified_wprogram_h)
.expect("failed to create program header")
.write_all(wprogram_h.replace("int32_t random(void);", "long random(void);").as_bytes())
.expect("failed to write to program header");
bindgen::Builder::default()
.no_unstable_rust()
.use_core()
.generate_inline_functions(true)
.header("bindings.h")
.ctypes_prefix("c_types")
.clang_args(&flags.compiler_args)
.clang_args(&includes)
.clang_arg("-include")
.clang_arg(modified_wprogram_h.to_str().expect("cant string program header"))
.clang_arg("-x")
.clang_arg("c++")
.clang_arg("-std=gnu++0x")
.clang_arg("-target")
.clang_arg(env::var("TARGET").expect("Why isn't Target set?"))
.generate()
.expect("error when generating bindings")
.write_to_file(out_dir.join("bindings.rs"))
.expect("error when writing bindings");
}
fn check(command: &mut Command) {
match command.status() {
Ok(ref status) if status.success() => {}
Ok(ref status) => panic!("command `{:?}` exited with {}.", command, status),
Err(ref error) => panic!("could not start command `{:?}`: {}.", command, error),
}
}