ninja-writer 0.3.1

Simple and ergonomic library for writing Ninja build files
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
//! Implementation of the `rule` keyword

use alloc::boxed::Box;
use alloc::string::String;
use core::fmt::{Display, Formatter, Result};
use core::ops::Deref;

use crate::stmt::{Stmt, StmtRef};
use crate::util::{AddOnlyVec, Indented, RefCounted};
use crate::{Build, BuildRef, Ninja, Pool, ToArg, Variable, Variables};

/// A rule, as defined by the `rule` keyword
///
/// See <https://ninja-build.org/manual.html#_rules>
///
/// # Creating a rule
/// The most common way to create a rule is with the [`Ninja::rule`](crate::Ninja::rule) method,
/// which creates the rule, adds it to the ninja file, then returns a reference for configuration
/// ```rust
/// use ninja_writer::*;
///
/// let ninja = Ninja::new();
/// let rule = ninja.rule("cc", "gcc -MD -MF $out.d -c $in -o $out")
///     .depfile("$out.d")
///     .deps_gcc()
///     .description("Compiling $out");
/// rule.build(["foo.o"]).with(["foo.c"]);
///
/// assert_eq!(ninja.to_string(), r###"
/// rule cc
///   command = gcc -MD -MF $out.d -c $in -o $out
///   depfile = $out.d
///   deps = gcc
///   description = Compiling $out
///
/// build foo.o: cc foo.c
/// "###);
/// ```
///
/// # Using owned `Rule` type
/// If you prefer, you can create a rule with the [`Rule::new`](crate::Rule::new) method,
/// then add it to the ninja file with [`Rule::add_to`](crate::Rule::add_to)
/// to starting adding build edges. This may be useful if you want to configure a rule
/// in a function, since you don't need to deal with explicit lifetimes.
/// ```rust
/// use ninja_writer::*;
///
/// fn make_rule() -> Rule {
///     Rule::new("cc", "gcc -MD -MF $out.d -c $in -o $out")
///         .depfile("$out.d")
///         .deps_gcc()
///         .description("Compiling $out")
/// }
///
/// # fn main() {
/// // in the main function ...
/// let ninja = Ninja::new();
/// // `add_to` returns a reference, similar to `ninja.rule()`
/// let rule = make_rule().add_to(&ninja);
/// rule.build(["foo.o"]).with(["foo.c"]);
/// assert_eq!(ninja.to_string(), r###"
/// rule cc
///   command = gcc -MD -MF $out.d -c $in -o $out
///   depfile = $out.d
///   deps = gcc
///   description = Compiling $out
///
/// build foo.o: cc foo.c
/// "###);
/// # }
///
/// ```
///
/// # Thread safety
/// Enable the `thread-safe` flag to make the API thread-safe.
/// Here is an example of adding build edges to the same rule from
/// multiple threads. Note that the example will not compile without the `thread-safe` feature.
/// ```rust
/// # #[cfg(feature = "thread-safe")]
/// # {
/// use ninja_writer::*;
///
/// let ninja = Ninja::new();
///
/// // The type of `rule` below is `RuleRef`
/// // which implement `clone` to clone the underlying ref-counted pointer
/// let rule = ninja.rule("cc", "gcc -c $in -o $out");
/// let rule2 = rule.clone();
///
/// assert_eq!(ninja.to_string(), r###"
/// rule cc
///   command = gcc -c $in -o $out
/// "###);
/// assert_eq!(ninja.stmts.inner().len(), 1);
///
/// let t1 = std::thread::spawn(move || {
///     for _ in 0..100 {
///         rule.build(["foo1"]).with(["bar1"]);
///     }
/// });
///
/// let t2 = std::thread::spawn(move || {
///     for _ in 0..100 {
///         rule2.build(["foo2"]).with(["bar2"]);
///     }
/// });
///
/// t1.join().unwrap();
/// t2.join().unwrap();
/// assert_eq!(ninja.stmts.inner().len(), 201);
/// # }
/// ```
///
#[derive(Debug)]
pub struct Rule {
    /// The rule name as in `rule <name>`
    ///
    /// This is ref-counted so that it can be copied not-too-costly to build edges.
    pub name: RefCounted<String>,

    /// The list of variables, as an indented block
    ///
    /// See <https://ninja-build.org/manual.html#ref_rule>
    pub variables: AddOnlyVec<Variable>,
}

/// Trait for implementing variables for `rule` and `build`
pub trait RuleVariables: Variables {
    /// Set the depfile for this `rule` or `build` to explicitly support C/C++ header
    /// dependencies
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("cc", "gcc -c $in -o $out")
    ///     .depfile("$out.d");
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule cc
    ///   command = gcc -c $in -o $out
    ///   depfile = $out.d
    /// "###);
    /// ```
    #[inline]
    fn depfile(self, depfile: impl ToArg) -> Self {
        self.variable("depfile", depfile)
    }

    /// Set `deps = gcc` for this `rule` or `build`
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("cc", "gcc -c $in -o $out")
    ///     .deps_gcc();
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule cc
    ///   command = gcc -c $in -o $out
    ///   deps = gcc
    /// "###);
    /// ```
    #[inline]
    fn deps_gcc(self) -> Self {
        self.variable("deps", "gcc")
    }

    /// Set `deps = msvc` for this rule (or build) and the `msvc_deps_prefix` variable
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("cl", "cl /c $in /Fo$out")
    ///     .deps_msvc_prefix("Note: including file: ");
    ///
    /// assert_eq!(ninja.to_string(), format!(r###"
    /// rule cl
    ///   command = cl /c $in /Fo$out
    ///   deps = msvc
    ///   msvc_deps_prefix = Note: including file: {}"###, "\n"));
    /// ```
    fn deps_msvc_prefix(self, msvc_deps_prefix: impl ToArg) -> Self {
        self.deps_msvc()
            .variable("msvc_deps_prefix", msvc_deps_prefix)
    }

