simpleos 0.3.0

A Simple OS use for MCU, that implements single-threaded asynchronous runtime.
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use anyhow::Result;
use std::fs;
use std::path::Path;
use std::process::Command;

fn main() {
    let bindings_path = "src/bindings/bindings.rs";

    let compile_config = &CompileConfig::CortexM4_HardFloat_C("gnu11");
    let mut bindings = Binding::new();

    // let stdlib = Binding::new().add_system_header("stdlib.h").clone();

    // let hal = Binding::from_makefile("../Makefile")
    //     .expect("Failed to create Binding from Makefile")
    //     .add_headers_from_dir("../Core/Inc")
    //     .clone();

    let sfud = Binding::new()
        .add_include_dir("-Isrc/driver/fs/sfud")
        // .add_include_dir_from_makefile("../Makefile", "C_INCLUDES")
        .add_headers_from_dir("src/driver/fs/sfud")
        .add_sources_from_dir("src/driver/fs/sfud")
        //.dump()
        .compile("sfud", compile_config)
        .expect("Failed to compile SFUD C code")
        .clone();
    bindings = bindings.concat(sfud);

    let littlefs = Binding::new()
        .add_include_dir("-Isrc/driver/fs/littlefs")
        // .add_include_dir_from_makefile("../Makefile", "C_INCLUDES")
        .add_headers_from_dir("src/driver/fs/littlefs")
        .add_sources_from_dir("src/driver/fs/littlefs")
        //.dump()
        .compile("littlefs", compile_config)
        .expect("Failed to compile LittleFS C code")
        .clone();
    bindings = bindings.concat(littlefs);

    bindings
        // .dump()
        .generate(bindings_path, compile_config)
        .expect("Failed to generate bindings");
}

