buildl-core 0.3.0

Domain data, ports, and pure pipeline logic for the buildl build system — no I/O, no Lua.
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
//! Expansion: a declared command's, and a rule's description's, placeholders replaced by what
//! they stand for.
//!
//! A build file never holds a real path: a command is written with `$in`, `$out`, `$deps` and
//! `$opt:name`, and what an action is handed is the command with each of them replaced. The
//! replacement is workspace-relative text, the same wherever the action runs, so it is known
//! before anything does. A description is expanded by the same rules, so
//! that the line shown for an action names what the action was handed.
//!
//! Responsibilities: [`Lists`], [`expand`] and [`expand_description`].
//!
//! Non-responsibilities: whether the expanded command names a program that may run; and where a
//! reference to a setting starts and ends, which is read here through the scanner
//! [`Argument::setting_references`] and [`Description::setting_references`] list their names
//! with.

use std::collections::BTreeMap;

use crate::types::setting_reference;
use crate::types::{
    Command, Description, ExpandedDescription, OutputPath, SettingName, SettingValue, SourcePath,
};

/// The three lists of paths one target's placeholders stand for.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Lists<'a> {
    /// What `$in` stands for: the target's inputs, in the order they were written.
    pub(crate) inputs: &'a [SourcePath],
    /// What `$out` stands for: where the target's own outputs live, in the order they were
    /// written.
    pub(crate) outputs: &'a [OutputPath],
    /// What `$deps` stands for: where every output of every dependency lives.
    pub(crate) deps: &'a [OutputPath],
}

impl<'a> Lists<'a> {
    /// The list a placeholder's word names: `in`, `out` or `deps`, and nothing else.
    fn named(&self, word: &str) -> Option<Vec<&'a str>> {
        match word {
            "in" => Some(self.inputs.iter().map(SourcePath::as_str).collect()),
            "out" => Some(self.outputs.iter().map(OutputPath::as_str).collect()),
            "deps" => Some(self.deps.iter().map(OutputPath::as_str).collect()),
            _ => None,
        }
    }
}

/// `command` with its placeholders replaced, as the texts of its arguments.
///
/// Each argument is read once, left to right. At a `$`:
///
/// - `$opt:` and a setting's name becomes the value `settings` holds for it, and stays as it is
///   when `settings` holds none;
/// - `$` and a run of ASCII letters that is exactly `in`, `out` or `deps` becomes that list of
///   `lists`. The run is the longest one, so `$input` is text and `$out/app` is the outputs
///   followed by `/app`;
/// - anything else is text.
///
/// An argument that is one list and nothing else becomes one argument per path, and none when
/// the list is empty. Anywhere else a list becomes its paths joined by one space. Text that was
/// substituted is never read again, so a setting whose value is `$in` yields `$in`.
///
/// The result may hold no argument at all, and its first may not name a program: both are for
/// the caller to refuse.
pub(crate) fn expand(
    command: &Command,
    settings: &BTreeMap<SettingName, SettingValue>,
    lists: &Lists<'_>,
) -> Vec<String> {
    let mut expanded = Vec::new();
    for argument in command.arguments() {
        let alone = argument
            .as_str()
            .strip_prefix('$')
            .and_then(|word| lists.named(word));
        match alone {
            Some(paths) => expanded.extend(paths.into_iter().map(str::to_owned)),
            None => expanded.push(within(argument.as_str(), settings, lists)),
        }
    }
    expanded
}

/// `description` with its placeholders replaced, as the line to show for an action.
///
/// Placeholders are read as [`expand`] reads them within one argument: `$opt:` and a setting's
/// name becomes the value `settings` holds, and stays as it is when `settings` holds none, and
/// `$in`, `$out` and `$deps` become their list of `lists`. A list is always its paths joined by
/// one space, since a description is one line and has no arguments to split a list across. Text
/// that was substituted is never read again.
pub(crate) fn expand_description(
    description: &Description,
    settings: &BTreeMap<SettingName, SettingValue>,
    lists: &Lists<'_>,
) -> ExpandedDescription {
    ExpandedDescription::new(&within(description.as_str(), settings, lists))
}

/// One text with every placeholder inside it replaced.
///
/// The text is read `$` by `$`. One that starts a reference to a setting is replaced by the
/// setting's value, or left as written when `settings` holds none; one followed by the word of a
/// list is replaced by that list; any other stays a `$`.
fn within(text: &str, settings: &BTreeMap<SettingName, SettingValue>, lists: &Lists<'_>) -> String {
    let mut expanded = String::new();
    let mut rest = text;
    while let Some(at) = rest.find('$') {
        let (before, from) = rest.split_at(at);
        expanded.push_str(before);
        rest = if let Some((name, tail)) = setting_reference::leading(from) {
            match value(settings, name) {
                Some(value) => expanded.push_str(value.as_str()),
                None => expanded.extend([setting_reference::MARKER, name]),
            }
            tail
        } else {
            let after = from.strip_prefix('$').unwrap_or(from);
            let tail = after.trim_start_matches(|letter: char| letter.is_ascii_alphabetic());
            let word = after.strip_suffix(tail).unwrap_or_default();
            if let Some(paths) = lists.named(word) {
                expanded.push_str(&paths.join(" "));
                tail
            } else {
                expanded.push('$');
                after
            }
        };
    }
    expanded.push_str(rest);
    expanded
}