    /// Set `deps = msvc` for this `rule` or `build` without `msvc_deps_prefix`
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("cl", "cl /c $in /Fo$out")
    ///     .deps_msvc();
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule cl
    ///   command = cl /c $in /Fo$out
    ///   deps = msvc
    /// "###);
    /// ```
    #[inline]
    fn deps_msvc(self) -> Self {
        self.variable("deps", "msvc")
    }

    /// Set the description of the rule to be printed during the build
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("cc", "gcc -c $in -o $out")
    ///    .description("Compiling $out");
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule cc
    ///   command = gcc -c $in -o $out
    ///   description = Compiling $out
    /// "###);
    /// ```
    #[inline]
    fn description(self, desc: impl ToArg) -> Self {
        self.variable("description", desc)
    }

    /// Indicate the rule is used to re-invoke the generator
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// let configure = ninja.rule("configure", "cargo run --manifest-path ./configure/Cargo.toml -- $out")
    ///    .generator();
    ///
    /// configure.build(["build.ninja"]);
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule configure
    ///   command = cargo run --manifest-path ./configure/Cargo.toml -- $out
    ///   generator = 1
    ///
    /// build build.ninja: configure
    /// "###);
    /// ```
    #[inline]
    fn generator(self) -> Self {
        self.variable("generator", "1")
    }

    /// Specify `restat = 1` for the `rule` or `build`
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("example", "...")
    ///     .restat();
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule example
    ///   command = ...
    ///   restat = 1
    /// "###);
    #[inline]
    fn restat(self) -> Self {
        self.variable("restat", "1")
    }

    /// Specify `rspfile` and `rspfile_content` variables for this `rule` or `build`
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("example", "...")
    ///     .rspfile("foo", "bar");
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule example
    ///   command = ...
    ///   rspfile = foo
    ///   rspfile_content = bar
    /// "###);
    /// ```
    fn rspfile(self, rspfile: impl ToArg, rspfile_content: impl ToArg) -> Self {
        self.variable("rspfile", rspfile)
            .variable("rspfile_content", rspfile_content)
    }

    /// Set `pool = console` for this `rule` or `build`
    ///
    /// See <https://ninja-build.org/manual.html#_the_literal_console_literal_pool>
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// ninja.rule("example", "...").pool_console();
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// rule example
    ///   command = ...
    ///   pool = console
    /// "###);
    /// ```
    fn pool_console(self) -> Self {
        self.variable("pool", "console")
    }

    /// Set the pool for this `rule` or `build`
    ///
    /// # Example
    /// ```rust
    /// use ninja_writer::*;
    ///
    /// let ninja = Ninja::new();
    /// let pool = ninja.pool("expensive", 4);
    /// let rule = ninja.rule("cc", "gcc -c $in -o $out").pool(pool);
    /// rule.build(["foo.o"]).with(["foo.c"]);
    ///
    /// assert_eq!(ninja.to_string(), r###"
    /// pool expensive
    ///   depth = 4
    ///
    /// rule cc
    ///   command = gcc -c $in -o $out
    ///   pool = expensive
    ///
    /// build foo.o: cc foo.c
    /// "###);
    /// ```
    #[inline]
    fn pool(self, pool: impl AsRef<Pool>) -> Self {
        self.variable("pool", &pool.as_ref().name)
    }
}

/// Reference to a rule statement that can be used to create build edges
/// using this rule
#[derive(Debug, Clone)]
pub struct RuleRef(pub(crate) StmtRef);

impl Deref for RuleRef {
    type Target = Rule;
    fn deref(&self) -> &Self::Target {
        match self.0.deref().deref() {
            Stmt::Rule(r) => r,
            // safety: RuleRef is only constructable within this crate
            _ => unreachable!(),
        }
    }
}

impl AsRef<Rule> for RuleRef {
    fn as_ref(&self) -> &Rule {
        self.deref()
    }
}

impl RuleRef {
    /// Create a build edge using this rule and the explicit outputs, then add it to
    /// the ninja file provided.
    ///
    /// # Example
    /// See [`Rule`]
    pub fn build(&self, outputs: impl IntoIterator<Item = impl ToArg>) -> BuildRef {
        let build = Build::new(self.deref(), outputs);
        BuildRef(self.0.add(Stmt::Build(Box::new(build))))
    }
}

impl Rule {
    /// Create a new rule with the given name and command
    pub fn new(name: impl ToArg, command: impl ToArg) -> Self {
        let s = Self {
            name: RefCounted::new(name.to_arg()),
            variables: AddOnlyVec::new(),
        };
        s.variable("command", command)
    }

    /// Add the rule to a ninja file and return a [`RuleRef`] for further configuration
    pub fn add_to(self, ninja: &Ninja) -> RuleRef {
        RuleRef(ninja.add_stmt(Stmt::Rule(self)))
    }
}

impl Variables for Rule {
    #[inline]
    fn add_variable_internal(&self, v: Variable) {
        self.variables.add(v);
    }
}

impl RuleVariables for Rule {}

impl Variables for RuleRef {
    fn add_variable_internal(&self, v: Variable) {
        self.deref().add_variable_internal(v)
    }
}
impl RuleVariables for RuleRef {}

impl Display for Rule {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        writeln!(f, "rule {}", self.name)?;
        for variable in self.variables.inner().iter() {
            Indented(variable).fmt(f)?;
            writeln!(f)?;
        }
        Ok(())
    }
}