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
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/.

//! # Build Details
//!
//! `build_details` is a code generation helper that provides build information
//! at runtime.
//!
//! There are two steps to adding `build_details` to a crate:
//!
//!   * Adding/modifying `build.rs`; and
//!   * Including the generated file.
//!
//! ## Invoking Build Details
//!
//! Invoking `build_details` is as simple as adding the following snippet to
//! `build.rs`:
//!
//! ```no_run
//! extern crate build_details;
//!
//! fn main() {
//!     build_details::BuildDetails::default()
//!         .generate("build_details.rs")
//!         .unwrap();
//! }
//! ```
//!
//! ## Including Generated File
//!
//! In `src/lib.rs`:
//!
//! ```no_compile
//! pub mod build_details {
//!     include!(concat!(env!("OUT_DIR"), "/build_details.rs"));
//! }
//! ```
//!
//! ## A note on [`BuildDetail::Cfg`]
//!
//! Using [`BuildDetail::Cfg`] requires a runtime dependency on `phf`.
//!
//! In `Cargo.toml`, add:
//!
//! ```toml
//! [dependencies]
//! phf = "0.7"
//! ```
//!
//! In `src/lib.rs` or `src/main.rs`:
//!
//! ```no_compile
//! extern crate phf;
//! ```
#![deny(
    missing_debug_implementations, missing_docs, trivial_casts, trivial_numeric_casts,
    unused_extern_crates, unused_import_braces, unused_qualifications
)]

#[macro_use]
extern crate maplit;
extern crate phf_codegen;

pub mod error;

use error::*;

use std::collections::{HashMap, HashSet};
use std::env;
use std::fmt;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Code generator for build details. See the crate documentation for an example.
#[derive(Debug, Clone)]
pub struct BuildDetails {
    optional: HashSet<BuildDetail>,
    required: HashSet<BuildDetail>,
}

impl Default for BuildDetails {
    fn default() -> Self {
        Self {
            optional: hashset![
                BuildDetail::Version,
                BuildDetail::Profile,
                BuildDetail::RustFlags,
            ],
            required: HashSet::new(),
        }
    }
}

impl BuildDetails {
    /// Construct a [`BuildDetails`] instance with all available details marked
    /// as optional.
    pub fn all() -> Self {
        Self {
            optional: hashset![
                BuildDetail::Timestamp,
                BuildDetail::Version,
                BuildDetail::Profile,
                BuildDetail::RustFlags,
                BuildDetail::Name,
                BuildDetail::Authors,
                BuildDetail::Description,
                BuildDetail::Homepage,
                BuildDetail::Cfg,
                BuildDetail::Features,
            ],
            required: HashSet::new(),
        }
    }

    /// Construct a [`BuildDetails`] instance with all available details marked
    /// as required.
    ///
    /// This method isn't particularly useful by itself, and will probably need
    /// customization with [`BuildDetails::include`] and [`BuildDetails::exclude`].
    ///
    /// It is impossible to use this method and not break API compatibility if
    /// new [`BuildDetail`] variants are added.
    #[doc(hidden)]
    pub fn require_all() -> Self {
        let mut x = Self::all();
        ::std::mem::swap(&mut x.optional, &mut x.required);
        x
    }

    /// Construct a [`BuildDetails`] instance with no included details.
    ///
    /// This method isn't particularly useful by itself, and will probably need
    /// customization with [`BuildDetails::include`] and [`BuildDetails::exclude`].
    pub fn none() -> Self {
        Self {
            optional: HashSet::new(),
            required: HashSet::new(),
        }
    }

    /// Include a [`BuildDetail`], and mark it as required.
    ///
    /// If a detail is marked as required and isn't available at build time, the
    /// build will fail.
    pub fn require(&mut self, detail: BuildDetail) -> &mut Self {
        self.optional.remove(&detail);
        self.required.insert(detail);
        self
    }

    /// Include a [`BuildDetail`], and mark it as optional.
    ///
    /// If a detail is marked as optional and isn't available at build time, the
    /// generated value will be `None`.
    pub fn include(&mut self, detail: BuildDetail) -> &mut Self {
        self.required.remove(&detail);
        self.optional.insert(detail);
        self
    }

