Skip to content

Commit

Permalink
Cmake: Add version from git to CPack
Browse files Browse the repository at this point in the history
  • Loading branch information
Dasperal committed Nov 16, 2024
1 parent 6166520 commit 22dc820
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ add_subdirectory(src)
# CPack
set(CPACK_PACKAGE_NAME "russian-doom-dos")
set(CPACK_SYSTEM_NAME "i386")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_CHECKSUM SHA256)
set(CPACK_GENERATOR ZIP)
set(CPACK_PROJECT_CONFIG_FILE "CPackProjectConfig.cmake")

# Archive Generator
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
Expand Down
7 changes: 7 additions & 0 deletions cmake/CPackConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## @Hash@@Version_suffix@
#
# This file was automatically generated by the Cmake.
# Do not edit by hand.

set(CPACK_PACKAGE_VERSION "@PROJECT_VERSION@@Version_suffix@")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
15 changes: 10 additions & 5 deletions cmake/VersionFromGit.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# configure_empty_git_info(<Template> <Output>)
function(configure_empty_git_info Template Output)
# VersionFromGit.cmake
#
# Public domain.

# configure_empty_git_info()
function(configure_empty_git_info)
set(Hash "<unknown>")
set(Timestamp "<unknown>")
set(Version_suffix "")
set(Display_version_suffix "")
configure_file("${Template}" "${Output}" @ONLY)
configure_file("cmake/git_info.h.in" "git_info.h" @ONLY)
configure_file("cmake/CPackConfig.cmake.in" "CPackProjectConfig.cmake" @ONLY)
endfunction()

# Git info options
Expand All @@ -14,10 +19,10 @@ cmake_dependent_option(RD_GIT_NO_HASH "For Devs only! Don't run 'revision_check'
# Update git_info.h
if(RD_GIT_NO_HASH)
# Use a function to avoid accidentally messing the real variables
configure_empty_git_info("cmake/git_info.h.in" "git_info.h")
configure_empty_git_info()
else()
add_custom_target(revision_check
COMMAND "${CMAKE_COMMAND}" -P "\"${PROJECT_SOURCE_DIR}/cmake/scripts/UpdateRevision.cmake\"" "${GIT_EXECUTABLE}" "\"git_info.h\""
COMMAND "${CMAKE_COMMAND}" -P "\"${PROJECT_SOURCE_DIR}/cmake/scripts/UpdateRevision.cmake\"" "${GIT_EXECUTABLE}" "\"git_info.h\"" "\"CPackProjectConfig.cmake\""
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
endif()
25 changes: 16 additions & 9 deletions cmake/scripts/UpdateRevision.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

# UpdateRevision.cmake
#
# Public domain. This program uses git commands command to get
# Public domain. This program uses git commands to get
# various bits of repository status for a particular directory
# and writes it into a header file so that it can be used for a
# and writes it into a header file and a CPackProjectConfig file so that it can be used for a
# project's versioning.
# It also outputs the evaluated version to std out if no optional arguments provided.

# Boilerplate to return a variable from a function.
macro(ret_var VAR)
Expand Down Expand Up @@ -142,8 +143,8 @@ function(get_project_version CmakeFile)
endfunction()

function(main)
if(CMAKE_ARGC LESS 4) # cmake -P UpdateRevision.cmake <path to git> [<OutputFile>]
message(NOTICE "Usage: ${CMAKE_ARGV2} <path to git> [<path to git_info.h>]")
if(CMAKE_ARGC LESS 4) # cmake -P UpdateRevision.cmake <path to git> [<HeaderOutputFile>] [<CPackOutputFile>]
message(NOTICE "Usage: ${CMAKE_ARGV2} <path to git> [<path to git_info.h>] [<path to CPackConfig.cmake>]")
return()
endif()
set(Git_executable "${CMAKE_ARGV3}")
Expand Down Expand Up @@ -178,13 +179,19 @@ function(main)
if(CMAKE_ARGV4)
set(OutputFile "${CMAKE_ARGV4}")
get_existing_hash("${OutputFile}")
if("${Hash}${Version_suffix}" STREQUAL OldHash)
return()
if(NOT "${Hash}${Version_suffix}" STREQUAL OldHash)
configure_file("${ModulesDir}/git_info.h.in" "${OutputFile}" @ONLY)
message(STATUS "Configuring ${OutputFile} - updated to commit ${Hash_suffix}")
endif()

configure_file("${ModulesDir}/git_info.h.in" "${OutputFile}" @ONLY)

message(STATUS "Configuring ${OutputFile} - updated to commit ${Hash_suffix}")
if(CMAKE_ARGV5)
set(OutputFile "${CMAKE_ARGV5}")
get_existing_hash("${OutputFile}")
if(NOT "${Hash}${Version_suffix}" STREQUAL OldHash)
configure_file("${ModulesDir}/CPackConfig.cmake.in" "${OutputFile}" @ONLY)
message(STATUS "Configuring ${OutputFile} - updated to commit ${Hash_suffix}")
endif()
endif()
else()
if(On_master_branch)
message(NOTICE "${PROJECT_VERSION}${Version_suffix}")
Expand Down

0 comments on commit 22dc820

Please sign in to comment.