]> git.uio.no Git - u/mrichter/AliRoot.git/blob - cmake/CheckGitVersion.cmake
CMake: Retrieve Git information
[u/mrichter/AliRoot.git] / cmake / CheckGitVersion.cmake
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
8
9 # Setting default values
10 set(ALIROOT_VERSION "")
11 set(ALIROOT_REVISION "")
12 set(ALIROOT_SERIAL 0)
13 set(ALIROOT_GITREFTYPE "")
14
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)
19
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
24   # GIT_REFSPEC
25   #     1. branches: refs/heads/master
26   #     2. detached mode(tags or hashes) empty string
27   get_git_head_revision(GIT_REFSPEC GIT_SHA1)
28
29   if(CMAKEDEBUG)
30     message(STATUS "DEBUG: GIT_REFSPEC = \"${GIT_REFSPEC}\", GIT_SHA1 = \"${GIT_SHA1}\"")
31   endif(CMAKEDEBUG)
32
33   # Setting ALIROOT_REVISION as the long hash
34   set(ALIROOT_REVISION ${GIT_SHA1})
35
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
44                 )
45   # Set ALIROOT_REVISION to short hash if no error
46   if(process_result EQUAL 0)
47     if(CMAKEDEBUG)
48       message(STATUS "DEBUG: Short SHA1 = \"${GIT_SHORT_SHA1}\"")
49     endif(CMAKEDEBUG)
50
51     set(ALIROOT_REVISION ${GIT_SHORT_SHA1})
52   else()
53     if(CMAKEDEBUG)
54       message(STATUS "DEBUG: result = \"${process_result}\",  parse-rev error : ${ERROR_VARIABLE}")
55     endif()
56
57     message(WARNING "Could not retrieve the short hash, using the long version : \"${ALIROOT_REVISION}\"")
58   endif()
59
60   # Generate ALIROOT_VERSION
61   # 1. Branch -> Branch name
62   # 2. Detached mode
63   #    2.1 Tags -> Tag name
64   #    2.2 Detached hash -> Short hash
65   
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
77                 )
78
79   if(ref_result EQUAL 0)
80     if(CMAKEDEBUG)
81       message(STATUS "DEBUG: rev-parse HEAD result = \"${ref_output}\"")
82     endif()
83
84     # detached mode
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
96                     )
97
98       if(tag_result EQUAL 0)
99       
100         if(CMAKEDEBUG)
101           message(STATUS "DEBUG: git describe tag_result = ${tag_output}")
102         endif()
103
104         set(ALIROOT_VERSION ${tag_output})
105         set(ALIROOT_GITREFTYPE "TAG")
106       else()
107         # Detached at a random hash, the version is the short SHA1
108         if(CMAKEDEBUG)
109           message(STATUS "DEBUG: git describe tar_error = ${tag_error}")
110         endif()  
111
112         set(ALIROOT_VERSION ${ALIROOT_REVISION})
113         set(ALIROOT_GITREFTYPE "DETACHED")
114       endif()
115     else()
116       # Branch
117       set(ALIROOT_VERSION ${ref_output})
118       set(ALIROOT_GITREFTYPE "BRANCH")
119     endif()
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)
123   
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 
127   
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}")
132
133   if(${GIT_VERSION_MAJOR} EQUAL 1 AND ${GIT_VERSION_MINOR} LESS 3)
134     if(CMAKEDEBUG)
135       message(STATUS "DEBUG: cmake version less that 1.7.3!")
136     endif()
137     
138     execute_process(COMMAND git rev-list ${GIT_SHA1}
139                     COMMAND wc -l
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
146                   )
147   else()
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
155       )
156
157   endif()
158
159   if(serial_result EQUAL 0)
160     if(CMAKEDEBUG)
161       message(STATUS "DEBUG: AliRoot serial: ${serial_output}")
162     endif()
163     
164     set(ALIROOT_SERIAL ${serial_output})
165   else()
166     message(FATAL_ERROR "Could not retrieve serial number: ${serial_error}")
167   endif()
168
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}\"")
175   else()
176     # it does not get here
177     message(FATAL_ERROR "Git type error")
178   endif()
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/)
182
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})
188 if(CMAKEDEBUG)
189   message(STATUS "DEBUG: ALIROOT_VERSION_RPM = ${ALIROOT_VERSION_RPM}")
190 endif()
191
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)
197 else()
198   set(ALIROOT_AR_REVISION ${ALIROOT_REVISION})
199   set(ALIROOT_AR_SERIAL ${ALIROOT_SERIAL})
200 endif()
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)