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
//! Build-time helpers for compiling ISLE DSL files to Rust code.
//!
//! This crate provides utilities for [Cargo build scripts] to compile
//! [ISLE] (Instruction Selection Lowering Expressions) domain-specific language
//! files into Rust code. It uses the [`cranelift-isle`] compiler internally.
//!
//! [ISLE]: https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/isle/docs/language-reference.md
//! [Cargo build scripts]: https://doc.rust-lang.org/cargo/reference/build-scripts.html
//! [`cranelift-isle`]: https://docs.rs/cranelift-isle/
//!
//! # Example
//!
//! In your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! intarsia = "0.1"
//!
//! [build-dependencies]
//! intarsia-build = "*"
//! ```
//!
//! In your `build.rs`:
//! ```no_run
//! fn main() {
//! intarsia_build::compile_isle_auto().unwrap();
//! }
//! ```
use Error;
use fs;
use ;
/// Automatically discover and compile ISLE files in a conventional location.
///
/// This function looks for an `isle/` directory in the current directory
/// and compiles all `.isle` files found there. The generated Rust code
/// is written to the same `isle/` directory with a `.rs` extension.
///
/// This is a convenience wrapper around [`compile_isle_dir`] with a fixed path.
///
/// # Directory Structure
///
/// ```text
/// your_project/
/// ├── build.rs (calls this function)
/// ├── Cargo.toml
/// └── src/
/// ├── main.rs
/// └── isle/
/// ├── rules.isle (your ISLE file)
/// └── rules.rs (generated - git ignore this)
/// ```
///
/// # Example
///
/// ```no_run
/// // build.rs
/// fn main() {
/// intarsia::build::compile_isle_auto().unwrap();
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The `isle/` directory doesn't exist
/// - No `.isle` files are found
/// - ISLE compilation fails (see [`cranelift_isle::error::Errors`])
/// - File I/O fails (see [`std::io::Error`])
///
/// [`cranelift_isle::error::Errors`]: https://docs.rs/cranelift-isle/latest/cranelift_isle/error/struct.Errors.html
/// Compile all ISLE files in a specified directory.
///
/// This function finds all `.isle` files in the given directory,
/// compiles them using the ISLE compiler from [`cranelift-isle`], and writes
/// the generated Rust code to `.rs` files in the same directory.
///
/// [`cranelift-isle`]: https://docs.rs/cranelift-isle/
///
/// # Arguments
///
/// * `isle_dir` - Path to directory containing `.isle` files (relative to current dir)
///
/// # Generated Files
///
/// For each `name.isle` file, generates `name.rs` in the same directory.
/// The generated files should be added to `.gitignore`.
///
/// # Example
///
/// ```no_run
/// // build.rs
/// fn main() {
/// // Compile ISLE files in examples/optimizer/isle/
/// intarsia_build::compile_isle_dir("examples/optimizer/isle").unwrap();
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The directory doesn't exist
/// - No `.isle` files are found
/// - ISLE compilation fails (see [`cranelift_isle::error::Errors`])
/// - File I/O fails (see [`std::io::Error`])
///
/// [`cranelift_isle::error::Errors`]: https://docs.rs/cranelift-isle/latest/cranelift_isle/error/struct.Errors.html
/// Compile a single ISLE file to Rust code.
///
/// This is a lower-level function that compiles a single ISLE file using
/// [`cranelift_isle::compile::from_files`]. The generated Rust code is written
/// to a `.rs` file in the same directory as the input file.
///
/// [`cranelift_isle::compile::from_files`]: https://docs.rs/cranelift-isle/latest/cranelift_isle/compile/fn.from_files.html
///
/// # Arguments
///
/// * `isle_file` - Path to the `.isle` file to compile
///
/// # Example
///
/// ```no_run
/// // build.rs
/// fn main() {
/// intarsia_build::compile_isle_file("isle/rules.isle").unwrap();
/// intarsia_build::compile_isle_file("isle/custom.isle").unwrap();
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The file doesn't exist
/// - ISLE compilation fails (see [`cranelift_isle::error::Errors`])
/// - File I/O fails (see [`std::io::Error`])
///
/// [`cranelift_isle::error::Errors`]: https://docs.rs/cranelift-isle/latest/cranelift_isle/error/struct.Errors.html
/// Compile multiple specific ISLE files.
///
/// This is a convenience function for compiling a list of ISLE files.
/// It calls [`compile_isle_file`] for each file in the provided slice.
///
/// # Example
///
/// ```no_run
/// // build.rs
/// fn main() {
/// intarsia_build::compile_isle_files(&[
/// "isle/rules.isle",
/// "isle/cost.isle",
/// ]).unwrap();
/// }
/// ```