/// The value `settings` holds for the setting written as `name`.
fn value<'s>(
    settings: &'s BTreeMap<SettingName, SettingValue>,
    name: &str,
) -> Option<&'s SettingValue> {
    SettingName::parse(name)
        .ok()
        .and_then(|name| settings.get(&name))
}

#[cfg(test)]
mod tests {
    #![expect(
        clippy::unwrap_used,
        reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
    )]

    use std::collections::BTreeMap;

    use super::{Lists, expand, expand_description};
    use crate::placement::fixtures::{command, output_path, path, settings};
    use crate::types::{Description, OutputPath, SettingName, SettingValue, SourcePath};

    /// `declared`, expanded with no setting and these three lists.
    fn expanded(
        declared: &[&str],
        inputs: &[SourcePath],
        outputs: &[OutputPath],
        deps: &[OutputPath],
    ) -> Vec<String> {
        with_settings(declared, &BTreeMap::new(), inputs, outputs, deps)
    }

    fn with_settings(
        declared: &[&str],
        settings: &BTreeMap<SettingName, SettingValue>,
        inputs: &[SourcePath],
        outputs: &[OutputPath],
        deps: &[OutputPath],
    ) -> Vec<String> {
        let lists = Lists {
            inputs,
            outputs,
            deps,
        };
        expand(&command(declared), settings, &lists)
    }

    #[test]
    fn a_compile_command_names_its_input_and_its_output() {
        assert_eq!(
            expanded(
                &["cc", "-c", "$in", "-o", "$out"],
                &[path("lib/a.c")],
                &[output_path("//lib:text", "text")],
                &[],
            ),
            ["cc", "-c", "lib/a.c", "-o", "out/lib/text"]
        );
    }

    #[test]
    fn a_link_command_names_every_output_of_its_dependencies() {
        assert_eq!(
            expanded(
                &["cc", "$deps", "-o", "$out"],
                &[],
                &[output_path("//:app", "app")],
                &[
                    output_path("//:main.o", "main.o"),
                    output_path("//lib:text", "text")
                ],
            ),
            ["cc", "out/main.o", "out/lib/text", "-o", "out/app"]
        );
    }

    #[test]
    fn a_test_command_runs_what_its_dependency_produced_with_a_setting() {
        assert_eq!(
            with_settings(
                &["$deps", "--filter=$opt:test_filter"],
                &settings(&[("test_filter", "Login")]),
                &[],
                &[],
                &[output_path("//:app", "app")],
            ),
            ["out/app", "--filter=Login"]
        );
    }

    #[test]
    fn an_empty_list_alone_leaves_no_argument() {
        assert_eq!(expanded(&["gen", "$in"], &[], &[], &[]), ["gen"]);
        assert_eq!(
            expanded(&["$deps"], &[], &[], &[]),
            Vec::<String>::new(),
            "a command may expand to nothing"
        );
    }

    #[test]
    fn a_list_alone_becomes_one_argument_per_path() {
        assert_eq!(
            expanded(
                &["cc", "$in"],
                &[path("src/b.c"), path("src/a.c")],
                &[],
                &[]
            ),
            ["cc", "src/b.c", "src/a.c"]
        );
    }

    #[test]
    fn a_list_inside_other_text_is_joined_by_one_space() {
        let inputs = [path("src/b.c"), path("src/a.c")];
        assert_eq!(
            expanded(&["--srcs=$in", "--none=$deps"], &inputs, &[], &[]),
            ["--srcs=src/b.c src/a.c", "--none="]
        );
    }

    #[test]
    fn two_placeholders_in_one_argument_are_both_expanded() {
        assert_eq!(
            expanded(
                &["$in:$out", "$out/app", "$in$deps"],
                &[path("a.c")],
                &[output_path("//:app", "bin")],
                &[output_path("//:lib", "lib")],
            ),
            ["a.c:out/bin", "out/bin/app", "a.cout/lib"]
        );
    }

    #[test]
    fn a_placeholder_is_the_longest_run_of_letters_after_the_dollar() {
        assert_eq!(
            expanded(
                &["$input", "$outs", "$dep", "$IN", "$in2", "$out.map", "$$in"],
                &[path("a.c")],
                &[output_path("//:app", "app")],
                &[],
            ),
            [
                "$input",
                "$outs",
                "$dep",
                "$IN",
                "a.c2",
                "out/app.map",
                "$a.c"
            ]
        );
    }

    #[test]
    fn a_dollar_that_starts_no_placeholder_stays_text() {
        assert_eq!(
            with_settings(
                &["$", "a$", "$1", "$opt:", "$opt:/x", "$opt", "é$é"],
                &settings(&[("x", "X")]),
                &[],
                &[],
                &[],
            ),
            ["$", "a$", "$1", "$opt:", "$opt:/x", "$opt", "é$é"]
        );
    }

    #[test]
    fn a_setting_reference_becomes_the_settings_value() {
        assert_eq!(
            with_settings(
                &["$opt:mode", "-O$opt:level-$opt:mode", "$opt:empty"],
                &settings(&[("mode", "debug"), ("level", "2"), ("empty", "")]),
                &[],
                &[],
                &[],
            ),
            ["debug", "-O$opt:level-debug", ""],
            "a name runs through `-`, so `level-` is one name, and it is not a setting"
        );
    }

    #[test]
    fn a_reference_to_a_setting_that_is_not_held_stays_text() {
        assert_eq!(
            with_settings(
                &["$opt:missing", "--a=$opt:missing,$opt:mode"],
                &settings(&[("mode", "debug")]),
                &[],
                &[],
                &[],
            ),
            ["$opt:missing", "--a=$opt:missing,debug"]
        );
    }

    #[test]
    fn substituted_text_is_never_expanded_again() {
        assert_eq!(
            with_settings(
                &["$opt:flag", "--x=$opt:flag", "$opt:nested"],
                &settings(&[("flag", "$in"), ("nested", "$opt:flag")]),
                &[path("a.c")],
                &[],
                &[],
            ),
            ["$in", "--x=$in", "$opt:flag"]
        );
    }

    /// `declared` as a description, expanded with these settings and lists.
    fn described(
        declared: &str,
        settings: &BTreeMap<SettingName, SettingValue>,
        inputs: &[SourcePath],
        outputs: &[OutputPath],
        deps: &[OutputPath],
    ) -> String {
        let lists = Lists {
            inputs,
            outputs,
            deps,
        };
        let description = Description::parse(declared).unwrap();
        expand_description(&description, settings, &lists)
            .as_str()
            .to_owned()
    }

    #[test]
    fn a_description_names_what_the_command_was_handed() {
        let inputs = [path("lib/a.c"), path("lib/b.c")];
        let outputs = [output_path("//lib:text", "text")];
        let deps = [
            output_path("//:gen", "gen.h"),
            output_path("//:gen", "gen.c"),
        ];
        assert_eq!(
            described("compile $in", &BTreeMap::new(), &inputs, &outputs, &deps),
            "compile lib/a.c lib/b.c",
            "a list alone is still one text, its paths joined by one space"
        );
        assert_eq!(
            described(
                "$in -> $out after $deps",
                &BTreeMap::new(),
                &inputs,
                &outputs,
                &deps
            ),
            "lib/a.c lib/b.c -> out/lib/text after out/gen.h out/gen.c"
        );
        assert_eq!(
            described("none: $in|$out|$deps", &BTreeMap::new(), &[], &[], &[]),
            "none: ||"
        );
    }

    #[test]
    fn a_description_follows_the_rules_of_a_command_for_everything_else() {
        let held = settings(&[("mode", "debug"), ("flag", "$in")]);
        assert_eq!(
            described(
                "$input $in2 $opt:mode-$opt:mode $opt:flag $opt:missing $ $opt:",
                &held,
                &[path("a.c")],
                &[],
                &[],
            ),
            "$input a.c2 $opt:mode-debug $in $opt:missing $ $opt:",
            "the longest run decides a placeholder, a substituted text is not read again, and an \
             unknown setting stays text"
        );
    }

    #[test]
    fn a_description_is_expanded_where_a_command_would_be_with_the_same_text() {
        let held = settings(&[("level", "2")]);
        let inputs = [path("a.c"), path("b.c")];
        let outputs = [output_path("//:app", "app")];
        let text = "cc -O$opt:level $in -o $out";
        let as_command = expand(
            &command(&[text]),
            &held,
            &Lists {
                inputs: &inputs,
                outputs: &outputs,
                deps: &[],
            },
        );
        assert_eq!(
            [described(text, &held, &inputs, &outputs, &[])],
            as_command.as_slice(),
            "the same text, inside other text, expands the same way in both"
        );
    }

    #[test]
    fn a_control_character_in_a_substituted_value_does_not_break_the_line() {
        assert_eq!(
            described(
                "mode $opt:mode.",
                &settings(&[("mode", "a\nb")]),
                &[],
                &[],
                &[]
            ),
            "mode a b."
        );
    }

    #[test]
    fn a_setting_named_like_a_list_is_still_a_setting() {
        assert_eq!(
            with_settings(
                &["$opt:in", "$opt:out$in"],
                &settings(&[("in", "I"), ("out", "O")]),
                &[path("a.c")],
                &[],
                &[],
            ),
            ["I", "Oa.c"]
        );
    }
}