1include(AddFileCopyCommand)
2
3# Add a Dakota unit test, creating executable from sources, optionally
4# linking Dakota libraries, and registering it with CTest
5#
6# NAME: the name of the test as seen by CTest, and executable target
7#       created
8# SOURCES: source .cpp files
9# LABELS: extra labels to apply to this test (UnitTest added by default)
10# LINK_DAKOTA_LIBS: if specified will link against Dakota core libraries
11#
12# TODO: decide if separate args for name and target are needed
13function(dakota_add_unit_test)
14
15  set(options LINK_DAKOTA_LIBS)
16  set(oneValueArgs NAME)
17  set(multiValueArgs SOURCES LABELS DEPENDS LINK_LIBS)
18  cmake_parse_arguments(DAUT
19    "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
20
21  #message(STATUS "Dakota adding unit test ${DAUT_NAME}")
22  set(exe_target ${DAUT_NAME})
23  add_executable(${exe_target} ${DAUT_SOURCES})
24  if (${DAUT_LINK_DAKOTA_LIBS})
25    target_link_libraries(${exe_target}
26      ${Dakota_LIBRARIES} ${Dakota_TPL_LIBRARIES})
27  endif()
28  if (DAUT_LINK_LIBS)
29    target_link_libraries(${exe_target} ${DAUT_LINK_LIBS})
30  endif()
31  # TODO: support dependencies directly in this call with DEPENDS
32  #add_dependencies(${exe_target} ${DAUT_DEPENDS})
33  add_test(${DAUT_NAME} ${exe_target})
34  set_property(TEST ${DAUT_NAME} PROPERTY LABELS UnitTest ${DAUT_LABELS})
35
36endfunction()
37
38
39
40# Add file to list of unit test dependency files for addition to
41# higher level target.  This is a macro to make it simpler to set the
42# calling envrionment variable.
43macro(dakota_copy_test_file src_file dest_file dep_files_variable)
44  add_file_copy_command(${src_file} ${dest_file})
45  list(APPEND ${dep_files_variable} ${dest_file})
46endmacro()
47
48
49# Add the python unittest script and Dakota input file for an h5py
50# test
51macro(dakota_add_h5py_test TEST_NAME)
52  dakota_copy_test_file(${CMAKE_CURRENT_SOURCE_DIR}/hdf5_${TEST_NAME}.py
53    ${CMAKE_CURRENT_BINARY_DIR}/hdf5_${TEST_NAME}.py dakota_unit_test_copied_files)
54  dakota_copy_test_file(${CMAKE_CURRENT_SOURCE_DIR}/dakota_hdf5_${TEST_NAME}.in
55    ${CMAKE_CURRENT_BINARY_DIR}/dakota_hdf5_${TEST_NAME}.in dakota_unit_test_copied_files)
56
57  add_test(NAME dakota_hdf5_${TEST_NAME}_test
58    COMMAND ${Python_EXECUTABLE} -B hdf5_${TEST_NAME}.py --bindir $<TARGET_FILE_DIR:dakota>
59    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
60  set_property(TEST dakota_hdf5_${TEST_NAME}_test PROPERTY LABELS UnitTest)
61endmacro()
62