File: CUnit.cmake

package info (click to toggle)
cyclonedds 0.10.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,372 kB
  • sloc: ansic: 224,361; perl: 1,904; xml: 1,894; yacc: 1,018; sh: 882; python: 106; makefile: 94
file content (321 lines) | stat: -rw-r--r-- 11,630 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#
# Copyright(c) 2006 to 2021 ZettaScale Technology and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
# v. 1.0 which is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#

set(CUNIT_DIR "${CMAKE_CURRENT_LIST_DIR}/CUnit")


function(get_cunit_header_file SOURCE_FILE HEADER_FILE)
  # Return a unique (but consistent) filename where Theory macros can be
  # written. The path that will be returned is the location to a header file
  # located in the same relative directory, using the basename of the source
  # file postfixed with .h. e.g. <project>/foo/bar.h would be converted to
  # <project>/build/foo/bar.h.
  get_filename_component(SOURCE_FILE "${SOURCE_FILE}" ABSOLUTE)
  file(RELATIVE_PATH SOURCE_FILE "${PROJECT_SOURCE_DIR}" "${SOURCE_FILE}")
  get_filename_component(basename "${SOURCE_FILE}" NAME_WE)
  get_filename_component(dir "${SOURCE_FILE}" DIRECTORY)
  set(${HEADER_FILE} "${CMAKE_BINARY_DIR}/${dir}/${basename}.h" PARENT_SCOPE)
endfunction()

