1 cmake_minimum_required(VERSION 3.5)
11 # Finds the current version of the current folder.
14 find_package(Git QUIET)
15 # ${CMAKE_SOURCE_DIR}/.git may be a directory or a regular file
16 if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
17 # Make sure LC_ALL is set to something desirable
18 set(SAVED_LC_ALL "$ENV{LC_ALL}")
21 # Assume the dir is not modified
24 # Refresh the index to make sure file stat info is in sync, then look for modifications
25 execute_process(COMMAND ${GIT_EXECUTABLE} update-index --refresh
26 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
30 # See if git tree is modified
31 execute_process(COMMAND ${GIT_EXECUTABLE} diff-index HEAD
32 OUTPUT_VARIABLE IS_MODIFIED
33 OUTPUT_STRIP_TRAILING_WHITESPACE
34 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
36 if(NOT IS_MODIFIED STREQUAL "")
40 # Get last commit hash
41 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --verify HEAD
42 OUTPUT_VARIABLE FULLHASH
43 OUTPUT_STRIP_TRAILING_WHITESPACE
44 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
47 set(REV_HASH "${FULLHASH}")
49 string(SUBSTRING "${FULLHASH}" 0 10 SHORTHASH)
51 # Get the last commit date
53 execute_process(COMMAND ${GIT_EXECUTABLE} show -s --date=iso-local --pretty=format:%cd HEAD
54 OUTPUT_VARIABLE COMMITDATE
55 OUTPUT_STRIP_TRAILING_WHITESPACE
56 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
58 string(REGEX REPLACE "([0-9]+)-([0-9]+)-([0-9]+).*" "\\1\\2\\3" COMMITDATE "${COMMITDATE}")
59 set(REV_ISODATE "${COMMITDATE}")
62 execute_process(COMMAND ${GIT_EXECUTABLE} symbolic-ref -q HEAD
63 OUTPUT_VARIABLE BRANCH
64 OUTPUT_STRIP_TRAILING_WHITESPACE
65 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
68 string(REGEX REPLACE ".*/" "" BRANCH "${BRANCH}")
71 execute_process(COMMAND ${GIT_EXECUTABLE} name-rev --name-only --tags --no-undefined HEAD
73 OUTPUT_STRIP_TRAILING_WHITESPACE
74 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
77 string(REGEX REPLACE "\^0$" "" TAG "${TAG}")
79 if(REV_MODIFIED EQUAL 0)
81 elseif(REV_MODIFIED EQUAL 2)
87 # Set the version string
88 if(NOT TAG STREQUAL "")
89 set(REV_VERSION "${TAG}")
92 string(REGEX REPLACE "^[0-9.]+$" "" STABLETAG "${TAG}")
93 if(STABLETAG STREQUAL "")
94 set(REV_ISSTABLETAG 1)
96 set(REV_ISSTABLETAG 0)
99 set(REV_VERSION "${REV_ISODATE}-${BRANCH}${HASHPREFIX}${SHORTHASH}")
101 set(REV_ISSTABLETAG 0)
105 set(ENV{LC_ALL} "${SAVED_LC_ALL}")
106 elseif(EXISTS "${CMAKE_SOURCE_DIR}/.ottdrev")
107 file(READ "${CMAKE_SOURCE_DIR}/.ottdrev" OTTDREV)
108 string(REPLACE "\n" "" OTTDREV "${OTTDREV}")
109 string(REPLACE "\t" ";" OTTDREV "${OTTDREV}")
110 list(GET OTTDREV 0 REV_VERSION)
111 list(GET OTTDREV 1 REV_ISODATE)
112 list(GET OTTDREV 2 REV_MODIFIED)
113 list(GET OTTDREV 3 REV_HASH)
114 list(GET OTTDREV 4 REV_ISTAG)
115 list(GET OTTDREV 5 REV_ISSTABLETAG)
117 message(WARNING "No version detected; this build will NOT be network compatible")
118 set(REV_VERSION "norev0000")
119 set(REV_ISODATE "19700101")
121 set(REV_HASH "unknown")
123 set(REV_ISSTABLETAG 0)
126 # Extract REV_YEAR and REV_DATE from REV_ISODATE
127 string(SUBSTRING "${REV_ISODATE}" 0 4 REV_YEAR)
128 string(SUBSTRING "${REV_ISODATE}" 4 4 REV_DATE)
129 # Drop leading 0 in REV_DATE if any
130 string(REGEX REPLACE "^0?([0-9]+)" "\\1" REV_DATE "${REV_DATE}")
132 message(STATUS "Version string: ${REV_VERSION}")
135 message(STATUS "Generating .ottdrev")
136 file(WRITE ${CMAKE_SOURCE_DIR}/.ottdrev "${REV_VERSION}\t${REV_ISODATE}\t${REV_MODIFIED}\t${REV_HASH}\t${REV_ISTAG}\t${REV_ISSTABLETAG}\n")
138 message(STATUS "Generating rev.cpp")
139 configure_file("${CMAKE_SOURCE_DIR}/src/rev.cpp.in"
140 "${FIND_VERSION_BINARY_DIR}/rev.cpp")
143 message(STATUS "Generating ottdres.rc")
144 configure_file("${CMAKE_SOURCE_DIR}/src/os/windows/ottdres.rc.in"
145 "${FIND_VERSION_BINARY_DIR}/ottdres.rc")
148 message(STATUS "Generating CPackProperties.cmake")
149 configure_file("${CMAKE_SOURCE_DIR}/CPackProperties.cmake.in"
150 "${CPACK_BINARY_DIR}/CPackProperties.cmake" @ONLY)
152 message(STATUS "Generating Doxyfile")
153 configure_file("${CMAKE_SOURCE_DIR}/Doxyfile.in"
154 "${CPACK_BINARY_DIR}/Doxyfile")
156 message(STATUS "Generating Doxyfile_AI")
157 configure_file("${CMAKE_SOURCE_DIR}/src/script/api/Doxyfile_AI.in"
158 "${CPACK_BINARY_DIR}/Doxyfile_AI")
160 message(STATUS "Generating Doxyfile_GS")
161 configure_file("${CMAKE_SOURCE_DIR}/src/script/api/Doxyfile_GS.in"
162 "${CPACK_BINARY_DIR}/Doxyfile_GS")