File: CMakeLists.txt

package info (click to toggle)
mwrap 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,548 kB
  • sloc: cpp: 3,315; python: 1,850; ansic: 856; makefile: 258; lex: 233; sh: 145
file content (297 lines) | stat: -rw-r--r-- 9,856 bytes parent folder | download | duplicates (3)
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
set(_mwrap_log_test_driver "${CMAKE_CURRENT_BINARY_DIR}/mwrap_log_test.cmake")
set(_mwrap_log_test_script [[
if(NOT DEFINED MWRAP_EXECUTABLE)
  message(FATAL_ERROR "MWRAP_EXECUTABLE is required")
endif()

foreach(var OUTPUT_FILE REFERENCE_FILE)
  if(NOT DEFINED ${var})
    message(FATAL_ERROR "${var} is required")
  endif()
endforeach()

if(NOT DEFINED TEST_NAME)
  set(TEST_NAME "mwrap_test")
endif()

set(_args)
if(DEFINED ARGC AND ARGC GREATER 0)
  math(EXPR _last_index "${ARGC} - 1")
  foreach(_arg_index RANGE 0 ${_last_index})
    set(_var_name ARG${_arg_index})
    if(NOT DEFINED ${_var_name})
      message(FATAL_ERROR "${TEST_NAME}: missing argument ${_var_name}")
    endif()
    list(APPEND _args "${${_var_name}}")
  endforeach()
endif()

set(_working_directory "${CMAKE_CURRENT_LIST_DIR}")
if(DEFINED WORKING_DIRECTORY)
  set(_working_directory "${WORKING_DIRECTORY}")
endif()

file(MAKE_DIRECTORY "${_working_directory}")

set(stdout_file "${OUTPUT_FILE}.stdout")
execute_process(
  COMMAND ${MWRAP_EXECUTABLE} ${_args}
  RESULT_VARIABLE run_result
  OUTPUT_FILE "${stdout_file}"
  ERROR_FILE "${OUTPUT_FILE}"
  WORKING_DIRECTORY "${_working_directory}"
)

set(expect_nonzero FALSE)
if(DEFINED EXPECT_NONZERO AND EXPECT_NONZERO)
  set(expect_nonzero TRUE)
endif()

if(expect_nonzero AND run_result EQUAL 0)
  message(FATAL_ERROR "${TEST_NAME}: expected failure but command succeeded")
endif()

if(NOT expect_nonzero AND NOT run_result EQUAL 0)
  message(FATAL_ERROR "${TEST_NAME}: command failed with exit code ${run_result}")
endif()

if(NOT EXISTS "${REFERENCE_FILE}")
  message(FATAL_ERROR "${TEST_NAME}: reference file '${REFERENCE_FILE}' not found")
endif()

file(READ "${OUTPUT_FILE}" output_contents)
string(REPLACE "end of file" "$end" normalized_output "${output_contents}")
if(NOT normalized_output STREQUAL output_contents)
  file(WRITE "${OUTPUT_FILE}" "${normalized_output}")
endif()

execute_process(
  COMMAND ${CMAKE_COMMAND} -E compare_files "${OUTPUT_FILE}" "${REFERENCE_FILE}"
  RESULT_VARIABLE diff_result
)

if(NOT diff_result EQUAL 0)
  file(READ "${OUTPUT_FILE}" normalized_output)
  message(FATAL_ERROR "${TEST_NAME}: output does not match reference.\n--- Actual stderr ---\n${normalized_output}")
endif()
]])
file(WRITE "${_mwrap_log_test_driver}" "${_mwrap_log_test_script}")

