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
/***************************************************************************
*
* osal-rs
* Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
//! Build script for OSAL-RS library.
//!
//! This build script runs at compile time to perform several critical setup tasks:
//!
//! # Purpose
//!
//! 1. **Generate FreeRTOS type mappings**: Creates Rust bindings for FreeRTOS C types
//! 2. **Extract configuration constants**: Reads `FreeRTOSConfig.h` and exposes constants to Rust
//! 3. **Set up rebuild triggers**: Ensures the library rebuilds when FFI code changes
//!
//! # What It Generates
//!
//! The script generates Rust source files containing:
//! - Type aliases for FreeRTOS types (e.g., `TickType`, `BaseType`)
//! - Configuration constants (e.g., `configTICK_RATE_HZ`, `configMAX_PRIORITIES`)
//! - Platform-specific type sizes and layouts
//!
//! These generated files are included by the `osal-rs` crate at compile time via
//! `include!` macros.
//!
//! # Configuration
//!
//! ## FreeRTOSConfig.h Location
//!
//! The script searches for `FreeRTOSConfig.h` in the following order:
//!
//! 1. **Environment variable**: `FREERTOS_CONFIG_PATH` (if set)
//! 2. **Default location**: `<workspace_root>/inc/FreeRTOSConfig.h`
//!
//! ### Setting Custom Config Path
//!
//! To use a custom configuration file location, set the environment variable:
//!
//! ```bash
//! export FREERTOS_CONFIG_PATH=/path/to/FreeRTOSConfig.h
//! cargo build
//! ```
//!
//! Or in `.cargo/config.toml`:
//!
//! ```toml
//! [env]
//! FREERTOS_CONFIG_PATH = { value = "/path/to/FreeRTOSConfig.h" }
//! ```
//!
//! # Rebuild Triggers
//!
//! The library will rebuild if any of these files change:
//! - `build.rs` (this file)
//! - `osal_rs_ffi_freertos.c` (FFI implementation)
//! - `osal_rs_ffi_freertos.h` (FFI header)
//!
//! # Build Dependencies
//!
//! Requires:
//! - `osal-rs-build` crate (provides `FreeRtosTypeGenerator`)
//! - Valid `FreeRTOSConfig.h` file
//! - C compiler for parsing FreeRTOS headers
//!
//! # Troubleshooting
//!
//! If the build fails:
//! 1. Verify `FreeRTOSConfig.h` exists at the expected location
//! 2. Check that `FREERTOS_CONFIG_PATH` is correct (if set)
//! 3. Ensure FreeRTOS headers are accessible
//! 4. Check build output for specific error messages
//!
//! # See Also
//!
//! - `osal-rs-build` crate for the type generation implementation
//! - `FreeRTOSConfig.h` for FreeRTOS configuration options
use FreeRtosTypeGenerator;
use env;
use PathBuf;
/// Main entry point for the build script.
///
/// This function performs the following tasks in order:
///
/// 1. **Sets rebuild triggers**: Configures cargo to rebuild when FFI files change
/// 2. **Locates workspace root**: Determines the workspace root directory
/// 3. **Finds FreeRTOSConfig.h**: Searches for the configuration file
/// 4. **Generates type bindings**: Creates Rust type mappings from FreeRTOS C types
///
/// # Rebuild Triggers
///
/// The script tells cargo to rebuild if these files change:
/// - `build.rs` - This build script itself
/// - `osal_rs_ffi_freertos.c` - FFI implementation in C
/// - `osal_rs_ffi_freertos.h` - FFI header declarations
///
/// # FreeRTOSConfig.h Discovery
///
/// The configuration file is located using this precedence:
/// 1. Environment variable `FREERTOS_CONFIG_PATH` (if set)
/// 2. Default path: `<workspace_root>/inc/FreeRTOSConfig.h`
///
/// # Generation Process
///
/// Uses `FreeRtosTypeGenerator` to:
/// - Parse FreeRTOSConfig.h for configuration constants
/// - Generate Rust type aliases for FreeRTOS types
/// - Create platform-specific type definitions
/// - Write generated code to source files in the build output directory
///
/// # Panics
///
/// - If `CARGO_MANIFEST_DIR` environment variable is not set (cargo always sets this)
/// - If workspace root cannot be determined from manifest path
/// - If FreeRTOSConfig.h cannot be found or parsed (handled by `FreeRtosTypeGenerator`)
///
/// # Environment Variables
///
/// - `CARGO_MANIFEST_DIR` - Set by cargo, points to the crate's directory
/// - `FREERTOS_CONFIG_PATH` - Optional, custom path to FreeRTOSConfig.h