Welcome to mirror list, hosted at ThFree Co, Russian Federation.

buildinfo.cmake « cmake « build_files - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f0a283ba0e0d1e91661c88ee5b3234671b846a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# This is called by cmake as an external process from
# ./source/creator/CMakeLists.txt to write ./source/creator/buildinfo.h
# Caller must define:
#   SOURCE_DIR
# Optional overrides:
#   BUILD_DATE
#   BUILD_TIME

# Extract working copy information for SOURCE_DIR into MY_XXX variables
# with a default in case anything fails, for example when using git-svn
set(MY_WC_HASH "unknown")
set(MY_WC_BRANCH "unknown")
set(MY_WC_COMMIT_TIMESTAMP 0)

# Guess if this is a git working copy and then look up the revision
if(EXISTS ${SOURCE_DIR}/.git)
	execute_process(COMMAND git rev-parse --abbrev-ref HEAD
	                WORKING_DIRECTORY ${SOURCE_DIR}
	                OUTPUT_VARIABLE MY_WC_BRANCH
	                OUTPUT_STRIP_TRAILING_WHITESPACE)

	if(MY_WC_BRANCH STREQUAL "HEAD")
		# Detached HEAD, check whether commit hash is reachable
		# in the master branch
		execute_process(COMMAND git rev-parse --short HEAD
		                WORKING_DIRECTORY ${SOURCE_DIR}
		                OUTPUT_VARIABLE MY_WC_HASH
		                OUTPUT_STRIP_TRAILING_WHITESPACE)

		execute_process(COMMAND git branch --list master blender-v* --contains ${MY_WC_HASH}
		                WORKING_DIRECTORY ${SOURCE_DIR}
		                OUTPUT_VARIABLE _git_contains_check
		                OUTPUT_STRIP_TRAILING_WHITESPACE)

		if(NOT _git_contains_check STREQUAL "")
			set(MY_WC_BRANCH "master")
		else()
			execute_process(COMMAND git show-ref --tags -d
			                WORKING_DIRECTORY ${SOURCE_DIR}
			                OUTPUT_VARIABLE _git_tag_hashes
			                OUTPUT_STRIP_TRAILING_WHITESPACE)

			execute_process(COMMAND git rev-parse HEAD
			                WORKING_DIRECTORY ${SOURCE_DIR}
			                OUTPUT_VARIABLE _git_head_hash
			                OUTPUT_STRIP_TRAILING_WHITESPACE)

			if(_git_tag_hashes MATCHES "${_git_head_hash}")
				set(MY_WC_BRANCH "master")
			else()
				execute_process(COMMAND git branch --contains ${MY_WC_HASH}
				                WORKING_DIRECTORY ${SOURCE_DIR}
				                OUTPUT_VARIABLE _git_contains_branches
				                OUTPUT_STRIP_TRAILING_WHITESPACE)
				string(REGEX REPLACE "^\\*[ \t]+" "" _git_contains_branches "${_git_contains_branches}")
				string(REGEX REPLACE "[\r\n]+" ";" _git_contains_branches "${_git_contains_branches}")
				string(REGEX REPLACE ";[ \t]+" ";" _git_contains_branches "${_git_contains_branches}")
				foreach(_branch ${_git_contains_branches})
					if (NOT "${_branch}" MATCHES "\\(HEAD.*")
						set(MY_WC_BRANCH "${_branch}")
						break()
					endif()
				endforeach()
				unset(_branch)
				unset(_git_contains_branches)
			endif()

			unset(_git_tag_hashes)
			unset(_git_head_hashs)
		endif()


		unset(_git_contains_check)
	else()
		execute_process(COMMAND git log HEAD..@{u}
		                WORKING_DIRECTORY ${SOURCE_DIR}
		                OUTPUT_VARIABLE _git_below_check
		                OUTPUT_STRIP_TRAILING_WHITESPACE
		                ERROR_QUIET)
		if(NOT _git_below_check STREQUAL "")
			# If there're commits between HEAD and upstream this means
			# that we're reset-ed to older revision. Use it's hash then.
			execute_process(COMMAND git rev-parse --short HEAD
			                WORKING_DIRECTORY ${SOURCE_DIR}
			                OUTPUT_VARIABLE MY_WC_HASH
			                OUTPUT_STRIP_TRAILING_WHITESPACE)
		else()
			execute_process(COMMAND git rev-parse --short @{u}
			                WORKING_DIRECTORY ${SOURCE_DIR}
			                OUTPUT_VARIABLE MY_WC_HASH
			                OUTPUT_STRIP_TRAILING_WHITESPACE
			                ERROR_QUIET)

			if(MY_WC_HASH STREQUAL "")
				# Local branch, not set to upstream.
				# Well, let's use HEAD for now
				execute_process(COMMAND git rev-parse --short HEAD
				                WORKING_DIRECTORY ${SOURCE_DIR}
				                OUTPUT_VARIABLE MY_WC_HASH
				                OUTPUT_STRIP_TRAILING_WHITESPACE)
			endif()
		endif()

		if(MY_WC_BRANCH MATCHES "^blender-v")
			set(MY_WC_BRANCH "master")
		endif()

		unset(_git_below_check)
	endif()

	execute_process(COMMAND git log -1 --format=%ct
	                WORKING_DIRECTORY ${SOURCE_DIR}
	                OUTPUT_VARIABLE MY_WC_COMMIT_TIMESTAMP
	                OUTPUT_STRIP_TRAILING_WHITESPACE)
	# May fail in rare cases
	if(MY_WC_COMMIT_TIMESTAMP STREQUAL "")
		set(MY_WC_COMMIT_TIMESTAMP 0)
	endif()

	# Update GIT index before getting dirty files
	execute_process(COMMAND git update-index -q --refresh
	                WORKING_DIRECTORY ${SOURCE_DIR}
	                OUTPUT_STRIP_TRAILING_WHITESPACE)

	execute_process(COMMAND git diff-index --name-only HEAD --
	                WORKING_DIRECTORY ${SOURCE_DIR}
	                OUTPUT_VARIABLE _git_changed_files
	                OUTPUT_STRIP_TRAILING_WHITESPACE)

	if(NOT _git_changed_files STREQUAL "")
		set(MY_WC_BRANCH "${MY_WC_BRANCH} (modified)")
	else()
		# Unpushed commits are also considered local modifications
		execute_process(COMMAND git log @{u}..
		                WORKING_DIRECTORY ${SOURCE_DIR}
		                OUTPUT_VARIABLE _git_unpushed_log
		                OUTPUT_STRIP_TRAILING_WHITESPACE
		                ERROR_QUIET)
		if(NOT _git_unpushed_log STREQUAL "")
			set(MY_WC_BRANCH "${MY_WC_BRANCH} (modified)")
		endif()
		unset(_git_unpushed_log)
	endif()

	unset(_git_changed_files)
