build: get_source_date_epoch allow external repos
authorPaul Spooren <mail@aparcar.org>
Sat, 15 Aug 2020 20:57:01 +0000 (10:57 -1000)
committerDaniel Golle <daniel@makrotopia.org>
Mon, 31 Aug 2020 10:18:06 +0000 (11:18 +0100)
The SOURCE_DATE_EPOCH variable is used to make builds reproducible even
if rebuild at different times. Instead of using the current timestamp,
the time of the last source change is used.

Created packages are `touch`ed with a specific timestamp so resulting
packages have the same checksums.

The `get_source_date_epoch.sh` script tries multiple ways (file, git,
hg) to determine the correct timestamp.

Until now the script would only consider the $TOPDIR instead of package
specific changes. Resulting in packages with same versions but different
timestamps, as $TOPDIR (openwrt.git) received changes not affecting
package versions. This results in warning/erros in `opkg` as the package
versions stay the same but checksums changed.

This commit adds an optional argument to get the `SOURCE_DATE_EPOCH` of
a specific path (e.g. package SOURCE) rather than the $TOPDIR. As a
consequence this allows granular but still reproducible timestamps.

As packages might be distributed over multiple repositories the check
for `.git/` becomes unfeasible. Instead tell `git` and `hg` to change
their working directories and automatically traverse the repo folder.

Signed-off-by: Paul Spooren <mail@aparcar.org>
scripts/get_source_date_epoch.sh

index 8a41eebae7cd86aa50251ae2e7fdad4f393849d6..d122acf385326b18da4ac0be7a442c2f8c2d0806 100755 (executable)
@@ -6,21 +6,23 @@ if [ -n "$TOPDIR" ]; then
        cd "$TOPDIR" || exit 1
 fi
 
        cd "$TOPDIR" || exit 1
 fi
 
+SOURCE="${1:-.}"
+
 try_version() {
 try_version() {
-       [ -f version.date ] || return 1
-       SOURCE_DATE_EPOCH="$(cat version.date)"
+       [ -f "$SOURCE/version.date" ] || return 1
+       SOURCE_DATE_EPOCH=$(cat "$SOURCE/version.date")
        [ -n "$SOURCE_DATE_EPOCH" ]
 }
 
 try_git() {
        [ -n "$SOURCE_DATE_EPOCH" ]
 }
 
 try_git() {
-       [ -e .git ] || return 1
-       SOURCE_DATE_EPOCH="$(git log -1 --format=format:%ct)"
+       SOURCE_DATE_EPOCH=$(git -C "$SOURCE" log -1 --format=format:%ct \
+               "$SOURCE" 2>/dev/null)
        [ -n "$SOURCE_DATE_EPOCH" ]
 }
 
 try_hg() {
        [ -n "$SOURCE_DATE_EPOCH" ]
 }
 
 try_hg() {
-       [ -d .hg ] || return 1
-       SOURCE_DATE_EPOCH="$(hg log --template '{date}' -l 1 | cut -d. -f1)"
+       SOURCE_DATE_EPOCH=$(hg --cwd "$SOURCE" log --template '{date}' -l 1 \
+               "$SOURCE" 2>/dev/null | cut -d. -f1)
        [ -n "$SOURCE_DATE_EPOCH" ]
 }
 
        [ -n "$SOURCE_DATE_EPOCH" ]
 }