Update: Translations from eints
[openttd-github.git] / cmake / scripts / FindVersion.cmake
blob5d53d89db5506d2ac9264b424e1dd83d1c1f8166
1 cmake_minimum_required(VERSION 3.5)
3 if(NOT REV_MAJOR)
4     set(REV_MAJOR 0)
5 endif()
6 if(NOT REV_MINOR)
7     set(REV_MINOR 0)
8 endif()
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}")
19     set(ENV{LC_ALL} C)
21     # Assume the dir is not modified
22     set(REV_MODIFIED 0)
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}
27                     OUTPUT_QUIET
28     )
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}
35     )
36     if(NOT IS_MODIFIED STREQUAL "")
37         set(REV_MODIFIED 2)
38     endif()
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}
45                     ERROR_QUIET
46     )
47     set(REV_HASH "${FULLHASH}")
49     string(SUBSTRING "${FULLHASH}" 0 10 SHORTHASH)
51     # Get the last commit date
52     set(ENV{TZ} "UTC0")
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}
57     )
58     string(REGEX REPLACE "([0-9]+)-([0-9]+)-([0-9]+).*" "\\1\\2\\3" COMMITDATE "${COMMITDATE}")
59     set(REV_ISODATE "${COMMITDATE}")
61     # Get the branch
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}
66                     ERROR_QUIET
67     )
68     string(REGEX REPLACE ".*/" "" BRANCH "${BRANCH}")
70     # Get the tag
71     execute_process(COMMAND ${GIT_EXECUTABLE} name-rev --name-only --tags --no-undefined HEAD
72                     OUTPUT_VARIABLE TAG
73                     OUTPUT_STRIP_TRAILING_WHITESPACE
74                     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
75                     ERROR_QUIET
76     )
77     string(REGEX REPLACE "\^0$" "" TAG "${TAG}")
79     if(REV_MODIFIED EQUAL 0)
80         set(HASHPREFIX "-g")
81     elseif(REV_MODIFIED EQUAL 2)
82         set(HASHPREFIX "-m")
83     else()
84         set(HASHPREFIX "-u")
85     endif()
87     # Set the version string
88     if(NOT TAG STREQUAL "")
89         set(REV_VERSION "${TAG}")
90         set(REV_ISTAG 1)
92         string(REGEX REPLACE "^[0-9.]+$" "" STABLETAG "${TAG}")
93         if(STABLETAG STREQUAL "")
94             set(REV_ISSTABLETAG 1)
95         else()
96             set(REV_ISSTABLETAG 0)
97         endif()
98     else()
99         set(REV_VERSION "${REV_ISODATE}-${BRANCH}${HASHPREFIX}${SHORTHASH}")
100         set(REV_ISTAG 0)
101         set(REV_ISSTABLETAG 0)
102     endif()
104     # Restore LC_ALL
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)
116 else()
117     message(WARNING "No version detected; this build will NOT be network compatible")
118     set(REV_VERSION "norev0000")
119     set(REV_ISODATE "19700101")
120     set(REV_MODIFIED 1)
121     set(REV_HASH "unknown")
122     set(REV_ISTAG 0)
123     set(REV_ISSTABLETAG 0)
124 endif()
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}")
134 if(GENERATE_OTTDREV)
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")
137 else()
138     message(STATUS "Generating rev.cpp")
139     configure_file("${CMAKE_SOURCE_DIR}/src/rev.cpp.in"
140             "${FIND_VERSION_BINARY_DIR}/rev.cpp")
142     if(WINDOWS)
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")
146     endif()
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")
163 endif()