formula: add a time method for use as the build time

A number of formulae use a variant of `Time.now` to generate the build
time of the formula. Let's define a method to make sure that:

1. it uses `SOURCE_DATE_EPOCH` whenever available; and,
2. it uses `utc`.

Both should help with build reproducibility.

See Homebrew/homebrew-core#80608.
This commit is contained in:
Carlo Cabrera 2021-07-06 12:26:03 +01:00
parent c20826a872
commit 3776ba9756
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -1551,6 +1551,18 @@ class Formula
"@loader_path/../lib"
end
# Creates a new `Time` object for use in the formula as the build time.
#
# @see https://www.rubydoc.info/stdlib/time/Time Time
sig { returns(Time) }
def time
if ENV["SOURCE_DATE_EPOCH"].present?
Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc
else
Time.now.utc
end
end
# an array of all core {Formula} names
# @private
def self.core_names