view cmake/function/MalikaniaDefineTest.cmake @ 135:5076a7b80fb2

Server: remove useless files
author David Demelier <markand@malikania.fr>
date Fri, 06 Oct 2017 16:24:20 +0200
parents 8963c68f023c
children ca125345a9cf
line wrap: on
line source

#
# MalikaniaDefineTest.cmake -- CMake build system for malikania
#
# Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#
# malikania_create_test
# ---------------------
#
# malikania_create_test(
#    NAME              Test name (must be lowercase)
#    SOURCES           Test sources files
#    LIBRARIES         (Optional) Libraries to link to
#    RESOURCES         (Optional) Resources files to copy verbatim
#    FLAGS             (Optional) Add list of compile definitions
# )
#
# This will generate a target named test-<name> where name is the parameter NAME. The test is created
# under CMAKE_BINARY_DIR/test/<NAME> and resources are copied there with the same hierarchy.
#

include(CMakeParseArguments)

include(${CMAKE_CURRENT_LIST_DIR}/MalikaniaVeraCheck.cmake)

function(malikania_create_test)
    set(singleArgs NAME)
    set(multiArgs FLAGS LIBRARIES SOURCES RESOURCES)

    cmake_parse_arguments(TEST "" "${singleArgs}" "${multiArgs}" ${ARGN})

    if (NOT TEST_NAME)
        message(FATAL_ERROR "Missing NAME parameter")
    endif ()
    if (NOT TEST_SOURCES)
        message(FATAL_ERROR "Missing SOURCES parameter")
    endif ()

    file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/test/${TEST_NAME})

    if (UNIX)
        list(APPEND TEST_LIBRARIES pthread)
    endif ()

    # Resources files added before as custom output
    foreach (f ${TEST_RESOURCES})
        get_filename_component(absolute ${f} ABSOLUTE)
        file(RELATIVE_PATH basename ${CMAKE_CURRENT_SOURCE_DIR} ${absolute})
        set(output ${CMAKE_BINARY_DIR}/test/${TEST_NAME}/${basename})

        add_custom_command(
            OUTPUT ${output}
            COMMAND ${CMAKE_COMMAND} -E copy ${absolute} ${output}
            DEPENDS ${absolute}
        )

        list(APPEND TEST_SOURCES ${absolute})
        list(APPEND outputs ${output})
    endforeach ()

    add_executable(test-${TEST_NAME} ${TEST_SOURCES} ${outputs})
    source_group(private\\Resources FILES ${outputs})
    target_compile_definitions(
        test-${TEST_NAME}
        PRIVATE
            BOOST_TEST_DYN_LINK
            CMAKE_CURRENT_BINARY_DIR=\"${CMAKE_CURRENT_BINARY_DIR}\"
            CMAKE_CURRENT_SOURCE_DIR=\"${CMAKE_CURRNET_SOURCE_DIR}\"
            SOURCE_DIRECTORY=\"${CMAKE_BINARY_DIR}/test/${TEST_NAME}\"
            ${TEST_FLAGS}
    )

    set_target_properties(
        test-${TEST_NAME}
        PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    )
    foreach (c ${CMAKE_CONFIGURATION_TYPES})
        string(TOUPPER ${c} cu)
        set_target_properties(
            test-${TEST_NAME}
            PROPERTIES
                RUNTIME_OUTPUT_DIRECTORY_${cu} ${CMAKE_BINARY_DIR}/bin/${c}
        )
    endforeach ()
    add_test(
        NAME ${TEST_NAME}
        COMMAND $<TARGET_FILE:test-${TEST_NAME}>
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
    )

    target_link_libraries(
        test-${TEST_NAME}
        ${TEST_LIBRARIES}
        Boost::boost
        Boost::unit_test_framework
    )

    add_dependencies(tests test-${TEST_NAME})

    malikania_vera_check(test-${TEST_NAME} "${TEST_SOURCES}")
endfunction()