Skip to content

Commit 96b2490

Browse files
committed
Include the build version in the trace log
1 parent c4c0eb5 commit 96b2490

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

CMakeLists.txt

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
add_subdirectory(fmt-11.1.1)
99

1010
include(CheckCXXCompilerFlag)
11+
include(GNUInstallDirs)
1112

1213
set(VERSION 0.9)
1314

@@ -61,6 +62,44 @@ else()
6162
set(DSOAL_LINK_FLAGS ${DSOAL_LINK_FLAGS} "-Wl,--push-state,-Bstatic,-lstdc++,-lwinpthread,--pop-state")
6263
endif(MSVC)
6364

65+
66+
find_package(Git)
67+
option(DSOAL_UPDATE_BUILD_VERSION "Update git build version info" ON)
68+
if(DSOAL_UPDATE_BUILD_VERSION AND GIT_FOUND AND EXISTS "${dsoal_SOURCE_DIR}/.git")
69+
set(GIT_DIR "${dsoal_SOURCE_DIR}/.git")
70+
71+
# Check if this is a submodule, if it is then find the .git directory
72+
if(NOT IS_DIRECTORY "${dsoal_SOURCE_DIR}/.git")
73+
file(READ ${GIT_DIR} submodule)
74+
string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE ${submodule})
75+
string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
76+
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
77+
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
78+
endif()
79+
80+
# Get the current working branch and its latest abbreviated commit hash
81+
add_custom_command(OUTPUT "${dsoal_BINARY_DIR}/version_witness.txt"
82+
BYPRODUCTS "${dsoal_BINARY_DIR}/version.h"
83+
COMMAND ${CMAKE_COMMAND} -D GIT_EXECUTABLE=${GIT_EXECUTABLE} -D LIB_VERSION=${VERSION}
84+
-D SRC=${dsoal_SOURCE_DIR}/version.h.in -D DST=${dsoal_BINARY_DIR}/version.h
85+
-P ${dsoal_SOURCE_DIR}/version.cmake
86+
COMMAND ${CMAKE_COMMAND} -E touch "${dsoal_BINARY_DIR}/version_witness.txt"
87+
WORKING_DIRECTORY "${dsoal_SOURCE_DIR}"
88+
MAIN_DEPENDENCY "${dsoal_SOURCE_DIR}/version.h.in"
89+
DEPENDS "${GIT_DIR}/index" "${dsoal_SOURCE_DIR}/version.cmake"
90+
VERBATIM
91+
)
92+
93+
add_custom_target(dsoal.build_version DEPENDS "${dsoal_BINARY_DIR}/version_witness.txt")
94+
else()
95+
set(GIT_BRANCH "UNKNOWN")
96+
set(GIT_COMMIT_HASH "unknown")
97+
configure_file(
98+
"${dsoal_SOURCE_DIR}/version.h.in"
99+
"${dsoal_BINARY_DIR}/version.h")
100+
endif()
101+
102+
64103
if(MINGW)
65104
set(CMAKE_RC_COMPILER_INIT windres)
66105
set(CMAKE_RC_COMPILE_OBJECT
@@ -108,16 +147,20 @@ set(TXT_FILES
108147
add_library(dsound SHARED ${SRC_OBJS} version.rc ${DEF_FILE} ${TXT_FILES})
109148
set_target_properties(dsound PROPERTIES PREFIX "")
110149
target_link_libraries(dsound PRIVATE dsoal::fmt ${DSOAL_LINK_FLAGS})
150+
target_include_directories(dsound PRIVATE ${dsoal_BINARY_DIR})
111151
target_compile_definitions(dsound PRIVATE DSOAL_LIBRARY ${DSOAL_CPPDEFS})
112152
target_compile_options(dsound PRIVATE ${DSOAL_CFLAGS})
153+
if(TARGET dsoal.build_version)
154+
add_dependencies(dsound dsoal.build_version)
155+
endif()
113156

114157
install(TARGETS dsound
115158
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
116159
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
117160
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
118-
if(EXISTS "${DSOAL_BINARY_DIR}/dsoal-aldrv.dll")
161+
if(EXISTS "${dsoal_BINARY_DIR}/dsoal-aldrv.dll")
119162
install(FILES
120-
"${DSOAL_BINARY_DIR}/dsoal-aldrv.dll"
163+
"${dsoal_BINARY_DIR}/dsoal-aldrv.dll"
121164
DESTINATION ${CMAKE_INSTALL_BINDIR})
122165
endif()
123166

src/dsoal.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "fullduplex.h"
2626
#include "guidprinter.h"
2727
#include "logging.h"
28+
#include "version.h"
2829

2930

3031
namespace {
@@ -414,6 +415,8 @@ DSOAL_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD reason, void *reserve
414415
}
415416
}
416417

418+
TRACE("DllMain Initializing library v{}-{} {}", DSOAL_VERSION, DSOAL_GIT_COMMIT_HASH,
419+
DSOAL_GIT_BRANCH);
417420
if(!load_openal())
418421
{
419422
if(gLogFile)

version.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
EXECUTE_PROCESS(
2+
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
3+
OUTPUT_VARIABLE GIT_BRANCH
4+
OUTPUT_STRIP_TRAILING_WHITESPACE
5+
)
6+
EXECUTE_PROCESS(
7+
COMMAND ${GIT_EXECUTABLE} log -1 --format=%h
8+
OUTPUT_VARIABLE GIT_COMMIT_HASH
9+
OUTPUT_STRIP_TRAILING_WHITESPACE
10+
)
11+
CONFIGURE_FILE(${SRC} ${DST})

version.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Define to the library version */
2+
#define DSOAL_VERSION "${LIB_VERSION}"
3+
4+
/* Define the branch being built */
5+
#define DSOAL_GIT_BRANCH "${GIT_BRANCH}"
6+
7+
/* Define the hash of the head commit */
8+
#define DSOAL_GIT_COMMIT_HASH "${GIT_COMMIT_HASH}"

0 commit comments

Comments
 (0)