function(parse_cunit_fixtures INPUT TEST_DISABLED TEST_TIMEOUT)
  if(INPUT MATCHES ".disabled${s}*=${s}*([tT][rR][uU][eE]|[0-9]+)")
    set(${TEST_DISABLED} "TRUE" PARENT_SCOPE)
  else()
    set(${TEST_DISABLED} "FALSE" PARENT_SCOPE)
  endif()

  if(INPUT MATCHES ".timeout${s}*=${s}*([0-9]+)")
    set(${TEST_TIMEOUT} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  else()
    set(${TEST_TIMEOUT} "0" PARENT_SCOPE)
  endif()
endfunction()

# Parse a single source file, generate a header file with theory definitions
# (if applicable) and return suite and test definitions.
function(process_cunit_source_file SOURCE_FILE HEADER_FILE SUITES TESTS)
  set(x "\\*")
  set(s "[ \t\r\n]")
  set(s_or_x "[ \t\r\n\\*]")
  set(w "[_a-zA-Z0-9]")
  set(ident_expr "(${s}*${w}+${s}*)")
  # Very basic type recognition, only things that contain word characters and
  # pointers are handled. And since this script does not actually have to
  # compile any code, type checking is left to the compiler. An error (or
  # warning) will be thrown if something is off.
  #
  # The "type" regular expression below will match things like:
  #  - <word> <word>
  #  - <word> *<word>
  #  - <word>* <word> *<word>
  set(type_expr "(${s}*${w}+${x}*${s}+${s_or_x}*)+")
  set(param_expr "${type_expr}${ident_expr}")
  # Test fixture support (based on test fixtures as implemented in Criterion),
  # to enable per-test (de)initializers, which is very different from
  # per-suite (de)initializers, and timeouts.
  #
  # The following fixtures are supported:
  #  - init
  #  - fini
  #  - disabled
  #  - timeout
  set(data_expr "(${s}*,${s}*\\.${w}+${s}*=[^,\\)]+)*")

  set(suites_wo_init_n_clean)
  set(suites_w_init)
  set(suites_w_clean)

  file(READ "${SOURCE_FILE}" content)

  # CU_Init and CU_Clean
  #
  # Extract all suite initializers and deinitializers so that the list of
  # suites can be probably populated when tests and theories are parsed. Suites
  # are only registered if it contains at least one test or theory.
  set(suite_expr "CU_(Init|Clean)${s}*\\(${ident_expr}\\)")
  string(REGEX MATCHALL "${suite_expr}" matches "${content}")
  foreach(match ${matches})
    string(REGEX REPLACE "${suite_expr}" "\\2" suite "${match}")

    if("${match}" MATCHES "CU_Init")
      list(APPEND suites_w_init ${suite})
    elseif("${match}" MATCHES "CU_Clean")
      list(APPEND suites_w_deinit ${suite})
    endif()
  endforeach()

  # CU_Test
  set(test_expr "CU_Test${s}*\\(${ident_expr},${ident_expr}${data_expr}\\)")
  string(REGEX MATCHALL "${test_expr}" matches "${content}")
  foreach(match ${matches})
    string(REGEX REPLACE "${test_expr}" "\\1" suite "${match}")
    string(REGEX REPLACE "${test_expr}" "\\2" test "${match}")
    # Remove leading and trailing whitespace
    string(STRIP "${suite}" suite)
    string(STRIP "${test}" test)

    # Extract fixtures that must be handled by CMake (.disabled and .timeout).
    parse_cunit_fixtures("${match}" disabled timeout)
    list(APPEND suites_wo_init_n_clean "${suite}")
    list(APPEND tests "${suite}:${test}:${disabled}:${timeout}")
  endforeach()

  # CU_Theory
  #
  # CU_Theory signatures must be recognized in order to generate structures to
  # hold the CU_DataPoints declarations. The generated type is added to the
  # compile definitions and inserted by the preprocessor when CU_TheoryDataPoints
  # is expanded.
  #
  # NOTE: Since not all compilers support pushing function-style definitions
  #       from the command line (CMake will generate a warning too), a header
  #       file is generated instead. A define is pushed and expanded at
  #       compile time. It is included by CUnit/Theory.h.
  get_cunit_header_file("${SOURCE_FILE}" header)

  set(theory_expr "CU_Theory${s}*\\(${s}*\\((${param_expr}(,${param_expr})*)\\)${s}*,${ident_expr},${ident_expr}${data_expr}\\)")
  string(REGEX MATCHALL "${theory_expr}" matches "${content}")
  foreach(match ${matches})
    if(NOT theories)
      # Ensure generated header is truncated before anything is written.
      file(WRITE "${header}" "")
    endif()
    string(REGEX REPLACE "${theory_expr}" "\\1" params "${match}")
    string(REGEX REPLACE "${theory_expr}" "\\7" suite "${match}")
    string(REGEX REPLACE "${theory_expr}" "\\8" test "${match}")
    # Remove leading and trailing whitespace
    string(STRIP "${params}" params)
    string(STRIP "${suite}" suite)
    string(STRIP "${test}" test)
    # Convert parameters from a string to a list
    string(REGEX REPLACE "${s}*,${s}*" ";" params "${params}")

    # Extract fixtures that must be handled by CMake (.disabled and .timeout)
    parse_cunit_fixtures("${match}" disabled timeout)
    list(APPEND suites_wo_init_n_clean "${suite}")
    list(APPEND theories "${suite}:${test}:${disabled}:${timeout}")

    set(sep)
    set(size "CU_TheoryDataPointsSize_${suite}_${test}(datapoints) (")
    set(slice "CU_TheoryDataPointsSlice_${suite}_${test}(datapoints, index) (")
    set(typedef "CU_TheoryDataPointsTypedef_${suite}_${test}() {")
    foreach(param ${params})
      string(
        REGEX REPLACE "(${type_expr})${ident_expr}" "\\3" name "${param}")
      string(
        REGEX REPLACE "(${type_expr})${ident_expr}" "\\1" type "${param}")
      string(STRIP "${name}" name)
      string(STRIP "${type}" type)

      set(slice "${slice}${sep} datapoints.${name}.p[index]")
      if (NOT sep)
        set(size "${size} datapoints.${name}.n")
        set(sep ",")
      endif()
      set(typedef "${typedef} struct { size_t n; ${type} *p; } ${name};")
    endforeach()
    set(typedef "${typedef} }")
    set(slice "${slice} )")
    set(size "${size} )")

    file(APPEND "${header}" "#define ${size}\n")
    file(APPEND "${header}" "#define ${slice}\n")
    file(APPEND "${header}" "#define ${typedef}\n")
  endforeach()

  # Propagate suites, tests and theories extracted from the source file.
  if(suites_wo_init_n_clean)
    list(REMOVE_DUPLICATES suites_wo_init_n_clean)
    list(SORT suites_wo_init_n_clean)
    foreach(suite ${suites_wo_init_n_clean})
      set(init "FALSE")
      set(clean "FALSE")
      if(${suite} IN_LIST suites_w_init)
        set(init "TRUE")
      endif()
      if(${suite} IN_LIST suites_w_deinit)
        set(clean "TRUE")
      endif()

      list(APPEND suites "${suite}:${init}:${clean}")
    endforeach()
  endif()

  if(theories)
    set(${HEADER_FILE} "${header}" PARENT_SCOPE)
  else()
    unset(${HEADER_FILE} PARENT_SCOPE)
  endif()
  set(${SUITES} ${suites} PARENT_SCOPE)
  set(${TESTS} ${tests};${theories} PARENT_SCOPE)
endfunction()

function(set_test_library_paths TEST_NAME)
  if(ENABLE_SHM)
    find_library(ICEORYX_LIB iceoryx_binding_c)
    get_filename_component(ICEORYX_LIB_PATH ${ICEORYX_LIB} DIRECTORY)
  endif ()
  file(TO_NATIVE_PATH "${CUNIT_LIBRARY_DIR}" cudir)
  if(APPLE)
    set_property(
      TEST ${TEST_NAME}
      PROPERTY ENVIRONMENT
      "DYLD_LIBRARY_PATH=${cudir}:${CMAKE_LIBRARY_OUTPUT_DIRECTORY}:${ICEORYX_LIB_PATH}:$ENV{DYLD_LIBRARY_PATH}")
  elseif(WIN32)
    string(REPLACE "/" "\\" cudir "${cudir}")
    string(REPLACE ";" "\\;" paths "$ENV{PATH}")
    set_property(
      TEST ${TEST_NAME}
      PROPERTY ENVIRONMENT
      "PATH=${cudir}\\;${ICEORYX_LIB_PATH}\\;${paths}")
  else()
    set_property(
      TEST ${TEST_NAME}
      PROPERTY ENVIRONMENT
      "LD_LIBRARY_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}:${ICEORYX_LIB_PATH}:$ENV{LD_LIBRARY_PATH}")
  endif()
endfunction()

function(add_cunit_executable TARGET)
  set(decls)
  set(defns)
  set(sources)

  foreach(source ${ARGN})
    if((EXISTS "${source}" OR EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${source}"))
      unset(suites)
      unset(tests)

      process_cunit_source_file("${source}" header suites tests)
      if(header)
        set_property(
          SOURCE "${source}"
          PROPERTY COMPILE_DEFINITIONS CU_THEORY_INCLUDE_FILE=\"${header}\")
      endif()

      # Disable missing-field-initializers warnings as not having to specify
      # every member, aka fixture, is intended behavior.
      if(${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR
         ${CMAKE_C_COMPILER_ID} STREQUAL "AppleClang" OR
         ${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
        set_property(
          SOURCE "${source}"
          PROPERTY COMPILE_FLAGS -Wno-missing-field-initializers)
      endif()

      foreach(suite ${suites})
        string(REPLACE ":" ";" suite ${suite})
        list(GET suite 2 clean)
        list(GET suite 1 init)
        list(GET suite 0 suite)
        set(init_func "NULL")
        set(clean_func "NULL")
        if(init)
          set(decls "${decls}\nCU_InitDecl(${suite});")
          set(init_func "CU_InitName(${suite})")
        endif()
        if(clean)
          set(decls "${decls}\nCU_CleanDecl(${suite});")
          set(clean_func "CU_CleanName(${suite})")
        endif()
        set(defns "${defns}\nADD_SUITE(${suite}, ${init_func}, ${clean_func});")
      endforeach()

      foreach(test ${tests})
        string(REPLACE ":" ";" test ${test})
        list(GET test 3 timeout)
        list(GET test 2 disabled)
        list(GET test 0 suite)
        list(GET test 1 test)

        set(enable "true")
        if(disabled)
          set(enable "false")
        endif()
        if(NOT timeout)
          set(timeout 10)
        endif()

        set(decls "${decls}\nCU_TestDecl(${suite}, ${test});")
        set(defns "${defns}\nADD_TEST(${suite}, ${test}, ${enable});")
        set(ctest "CUnit_${suite}_${test}")

        add_test(
          NAME ${ctest}
          COMMAND ${TARGET} -s ${suite} -t ${test})
        set_property(TEST ${ctest} PROPERTY TIMEOUT ${timeout})
        set_property(TEST ${ctest} PROPERTY DISABLED ${disabled})
        set_test_library_paths(${ctest})

      endforeach()

      list(APPEND sources "${source}")
    endif()
  endforeach()

  configure_file(
    "${CUNIT_DIR}/src/main.c.in" "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.c" @ONLY)
  if("2.1.3" VERSION_LESS_EQUAL
       "${CUNIT_VERSION_MAJOR}.${CUNIT_VERSION_MINOR}.${CUNIT_VERSION_PATCH}")
    set_property(
      SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.c"
      PROPERTY COMPILE_DEFINITIONS HAVE_ENABLE_JUNIT_XML)
  endif()

  add_executable(
    ${TARGET} "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.c" ${sources})
  target_link_libraries(${TARGET} PRIVATE CycloneDDS::ucunit)
  if(MSVC)
    target_compile_definitions(${TARGET} PRIVATE _CRT_SECURE_NO_WARNINGS)
  endif()
endfunction()