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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! build-data
//! ==========
//! [](https://crates.io/crates/build-data)
//! [](https://gitlab.com/leonhard-llc/ops/-/raw/main/build-data/LICENSE)
//! [](https://github.com/rust-secure-code/safety-dance/)
//! [](https://gitlab.com/leonhard-llc/ops/-/pipelines)
//!
//! Include build data in your program.
//!
//! # Features
//! - Saves build-time data:
//! - Git commit, branch, and dirtiness
//! - Source modification date & time
//! - Rustc version
//! - Rust channel (stable, nightly, or beta)
//! - Does all of its work in your
//! [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
//! - Sets environment variables.
//! Use [`env!`](https://doc.rust-lang.org/core/macro.env.html) to use them
//! in your program.
//! - No macros
//! - No runtime dependencies
//! - Light build dependencies
//! - `forbid(unsafe_code)`
//! - 100% test coverage
//!
//! # Alternatives
//! - Environment variables that cargo sets for crates:
//! - `CARGO_PKG_NAME`
//! - `CARGO_PKG_VERSION`
//! - `CARGO_BIN_NAME`
//! - [others](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
//! - [`vergen`](https://crates.io/crates/vergen)
//! - Mature & very popular
//! - Good API, needs only `env!` to retrieve values
//! - Excellent test coverage
//! - Heavy build dependencies
//! - [`build-info`](https://crates.io/crates/build-info)
//! - Mature
//! - Confusing API
//! - Uses procedural macros
//!
//! # Example
//!
//! ```toml
//! # Cargo.toml
//! [dependencies]
//!
//! [build-dependencies]
//! build-data = "0.3"
//! ```
//!
//! Add a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html)
//! file next to your `Cargo.toml`.
//! Call [`build_data::set_*`](https://docs.rs/build-data/) functions to
//! set variables.
//! ```
//! // build.rs
//!
//! fn main() {
//! # }
//! # fn f() {
//! build_data::set_GIT_BRANCH().unwrap();
//! build_data::set_GIT_COMMIT().unwrap();
//! build_data::set_GIT_DIRTY().unwrap();
//! build_data::set_SOURCE_TIMESTAMP().unwrap();
//! build_data::no_debug_rebuilds().unwrap();
//! }
//! ```
//!
//! Use [`env!`](https://doc.rust-lang.org/core/macro.env.html) to access the
//! variables in your program:
//! ```
//! // src/bin/main.rs
//! # macro_rules! env (
//! # ($arg:expr) => { "" };
//! # );
//! fn main() {
//! // Built from branch=release
//! // commit=a5547bfb1edb9712588f0f85d3e2c8ba618ac51f
//! // dirty=false
//! // source_timestamp=2021-04-14T06:25:59+00:00
//! println!("Built from branch={} commit={} dirty={} source_timestamp={}",
//! env!("GIT_BRANCH"),
//! env!("GIT_COMMIT"),
//! env!("GIT_DIRTY"),
//! env!("SOURCE_TIMESTAMP"),
//! );
//! }
//! ```
//!
//! # Cargo Geiger Safety Report
//! # Changelog
//! - v0.3.3 - Rebuild docs.
//! - v0.3.2 - Remove test binaries.
//! - v0.3.1 - Rebuild docs.
//! - v0.3.0 - Return `Result` and don't panic.
//! - v0.2.3 - Add `rerun_if_git_commit_or_branch_changed`.
//! - v0.2.2 - Fix `get_source_time` when git config has `log.showsignature=true`.
//! - v0.2.1
//! - Add `set_TARGET_PLATFORM`. Thanks [tison](https://gitlab.com/tisonkun)!
//! - Use u64 for timestamps in helper functions.
//! - v0.1.5 - Update a dependency. Thanks [dignifiedquire](https://gitlab.com/dignifiedquire)!
//! - v0.1.4 - Update a dependency.
//! - v0.1.3 - Update docs.
//! - v0.1.2 - Rewrote based on
//! [feedback](https://www.reddit.com/r/rust/comments/mqnbvw/)
//! from r/rust.
//! - v0.1.1 - Update docs.
//! - v0.1.0 - Initial version
//!
//! # To Do
// https://doc.rust-lang.org/cargo/reference/build-scripts.html
// https://doc.rust-lang.org/cargo/reference/build-script-examples.html
use Utc;
use OsStr;
use ;
/// Converts a byte slice into a string using
/// [`core::ascii::escape_default`](https://doc.rust-lang.org/core/ascii/fn.escape_default.html)
/// to escape each byte.
///
/// # Example
/// ```
/// use build_data::escape_ascii;
/// assert_eq!("abc", escape_ascii(b"abc"));
/// assert_eq!("abc\\n", escape_ascii(b"abc\n"));
/// assert_eq!(
/// "Euro sign: \\xe2\\x82\\xac",
/// escape_ascii("Euro sign: \u{20AC}".as_bytes())
/// );
/// assert_eq!("\\x01\\x02\\x03", escape_ascii(&[1, 2, 3]));
/// ```
/// Executes `cmd` with `args` as parameters, waits for it to exit, and
/// returns its stdout, trimmed, and escaped with
/// [`escape_ascii`](#method.escape_ascii).
///
/// # Errors
/// Returns a descriptive error string if it fails to execute the command
/// or if the command exits with a non-zero status.
/// Converts the epoch timestamp to a UTC time.
///
/// # Errors
/// Returns an error when `epoch` is out of range.
/// Formats the epoch timestamp as a UTC date like `"2021-05-04Z"`.
///
/// # Errors
/// Returns an error when `epoch` is out of range.
/// Formats the epoch timestamp as a UTC time like `"13:02:59Z"`.
///
/// # Errors
/// Returns an error when `epoch` is out of range.
/// Formats the epoch timestamp as a UTC timestamp like `"20201-05-04T13:02:59Z"`.
///
/// # Errors
/// Returns an error when `epoch` is out of range.
/// Gets the current time as an epoch timestamp, caching it so future calls return the same time.
/// Gets the environment variable named `name` if it is set.
///
/// Returns `None` if the variable is unset, is empty, or contains only whitespace.
///
/// Trims whitespace from the start and end of the value before returning it.
///
/// # Errors
/// Returns an error if the environment variable value is not UTF-8.
/// Gets the latest git commit of the source code directory.
///
/// Example: `"a5547bfb1edb9712588f0f85d3e2c8ba618ac51f"`
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Gets the latest git commit of the source code directory.
/// Returns the truncated hash.
///
/// Example: `"a5547bf"`
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Gets the current branch of the source code directory.
///
/// Example: `"release"`
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Returns `true` if the source directory contains uncommitted changes.
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Tells Cargo to re-run the build if the git commit or branch changed.
///
/// NOTE: By default, Cargo re-runs `build.rs` when any file in the directory changes.
/// But if you use any `rerun-if-changed` rule, then it removes that default and uses only the rules
/// you specify. Only call this function if you are already using `rerun-if-changed` rules.
/// See [cargo::rerun-if-changed=PATH](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed).
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Gets the name of the current machine.
///
/// Cargo doesn't pass the `HOSTNAME` env var to build scripts.
/// Uses the `hostname` command.
///
/// # Errors
/// Returns an error if it fails to execute the `hostname` command.
/// Gets the version of the Rust compiler used to build the build script.
///
/// Example: `"rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)"`
///
/// # Errors
/// Returns an error if it fails to execute the `rustc` command.
/// Parses the output of `rustc --version`.
///
/// # Errors
/// Returns an error when `version` has an unexpected format.
/// Gets the dotted-numeric version from the rustc version string.
///
/// Example: `"1.53.0"`
///
/// # Errors
/// Returns an error when `version` has an unexpected format.
/// Gets the channel from the rustc version string.
///
/// # Errors
/// Returns an error when `version` has an unexpected format.
static SOURCE_EPOCH_SECONDS: AtomicU64 = new;
/// Erases the cached source timestamp. Public for tests.
/// Gets the modification time of the source code.
///
/// Reads the
/// [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
/// env var if set. Otherwise, runs `git` to get the value.
///
/// # Errors
/// Returns an error when:
/// - `SOURCE_DATE_EPOCH` is non-empty and cannot be parsed as an `u64`
/// - it failed to execute `git`
/// - `git` exited with non-zero status
/// - `git` wrote stdout data in an unexpected format
/// Gets the Rust target platform string from the TARGET env var
/// [set by cargo for build scripts](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts).
/// See [`set_TARGET_PLATFORM`].
///
/// # Errors
/// Returns `Err` when the `TARGET` env var is not set or is not valid UTF-8.
/// Tells cargo not to rebuild `build.rs` during debug builds when other files
/// change.
///
/// This speeds up development builds.
///
/// Reads the PROFILE env var
/// [set by cargo for build scripts](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts).
///
/// # Errors
/// Returns an error when the PROFILE env var is not set.
/// Sets the `SOURCE_DATE` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"2021-04-14Z"`
///
/// Reads the
/// [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
/// env var if set. Otherwise, runs `git` to get the value.
///
/// # Errors
/// Returns an error when:
/// - `SOURCE_DATE_EPOCH` env var is non-empty and invalid
/// - it fails to get the timestamp from `git`
/// Sets the `SOURCE_TIME` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"03:25:07Z"`
///
/// Reads the
/// [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
/// env var if set. Otherwise, runs `git` to get the value.
///
/// # Errors
/// Returns an error when:
/// - `SOURCE_DATE_EPOCH` env var is non-empty and invalid
/// - it fails to get the timestamp from `git`
/// Sets the `SOURCE_TIMESTAMP` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"2021-04-14T03:25:07Z"`
///
/// Reads the
/// [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
/// env var if set. Otherwise, runs `git` to get the value.
///
/// # Errors
/// Returns an error when:
/// - `SOURCE_DATE_EPOCH` env var is non-empty and invalid
/// - it fails to get the timestamp from `git`
/// Sets the `SOURCE_EPOCH_TIME` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"1618370707"`
///
/// Reads the
/// [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
/// env var if set. Otherwise, runs `git` to get the value.
///
/// # Errors
/// Returns an error when:
/// - `SOURCE_DATE_EPOCH` env var is non-empty and invalid
/// - it fails to get the timestamp from `git`
/// Sets the `BUILD_DATE` env variable with the current date, in UTC.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Calling this will make your build
/// [non-reproducible](https://reproducible-builds.org/docs/timestamps/).
///
/// Example value: `"2021-04-14Z"`
/// Sets the `BUILD_TIME` env variable, with the current time, in UTC.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Calling this will make your build
/// [non-reproducible](https://reproducible-builds.org/docs/timestamps/).
///
/// Example value: `"03:25:07Z"`
/// Sets the `BUILD_TIMESTAMP` env variable, with the current date & time, in UTC.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Calling this will make your build
/// [non-reproducible](https://reproducible-builds.org/docs/timestamps/).
///
/// Example value: `"2021-04-14T03:25:07Z"`
/// Sets the `BUILD_EPOCH_TIME` env variable, with the current time.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Calling this will make your build
/// [non-reproducible](https://reproducible-builds.org/docs/timestamps/).
///
/// Example value: `"1618370707"`
/// Sets the `BUILD_HOSTNAME` env variable, with the hostname of the machine executing the build.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Calling this will make your build
/// [non-reproducible](https://reproducible-builds.org/docs/timestamps/).
///
/// Example value: `"builder2"`
///
/// # Errors
/// Returns an error if it fails to execute the `hostname` command.
/// Sets the `GIT_BRANCH` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"release"`
///
/// Related: [`rerun_if_git_commit_or_branch_changed`]
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Sets the `GIT_COMMIT` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"a5547bfb1edb9712588f0f85d3e2c8ba618ac51f"`
///
/// Related: [`rerun_if_git_commit_or_branch_changed`]
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Sets the `GIT_COMMIT_SHORT` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"a5547bf"`
///
/// Related: [`rerun_if_git_commit_or_branch_changed`]
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Sets the `GIT_DIRTY` env variable.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Sets the variable to `"true"` if the git repository contains uncommitted
/// changes. Otherwise, sets it to `"false"`.
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Sets the `RUSTC_VERSION` env variable to the output of `rustc --version`.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)"`
///
/// # Errors
/// Returns an error if it fails to execute the `rustc` command.
/// Sets the `RUSTC_VERSION_SEMVER` to the dotted version number of the `rustc`
/// used by the current build.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Example value: `"1.53.0"`
///
/// # Errors
/// Returns an error if it fails to execute the `rustc` command.
/// Sets the `RUST_CHANNEL` env variable to Rust channel used by the current build.
///
/// Call this from `build.rs`.
/// Use `env!` in your `main.rs` to use the variable.
///
/// Possible values:
/// - `"stable"`
/// - `"beta"`
/// - `"nightly"`
///
/// # Errors
/// Returns an error if it fails to execute the `rustc` command.
/// Sets the `TARGET_PLATFORM` env variable to the Rust target triple.
/// See "Target Triple" in
/// [The Cargo Book - Glossary](https://doc.rust-lang.org/cargo/appendix/glossary.html#target).
///
/// Gets the string from the TARGET env var
/// [set by cargo for build scripts](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts).
///
/// Examples:
/// - `x86_64-unknown-linux-gnu` (Linux on Intel)
/// - `x86_64-apple-darwin` (macOS on Intel)
/// - `aarch64-unknown-linux-gnu` (Linux on ARM)
/// - `aarch64-apple-darwin` (macOS on Apple Silicon)
/// - See <https://doc.rust-lang.org/rustc/platform-support.html#tier-1-with-host-tools>
///
/// # Errors
/// Returns `Err` when the `TARGET` env var is not set.