From 1c60f4e8542e5b286c481f68982cb2bfc70f004b Mon Sep 17 00:00:00 2001 From: tersec Date: Thu, 19 Apr 2018 18:30:17 -0700 Subject: [PATCH] https://reproducible-builds.org/ support via SOURCE_DATE_EPOCH (#7644) --- compiler/semfold.nim | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 096fc19e0715..6fcc9a0a4ba8 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -483,6 +483,19 @@ proc newSymNodeTypeDesc*(s: PSym; info: TLineInfo): PNode = proc getConstExpr(m: PSym, n: PNode): PNode = result = nil + + proc getSrcTimestamp(): DateTime = + try: + result = utc(fromUnix(parseInt(getEnv("SOURCE_DATE_EPOCH", + "not a number")))) + except ValueError: + # Environment variable malformed. + # https://reproducible-builds.org/specs/source-date-epoch/: "If the + # value is malformed, the build process SHOULD exit with a non-zero + # error code", which this doesn't do. This uses local time, because + # that maintains compatibility with existing usage. + result = local(getTime()) + case n.kind of nkSym: var s = n.sym @@ -492,8 +505,10 @@ proc getConstExpr(m: PSym, n: PNode): PNode = of skConst: case s.magic of mIsMainModule: result = newIntNodeT(ord(sfMainModule in m.flags), n) - of mCompileDate: result = newStrNodeT(times.getDateStr(), n) - of mCompileTime: result = newStrNodeT(times.getClockStr(), n) + of mCompileDate: result = newStrNodeT(format(getSrcTimestamp(), + "yyyy-MM-dd"), n) + of mCompileTime: result = newStrNodeT(format(getSrcTimestamp(), + "HH:mm:ss"), n) of mCpuEndian: result = newIntNodeT(ord(CPU[targetCPU].endian), n) of mHostOS: result = newStrNodeT(toLowerAscii(platform.OS[targetOS].name), n) of mHostCPU: result = newStrNodeT(platform.CPU[targetCPU].name.toLowerAscii, n)