endif()

# BUILD_PLATFORM and BUILD_PLATFORM are taken from CMake
# but BUILD_DATE and BUILD_TIME are platform dependent
if(UNIX)
	if(NOT BUILD_DATE)
		execute_process(COMMAND date "+%Y-%m-%d" OUTPUT_VARIABLE BUILD_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
	endif()
	if(NOT BUILD_TIME)
		execute_process(COMMAND date "+%H:%M:%S" OUTPUT_VARIABLE BUILD_TIME OUTPUT_STRIP_TRAILING_WHITESPACE)
	endif()
elseif(WIN32)
	if(NOT BUILD_DATE)
		execute_process(COMMAND cmd /c date /t OUTPUT_VARIABLE BUILD_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
	endif()
	if(NOT BUILD_TIME)
		execute_process(COMMAND cmd /c time /t OUTPUT_VARIABLE BUILD_TIME OUTPUT_STRIP_TRAILING_WHITESPACE)
	endif()
endif()

# Write a file with the BUILD_HASH define
file(WRITE buildinfo.h.txt
	"#define BUILD_HASH \"${MY_WC_HASH}\"\n"
	"#define BUILD_COMMIT_TIMESTAMP ${MY_WC_COMMIT_TIMESTAMP}\n"
	"#define BUILD_BRANCH \"${MY_WC_BRANCH}\"\n"
	"#define BUILD_DATE \"${BUILD_DATE}\"\n"
	"#define BUILD_TIME \"${BUILD_TIME}\"\n"
)

# cleanup
unset(MY_WC_HASH)
unset(MY_WC_COMMIT_TIMESTAMP)
unset(MY_WC_BRANCH)
unset(BUILD_DATE)
unset(BUILD_TIME)


# Copy the file to the final header only if the version changes
# and avoid needless rebuilds
# TODO: verify this comment is true, as BUILD_TIME probably changes
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
                        buildinfo.h.txt buildinfo.h)