/
haiUtil.cmake
289 lines (254 loc) · 10 KB
/
haiUtil.cmake
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#
# SRT - Secure, Reliable, Transport
# Copyright (c) 2018 Haivision Systems Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
include(CheckCXXSourceCompiles)
# Useful for combinging paths
function(adddirname prefix lst out_lst)
set(output)
foreach(item ${lst})
list(APPEND output "${prefix}/${item}")
endforeach()
set(${out_lst} ${${out_lst}} ${output} PARENT_SCOPE)
endfunction()
# Splits a version formed as "major.minor.patch" recorded in variable 'prefix'
# and writes it into variables started with 'prefix' and ended with _MAJOR, _MINOR and _PATCH.
MACRO(set_version_variables prefix value)
string(REPLACE "." ";" VERSION_LIST ${value})
list(GET VERSION_LIST 0 ${prefix}_MAJOR)
list(GET VERSION_LIST 1 ${prefix}_MINOR)
list(GET VERSION_LIST 2 ${prefix}_PATCH)
set(${prefix}_DEFINESTR "")
ENDMACRO(set_version_variables)
# Sets given variable to 1, if the condition that follows it is satisfied.
# Otherwise set it to 0.
MACRO(set_if varname)
IF(${ARGN})
SET(${varname} 1)
ELSE(${ARGN})
SET(${varname} 0)
ENDIF(${ARGN})
ENDMACRO(set_if)
FUNCTION(join_arguments outvar)
set (output)
foreach (i ${ARGN})
set(output "${output} ${i}")
endforeach()
set (${outvar} ${output} PARENT_SCOPE)
ENDFUNCTION()
# LEGACY. PLEASE DON'T USE ANYMORE.
MACRO(MafRead maffile)
message(WARNING "MafRead is deprecated. Please use MafReadDir instead")
# ARGN contains the extra "section-variable" pairs
# If empty, return nothing
set (MAFREAD_TAGS
SOURCES # source files
PUBLIC_HEADERS # installable headers for include
PROTECTED_HEADERS # installable headers used by other headers
PRIVATE_HEADERS # non-installable headers
)
cmake_parse_arguments(MAFREAD_VAR "" "${MAFREAD_TAGS}" "" ${ARGN})
# Arguments for these tags are variables to be filled
# with the contents of particular section.
# While reading the file, extract the section.
# Section is recognized by either first uppercase character or space.
# @c http://cmake.org/pipermail/cmake/2007-May/014222.html
FILE(READ ${maffile} MAFREAD_CONTENTS)
STRING(REGEX REPLACE ";" "\\\\;" MAFREAD_CONTENTS "${MAFREAD_CONTENTS}")
STRING(REGEX REPLACE "\n" ";" MAFREAD_CONTENTS "${MAFREAD_CONTENTS}")
#message("DEBUG: MAF FILE CONTENTS: ${MAFREAD_CONTENTS}")
#message("DEBUG: PASSED VARIABLES:")
#foreach(DEBUG_VAR ${MAFREAD_TAGS})
# message("DEBUG: ${DEBUG_VAR}=${MAFREAD_VAR_${DEBUG_VAR}}")
#endforeach()
# The unnamed section becomes SOURCES
set (MAFREAD_VARIABLE ${MAFREAD_VAR_SOURCES})
set (MAFREAD_UNASSIGNED "")
FOREACH(MAFREAD_LINE ${MAFREAD_CONTENTS})
# Test what this line is
string(STRIP ${MAFREAD_LINE} MAFREAD_OLINE)
string(SUBSTRING ${MAFREAD_OLINE} 0 1 MAFREAD_FIRST)
#message("DEBUG: LINE='${MAFREAD_LINE}' FIRST='${MAFREAD_FIRST}'")
# The 'continue' command is cmake 3.2 - very late discovery
if (MAFREAD_FIRST STREQUAL "")
#message("DEBUG: ... skipped: empty")
elseif (MAFREAD_FIRST STREQUAL "#")
#message("DEBUG: ... skipped: comment")
else()
# Will be skipped if the line was a comment/empty
string(REGEX MATCH "[ A-Z]" MAFREAD_SECMARK ${MAFREAD_FIRST})
if (MAFREAD_SECMARK STREQUAL "")
# This isn't a section, it's a list element.
#message("DEBUG: ITEM: ${MAFREAD_OLINE} --> ${MAFREAD_VARIABLE}")
LIST(APPEND ${MAFREAD_VARIABLE} ${MAFREAD_OLINE})
else()
# It's a section - change the running variable
# Make it section name
STRING(REPLACE " " "_" MAFREAD_SECNAME ${MAFREAD_OLINE})
set(MAFREAD_VARIABLE ${MAFREAD_VAR_${MAFREAD_SECNAME}})
if (MAFREAD_VARIABLE STREQUAL "")
set(MAFREAD_VARIABLE MAFREAD_UNASSIGNED)
endif()
#message("DEBUG: NEW SECTION: '${MAFREAD_SECNAME}' --> VARIABLE: '${MAFREAD_VARIABLE}'")
endif()
endif()
ENDFOREACH()
# Final debug report
#set (ALL_VARS "")
#message("DEBUG: extracted variables:")
#foreach(DEBUG_VAR ${MAFREAD_TAGS})
# list(APPEND ALL_VARS ${MAFREAD_VAR_${DEBUG_VAR}})
#endforeach()
#list(REMOVE_DUPLICATES ALL_VARS)
#foreach(DEBUG_VAR ${ALL_VARS})
# message("DEBUG: --> ${DEBUG_VAR} = ${${DEBUG_VAR}}")
#endforeach()
ENDMACRO(MafRead)
# New version of MafRead macro, which automatically adds directory
# prefix. This should also resolve each relative path.
MACRO(MafReadDir directory maffile)
# ARGN contains the extra "section-variable" pairs
# If empty, return nothing
set (MAFREAD_TAGS
SOURCES # source files
PUBLIC_HEADERS # installable headers for include
PROTECTED_HEADERS # installable headers used by other headers
PRIVATE_HEADERS # non-installable headers
OPTIONS
)
cmake_parse_arguments(MAFREAD_VAR "" "${MAFREAD_TAGS}" "" ${ARGN})
# Arguments for these tags are variables to be filled
# with the contents of particular section.
# While reading the file, extract the section.
# Section is recognized by either first uppercase character or space.
# @c http://cmake.org/pipermail/cmake/2007-May/014222.html
FILE(READ ${directory}/${maffile} MAFREAD_CONTENTS)
STRING(REGEX REPLACE ";" "\\\\;" MAFREAD_CONTENTS "${MAFREAD_CONTENTS}")
STRING(REGEX REPLACE "\n" ";" MAFREAD_CONTENTS "${MAFREAD_CONTENTS}")
# Once correctly read, declare this file as dependency of the build file.
# Normally you should use cmake_configure_depends(), but this is
# available only since 3.0 version.
configure_file(${directory}/${maffile} dummy_${maffile}.cmake.out)
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/dummy_${maffile}.cmake.out)
#message("DEBUG: MAF FILE CONTENTS: ${MAFREAD_CONTENTS}")
#message("DEBUG: PASSED VARIABLES:")
#foreach(DEBUG_VAR ${MAFREAD_TAGS})
# message("DEBUG: ${DEBUG_VAR}=${MAFREAD_VAR_${DEBUG_VAR}}")
#endforeach()
# The unnamed section becomes SOURCES
set (MAFREAD_VARIABLE ${MAFREAD_VAR_SOURCES})
set (MAFREAD_UNASSIGNED "")
# Default section type. Another is 'flags'.
set (MAFREAD_SECTION_TYPE file)
FOREACH(MAFREAD_LINE ${MAFREAD_CONTENTS})
# Test what this line is
string(STRIP ${MAFREAD_LINE} MAFREAD_OLINE)
string(SUBSTRING ${MAFREAD_OLINE} 0 1 MAFREAD_FIRST)
#message("DEBUG: LINE='${MAFREAD_LINE}' FIRST='${MAFREAD_FIRST}'")
# The 'continue' command is cmake 3.2 - very late discovery
if (MAFREAD_FIRST STREQUAL "")
#message("DEBUG: ... skipped: empty")
elseif (MAFREAD_FIRST STREQUAL "#")
#message("DEBUG: ... skipped: comment")
else()
# Will be skipped if the line was a comment/empty
string(REGEX MATCH "[ A-Z-]" MAFREAD_SECMARK ${MAFREAD_FIRST})
if (MAFREAD_SECMARK STREQUAL "")
# This isn't a section, it's a list element.
#message("DEBUG: ITEM: ${MAFREAD_OLINE} --> ${MAFREAD_VARIABLE}")
if (${MAFREAD_SECTION_TYPE} STREQUAL file)
get_filename_component(MAFREAD_OLINE ${directory}/${MAFREAD_OLINE} ABSOLUTE)
endif()
LIST(APPEND ${MAFREAD_VARIABLE} ${MAFREAD_OLINE})
else()
# It's a section - change the running variable
# Make it section name
STRING(REPLACE " " "_" MAFREAD_SECNAME ${MAFREAD_OLINE})
# The cmake's version of 'if (MAFREAD_SECNAME[0] == '-')' - sigh...
string(SUBSTRING ${MAFREAD_SECNAME} 0 1 MAFREAD_SECNAME0)
if (${MAFREAD_SECNAME0} STREQUAL "-")
set (MAFREAD_SECTION_TYPE option)
string(SUBSTRING ${MAFREAD_SECNAME} 1 -1 MAFREAD_SECNAME)
else()
set (MAFREAD_SECTION_TYPE file)
endif()
set(MAFREAD_VARIABLE ${MAFREAD_VAR_${MAFREAD_SECNAME}})
if (MAFREAD_VARIABLE STREQUAL "")
set(MAFREAD_VARIABLE MAFREAD_UNASSIGNED)
endif()
#message("DEBUG: NEW SECTION: '${MAFREAD_SECNAME}' --> VARIABLE: '${MAFREAD_VARIABLE}'")
endif()
endif()
ENDFOREACH()
# Final debug report
#set (ALL_VARS "")
#message("DEBUG: extracted variables:")
#foreach(DEBUG_VAR ${MAFREAD_TAGS})
# list(APPEND ALL_VARS ${MAFREAD_VAR_${DEBUG_VAR}})
#endforeach()
#list(REMOVE_DUPLICATES ALL_VARS)
#foreach(DEBUG_VAR ${ALL_VARS})
# message("DEBUG: --> ${DEBUG_VAR} = ${${DEBUG_VAR}}")
#endforeach()
ENDMACRO(MafReadDir)
# NOTE: This is historical only. Not in use.
# It should be a similar interface to mafread.tcl like
# the above MafRead macro.
MACRO(GetMafHeaders directory outvar)
EXECUTE_PROCESS(
COMMAND ${CMAKE_MODULE_PATH}/mafread.tcl
${CMAKE_SOURCE_DIR}/${directory}/HEADERS.maf
"PUBLIC HEADERS"
"PROTECTED HEADERS"
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE ${outvar}
)
SEPARATE_ARGUMENTS(${outvar})
adddirname(${CMAKE_SOURCE_DIR}/${directory} "${${outvar}}" ${outvar})
ENDMACRO(GetMafHeaders)
function (getVarsWith _prefix _varResult)
get_cmake_property(_vars VARIABLES)
string (REGEX MATCHALL "(^|;)${_prefix}[A-Za-z0-9_]*" _matchedVars "${_vars}")
set (${_varResult} ${_matchedVars} PARENT_SCOPE)
endfunction()
function (check_testcode_compiles testcode libraries _successful)
set (save_required_libraries ${CMAKE_REQUIRED_LIBRARIES})
set (CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${libraries}")
check_cxx_source_compiles("${testcode}" ${_successful})
set (${_successful} ${${_successful}} PARENT_SCOPE)
set (CMAKE_REQUIRED_LIBRARIES ${save_required_libraries})
endfunction()
function (test_requires_clock_gettime _result)
# This function tests if clock_gettime can be used
# - at all
# - with or without librt
# Result will be:
# rt (if librt required)
# "" (if no extra libraries required)
# -- killed by FATAL_ERROR if clock_gettime is not available
set (code "
#include <time.h>
int main() {
timespec res\;
int result = clock_gettime(CLOCK_MONOTONIC, &res)\;
return result == 0\;
}
")
check_testcode_compiles(${code} "" HAVE_CLOCK_GETTIME_IN)
if (HAVE_CLOCK_GETTIME_IN)
message(STATUS "Checked clock_gettime(): no extra libs needed")
set (${_result} "" PARENT_SCOPE)
return()
endif()
check_testcode_compiles(${code} "rt" HAVE_CLOCK_GETTIME_LIBRT)
if (HAVE_CLOCK_GETTIME_LIBRT)
message(STATUS "Checked clock_gettime(): requires -lrt")
set (${_result} "-lrt" PARENT_SCOPE)
return()
endif()
message(FATAL_ERROR "clock_gettime() is not available on this system")
endfunction()