0
0
mirror of https://github.com/wolfpld/tracy.git synced 2026-01-18 17:11:26 +01:00

Fix #996 Remove any need for python during build

We are now using only CMake to generate `GitRef.hpp`. This should make it easier to build on Windows.
We use git's pretty formatting instead of rev-parse to output the full file in a single command, then CMake's `copy_if_different` to avoid unnecessary rebuilds.

Trick is to use %n for newlines and %x22 for quotes. See https://git-scm.com/docs/pretty-formats

We also output "unknown" and a warning when git is not available.
This commit is contained in:
Clément Grégoire
2025-05-23 23:46:58 +02:00
parent 8b3a421153
commit eba268ad9d
2 changed files with 19 additions and 36 deletions

View File

@@ -1,30 +0,0 @@
#!/bin/env python3
import filecmp
import subprocess
import sys
import os
out = "GitRef.hpp"
tmp = f"{out}.tmp"
if len(sys.argv) > 1:
rev = sys.argv[1]
else:
rev = "HEAD"
try:
ref = subprocess.run(["git", "rev-parse", "--short", rev], 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)

View File

@@ -189,12 +189,25 @@ if(NOT DEFINED GIT_REV)
set(GIT_REV "HEAD")
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
add_custom_target(git-ref
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/../extra/git-ref.py ${GIT_REV}
BYPRODUCTS GitRef.hpp
)
add_dependencies(${PROJECT_NAME} git-ref)
find_package(Git)
if(Git_FOUND)
add_custom_target(git-ref
COMMAND ${GIT_EXECUTABLE} log -1 "--format=#pragma once %nnamespace tracy { static inline const char* GitRef = %x22%h%x22; }" ${GIT_REV} > GitRef.hpp.tmp
COMMAND ${CMAKE_COMMAND} -E copy_if_different GitRef.hpp.tmp GitRef.hpp
BYPRODUCTS GitRef.hpp GitRef.hpp.tmp
VERBATIM
)
add_dependencies(${PROJECT_NAME} git-ref)
else()
message(WARNING "git not found, using 'unknown' as git ref.")
add_custom_command(
OUTPUT GitRef.hpp
COMMAND ${CMAKE_COMMAND} -E echo "#pragma once" > GitRef.hpp
COMMAND ${CMAKE_COMMAND} -E echo "namespace tracy { static inline const char* GitRef = \"unknown\"; }" >> GitRef.hpp
VERBATIM
)
target_sources(${PROJECT_NAME} PUBLIC GitRef.hpp)
endif()
if(NOT EMSCRIPTEN)
if(NOT NO_FILESELECTOR)