FindLibcheck.cmake 3.15 KB
Newer Older
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
# Copyright 2019 Collabora, Ltd.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
# Original Author:
# 2019 Ryan Pavlik <ryan.pavlik@collabora.com>

#.rst:
# FindCheck
# ---------------
#
# Find the "Check" C unit testing framework.
#
# See https://libcheck.github.io
#
# The Debian package for this is called ``check``
#
# Targets
# ^^^^^^^
#
# If successful, the following imported targets are created.
#
# ``libcheck::check``
#
# Cache variables
# ^^^^^^^^^^^^^^^
#
# The following cache variable may also be set to assist/control the operation of this module:
#
# ``LIBCHECK_ROOT_DIR``
#  The root to search for libcheck.

set(LIBCHECK_ROOT_DIR "${LIBCHECK_ROOT_DIR}" CACHE PATH "Root to search for libcheck")

find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
Ryan Pavlik's avatar
Ryan Pavlik committed
38 39 40 41 42
    set(_old_prefix_path "${CMAKE_PREFIX_PATH}")
    # So pkg-config uses LIBCHECK_ROOT_DIR too.
    if(LIBCHECK_ROOT_DIR)
        list(APPEND CMAKE_PREFIX_PATH ${LIBCHECK_ROOT_DIR})
    endif()
43
    pkg_check_modules(PC_LIBCHECK QUIET check)
Ryan Pavlik's avatar
Ryan Pavlik committed
44 45
    # Restore
    set(CMAKE_PREFIX_PATH "${_old_prefix_path}")
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
endif()
find_path(LIBCHECK_INCLUDE_DIR
    NAMES
    check.h
    PATHS
    ${LIBCHECK_ROOT_DIR}
    HINTS
    ${PC_LIBCHECK_INCLUDE_DIRS}
    PATH_SUFFIXES
    include
)
find_library(LIBCHECK_LIBRARY
    NAMES
    check_pic
    check
    PATHS
    ${LIBCHECK_ROOT_DIR}
    HINTS
    ${PC_LIBCHECK_LIBRARY_DIRS}
    PATH_SUFFIXES
    lib
)
find_library(LIBCHECK_SUBUNIT_LIBRARY
    NAMES
    subunit
    PATHS
    ${LIBCHECK_ROOT_DIR}
    HINTS
    ${PC_LIBCHECK_LIBRARY_DIRS}
    PATH_SUFFIXES
    lib
)
find_library(LIBCHECK_LIBRT rt)
find_library(LIBCHECK_LIBM m)

find_package(Threads QUIET)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Libcheck
    REQUIRED_VARS
    LIBCHECK_INCLUDE_DIR
    LIBCHECK_LIBRARY
    THREADS_FOUND
)
if(LIBCHECK_FOUND)
    if(NOT TARGET libcheck::check)
        add_library(libcheck::check UNKNOWN IMPORTED)

        set_target_properties(libcheck::check PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES "${LIBCHECK_INCLUDE_DIR}")
        set_target_properties(libcheck::check PROPERTIES
            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
            IMPORTED_LOCATION ${LIBCHECK_LIBRARY})
        set_property(TARGET libcheck::check PROPERTY
100
                IMPORTED_LINK_INTERFACE_LIBRARIES Threads::Threads)
101 102 103 104

        # if we found librt or libm, link them.
        if(LIBCHECK_LIBRT)
            set_property(TARGET libcheck::check APPEND PROPERTY
105
                IMPORTED_LINK_INTERFACE_LIBRARIES "${LIBCHECK_LIBRT}")
106 107 108
        endif()
        if(LIBCHECK_LIBM)
            set_property(TARGET libcheck::check APPEND PROPERTY
109 110 111 112 113
                IMPORTED_LINK_INTERFACE_LIBRARIES "${LIBCHECK_LIBM}")
        endif()
        if(LIBCHECK_SUBUNIT_LIBRARY)
            set_property(TARGET libcheck::check APPEND PROPERTY
                IMPORTED_LINK_INTERFACE_LIBRARIES "${LIBCHECK_SUBUNIT_LIBRARY}")
114 115 116 117 118 119
        endif()

    endif()
    mark_as_advanced(LIBCHECK_INCLUDE_DIR LIBCHECK_LIBRARY LIBCHECK_SUBUNIT_LIBRARY)
endif()
mark_as_advanced(LIBCHECK_ROOT_DIR LIBCHECK_LIBRT LIBCHECK_LIBM)