1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00
cdt/releng/scripts/reproducible_build_wrapper.py
Torbjörn Svensson 66f3c6b08d Bug 579039: Limit SOURCE_DATE_EPOCH to fit 32bit unsigned int
The impementation in GCC allows a value up to 253402300799 that
corresponds to "Dec 31 9999 23:59:59 UTC". Apparently, this is due
to some limit of __DATE__ and __TIME__ within the preprocessor.
Regardless, as the reporter claims to have problem with values
outside the range of a 32bit unsigned integer, lets use
"(1 << 32) - 1" as a limit.

Contributed by STMicroelectronics

Change-Id: Ifa7995cd9edb460d4ad6544b5231eef88d7e39a0
Signed-off-by: Torbjörn Svensson <torbjorn.svensson@st.com>
2022-03-10 01:43:40 -05:00

72 lines
2 KiB
Python
Executable file

#!/usr/bin/env python3
###############################################################################
# Copyright (c) 2020 Torbjörn Svensson
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
###############################################################################
import sys
import os
import hashlib
import subprocess
UINT32_MAX = (1 << 32) - 1
DEBUG = True
def usage(msg=None):
if msg:
print(msg)
print("Usage: {0} <gcc/g++ command>".format(sys.argv[0]))
sys.exit(1)
def debug(s):
if DEBUG:
print("{} {}".format(sys.argv[0], s))
compiler_command = sys.argv[1:]
if len(compiler_command) == 0:
usage()
# Hash all the source files and traverse any directories recursively
sha1 = hashlib.sha1()
# Hash the build command too
sha1.update(" ".join(compiler_command).encode())
debug("Compiler command hashed: {}".format(sha1.hexdigest()))
preprocess_command = [*compiler_command, "-E"]
# Remove any output file (needs to write to stdout)
try:
index = compiler_command.index("-o")
del preprocess_command[index:index+2]
except ValueError:
usage("Missing output compiler flag")
# Preprocess the source file(s)
debug("Preprocess cmd: {}".format(preprocess_command))
try:
data = subprocess.check_output(preprocess_command)
except subprocess.CalledProcessError as e:
print("Failed to hash source code, exit code {}".format(e.returncode))
sys.exit(e.returncode)
# Hash the content
sha1.update(data)
debug("Content hashed: {}".format(sha1.hexdigest()))
# Set the SOURCE_DATE_EPOCH environment variable to the hash value
os.environ["SOURCE_DATE_EPOCH"] = str(int(sha1.hexdigest(), base=16) % UINT32_MAX)
debug("SOURCE_DATE_EPOCH: {}".format(os.environ["SOURCE_DATE_EPOCH"]))
# Run the compiler with the environement variable set
sys.exit(subprocess.run(compiler_command).returncode)