File: Util.cmake

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (271 lines) | stat: -rwxr-xr-x 8,826 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# This file is part of the Spring engine (GPL v2 or later), see LICENSE.html

#
# Spring CMake utilities
# ----------------------
#
# Variables set in this file:
# * PATH_SEP_H
# * PATH_SEP_T
# * PATH_DELIM_H
# * PATH_DELIM_T
# * ABS_DIR_REGEX_H
# * ABS_DIR_REGEX_T
# * PIC_FLAG
#
# Functions and macros defined in this file:
# * FixLibName
# * CreateInstallTarget
# * RemoveString
# * RemoveFlag
# * SetGlobal
# * MakeGlobal
# * GetListOfSubModules
# * GetVersionFromFile
# * GetLastPathPart
# * MakeAbsolute
# * GetVersionPlusDepFile
# * GetNativeSourcesRecursive
# * CheckMinCMakeVersion
#


If   (CMAKE_HOST_WIN32)
	Set(PATH_SEP_H      "\\")
	Set(PATH_DELIM_H    ";")
	Set(ABS_DIR_REGEX_H "^[a-zA-Z]:\\")
Else (CMAKE_HOST_WIN32)
	Set(PATH_SEP_H      "/")
	Set(PATH_DELIM_H    ":")
	Set(ABS_DIR_REGEX_H "^/")
EndIf (CMAKE_HOST_WIN32)
If    (WIN32)
	Set(PATH_SEP_T      "\\")
	Set(PATH_DELIM_T    ";")
	Set(ABS_DIR_REGEX_T "^[a-zA-Z]:\\")
Else  (WIN32)
	Set(PATH_SEP_T      "/")
	Set(PATH_DELIM_T    ":")
	Set(ABS_DIR_REGEX_T "^/")
EndIf (WIN32)


# define the fPic compiler flag
If     (APPLE)
	Set(PIC_FLAG "-fPIC")
ElseIf (MINGW)
	Set(PIC_FLAG "")
Else   ()
	Set(PIC_FLAG "-fpic")
EndIf  ()


# This is needed because CMake, or at least some versions of it (eg. 2.8),
# falsely use the ".so" suffix under Mac OS X for MODULE's
Macro    (FixLibName targetName)
	If    (UNIX)
		Set_TARGET_PROPERTIES(${targetName} PROPERTIES PREFIX "lib")
		If    (APPLE)
			Set_TARGET_PROPERTIES(${targetName} PROPERTIES SUFFIX ".dylib")
		EndIf (APPLE)
	EndIf (UNIX)
EndMacro (FixLibName targetName)


# Create an install target which installs multiple sub-projects.
# Sub-projects have to be specified by paths relative to CMAKE_SOURCE_DIR.
# All install instructions in the specified dirs (recursively) are executed.
# example:
# 	Set(myInstallDirs
# 			"rts/builds/default"
# 			"tools/unitsync"
# 			"cont"
# 			"AI"
# 		)
# 	Set(myInstallDeps
# 			spring
# 			gamedata
# 			unitsync
# 			C-AIInterface
# 			NullAI
# 			KAIK
# 			ArchiveMover
# 		)
# 	CreateInstallTarget(myPkg myInstallDeps myInstallDirs)
# This creates a new target "install-myPkg"
Macro    (CreateInstallTarget targetName var_list_depends var_list_instDirs)
	# Assemble the list of commands
	Set(installCmds "")
	ForEach    (instDir ${${var_list_instDirs}})
		If    (NOT EXISTS "${CMAKE_SOURCE_DIR}/${instDir}/CMakeLists.txt")
			Message(FATAL_ERROR "Not a valid dir for installer target: ${instDir}, \"${CMAKE_SOURCE_DIR}/${instDir}/CMakeLists.txt\" does not exist.")
		EndIf (NOT EXISTS "${CMAKE_SOURCE_DIR}/${instDir}/CMakeLists.txt")
		Set(installCmds ${installCmds} 
			COMMAND "${CMAKE_COMMAND}"
				"-P" "${CMAKE_BINARY_DIR}/${instDir}/cmake_install.cmake"
				# NOTE: The following does not work in CMake 2.6.4
				#"-DCMAKE_INSTALL_COMPONENT=${targetName}"
			)
	EndForEach (instDir)

	# Make sure we do have commands at all
	If    ("${installCmds}" STREQUAL "")
		Message(FATAL_ERROR "No valid install dirs supplied.")
	EndIf ("${installCmds}" STREQUAL "")

	# Create a custom install target
	Add_Custom_Target(install-${targetName}
		${installCmds}
		WORKING_DIRECTORY
			"${CMAKE_BINARY_DIR}"
		COMMENT
			"  ${targetName}: Installing ..." VERBATIM
		)
	# This also works for custom targets
	Add_Dependencies(install-${targetName} ${${var_list_depends}})
