-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.rs
More file actions
336 lines (321 loc) · 10.9 KB
/
main.rs
File metadata and controls
336 lines (321 loc) · 10.9 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! A series of utilities for building Neotron OS
use clap::{Parser, Subcommand};
#[derive(Debug, Subcommand)]
enum Commands {
/// Builds the OS and the ROMFS
Binary {
/// The start address in Flash where Neotron OS should live
#[clap(long, default_value = "0x1002_0000")]
start_address: String,
/// The target we're building Neotron OS for
#[clap(long, default_value = "thumbv6m-none-eabi")]
target: String,
},
/// Builds the OS as a library, for the native machine
Library {
/// The target we're building Neotron OS for
#[clap(long)]
target: Option<String>,
},
/// Handles formatting of the Neotron OS source code
Format {
/// Whether to just check the formatting
#[clap(long)]
check: bool,
},
/// Checks the Neotron OS source code using clippy
Clippy,
/// Runs any tests
Test,
}
/// A simple utility for building Neotron OS and a suitable ROMFS image
#[derive(Debug, Parser)]
#[clap(name = "nbuild", version = "0.1.0", author = "The Neotron Developers")]
pub struct NBuildApp {
/// The task to perform
#[command(subcommand)]
command: Option<Commands>,
}
fn packages() -> Vec<nbuild::Package> {
vec![
// *** build system ***
nbuild::Package {
name: "nbuild",
path: std::path::Path::new("./nbuild/Cargo.toml"),
output_template: None,
kind: nbuild::PackageKind::NBuild,
testable: nbuild::Testable::All,
},
// *** utilities ***
nbuild::Package {
name: "desktop",
path: std::path::Path::new("./utilities/desktop/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/desktop"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
nbuild::Package {
name: "flames",
path: std::path::Path::new("./utilities/flames/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/flames"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
nbuild::Package {
name: "logo",
path: std::path::Path::new("./utilities/logo/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/logo"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
nbuild::Package {
name: "neoplay",
path: std::path::Path::new("./utilities/neoplay/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/neoplay"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
nbuild::Package {
name: "snake",
path: std::path::Path::new("./utilities/snake/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/snake"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
nbuild::Package {
name: "vidtest",
path: std::path::Path::new("./utilities/vidtest/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/vidtest"),
kind: nbuild::PackageKind::Utility,
testable: nbuild::Testable::No,
},
// *** OS ***
nbuild::Package {
name: "Neotron OS",
path: std::path::Path::new("./neotron-os/Cargo.toml"),
output_template: Some("./target/{target}/{profile}/neotron-os"),
kind: nbuild::PackageKind::Os,
testable: nbuild::Testable::Libs,
},
]
}
fn main() {
println!("Neotron OS nbuild tool");
let args = NBuildApp::parse();
let packages = packages();
match args.command {
None => {
// No command given
println!("No command given. Try `cargo nbuild help`.");
std::process::exit(1);
}
Some(Commands::Binary {
start_address,
target,
}) => {
binary(&packages, &start_address, &target);
}
Some(Commands::Library { target }) => library(&packages, target.as_deref()),
Some(Commands::Format { check }) => format(&packages, check),
Some(Commands::Clippy) => clippy(&packages),
Some(Commands::Test) => test(&packages),
}
}
/// Builds the utility and OS packages as binaries
fn binary(packages: &[nbuild::Package], start_address: &str, target: &str) {
use chrono::{Datelike, Timelike};
let mut is_error = false;
let Ok(start_address) = nbuild::parse_int(start_address) else {
eprintln!("{:?} was not a valid integer", start_address);
std::process::exit(1);
};
let mut romfs_entries = Vec::new();
// Build utilities
for package in packages
.iter()
.filter(|p| p.kind == nbuild::PackageKind::Utility)
{
println!(
"Cross-compiling {}, using target {:?}",
package.name, target
);
if let Err(e) = nbuild::cargo(&["build", "--release"], Some(target), package.path) {
eprintln!("Build of {} failed: {}", package.name, e);
is_error = true;
}
let package_output = package
.output(target, "release")
.expect("utilties should have an output");
let stripped = package_output.clone() + ".stripped";
if let Err(e) = nbuild::strip_elf(&package_output, &stripped) {
eprintln!("Reading of {} failed: {}", stripped, e);
continue;
};
let contents = match std::fs::read(&stripped) {
Ok(contents) => contents,
Err(e) => {
eprintln!("Reading of {} failed: {}", stripped, e);
continue;
}
};
let ctime = std::time::SystemTime::now();
let ctime = chrono::DateTime::<chrono::Utc>::from(ctime);
romfs_entries.push(neotron_romfs::Entry {
metadata: neotron_romfs::EntryMetadata {
file_name: package.name,
ctime: neotron_api::file::Time {
year_since_1970: (ctime.year() - 1970) as u8,
zero_indexed_month: ctime.month0() as u8,
zero_indexed_day: ctime.day0() as u8,
hours: ctime.hour() as u8,
minutes: ctime.minute() as u8,
seconds: ctime.second() as u8,
},
file_size: contents.len() as u32,
},
contents,
});
}
// Build ROMFS
let mut buffer = Vec::new();
let _size = match neotron_romfs::RomFs::construct_into(&mut buffer, &romfs_entries) {
Ok(size) => size,
Err(e) => {
eprintln!("Making ROMFS failed: {:?}", e);
std::process::exit(1);
}
};
let mut romfs_path = std::path::PathBuf::new();
romfs_path.push(std::env::current_dir().expect("We have no CWD?"));
romfs_path.push("target");
romfs_path.push(target);
romfs_path.push("release");
romfs_path.push("romfs.bin");
if let Err(e) = std::fs::write(&romfs_path, &buffer) {
eprintln!("Writing ROMFS to {} failed: {:?}", romfs_path.display(), e);
std::process::exit(1);
}
println!("Built ROMFS at {}", romfs_path.display());
// Build OS
for package in packages
.iter()
.filter(|p| p.kind == nbuild::PackageKind::Os)
{
println!(
"Cross-compiling {}, using start address 0x{:08x} and target {:?}",
package.name, start_address, target
);
let environment = [
(
"NEOTRON_OS_START_ADDRESS",
format!("0x{:08x}", start_address),
),
("ROMFS_PATH", romfs_path.to_string_lossy().to_string()),
];
if let Err(e) = nbuild::cargo_with_env(
&["build", "--release"],
Some(target),
package.path,
&environment,
) {
eprintln!("Build of {} failed: {}", package.name, e);
is_error = true;
}
let package_output = package
.output(target, "release")
.expect("PackageKind::Os should always have output");
if let Err(e) = nbuild::make_bin(&package_output) {
eprintln!("objcopy of {} failed: {}", package_output, e);
is_error = true;
}
}
if is_error {
std::process::exit(1);
}
}
/// Builds the OS packages as a library
fn library(packages: &[nbuild::Package], target: Option<&str>) {
let mut is_error = false;
println!(
"Compiling Neotron OS library, using target {:?}",
target.unwrap_or("native")
);
for package in packages
.iter()
.filter(|p| p.kind == nbuild::PackageKind::Os)
{
println!(
"Compiling {}, target {:?}",
package.name,
target.unwrap_or("native")
);
if let Err(e) = nbuild::cargo(&["build", "--release", "--lib"], target, package.path) {
eprintln!("Build of {} failed: {}", package.name, e);
is_error = true;
}
}
if is_error {
std::process::exit(1);
}
}
/// Runs `cargo fmt` over all the packages
fn format(packages: &[nbuild::Package], check: bool) {
let mut is_error = false;
let commands = if check {
vec!["fmt", "--check"]
} else {
vec!["fmt"]
};
for package in packages.iter() {
println!("Formatting {}", package.name);
if let Err(e) = nbuild::cargo(&commands, None, package.path) {
eprintln!("Format failed: {}", e);
is_error = true;
}
}
if is_error {
std::process::exit(1);
}
}
/// Runs `cargo clippy` over all the packages
fn clippy(packages: &[nbuild::Package]) {
let mut is_error = false;
for package in packages.iter() {
println!("Linting {} with clippy", package.name);
if let Err(e) = nbuild::cargo(&["clippy"], None, package.path) {
eprintln!("Lint failed: {}", e);
is_error = true;
}
}
if is_error {
std::process::exit(1);
}
}
/// Runs `cargo test` over all the packages
fn test(packages: &[nbuild::Package]) {
let mut is_error = false;
for package in packages
.iter()
.filter(|p| p.testable == nbuild::Testable::Libs)
{
println!("Testing {}", package.name);
if let Err(e) = nbuild::cargo(&["test", "--lib"], None, package.path) {
eprintln!("Test failed: {}", e);
is_error = true;
}
}
for package in packages
.iter()
.filter(|p| p.testable == nbuild::Testable::All)
{
println!("Testing {}", package.name);
if let Err(e) = nbuild::cargo(&["test"], None, package.path) {
eprintln!("Test failed: {}", e);
is_error = true;
}
}
if is_error {
std::process::exit(1);
}
}
// End of file