Skip to contentSkip to conversation

kernel.bbclass: fix SOURCE_DATE_EPOCH for non-git kernel builds

PATCH Superseded [email protected] 2 participants 3 messages Mar 7, 2020 – Mar 9, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel.bbclass: fix SOURCE_DATE_EPOCH for non-git kernel builds

From: Christopher Clark <[email protected]>

Only use git to set SOURCE_DATE_EPOCH if ${S} is the top level of a git repository.

Fixes the following errors with the prior logic when building a kernel that is not obtained from a git repository:

1. With TMPDIR set to a directory outside any git repository on a mounted filesystem, reproducible builds fail in do_compile with this git error:

fatal: not a git repository (or any parent up to mount point <abspath>) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

aborting before the error handling logic.

2. With TMPDIR located within a subdirectory of a git repository, the SOURCE_DATE_EPOCH timestamp would be that of said repository rather than that of the kernel.

---
 meta/classes/kernel.bbclass | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
meta/classes/kernel.bbclass +8 -4
index 0eadd3efb8..2603f24709 100644
@@ -296,10 +296,14 @@ kernel_do_compile() {
 		if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
 			olddir=`pwd`
 			cd ${S}
-			SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
-			# git repo not guaranteed, so fall back to REPRODUCIBLE_TIMESTAMP_ROOTFS
-			if [ $? -ne 0 ]; then
-				SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
+			# This kernel dir is not necessarily a git repo and we
+			# must ensure that git does not incorrectly query a
+			# repository in a parent directory.
+			GIT_TOP=`git rev-parse --show-toplevel 2>/dev/null || echo ""`
+			if [ "${S}" = "${GIT_TOP}" ]; then
+				SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
+			else
+				SOURCE_DATE_EPOCH="${REPRODUCIBLE_TIMESTAMP_ROOTFS}"
 			fi
 			cd $olddir
 		fi
Signature · 1 line
2.17.1
#1 lore raw
Quoted 34 lines · On Sat, Mar 7, 2020 at 8:04 PM <[email protected]> wrote:

From: Christopher Clark <[email protected]>

Only use git to set SOURCE_DATE_EPOCH if ${S} is the top level of a git repository.

Fixes the following errors with the prior logic when building a kernel that is not obtained from a git repository:

1. With TMPDIR set to a directory outside any git repository on a mounted filesystem, reproducible builds fail in do_compile with this git error:

fatal: not a git repository (or any parent up to mount point <abspath>) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

aborting before the error handling logic.

2. With TMPDIR located within a subdirectory of a git repository, the SOURCE_DATE_EPOCH timestamp would be that of said repository rather than that of the kernel.

---
 meta/classes/kernel.bbclass | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
meta/classes/kernel.bbclass +0 -0
index 0eadd3efb8..2603f24709 100644
@@ -296,10 +296,14 @@ kernel_do_compile() {
                if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
                        olddir=`pwd`

meta/classes/kernel.bbclass kernel_do_compile() { · line 303

Quoted 10 lines
meta/classes/kernel.bbclass +5 -4
                        cd ${S}
-                       SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
-                       # git repo not guaranteed, so fall back to REPRODUCIBLE_TIMESTAMP_ROOTFS
-                       if [ $? -ne 0 ]; then
-                               SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
+                       # This kernel dir is not necessarily a git repo and we
+                       # must ensure that git does not incorrectly query a
+                       # repository in a parent directory.
+                       GIT_TOP=`git rev-parse --show-toplevel 2>/dev/null || echo ""`
+                       if [ "${S}" = "${GIT_TOP}" ]; then

Will that work if ${S} isn't a canonical path?

meta/classes/kernel.bbclass kernel_do_compile() { · line 309

Quoted 6 lines
meta/classes/kernel.bbclass +3 -0
+                               SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
+                       else
+                               SOURCE_DATE_EPOCH="${REPRODUCIBLE_TIMESTAMP_ROOTFS}"
                        fi
                        cd $olddir
                fi

Would something like this resolve the problem (untested):

meta/classes/kernel.bbclass +0 -0
index 0eadd3efb8d9..a17ca7adccca 100644
@@ -294,14 +294,11 @@ kernel_do_compile() {
                # kernel sources do not use do_unpack, so

SOURCE_DATE_EPOCH may not

                # be set....
                if [ "${SOURCE_DATE_EPOCH}" = "" -o
"${SOURCE_DATE_EPOCH}" = "0" ]; then
-                       olddir=`pwd`
-                       cd ${S}
-                       SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
+                       SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git}"
log  -1 --pretty=%ct`
                        # git repo not guaranteed, so fall back to
REPRODUCIBLE_TIMESTAMP_ROOTFS
                        if [ $? -ne 0 ]; then

SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
fi
- cd $olddir
fi

ts=LC_ALL=C date -d @$SOURCE_DATE_EPOCH

--
Alex Kiernan

#2 lore raw
Quoted 40 lines · Alex Kiernan
Quoted 38 lines · On Sat, Mar 7, 2020 at 8:04 PM <[email protected]> wrote:

From: Christopher Clark <[email protected]>

Only use git to set SOURCE_DATE_EPOCH if ${S} is the top level of a git repository.

Fixes the following errors with the prior logic when building a kernel that is not obtained from a git repository:

1. With TMPDIR set to a directory outside any git repository on a mounted filesystem, reproducible builds fail in do_compile with this git error:

fatal: not a git repository (or any parent up to mount point <abspath>) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

aborting before the error handling logic.

2. With TMPDIR located within a subdirectory of a git repository, the SOURCE_DATE_EPOCH timestamp would be that of said repository rather than that of the kernel.

---
 meta/classes/kernel.bbclass | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
meta/classes/kernel.bbclass +0 -3
index 0eadd3efb8..2603f24709 100644
@@ -296,10 +296,14 @@ kernel_do_compile() {
                if [ "${SOURCE_DATE_EPOCH}" = "" -o "${SOURCE_DATE_EPOCH}" = "0" ]; then
                        olddir=`pwd`
                        cd ${S}
-                       SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
-                       # git repo not guaranteed, so fall back to REPRODUCIBLE_TIMESTAMP_ROOTFS
-                       if [ $? -ne 0 ]; then
Quoted 8 lines

meta/classes/kernel.bbclass kernel_do_compile() { · line 303

Quoted 6 lines
meta/classes/kernel.bbclass +5 -1
-                               SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
+                       # This kernel dir is not necessarily a git repo and we
+                       # must ensure that git does not incorrectly query a
+                       # repository in a parent directory.
+                       GIT_TOP=`git rev-parse --show-toplevel 2>/dev/null || echo ""`
+                       if [ "${S}" = "${GIT_TOP}" ]; then

Will that work if ${S} isn't a canonical path?

Good question; it would be better to avoid the comparison.

Quoted 9 lines

meta/classes/kernel.bbclass kernel_do_compile() { · line 309

Quoted 7 lines
meta/classes/kernel.bbclass +3 -0
+                               SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
+                       else
+                               SOURCE_DATE_EPOCH="${REPRODUCIBLE_TIMESTAMP_ROOTFS}"
                        fi
                        cd $olddir
                fi

Would something like this resolve the problem (untested):

Thanks for the suggestion to use the git-dir option. git is somewhat picky about argument ordering and any error encountered at that point aborts the build rather than falling into error handling logic below, but I have a revised v2 patch now that I will post shortly.

Christopher

Quoted 29 lines
meta/classes/kernel.bbclass +0 -0
index 0eadd3efb8d9..a17ca7adccca 100644
@@ -294,14 +294,11 @@ kernel_do_compile() {
                # kernel sources do not use do_unpack, so

SOURCE_DATE_EPOCH may not

                # be set....
                if [ "${SOURCE_DATE_EPOCH}" = "" -o
"${SOURCE_DATE_EPOCH}" = "0" ]; then
meta/classes/kernel.bbclass +1 -3
-                       olddir=`pwd`
-                       cd ${S}
-                       SOURCE_DATE_EPOCH=`git log  -1 --pretty=%ct`
+                       SOURCE_DATE_EPOCH=`git --git-dir="${S}/.git}"

log -1 --pretty=%ct`

                        # git repo not guaranteed, so fall back to
REPRODUCIBLE_TIMESTAMP_ROOTFS
                        if [ $? -ne 0 ]; then

SOURCE_DATE_EPOCH=${REPRODUCIBLE_TIMESTAMP_ROOTFS}
fi
- cd $olddir
fi

ts=LC_ALL=C date -d @$SOURCE_DATE_EPOCH

--
Alex Kiernan