EndMacro (CreateInstallTarget targetName)


# Removes a given string from a variable
Macro    (RemoveString var str)
	String(REPLACE "${str}" "" ${var} "${${var}}")
EndMacro (RemoveString)


# Removes a compiler flag from all commonly used vars used for that purpose
Macro    (RemoveFlag flag)
	RemoveString(CMAKE_CXX_FLAGS "${flag}")
	RemoveString(CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE} "${flag}")
	RemoveString(CMAKE_C_FLAGS "${flag}")
	RemoveString(CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE} "${flag}")
	RemoveString(CMAKE_EXE_LINKER_FLAGS "${flag}")
	RemoveString(CMAKE_MODULE_LINKER_FLAGS "${flag}")
EndMacro (RemoveFlag)


# Sets a variable in global scope
Function    (SetGlobal var value)
	Set(${var} "${value}" CACHE INTERNAL "" FORCE)
	Mark_As_Advanced(${var})
EndFunction (SetGlobal)


# Makes variables available in global scope
# MakeGlobal(var0 [var1 [var2 [var3 ...]]])
Function    (MakeGlobal)
	ForEach    (var ${ARGV})
		SetGlobal(${var} "${${var}}")
	EndForEach (var)
EndFunction (MakeGlobal)


# Find all CMakeLists.txt files in sub-directories
Macro    (GetListOfSubModules list_var)
	File(GLOB ${list_var} RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" FOLLOW_SYMLINKS "${CMAKE_CURRENT_SOURCE_DIR}/*/CMakeLists.txt")

	# Strip away the "/CMakeLists.txt" parts, so we end up with just a list of dirs,
	# for example: AAI;RAI;KAIK
	String(REPLACE "//CMakeLists.txt" "" ${list_var} "${${list_var}}")
EndMacro (GetListOfSubModules list_var)


# Gets the version from a text file.
# (actually just reads the text file content into a variable)
Macro    (GetVersionFromFile vers_var vers_file)
	If    (EXISTS ${vers_file})
		File(STRINGS "${vers_file}" ${vers_var} LIMIT_COUNT 1)
	Else  (EXISTS ${vers_file})
		Set(${vers_var} "UNKNOWN_VERSION")
	EndIf (EXISTS ${vers_file})
EndMacro (GetVersionFromFile vers_var vers_file)


# Returns the name of the dir or file specified by a path.
# example: "/A/B/C" -> "C"
Macro    (GetLastPathPart part_var dir)
	String(REGEX REPLACE ".*[\\/]" "" ${part_var} ${dir})
EndMacro (GetLastPathPart part_var dir)


# Create an absolute directory from a base- and a relative-dir
Function    (MakeAbsolute absDir_var baseDir relDir)
	Set(_absDir "${baseDir}")
	If    (NOT "${relDir}" STREQUAL "")
		Set(_absDir "${_absDir}/${relDir}")
	EndIf (NOT "${relDir}" STREQUAL "")
	Set(${absDir_var} ${_absDir} PARENT_SCOPE)
EndFunction (MakeAbsolute)


# Gets the version from a text file (${CMAKE_CURRENT_SOURCE_DIR}/VERSION),
# and prepare a file for dependency tracking.
# The project will reconfigure whenever the VERSION file gets touched.
Macro    (GetVersionPlusDepFile vers_var versDepFile_var)
	Set(myVersionFile    "${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
	Set(myVersionDepFile "${CMAKE_CURRENT_BINARY_DIR}/VERSION")
	If    (EXISTS ${myVersionFile})
		GetVersionFromFile(${vers_var} "${myVersionFile}")
		Configure_File("${myVersionFile}" "${myVersionDepFile}" COPYONLY)
		Set_source_files_properties("${myVersionDepFile}" PROPERTIES HEADER_FILE_ONLY TRUE)
		Set_source_files_properties("${myVersionDepFile}" PROPERTIES GENERATED TRUE)
		Set(${versDepFile_var} "${myVersionDepFile}")
	Else  (EXISTS ${myVersionFile})
		Set(${vers_var}        "UNKNOWN_VERSION")
		Set(${versDepFile_var} "${myVersionFile}")
	EndIf (EXISTS ${myVersionFile})
EndMacro (GetVersionPlusDepFile vers_var versDepFile_var)


# Recursively lists all native source files in a given directory,
# relative to _relDir, or absolut, If _relDir == "".
Macro    (GetNativeSourcesRecursive _var _dir _relDir)
	Set(NATIVE_SOURCE_EXTENSIONS ".c;.cpp;.c++;.cxx")
	ForEach    (_ext ${NATIVE_SOURCE_EXTENSIONS})
		# Recursively get sources for source extension _ext
		If    ("${_relDir}" STREQUAL "")
			File(GLOB_RECURSE _sources FOLLOW_SYMLINKS "${_dir}/*${_ext}")
		Else  ("${_relDir}" STREQUAL "")
			File(GLOB_RECURSE _sources RELATIVE "${_relDir}" FOLLOW_SYMLINKS "${_dir}/*${_ext}")
		EndIf ("${_relDir}" STREQUAL "")
		# Concatenate to previous results
		If    ("${_sources}" STREQUAL "" OR "${${_var}}" STREQUAL "")
			Set(${_var} "${${_var}}${_sources}")
		Else  ("${_sources}" STREQUAL "" OR "${${_var}}" STREQUAL "")
			Set(${_var} "${${_var}};${_sources}")
		EndIf ("${_sources}" STREQUAL "" OR "${${_var}}" STREQUAL "")
	EndForEach (_ext)
EndMacro (GetNativeSourcesRecursive _var _dir _relDir)


# Check If the CMake version used is >= "major.minor.patch".
Macro    (CheckMinCMakeVersion res_var major minor patch)
	Set(${res_var} FALSE)
	If     (${CMAKE_MAJOR_VERSION} GREATER ${major})
		Set(${res_var} TRUE)
	ElseIf (${CMAKE_MAJOR_VERSION} EQUAL ${major})
		If     (${CMAKE_MINOR_VERSION} GREATER ${minor})
			Set(${res_var} TRUE)
		ElseIf (${CMAKE_MINOR_VERSION} EQUAL ${minor})
			If     (${CMAKE_PATCH_VERSION} GREATER ${patch})
				Set(${res_var} TRUE)
			ElseIf (${CMAKE_PATCH_VERSION} EQUAL ${patch})
				Set(${res_var} TRUE)
			EndIf  ()
		EndIf  ()
	EndIf  ()
EndMacro (CheckMinCMakeVersion res_var major minor patch)


# Tries to capture a specific regex group from a string.
# The regex has to match the whole string.
# @param pattern the regex to match to
# @param group starts at 1
# @param var to write the result to, "" in case of no match
# @param str the string that will be tried to be match
Macro    (CatchRegexGroup pattern group var str)
	String(REGEX MATCH "^${pattern}\$" "${var}_MATCH_TEST" "${str}")
	Set(${var} "")
	If     ("${${var}_MATCH_TEST}" STREQUAL "${str}")
		String(REGEX REPLACE "${pattern}" "\\${group}" ${var} "${str}")
	EndIf  ()
EndMacro (CatchRegexGroup)