Modularize compilation of tests and tutorials

This commit is contained in:
Roland Kaufmann
2013-02-11 12:35:04 +01:00
parent eaf690f870
commit 32b7e527ee
2 changed files with 78 additions and 44 deletions

View File

@@ -183,30 +183,14 @@ message (STATUS "This build defaults to installing in ${CMAKE_INSTALL_PREFIX}")
include (OpmProject)
opm_cmake_config (opm-core)
### tutorials ###
# routines to build satellites such as tests, tutorials and samples
include (OpmSatellites)
# tutorial programs are found in the tutorials/ directory
opm_find_tutorials ()
opm_compile_satellites (opm-core tutorial "" "")
# add each tutorial as a target of its own
add_custom_target (tutorials)
foreach (tutorial_FILE IN LISTS tutorial_SOURCES)
# get the name of the executable based on the source code file
get_filename_component (tutorial_NAME "${tutorial_FILE}" NAME_WE)
# setup an executable, linking to the main library
add_executable (${tutorial_NAME} EXCLUDE_FROM_ALL ${tutorial_FILE})
set_target_properties (${tutorial_NAME} PROPERTIES
LINK_FLAGS "${opm-core_LINKER_FLAGS_STR}"
)
target_link_libraries (${tutorial_NAME} ${opm-core_TARGET} ${opm-core_LIBRARIES})
strip_debug_symbols (${tutorial_NAME})
# add to list of tutorials to build
add_dependencies (tutorials ${tutorial_NAME})
endforeach (tutorial_FILE)
### test programs ###
# infrastructure for testing
enable_testing ()
include (CTest)
@@ -226,31 +210,10 @@ cond_disable_test ("AGMG")
cond_disable_test ("ERT")
# make datafiles necessary for tests available in output directory
include (OpmSatellites)
opm_data (test datafiles "tests/*.xml")
opm_data (tests datafiles "tests/*.xml")
opm_compile_satellites (opm-core tests "" "^test_([^/]*)$")
# compile each of these separately
add_custom_target (tests DEPENDS datafiles)
foreach (test_FILE IN LISTS tests_SOURCES)
get_filename_component (test_NAME "${test_FILE}" NAME_WE)
add_executable (${test_NAME} EXCLUDE_FROM_ALL ${test_FILE})
add_dependencies (tests ${test_NAME})
set_target_properties (${test_NAME} PROPERTIES
LINK_FLAGS "${opm-core_LINKER_FLAGS_STR}"
)
target_link_libraries (${test_NAME} ${opm-core_TARGET} ${opm-core_LIBRARIES})
strip_debug_symbols (${test_NAME})
string (REGEX REPLACE "^test_([^/]*)$" "\\1" test_FANCY "${test_NAME}")
get_target_property (test_LOC ${test_NAME} LOCATION)
add_test (${test_FANCY} ${test_LOC})
# run the test in the directory where the data files are
set_tests_properties (${test_FANCY} PROPERTIES
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/tests
)
endforeach (test_FILE)
# ideally this should run the tests, but for now we only test if they
# compile without errors
# use this target to run all tests
add_custom_target (check
COMMAND ${CMAKE_CTEST_COMMAND}
DEPENDS tests

View File

@@ -1,4 +1,75 @@
# - Build satellites that are dependent of main library
#
# Enumerate all source code in a "satellite" directory such as tests/,
# compile each of them and optionally set them as a test for CTest to
# run. They will be linked to the main library created by the project.
#
# The following suffices must be defined for the opm prefix passed as
# parameter:
#
# _LINKER_FLAGS Necessary flags to link with this library
# _TARGET CMake target which creates the library
# _LIBRARIES Other dependencies that must also be linked
# Synopsis:
# opm_compile_satellites (opm satellite excl_all test_regexp)
#
# Parameters:
# opm Prefix of the variable which contain information
# about the library these satellites depends on, e.g.
# pass "opm-core" if opm-core_TARGET is the name of
# the target the builds this library. Variables with
# suffixes _TARGET and _LIBRARIES must exist.
#
# satellite Prefix of variable which contain the names of the
# files, e.g. pass "tests" if the files are in the
# variable tests_SOURCES. Variables with suffixes
# _DATAFILES, _SOURCES and _DIR should exist. This
# name is also used as name of the target that builds
# all these files.
#
# excl_all EXCLUDE_ALL if these targets should not be built by
# default, otherwise empty string.
#
# test_regexp Regular expression which picks the name of a test
# out of the filename, or blank if no test should be
# setup.
#
# Example:
# opm_compile_satellites (opm-core test "" "^test_([^/]*)$")
#
macro (opm_compile_satellites opm satellite excl_all test_regexp)
# if a set of datafiles has been setup, pull those in
if (${satellite}_DATAFILES)
add_custom_target (${satellite} DEPENDS ${${satellite}_DATAFILES})
else (${satellite}_DATAFILES)
add_custom_target (${satellite})
endif (${satellite}_DATAFILES)
# compile each of these separately
foreach (_sat_FILE IN LISTS ${satellite}_SOURCES)
get_filename_component (_sat_NAME "${_sat_FILE}" NAME_WE)
add_executable (${_sat_NAME} ${excl_all} ${_sat_FILE})
add_dependencies (${satellite} ${_sat_NAME})
set_target_properties (${_sat_NAME} PROPERTIES
LINK_FLAGS "${${opm}_LINKER_FLAGS_STR}"
)
target_link_libraries (${_sat_NAME} ${${opm}_TARGET} ${${opm}_LIBRARIES})
strip_debug_symbols (${_sat_NAME})
# variable with regular expression doubles as a flag for
# whether tests should be setup or not
if (NOT ${test_regexp} STREQUAL "")
string (REGEX REPLACE "${test_regexp}" "\\1" _sat_FANCY "${_sat_NAME}")
get_target_property (_sat_LOC ${_sat_NAME} LOCATION)
add_test (${_sat_FANCY} ${_sat_LOC})
# run the test in the directory where the data files are
set_tests_properties (${_sat_FANCY} PROPERTIES
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/${${satellite}_DIR}
)
endif(NOT ${test_regexp} STREQUAL "")
endforeach (_sat_FILE)
endmacro (opm_compile_satellites opm prefix)
# Synopsis:
# opm_data (satellite target files)