Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions projects/hrr/playback/HrrVersionStamp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@ endif()
set(HRR_BUILD_REV "unknown")
set(HRR_BUILD_BRANCH "unknown")

# HRR is routinely built in a container against a source tree bind-mounted from
# the host, where the tree's owner is not the build user and git refuses to read
# it at all ("detected dubious ownership") - which would leave every such build
# reporting an unknown revision. safe.directory is honoured only from a config
# *file*: neither "git -c safe.directory=*" nor GIT_CONFIG_COUNT injection is
# accepted. So point git at a throwaway global config for these read-only
# queries instead of modifying the user's real one.
get_filename_component(_out_dir "${OUT_FILE}" DIRECTORY)
file(MAKE_DIRECTORY "${_out_dir}")
set(_git_config "${_out_dir}/hrr_version_stamp.gitconfig")
if(NOT EXISTS "${_git_config}")
file(WRITE "${_git_config}" "[safe]\n\tdirectory = *\n")
endif()
set(ENV{GIT_CONFIG_GLOBAL} "${_git_config}")
# Do not disable git's ownership check. A bind-mounted tree the build user does
# not own then reports "unknown", which is the honest answer. Trusting every
# directory would also trust a repository planted in a parent of a git-less
# source tree, and git would run that repository's commands as the builder.
# A checkout the builder already trusts belongs in their own git config.

find_package(Git QUIET)
if(GIT_FOUND)
Expand Down
44 changes: 44 additions & 0 deletions projects/hrr/tests/scripts/test_version_stamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""The version stamp must not turn off git's ownership check."""

import os
import subprocess
import tempfile
import unittest
from pathlib import Path

STAMP = Path(__file__).resolve().parents[2] / "playback" / "HrrVersionStamp.cmake"
PLAYBACK = STAMP.parent


class VersionStampTest(unittest.TestCase):
def stamp(self, extra_env):
with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp) / "hrr_build_info.h"
env = os.environ.copy()
env.pop("GIT_CONFIG_GLOBAL", None)
env.pop("GIT_CONFIG_SYSTEM", None)
env.update(extra_env)
proc = subprocess.run(
["cmake", "-DSRC_DIR=" + str(PLAYBACK), "-DOUT_FILE=" + str(out),
"-P", str(STAMP)],
check=False, capture_output=True, text=True, env=env)
self.assertEqual(proc.returncode, 0, proc.stderr)
return out.read_text()

def test_owned_repo_still_stamps(self):
text = self.stamp({})
self.assertIn('#define HRR_BUILD_REV "', text)
self.assertNotIn('HRR_BUILD_REV "unknown"', text)

def test_checkout_owned_by_someone_else_reports_unknown(self):
# Git treats the checkout as owned by another user. A stamp that
# bypasses the ownership check, by whatever route, still reads it.
text = self.stamp({"GIT_TEST_ASSUME_DIFFERENT_OWNER": "1"})
self.assertIn('HRR_BUILD_REV "unknown"', text)


if __name__ == "__main__":
unittest.main()
Loading