1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00
cdt/releng/scripts/reproducible_build_wrapper.py
Torbjörn Svensson 07b50ba2a2 Bug 521515: Do not rely on commit date for reproducible builds
As mentioned in
https://devblogs.microsoft.com/oldnewthing/20180103-00/?p=97705,
Microsoft has stopped using the _IMAGE_FILE_HEADER.TimeDateStamp as a
time stamp and rather as a hash of the source files to make the build
result predictable.

Change-Id: I4f4a7b9557330e4c478ef7fb25653144c5b2d4ad
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
2020-08-27 15:42:44 -04:00

58 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import os
import hashlib
import subprocess
LONG_MAX = (1 << 64) - 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))
data = subprocess.check_output(preprocess_command)
# 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) % LONG_MAX)
debug("SOURCE_DATE_EPOCH: {}".format(os.environ["SOURCE_DATE_EPOCH"]))
# Run the compiler with the environement variable set
subprocess.run(compiler_command)