Skip to content

Commit

Permalink
Merge pull request #71316 from alef/newversionh
Browse files Browse the repository at this point in the history
Change how CMake generates version files
Maleclypse authored Jan 31, 2024
2 parents f2c8f47 + dab543c commit 34ea511
Showing 4 changed files with 67 additions and 46 deletions.
28 changes: 10 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -55,27 +55,19 @@ endif ()

add_definitions(-DCMAKE)

include(GetGitRevisionDescription)
git_describe(GIT_VERSION --tags --always --match "[0-9A-Z]*.[0-9A-Z]*")
# Retrieve version from git into GIT_VERSION and creates VERSION.txt
execute_process(COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/src/version.cmake
ERROR_VARIABLE GIT_VERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

if (NOT "${GIT_VERSION}" MATCHES "GIT-NOTFOUND")
string(REPLACE "-NOTFOUND" "" GIT_VERSION ${GIT_VERSION})
file(WRITE ${CMAKE_SOURCE_DIR}/src/version.h
"// NOLINT(cata-header-guard)\n\#define VERSION \"${GIT_VERSION}\"\n")
message(STATUS "${PROJECT_NAME} build version is: ${GIT_VERSION}")
add_definitions(-DGIT_VERSION)

# get_git_head_revision() does not work with worktrees in Windows
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _sha1
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(TIMESTAMP _timestamp %Y-%m-%d-%H%M)
file(WRITE VERSION.txt "\
build type: ${BUILD_PRESET_NAME}\n\
build number: ${_timestamp}\n\
commit sha: ${_sha1}\n\
commit url: https://github.com/CleverRaven/Cataclysm-DDA/commit/${_sha1}"
)
else()
message(WARNING "Git binary not found. Build version will be set to NULL. Install Git package
or use -DGIT_BINARY to set path to git binary.")
set(VERSION "NULL")
endif ()

#OS Check Placeholders. Will be used for BINDIST
26 changes: 11 additions & 15 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
add_subdirectory(${CMAKE_SOURCE_DIR}/src/third-party)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party)

set(MAIN_CPP ${CMAKE_SOURCE_DIR}/src/main.cpp)
set(MESSAGES_CPP ${CMAKE_SOURCE_DIR}/src/messages.cpp)
set(RESOURCE_RC ${CMAKE_SOURCE_DIR}/src/resource.rc)
set(MAIN_CPP ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(MESSAGES_CPP ${CMAKE_CURRENT_SOURCE_DIR}/messages.cpp)
set(RESOURCE_RC ${CMAKE_CURRENT_SOURCE_DIR}/resource.rc)

if (WIN32)
set(RSRC_RC_DEP "${CMAKE_SOURCE_DIR}/data/cataicon.ico")
@@ -13,25 +13,21 @@ if (WIN32)
OBJECT_DEPENDS "${RSRC_RC_DEP}")
endif ()

file(GLOB CATACLYSM_DDA_SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB CATACLYSM_DDA_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

list(REMOVE_ITEM CATACLYSM_DDA_SOURCES ${MAIN_CPP} ${MESSAGES_CPP})

file(GLOB CATACLYSM_DDA_HEADERS ${CMAKE_SOURCE_DIR}/src/*.h)
file(GLOB CATACLYSM_DDA_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)

# Get GIT version strings
add_custom_target(
get_version
DEPENDS ${CMAKE_SOURCE_DIR}/src/version.h
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/version.h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/src/version.h
COMMAND ${CMAKE_COMMAND}
-D SRC=${CMAKE_SOURCE_DIR}/src/version.h.in
-D DST=${CMAKE_SOURCE_DIR}/src/version.h
-D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-P ${CMAKE_SOURCE_DIR}/src/version.cmake
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/version.h
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/version.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

# Build tiles version if requested
@@ -40,7 +36,7 @@ if (TILES)
cataclysm-tiles-common OBJECT
${CATACLYSM_DDA_SOURCES}
${CATACLYSM_DDA_HEADERS})
target_include_directories(cataclysm-tiles-common INTERFACE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(cataclysm-tiles-common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(cataclysm-tiles-common PUBLIC third-party)

@@ -154,7 +150,7 @@ if (CURSES)
add_library(cataclysm-common OBJECT
${CATACLYSM_DDA_SOURCES}
${CATACLYSM_DDA_HEADERS})
target_include_directories(cataclysm-common INTERFACE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(cataclysm-common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(cataclysm-common PUBLIC third-party)

58 changes: 46 additions & 12 deletions src/version.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
IF(GIT_EXECUTABLE)
EXECUTE_PROCESS(
COMMAND ${GIT_EXECUTABLE} describe --tags --always --dirty --match "[0-9A-Z]*.[0-9A-Z]*"
OUTPUT_VARIABLE VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
ELSE(GIT_EXECUTABLE)
MESSAGE(WARNING "Git binary not found. Build version will be set to NULL. Install Git package or use -DGIT_BINARY to set path to git binary.")
SET (VERSION "NULL")
ENDIF(GIT_EXECUTABLE)

CONFIGURE_FILE(${SRC} ${DST} @ONLY)
list(APPEND CMAKE_MODULE_PATH
${CMAKE_SOURCE_DIR}/CMakeModules)
include(GetGitRevisionDescription)

git_describe(GIT_VERSION --tags --always --match "[0-9A-Z]*.[0-9A-Z]*")

if(EXISTS ${GIT_EXECUTABLE})
execute_process(COMMAND ${GIT_EXECUTABLE} diff --quiet
RESULT_VARIABLE DIRTY_FLAG
)

if(DIRTY_FLAG)
string(APPEND GIT_VERSION "-dirty")
endif()
endif()

message(NOTICE ${GIT_VERSION})

if("${GIT_VERSION}" MATCHES "GIT-NOTFOUND")
return()
endif()

string(REPLACE "-NOTFOUND" "" GIT_VERSION ${GIT_VERSION})

file(TOUCH ${CMAKE_SOURCE_DIR}/src/version.h)
file(READ ${CMAKE_SOURCE_DIR}/src/version.h VERSION_H)
string(REGEX MATCH "#define VERSION \"(.+)\"" VERSION_H "${VERSION_H}")

if(NOT GIT_VERSION STREQUAL VERSION_H)
file(WRITE ${CMAKE_SOURCE_DIR}/src/version.h
"// NOLINT(cata-header-guard)\n\#define VERSION \"${GIT_VERSION}\"\n")
endif()

# get_git_head_revision() does not work with worktrees in Windows
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _sha1
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(TIMESTAMP _timestamp %Y-%m-%d-%H%M)
file(WRITE ${CMAKE_SOURCE_DIR}/VERSION.txt "\
build type: Release\n\
build number: ${_timestamp}\n\
commit sha: ${_sha1}\n\
commit url: https://github.com/CleverRaven/Cataclysm-DDA/commit/${_sha1}"
)

1 change: 0 additions & 1 deletion src/version.h.in

This file was deleted.

0 comments on commit 34ea511

Please sign in to comment.