    /// Exclude a [`BuildDetail`]. It will not show up in the generated output.
    pub fn exclude(&mut self, detail: BuildDetail) -> &mut Self {
        self.required.remove(&detail);
        self.optional.remove(&detail);
        self
    }

    /// Creates a file called `path` in the build's `OUT_DIR` directory. See
    /// the crate documentation for an example.
    pub fn generate<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let out_dir = match env::var_os("OUT_DIR") {
            Some(x) => x,
            None => return Err(Error::MissingEnv("OUT_DIR")),
        };

        let mut out_path = PathBuf::from(out_dir);
        out_path.push(path);

        let mut out_file = File::create(out_path)?;

        self.write_to(&mut out_file)
    }

    /// Writes the generated code to a [`::std::io::Write'] instead of to a file.
    pub fn write_to(&self, out_file: &mut Write) -> Result<()> {
        for detail in &self.optional {
            writeln!(out_file, "{}", detail.render_option()?)?;
        }

        for detail in &self.required {
            writeln!(out_file, "{}", detail.render()?)?;
        }

        Ok(())
    }
}

/// List of build details that can be included in the generated code.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum BuildDetail {
    /// Number of seconds since [`::std::time::UNIX_EPOCH`]
    Timestamp,

    /// Equivalent to the `CARGO_PKG_VERSION` environment variable.
    Version,

    /// Equivalent to `PROFILE` in environment variables passed to `build.rs'.
    ///
    /// Should usually be `"debug"` or `"release"`.
    Profile,

    /// Equivalent to the `RUSTFLAGS` environment variable.
    ///
    /// Note that this isn't _all_ of the flags passed to `rustc`, but instead
    /// it is only the custom extra flags.
    RustFlags,

    /// Equivalent to the `CARGO_PKG_NAME` environment variable.
    Name,

    /// Equivalent to the `CARGO_PKG_AUTHORS` environment variable.
    Authors,

    /// Equivalent to the `CARGO_PKG_DESCRIPTION` environment variable.
    Description,

    /// Equivalent to the `CARGO_PKG_HOMEPAGE` environment variable.
    Homepage,

    /// Equivalent to the `OPT_LEVEL` environment variable in `build.rs`.
    OptLevel,

    /// Equivalent to the `CARGO_CFG_*` environment variables in `build.rs`.
    Cfg,

    /// Equivalent to the `CARGO_FEATURE_*` environment variables in `build.rs`.
    Features,

    #[doc(hidden)]
    __Nonexhaustive,
}

impl BuildDetail {
    fn into_render(self) -> Box<Render> {
        use self::BuildDetail::*;

        match self {
            Timestamp => Box::from(self::Timestamp::new()),

            Version => Box::from(Env::new("VERSION", "CARGO_PKG_VERSION")),
            Name => Box::from(Env::new("NAME", "CARGO_PKG_NAME")),
            Authors => Box::from(Env::new("AUTHORS", "CARGO_PKG_AUTHORS")),
            Description => Box::from(Env::new("DESCRIPTION", "CARGO_PKG_DESCRIPTION")),
            Homepage => Box::from(Env::new("HOMEPAGE", "CARGO_PKG_HOMEPAGE")),
            RustFlags => Box::from(Env::new("RUST_FLAGS", "RUSTFLAGS")),

            Profile => Box::from(BuildEnv::new("PROFILE", "PROFILE")),
            OptLevel => Box::from(BuildEnv::new("OPT_LEVEL", "OPT_LEVEL")),

            Cfg => Box::from(BuildEnvMap::new("CFG", "CARGO_CFG_")),
            Features => Box::from(BuildEnvList::new("FEATURES", "CARGO_FEATURE_")),

            __Nonexhaustive => unreachable!(),
        }
    }
}

impl Render for BuildDetail {
    fn render_option(&self) -> Result<String> {
        self.into_render().render_option()
    }

    fn render(&self) -> Result<String> {
        self.into_render().render()
    }
}

struct Detail<T>
where
    T: Render,
{
    name: &'static str,
    value_type: &'static str,
    value: T,
}

impl<T> Render for Detail<T>
where
    T: Render,
{
    fn render_option(&self) -> Result<String> {
        let value = self.value.render_option()?;

        Ok(format!(
            "pub const {}: Option<{}> = {};",
            self.name, self.value_type, value
        ))
    }

    fn render(&self) -> Result<String> {
        let value = match self.value.render() {
            Ok(x) => x,
            Err(Error::Missing) => {
                return Err(Error::MissingDetail(self.name.to_owned()));
            }
            e => return e,
        };

        Ok(format!(
            "pub const {}: {} = {};",
            self.name, self.value_type, value
        ))
    }
}

