From 5934f5da9a39100f0a2baffac0eadc61d16e11cd Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 8 Sep 2024 14:17:11 +0200 Subject: [PATCH] Generate git revision to a header file. --- extra/git-ref.py | 24 ++++++++++++++++++++++++ profiler/CMakeLists.txt | 6 ++++++ 2 files changed, 30 insertions(+) create mode 100644 extra/git-ref.py diff --git a/extra/git-ref.py b/extra/git-ref.py new file mode 100644 index 00000000..fa384fa3 --- /dev/null +++ b/extra/git-ref.py @@ -0,0 +1,24 @@ +#!/bin/env python3 + +import filecmp +import subprocess +import os + +out = "GitRef.hpp" +tmp = f"{out}.tmp" + +try: + ref = subprocess.run(["git", "rev-parse", "--short", "HEAD"], check=True, capture_output=True).stdout.decode().strip() +except subprocess.CalledProcessError: + ref = "unknown" + +if not os.path.exists(out): + with open(out, "w") as f: + print(f"#pragma once\n\nnamespace tracy {{ static inline const char* GitRef = \"{ref}\"; }}", file=f) +else: + with open(tmp, "w") as f: + print(f"#pragma once\n\nnamespace tracy {{ static inline const char* GitRef = \"{ref}\"; }}", file=f) + if not filecmp.cmp(out, tmp, shallow=False): + os.replace(tmp, out) + else: + os.unlink(tmp) \ No newline at end of file diff --git a/profiler/CMakeLists.txt b/profiler/CMakeLists.txt index 2993dee6..ff284d65 100644 --- a/profiler/CMakeLists.txt +++ b/profiler/CMakeLists.txt @@ -174,6 +174,12 @@ endif() target_link_libraries(${PROJECT_NAME} PRIVATE TracyServer TracyImGui) +add_custom_target(git-ref + COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../extra/git-ref.py + BYPRODUCTS GitRef.hpp +) +add_dependencies(${PROJECT_NAME} git-ref) + if(NOT EMSCRIPTEN) target_link_libraries(${PROJECT_NAME} PRIVATE TracyNfd) if (NOT USE_WAYLAND)