#[allow(unused)]
#[allow(non_camel_case_types)]
enum CompileConfig {
    CortexM4_HardFloat_C(&'static str),
    CortexM4_HardFloat_CPP(&'static str),
    CortexM4_SoftFloat_C(&'static str),
    CortexM4_SoftFloat_CPP(&'static str),
}

#[derive(Debug, Clone)]
struct Binding {
    defines: Vec<String>,      // 预处理定义 "-Dxxx"
    include_dirs: Vec<String>, // 包含目录 "-Ixxx"
    headers: Vec<String>,      // 头文件列表 "xxx.h"
    sources: Vec<String>,      // 源文件列表 "xxx.c"
}

#[allow(unused)]
impl Binding {
    pub fn new() -> Self {
        Binding {
            defines: vec![],
            include_dirs: vec![],
            headers: vec![],
            sources: vec![],
        }
    }

    pub fn concat(&self, other: Binding) -> Binding {
        let mut new_binding = Binding::new();
        // 合并并且去除重复, 并保持插入顺序
        new_binding.defines = {
            let mut seen = std::collections::HashSet::new();
            let mut result = Vec::new();
            for define in self.defines.iter().chain(other.defines.iter()) {
                if seen.insert(define) {
                    result.push(define.clone());
                }
            }
            result
        };
        new_binding.include_dirs = {
            let mut seen = std::collections::HashSet::new();
            let mut result = Vec::new();
            for include_dir in self.include_dirs.iter().chain(other.include_dirs.iter()) {
                if seen.insert(include_dir) {
                    result.push(include_dir.clone());
                }
            }
            result
        };
        new_binding.headers = {
            let mut seen = std::collections::HashSet::new();
            let mut result = Vec::new();
            for header in self.headers.iter().chain(other.headers.iter()) {
                if seen.insert(header) {
                    result.push(header.clone());
                }
            }
            result
        };
        new_binding.sources = {
            let mut seen = std::collections::HashSet::new();
            let mut result = Vec::new();
            for source in self.sources.iter().chain(other.sources.iter()) {
                if seen.insert(source) {
                    result.push(source.clone());
                }
            }
            result
        };
        new_binding
    }

    pub fn dump(&self) -> &Self {
        println!("RUST_BACKTRACE=1Binding:");
        println!("RUST_BACKTRACE=1  Defines:");
        for define in &self.defines {
            println!("RUST_BACKTRACE=1    {}", define);
        }
        println!("RUST_BACKTRACE=1  Include Dirs:");
        for include_dir in &self.include_dirs {
            println!("RUST_BACKTRACE=1    {}", include_dir);
        }
        println!("RUST_BACKTRACE=1  Headers:");
        for header in &self.headers {
            println!("RUST_BACKTRACE=1    {}", header);
        }
        self
    }

    pub fn from_makefile(makefile_path: &str) -> Result<Self> {
        let top_dir = Path::new(makefile_path)
            .parent()
            .ok_or_else(|| anyhow::anyhow!("Failed to get parent directory of Makefile"))?;
        let defines = Self::get_makefile_variable(makefile_path, "C_DEFS").map_err(|err| anyhow::anyhow!("Failed to get defines: {}", err))?;
        let include_dirs =
            Self::get_makefile_variable(makefile_path, "C_INCLUDES").map_err(|err| anyhow::anyhow!("Failed to get include dirs: {}", err))?;
        let include_dirs = include_dirs
            .iter()
            .map(|dir| {
                // 去除-I前缀, 增加绝对路径
                let path = dir.trim_start_matches("-I");
                format!("-I{}", top_dir.join(path).to_str().unwrap())
            })
            .collect::<Vec<String>>();
        Ok(Binding {
            defines,
            include_dirs,
            headers: vec![],
            sources: vec![],
        })
    }

    pub fn add_header(&mut self, header: &str) -> &mut Self {
        self.headers.push(header.to_string());
        self
    }

    pub fn add_system_header(&mut self, header: &str) -> &mut Self {
        let gcc_include_paths = Self::get_gcc_include_paths().unwrap_or_default();
        for include_path in &gcc_include_paths {
            let full_path = format!("{}/{}", include_path.trim_start_matches("-I"), header);
            if Path::new(&full_path).exists() {
                self.headers.push(full_path);
                return self;
            }
        }
        panic!(
            "System header {} not found in GCC include paths: {}",
            header,
            gcc_include_paths.join("\n")
        );
    }

    pub fn add_headers(&mut self, headers: Vec<&str>) -> &mut Self {
        for header in headers {
            self.headers.push(header.to_string());
        }
        self
    }

    pub fn add_source(&mut self, source: &str) -> &mut Self {
        self.sources.push(source.to_string());
        self
    }

    pub fn add_sources(&mut self, sources: Vec<&str>) -> &mut Self {
        for source in sources {
            self.sources.push(source.to_string());
        }
        self
    }

    pub fn add_sources_from_dir(&mut self, dir_path: &str) -> &mut Self {
        let entries = fs::read_dir(dir_path)
            .map_err(|e| anyhow::anyhow!("Failed to read directory {}: {}", dir_path, e))
            .unwrap();
        for entry in entries {
            let entry = entry.map_err(|e| anyhow::anyhow!("Failed to read entry in {}: {}", dir_path, e)).unwrap();
            let path = entry.path();
            if path.is_file() {
                if let Some(ext) = path.extension() {
                    if ext == "c" {
                        if let Some(path_str) = path.to_str() {
                            self.sources.push(path_str.to_string());
                        }
                    }
                }
            }
        }
        self
    }

    pub fn add_headers_from_dir(&mut self, dir_path: &str) -> &mut Self {
        let entries = fs::read_dir(dir_path)
            .map_err(|e| anyhow::anyhow!("Failed to read directory {}: {}", dir_path, e))
            .unwrap();
        for entry in entries {
            let entry = entry.map_err(|e| anyhow::anyhow!("Failed to read entry in {}: {}", dir_path, e)).unwrap();
            let path = entry.path();
            if path.is_file() {
                if let Some(ext) = path.extension() {
                    if ext == "h" {
                        if let Some(path_str) = path.to_str() {
                            self.headers.push(path_str.to_string());
                        }
                    }
                }
            }
        }
        self
    }

    pub fn add_define(&mut self, define: &str) -> &mut Self {
        if define.starts_with("-D") == false {
            panic!("Define must start with '-D'");
        }
        self.defines.push(define.to_string());
        self
    }

    pub fn add_include_dir(&mut self, include_dir: &str) -> &mut Self {
        if include_dir.starts_with("-I") == false {
            panic!("Include dir must start with '-I'");
        }
        self.include_dirs.push(include_dir.to_string());
        self
    }

    pub fn add_include_dir_from_makefile(&mut self, makefile_path: &str, var_name: &str) -> &mut Self {
        let include_dirs = Self::get_makefile_variable(makefile_path, var_name).unwrap_or_default();
        for include_dir in include_dirs {
            self.include_dirs.push(include_dir);
        }
        self
    }

    pub fn generate(&self, output_path: &str, config: &CompileConfig) -> Result<&Self> {
        println!("cargo:rerun-if-changed={}", output_path);

        // 使用 bindgen 生成绑定
        let mut builder = bindgen::Builder::default();

        // 设置编译目标和选项
        match config {
            CompileConfig::CortexM4_HardFloat_C(std) => {
                builder = builder.clang_arg("--target=thumbv7em-none-eabihf");
                builder = builder.clang_arg("-mcpu=cortex-m4");
                builder = builder.clang_arg("-mthumb");
                builder = builder.clang_arg("-mfpu=fpv4-sp-d16");
                builder = builder.clang_arg("-mfloat-abi=hard");
                builder = builder.clang_arg("-xc");
                if !std.is_empty() {
                    builder = builder.clang_arg(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_HardFloat_CPP(std) => {
                builder = builder.clang_arg("--target=thumbv7em-none-eabihf");
                builder = builder.clang_arg("-mcpu=cortex-m4");
                builder = builder.clang_arg("-mthumb");
                builder = builder.clang_arg("-mfpu=fpv4-sp-d16");
                builder = builder.clang_arg("-mfloat-abi=hard");
                builder = builder.clang_arg("-xc++");
                if !std.is_empty() {
                    builder = builder.clang_arg(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_SoftFloat_C(std) => {
                builder = builder.clang_arg("--target=thumbv7em-none-eabihf");
                builder = builder.clang_arg("-mcpu=cortex-m4");
                builder = builder.clang_arg("-mthumb");
                // builder = builder.clang_arg("-mfpu=fpv4-sp-d16");
                builder = builder.clang_arg("-mfloat-abi=soft");
                builder = builder.clang_arg("-xc");
                if !std.is_empty() {
                    builder = builder.clang_arg(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_SoftFloat_CPP(std) => {
                builder = builder.clang_arg("--target=thumbv7em-none-eabihf");
                builder = builder.clang_arg("-mcpu=cortex-m4");
                builder = builder.clang_arg("-mthumb");
                // builder = builder.clang_arg("-mfpu=fpv4-sp-d16");
                builder = builder.clang_arg("-mfloat-abi=soft");
                builder = builder.clang_arg("-xc++");
                if !std.is_empty() {
                    builder = builder.clang_arg(format!("-std={}", std));
                }
            }
        }

        // 添加预处理定义和包含目录
        for define in &self.defines {
            builder = builder.clang_arg(define);
        }
        for include_dir in &self.include_dirs {
            builder = builder.clang_arg(include_dir);
        }

        // 添加工具链系统头文件路径
        let gcc_include_paths = Self::get_gcc_include_paths()?;
        for include_path in gcc_include_paths {
            // println!("cargo:warning=Adding GCC include path: {}", include_path);
            builder = builder.clang_arg(include_path);
        }

        // 添加头文件
        builder = builder.headers(self.headers.clone());

        // 将头文件列表写入临时文件
        let header_list_path = output_path.to_string() + ".headers";
        fs::write(&header_list_path, self.headers.join("\n")).expect("Failed to write header list file");
        // 将包含文件列表写入到临时文件
        let include_dir_list_path = output_path.to_string() + ".includes";
        fs::write(&include_dir_list_path, self.include_dirs.join("\n")).expect("Failed to write include dir list file");
        // 将预处理定义列表写入到临时文件
        let define_list_path = output_path.to_string() + ".defines";
        fs::write(&define_list_path, self.defines.join("\n")).expect("Failed to write define list file");

        // 配置生成选项
        let bindings = builder
            .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) // 解析回调
            .allowlist_recursively(true) // 递归允许所有类型和函数
            .generate_comments(true) // 生成注释
            // .blocklist_type("__BindgenBitfieldUnit") // 排除特定类型
            .use_core() // 使用 core 库
            .derive_default(true) // 允许默认派生
            .derive_debug(true) // 允许调试派生
            .size_t_is_usize(true) // 将 size_t 映射为 usize
            .raw_line("#![allow(non_upper_case_globals)]") // 允许非大写全局变量
            .raw_line("#![allow(non_camel_case_types)]") // 允许非驼峰类型
            .raw_line("#![allow(non_snake_case)]") // 允许非蛇形命名
            .generate()
            .expect("Unable to generate bindings");

        // 写入生成的绑定
        bindings.write_to_file(output_path).expect("Couldn't write bindings!");

        Ok(self)
    }

    pub fn compile(&self, output_lib: &str, config: &CompileConfig) -> Result<&Self> {
        let mut builder = cc::Build::new();

        // 设置编译目标和选项
        match config {
            CompileConfig::CortexM4_HardFloat_C(std) => {
                let mut builder = &mut builder;
                builder = builder.compiler("arm-none-eabi-gcc");
                builder = builder.target("thumbv7em-none-eabihf");
                builder = builder.flag("-mcpu=cortex-m4");
                builder = builder.flag("-mthumb");
                builder = builder.flag("-mfloat-abi=hard");
                builder = builder.flag("-mfpu=fpv4-sp-d16");
                if !std.is_empty() {
                    builder = builder.flag(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_HardFloat_CPP(std) => {
                let mut builder = &mut builder;
                builder = builder.compiler("arm-none-eabi-g++");
                builder = builder.target("thumbv7em-none-eabihf");
                builder = builder.flag("-mcpu=cortex-m4");
                builder = builder.flag("-mthumb");
                builder = builder.flag("-mfloat-abi=hard");
                builder = builder.flag("-mfpu=fpv4-sp-d16");
                if !std.is_empty() {
                    builder = builder.flag(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_SoftFloat_C(std) => {
                let mut builder = &mut builder;
                builder = builder.compiler("arm-none-eabi-gcc");
                builder = builder.target("thumbv7em-none-eabihf");
                builder = builder.flag("-mcpu=cortex-m4");
                builder = builder.flag("-mthumb");
                builder = builder.flag("-mfloat-abi=soft");
                if !std.is_empty() {
                    builder = builder.flag(format!("-std={}", std));
                }
            }
            CompileConfig::CortexM4_SoftFloat_CPP(std) => {
                let mut builder = &mut builder;
                builder = builder.compiler("arm-none-eabi-g++");
                builder = builder.target("thumbv7em-none-eabihf");
                builder = builder.flag("-mcpu=cortex-m4");
                builder = builder.flag("-mthumb");
                builder = builder.flag("-mfloat-abi=soft");
                if !std.is_empty() {
                    builder = builder.flag(format!("-std={}", std));
                }
            }
        }

        // 添加定义
        for define in &self.defines {
            let def = define.trim_start_matches("-D");
            if let Some(pos) = def.find('=') {
                builder.define(&def[..pos], Some(&def[pos + 1..]));
            } else {
                builder.define(def, None);
            }
        }

        // 添加包含路径
        for include in &self.include_dirs {
            let path = include.trim_start_matches("-I");
            builder.include(path);
            println!("cargo:rerun-if-changed={}", path);
        }

        // 添加 C 文件
        for source in &self.sources {
            builder.file(source);
            eprintln!("  Compiling: {}", source);
            println!("cargo:rerun-if-changed={}", source);
        }

        builder.compile(output_lib);

        Ok(self)
    }

    /// 获取 arm-none-eabi-gcc 的系统头文件路径
    fn get_gcc_include_paths() -> Result<Vec<String>> {
        let output = Command::new("arm-none-eabi-gcc").args(&["-E", "-Wp,-v", "-xc", "/dev/null"]).output()?;

        let stderr = String::from_utf8_lossy(&output.stderr);
        let mut paths = Vec::new();
        let mut in_include_section = false;

        for line in stderr.lines() {
            if line.contains("#include <...> search starts here:") {
                in_include_section = true;
                continue;
            }
            if line.contains("End of search list.") {
                break;
            }
            if in_include_section {
                let path = line.trim();
                if !path.is_empty() {
                    paths.push(format!("-I{}", path));
                }
            }
        }

        Ok(paths)
    }

    /// 从 Makefile 中获取指定变量的值
    /// 支持多行续行 (\) 和变量追加 (+=) 操作
    ///
    /// # 参数
    /// * `makefile_path` - Makefile 文件路径
    /// * `var_name` - 要获取的变量名
    ///
    /// # 返回
    /// * `Ok(Vec<String>)` - 变量的所有值(按空格分割)
    /// * `Err` - 读取文件失败或其他错误
    ///
    /// # 示例
    /// ```rust
    /// let c_defs = get_makefile_variable("Makefile", "C_DEFS")?;
    /// let c_includes = get_makefile_variable("Makefile", "C_INCLUDES")?;
    /// ```
    fn get_makefile_variable(makefile_path: &str, var_name: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let content = fs::read_to_string(makefile_path)?;
        let lines: Vec<&str> = content.lines().collect();

        let mut result = Vec::new();
        let mut i = 0;

        while i < lines.len() {
            let line = lines[i].trim();

            // 跳过注释和空行
            if line.starts_with('#') || line.is_empty() {
                i += 1;
                continue;
            }

            // 检查是否是目标变量的赋值
            if let Some((found_var, operator, initial_value)) = Self::parse_variable_assignment(line) {
                if found_var == var_name {
                    // 处理初始值
                    if !initial_value.is_empty() {
                        Self::extract_values(&initial_value, &mut result);
                    }

                    // 处理多行续行
                    if line.ends_with('\\') {
                        i += 1;
                        while i < lines.len() {
                            let continuation_line = lines[i].trim();

                            let content = if continuation_line.ends_with('\\') {
                                &continuation_line[..continuation_line.len() - 1].trim()
                            } else {
                                continuation_line
                            };

                            if !content.is_empty() {
                                Self::extract_values(content, &mut result);
                            }

                            if !continuation_line.ends_with('\\') {
                                break;
                            }
                            i += 1;
                        }
                    }

                    // 如果是 = 操作,直接返回结果
                    // 如果是 +=操作,继续查找后续的追加赋值
                    if operator == "=" {
                        break;
                    }
                }
            }

            i += 1;
        }

        Ok(result)
    }

    /// 解析变量赋值行,返回 (变量名, 操作符, 初始值)
    fn parse_variable_assignment(line: &str) -> Option<(String, String, String)> {
        if let Some(eq_pos) = line.find('=') {
            let before_eq = &line[..eq_pos].trim();
            let after_eq = &line[eq_pos + 1..].trim();

            // 检查是否是 += 操作
            let (var_name, operator) = if before_eq.ends_with('+') {
                (&before_eq[..before_eq.len() - 1].trim(), "+=")
            } else {
                (before_eq, "=")
            };

            // 验证变量名是否有效(只包含字母、数字、下划线)
            if var_name.chars().all(|c| c.is_alphanumeric() || c == '_') {
                let initial_value = if after_eq.ends_with('\\') {
                    &after_eq[..after_eq.len() - 1].trim()
                } else {
                    after_eq
                };

                return Some((var_name.to_string(), operator.to_string(), initial_value.to_string()));
            }
        }

        None
    }

    /// 从字符串中提取值(按空格分割)
    fn extract_values(content: &str, values: &mut Vec<String>) {
        let items: Vec<String> = content.split_whitespace().filter(|s| !s.is_empty()).map(|s| s.to_string()).collect();

        values.extend(items);
    }
}