trait Render {
    fn render_option(&self) -> Result<String>;
    fn render(&self) -> Result<String>;
}

impl<T> Render for Option<T>
where
    T: fmt::Display,
{
    fn render_option(&self) -> Result<String> {
        match self {
            Some(x) => Ok(format!("Some({})", x)),
            None => Ok(format!("None")),
        }
    }

    fn render(&self) -> Result<String> {
        match self {
            Some(x) => Ok(format!("{}", x)),
            None => Err(Error::Missing),
        }
    }
}

struct Timestamp;

impl Timestamp {
    pub fn new() -> Detail<Option<u64>> {
        // TODO: Touch build.rs to trigger a rebuild every time

        let secs = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .as_ref()
            .map(Duration::as_secs)
            .ok();

        Detail {
            name: "TIMESTAMP",
            value_type: "u64",
            value: secs,
        }
    }
}

struct Env(&'static str);

impl Render for Env {
    fn render_option(&self) -> Result<String> {
        Ok(format!("option_env!(\"{}\")", self.0))
    }

    fn render(&self) -> Result<String> {
        Ok(format!("env!(\"{}\")", self.0))
    }
}

impl Env {
    pub fn new(name: &'static str, env: &'static str) -> Detail<Env> {
        Detail {
            name,
            value_type: "&'static str",
            value: Env(env),
        }
    }
}

struct BuildEnv(Option<String>);

impl Render for BuildEnv {
    fn render_option(&self) -> Result<String> {
        match self.0 {
            Some(ref x) => Ok(format!("Some({:?})", x)),
            None => Ok("None".to_owned()),
        }
    }

    fn render(&self) -> Result<String> {
        match self.0 {
            Some(ref x) => Ok(format!("{:?}", x)),
            None => Err(Error::Missing),
        }
    }
}

impl BuildEnv {
    pub fn new(name: &'static str, env: &'static str) -> Detail<Self> {
        let env = env::var(env).ok();

        Detail {
            name,
            value_type: "&'static str",
            value: BuildEnv(env),
        }
    }
}

fn find_matching_vars(prefix: &'static str) -> HashMap<String, String> {
    env::vars()
        .filter_map(|(k, v)| {
            if k.starts_with(prefix) {
                let k = k[prefix.len()..].to_owned();
                Some((k, v))
            } else {
                None
            }
        })
        .collect()
}

struct BuildEnvList(Vec<String>);

impl BuildEnvList {
    pub fn new(name: &'static str, prefix: &'static str) -> Detail<Self> {
        Detail {
            name,
            value_type: "&'static [&'static str]",
            value: BuildEnvList(
                find_matching_vars(prefix)
                    .into_iter()
                    .map(|(k, _)| k)
                    .collect(),
            ),
        }
    }
}

impl Render for BuildEnvList {
    fn render_option(&self) -> Result<String> {
        Ok(format!("Some({})", self.render()?))
    }

    fn render(&self) -> Result<String> {
        use std::fmt::Write;

        let mut txt = String::from("&[\n");

        for item in &self.0 {
            write!(txt, "    {:?},\n", item)?;
        }

        write!(txt, "]")?;

        Ok(txt)
    }
}

struct BuildEnvMap(HashMap<String, String>);

impl BuildEnvMap {
    pub fn new(name: &'static str, prefix: &'static str) -> Detail<Self> {
        Detail {
            name,
            value_type: "::phf::Map<&'static str, &'static str>",
            value: BuildEnvMap(find_matching_vars(prefix)),
        }
    }
}

impl Render for BuildEnvMap {
    fn render_option(&self) -> Result<String> {
        Ok(format!("Some({})", self.render()?))
    }

    fn render(&self) -> Result<String> {
        let mut txt = vec![];

        let mut map = phf_codegen::Map::<&str>::new();

        for (k, v) in &self.0 {
            map.entry(k, &format!("{:?}", v));
        }

        map.build(&mut txt)?;

        Ok(String::from_utf8(txt).unwrap())
    }
}