1 # 1. Extracts versioning information from the Git repository
2 # 2. Enables rerun of cmake configuration on each pull: GetGitRevisionDescription
3 # - ALIROOT_VERSION - branch/tag name or short hash if detached at randon hash
4 # - ALIROOT_REVISION - short sha1
5 # - ALIROOT_SERIAL - number of commits
6 # - ALIROOT_VERSION_RPM - name of the branch/tag in rpm format, - replaced with .
7 # - ALIROOT_GITREFTYPE - BRANCH/TAG/DETACHED
9 # Setting default values
10 set(ALIROOT_VERSION "")
11 set(ALIROOT_REVISION "")
13 set(ALIROOT_GITREFTYPE "")
15 # Checks if the sources where cloned as a full git repository
16 if(EXISTS ${AliRoot_SOURCE_DIR}/.git/)
17 # Git installation mandatory
18 find_package(Git REQUIRED)
20 # The simple include will not trigger the reconfiguration
21 # get_git_head_revision has to be called at least once
22 include(GetGitRevisionDescription)
23 # GIT_SHA1 - current long hash
25 # 1. branches: refs/heads/master
26 # 2. detached mode(tags or hashes) empty string
27 get_git_head_revision(GIT_REFSPEC GIT_SHA1)
30 message(STATUS "DEBUG: GIT_REFSPEC = \"${GIT_REFSPEC}\", GIT_SHA1 = \"${GIT_SHA1}\"")
33 # Setting ALIROOT_REVISION as the long hash
34 set(ALIROOT_REVISION ${GIT_SHA1})
36 # Generate the short version of the revision hash
37 execute_process(COMMAND git rev-parse --short ${GIT_SHA1}
38 WORKING_DIRECTORY ${AliRoot_SOURCE_DIR}
39 OUTPUT_VARIABLE GIT_SHORT_SHA1
40 RESULT_VARIABLE process_result
41 ERROR_VARIABLE process_error
42 OUTPUT_STRIP_TRAILING_WHITESPACE
43 ERROR_STRIP_TRAILING_WHITESPACE
45 # Set ALIROOT_REVISION to short hash if no error
46 if(process_result EQUAL 0)
48 message(STATUS "DEBUG: Short SHA1 = \"${GIT_SHORT_SHA1}\"")
51 set(ALIROOT_REVISION ${GIT_SHORT_SHA1})
54 message(STATUS "DEBUG: result = \"${process_result}\", parse-rev error : ${ERROR_VARIABLE}")
57 message(WARNING "Could not retrieve the short hash, using the long version : \"${ALIROOT_REVISION}\"")
60 # Generate ALIROOT_VERSION
61 # 1. Branch -> Branch name
63 # 2.1 Tags -> Tag name
64 # 2.2 Detached hash -> Short hash
66 # Check if dettached mode
67 # rev-parse will return:
68 # 1. Branch -> Branch name, ex: master
69 # 2. Detached mode: "HEAD" for both tags and random hashes
70 execute_process(COMMAND git rev-parse --abbrev-ref HEAD
71 WORKING_DIRECTORY ${AliRoot_SOURCE_DIR}
72 OUTPUT_VARIABLE ref_output
73 RESULT_VARIABLE ref_result
74 ERROR_VARIABLE ref_error
75 OUTPUT_STRIP_TRAILING_WHITESPACE
76 ERROR_STRIP_TRAILING_WHITESPACE
79 if(ref_result EQUAL 0)
81 message(STATUS "DEBUG: rev-parse HEAD result = \"${ref_output}\"")
85 if(ref_output STREQUAL "HEAD")
86 # Checking if this is a tag in detached mode
87 # 1. If tag the OUTPUT_VARIABLE will contain the tag name
88 # 2. If random hash the RESULT_VARIABLE is 128 and ERROR_VARIABLE contains the error message
89 execute_process(COMMAND git describe --exact-match
90 WORKING_DIRECTORY ${AliRoot_SOURCE_DIR}
91 OUTPUT_VARIABLE tag_output
92 RESULT_VARIABLE tag_result
93 ERROR_VARIABLE tag_error
94 OUTPUT_STRIP_TRAILING_WHITESPACE
95 ERROR_STRIP_TRAILING_WHITESPACE
98 if(tag_result EQUAL 0)
101 message(STATUS "DEBUG: git describe tag_result = ${tag_output}")
104 set(ALIROOT_VERSION ${tag_output})
105 set(ALIROOT_GITREFTYPE "TAG")
107 # Detached at a random hash, the version is the short SHA1
109 message(STATUS "DEBUG: git describe tar_error = ${tag_error}")
112 set(ALIROOT_VERSION ${ALIROOT_REVISION})
113 set(ALIROOT_GITREFTYPE "DETACHED")
117 set(ALIROOT_VERSION ${ref_output})
118 set(ALIROOT_GITREFTYPE "BRANCH")
120 else(ref_result EQUAL 0)
121 message(FATAL_ERROR "Could not retreive information about the current git hash: ${ref_error}")
122 endif(ref_result EQUAL 0)
124 # Generating the ALIROOT_SERIAL using git rev-list
125 # Older Git version < 1.7.3 do not have --count option for rev-list
126 # We use simple rev-list and count the lines of the output
128 # extract major minor and patch from Git version
129 string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.[0-9]+" "\\1" GIT_VERSION_MAJOR "${GIT_VERSION_STRING}")
130 string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9]+" "\\1" GIT_VERSION_MINOR "${GIT_VERSION_STRING}")
131 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" GIT_VERSION_PATCH "${GIT_VERSION_STRING}")
133 if(${GIT_VERSION_MAJOR} EQUAL 1 AND ${GIT_VERSION_MINOR} LESS 3)
135 message(STATUS "DEBUG: cmake version less that 1.7.3!")
138 execute_process(COMMAND git rev-list ${GIT_SHA1}
140 WORKING_DIRECTORY ${AliRoot_SOURCE_DIR}
141 RESULT_VARIABLE serial_result
142 ERROR_VARIABLE serial_error
143 OUTPUT_VARIABLE serial_output
144 OUTPUT_STRIP_TRAILING_WHITESPACE
145 ERROR_STRIP_TRAILING_WHITESPACE
148 execute_process(COMMAND git rev-list --count ${GIT_SHA1}
149 WORKING_DIRECTORY ${AliRoot_SOURCE_DIR}
150 RESULT_VARIABLE serial_result
151 ERROR_VARIABLE serial_error
152 OUTPUT_VARIABLE serial_output
153 OUTPUT_STRIP_TRAILING_WHITESPACE
154 ERROR_STRIP_TRAILING_WHITESPACE
159 if(serial_result EQUAL 0)
161 message(STATUS "DEBUG: AliRoot serial: ${serial_output}")
164 set(ALIROOT_SERIAL ${serial_output})
166 message(FATAL_ERROR "Could not retrieve serial number: ${serial_error}")
169 if(${ALIROOT_GITREFTYPE} STREQUAL "DETACHED")
170 message(STATUS "Found AliRoot in detached mode, hash \"${ALIROOT_REVISION}\", serial \"${ALIROOT_SERIAL}\"")
171 elseif(${ALIROOT_GITREFTYPE} STREQUAL "BRANCH")
172 message(STATUS "Found AliRoot branch \"${ALIROOT_VERSION}\", hash \"${ALIROOT_REVISION}\", serial \"${ALIROOT_SERIAL}\"")
173 elseif(${ALIROOT_GITREFTYPE} STREQUAL "TAG")
174 message(STATUS "Found AliRoot tag \"${ALIROOT_VERSION}\", hash \"${ALIROOT_REVISION}\", serial \"${ALIROOT_SERIAL}\"")
176 # it does not get here
177 message(FATAL_ERROR "Git type error")
179 else(EXISTS ${AliRoot_SOURCE_DIR}/.git/)
180 message(WARNING "AliRoot sources not downloaded from a Version Control System. I can't tell which revision you are using!")
181 endif(EXISTS ${AliRoot_SOURCE_DIR}/.git/)
183 # ALIROOT_VERSION_RPM
184 # Replacing -/ with . , normally it should not contain /
185 # - and / are forbidden characters in rpm creation
186 string(REPLACE "-" "." ALIROOT_VERSION_RPM ${ALIROOT_VERSION})
187 string(REPLACE "/" "." ALIROOT_VERSION_RPM ${ALIROOT_VERSION_RPM})
189 message(STATUS "DEBUG: ALIROOT_VERSION_RPM = ${ALIROOT_VERSION_RPM}")
192 # Generating ARVersion.h from ARVersion.h.tmp
193 set(ALIROOT_AR_VERSION ${ALIROOT_VERSION})
194 if(${ALIROOT_GITREFTYPE} STREQUAL "BRANCH")
195 set(ALIROOT_AR_REVISION "")
196 set(ALIROOT_AR_SERIAL 0)
198 set(ALIROOT_AR_REVISION ${ALIROOT_REVISION})
199 set(ALIROOT_AR_SERIAL ${ALIROOT_SERIAL})
201 configure_file(${PROJECT_SOURCE_DIR}/cmake/ARVersion.h.tmp ${CMAKE_BINARY_DIR}/version/ARVersion.h @ONLY)
202 install(FILES ${PROJECT_BINARY_DIR}/version/ARVersion.h DESTINATION include)