function(_mwrap_add_log_test name)
  set(options EXPECT_NONZERO)
  set(oneValueArgs REFERENCE)
  set(multiValueArgs ARGS)
  cmake_parse_arguments(_MWRAP_TEST "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  if(NOT _MWRAP_TEST_REFERENCE)
    message(FATAL_ERROR "_mwrap_add_log_test requires REFERENCE for test '${name}'")
  endif()

  set(log_file "${CMAKE_CURRENT_BINARY_DIR}/${name}.log")
  list(LENGTH _MWRAP_TEST_ARGS arg_count)
  set(arg_definitions -DARGC=${arg_count})
  if(arg_count GREATER 0)
    math(EXPR arg_max "${arg_count} - 1")
    foreach(arg_index RANGE 0 ${arg_max})
      list(GET _MWRAP_TEST_ARGS ${arg_index} arg_value)
      list(APPEND arg_definitions -DARG${arg_index}=${arg_value})
    endforeach()
  endif()

  add_test(
    NAME ${name}
    COMMAND ${CMAKE_COMMAND}
            -DMWRAP_EXECUTABLE=$<TARGET_FILE:mwrap>
            -DOUTPUT_FILE=${log_file}
            -DREFERENCE_FILE=${_MWRAP_TEST_REFERENCE}
            -DEXPECT_NONZERO=${_MWRAP_TEST_EXPECT_NONZERO}
            -DTEST_NAME=${name}
            -DWORKING_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}
            ${arg_definitions}
            -P ${_mwrap_log_test_driver}
  )

  set_tests_properties(${name} PROPERTIES WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endfunction()

_mwrap_add_log_test(
  mwrap.test_syntax
  EXPECT_NONZERO
  REFERENCE ${CMAKE_CURRENT_SOURCE_DIR}/test_syntax.ref
  ARGS -cppcomplex test_syntax.mw
)

_mwrap_add_log_test(
  mwrap.test_typecheck
  EXPECT_NONZERO
  REFERENCE ${CMAKE_CURRENT_SOURCE_DIR}/test_typecheck.ref
  ARGS -cppcomplex test_typecheck.mw
)

if(MWRAP_COMPILE_MEX AND MWRAP_MEX_BACKENDS)
  include(${CMAKE_SOURCE_DIR}/cmake/MwrapAddMex.cmake)

  set(_mwrap_testing_source_dir "${CMAKE_CURRENT_SOURCE_DIR}")
  set(_mwrap_testing_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/octave")
  file(MAKE_DIRECTORY "${_mwrap_testing_binary_dir}")

  configure_file(
    "${_mwrap_testing_source_dir}/test_include2.mw"
    "${_mwrap_testing_binary_dir}/test_include2.mw"
    COPYONLY
  )

  set(_mwrap_testing_generators)

  mwrap_add_mex(test_transfers
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_transfersmex
    CC_FILENAME test_transfersmex.cc
    M_FILENAME test_transfers.m
    MW_FILES "${_mwrap_testing_source_dir}/test_transfers.mw"
  )
  list(APPEND _mwrap_testing_generators test_transfers)

  mwrap_add_mex(test_cpp_complex
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_cpp_complexmex
    CC_FILENAME test_cpp_complexmex.cc
    M_FILENAME test_cpp_complex.m
    MW_FILES "${_mwrap_testing_source_dir}/test_cpp_complex.mw"
    MWRAP_FLAGS -cppcomplex
  )
  list(APPEND _mwrap_testing_generators test_cpp_complex)

  mwrap_add_mex(test_c99_complex
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_c99_complexmex
    CC_FILENAME test_c99_complexmex.c
    M_FILENAME test_c99_complex.m
    MW_FILES "${_mwrap_testing_source_dir}/test_c99_complex.mw"
    MWRAP_FLAGS -c99complex
  )
  list(APPEND _mwrap_testing_generators test_c99_complex)

  mwrap_add_mex(test_catch
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_catchmex
    CC_FILENAME test_catchmex.cc
    M_FILENAME test_catch.m
    MW_FILES "${_mwrap_testing_source_dir}/test_catch.mw"
    MWRAP_FLAGS -catch
  )
  list(APPEND _mwrap_testing_generators test_catch)

  mwrap_add_mex(test_fortran1
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_fortran1mex
    CC_FILENAME test_fortran1mex.cc
    M_FILENAME test_fortran1.m
    MW_FILES "${_mwrap_testing_source_dir}/test_fortran1.mw"
  )
  list(APPEND _mwrap_testing_generators test_fortran1)

  mwrap_add_mex(test_fortran2
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_fortran2mex
    CC_FILENAME test_fortran2mex.c
    M_FILENAME test_fortran2.m
    MW_FILES "${_mwrap_testing_source_dir}/test_fortran2.mw"
  )
  list(APPEND _mwrap_testing_generators test_fortran2)

  mwrap_add_mex(test_include
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_includemex
    CC_FILENAME test_includemex.cc
    M_FILENAME test_include.m
    MW_FILES "${_mwrap_testing_source_dir}/test_include.mw"
  )
  list(APPEND _mwrap_testing_generators test_include)

  mwrap_add_mex(test_single_cpp
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_singlemex
    CC_FILENAME test_singlemex.cc
    MW_FILES "${_mwrap_testing_source_dir}/test_single.mw"
    MWRAP_FLAGS -cppcomplex -mb
  )
  list(APPEND _mwrap_testing_generators test_single_cpp)

  mwrap_add_mex(test_char_cpp
    WORK_DIR "${_mwrap_testing_binary_dir}"
    MEX_NAME test_charmex
    CC_FILENAME test_charmex.cc
    MW_FILES "${_mwrap_testing_source_dir}/test_char.mw"
    MWRAP_FLAGS -cppcomplex -mb
  )
  list(APPEND _mwrap_testing_generators test_char_cpp)

  set(_mwrap_testing_mex_targets)
  foreach(_mwrap_generator IN LISTS _mwrap_testing_generators)
    _mwrap_compile_mex(${_mwrap_generator} OUTPUT_VAR _mwrap_generator_mex_targets)
    if(_mwrap_generator_mex_targets)
      list(APPEND _mwrap_testing_mex_targets ${_mwrap_generator_mex_targets})
    endif()
  endforeach()
  if(_mwrap_testing_mex_targets)
    list(REMOVE_DUPLICATES _mwrap_testing_mex_targets)
  endif()

  set(_mwrap_redirect_output "${_mwrap_testing_binary_dir}/test_redirect.m")
  add_custom_command(
    OUTPUT "${_mwrap_redirect_output}"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${_mwrap_testing_binary_dir}"
    COMMAND $<TARGET_FILE:mwrap> -mb "${_mwrap_testing_source_dir}/test_redirect.mw"
    DEPENDS mwrap "${_mwrap_testing_source_dir}/test_redirect.mw"
    WORKING_DIRECTORY "${_mwrap_testing_binary_dir}"
    COMMENT "Generating test_redirect.m with mwrap"
    VERBATIM
  )
  add_custom_target(mwrap_testing_redirect DEPENDS "${_mwrap_redirect_output}")
  set_property(TARGET mwrap_testing_redirect PROPERTY FOLDER "tests")

  set(_mwrap_testing_artifacts ${_mwrap_testing_mex_targets} mwrap_testing_redirect)
  list(REMOVE_DUPLICATES _mwrap_testing_artifacts)

  if(_mwrap_testing_artifacts)
    add_custom_target(mwrap_testing_mex DEPENDS ${_mwrap_testing_artifacts})
    set_property(TARGET mwrap_testing_mex PROPERTY FOLDER "tests")
  endif()

  if("OCTAVE" IN_LIST MWRAP_MEX_BACKENDS)
    if(MWRAP_OCTAVE_EXECUTABLE)
      string(REPLACE "'" "''" _mwrap_octave_binary_dir_escaped "${_mwrap_testing_binary_dir}")
      string(REPLACE "'" "''" _mwrap_octave_source_dir_escaped "${_mwrap_testing_source_dir}")
      set(_mwrap_octave_eval
        "addpath('${_mwrap_octave_binary_dir_escaped}'); addpath('${_mwrap_octave_source_dir_escaped}'); test_all; exit;")
      add_test(NAME mwrap.octave.test_all
        COMMAND ${MWRAP_OCTAVE_EXECUTABLE}
                --quiet --no-init-file
                --eval "${_mwrap_octave_eval}")
      set_tests_properties(mwrap.octave.test_all PROPERTIES
        WORKING_DIRECTORY "${_mwrap_testing_binary_dir}")
    else()
      message(WARNING
        "Octave backend enabled but no Octave interpreter was found; skipping Octave runtime tests.")
    endif()
  endif()
endif()

if(CMAKE_CONFIGURATION_TYPES)
  add_custom_target(mwrap_tests
    COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $<CONFIG>
    WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  )
else()
  add_custom_target(mwrap_tests
    COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
    WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  )
endif()

add_dependencies(mwrap_tests mwrap)
if(TARGET mwrap_testing_mex)
  add_dependencies(mwrap_tests mwrap_testing_mex)
endif()
set_property(TARGET mwrap_tests PROPERTY FOLDER "tests")