aboutsummaryrefslogtreecommitdiff
path: root/cmake/FindDependency.cmake
blob: 559bdde3855cfb35d432baa4bf2c797b0b7dd703 (plain)
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

# Installation policy belongs in doc/build.md. All lookups use local, then system.
function(cpsc_find_dependency kind output)
    cmake_parse_arguments(dependency "REQUIRED" "" "NAMES;PATH_SUFFIXES" ${ARGN})
    if(kind STREQUAL "PROGRAM")
        set(directories ${CPSC_LOCAL_BIN_DIRS})
        set(cache_type FILEPATH)
    elseif(kind STREQUAL "LIBRARY")
        set(directories ${CPSC_LOCAL_LIBRARY_DIRS})
        set(cache_type FILEPATH)
    elseif(kind STREQUAL "PATH")
        set(directories ${CPSC_LOCAL_PREFIXES} ${CPSC_LOCAL_INCLUDE_DIRS})
        set(cache_type PATH)
    else()
        message(FATAL_ERROR "Unknown dependency kind: ${kind}")
    endif()

    # Preserve a system path supplied by the caller, after checking local paths.
    set(system_hint)
    if(DEFINED ${output} AND EXISTS "${${output}}")
        if(IS_DIRECTORY "${${output}}")
            set(system_hint "${${output}}")
        else()
            get_filename_component(system_hint "${${output}}" DIRECTORY)
        endif()
    endif()
    set(selected "${output}-NOTFOUND")
    set(is_local FALSE)
    foreach(location LOCAL SYSTEM)
        unset(candidate CACHE)
        unset(candidate)
        if(location STREQUAL "LOCAL")
            if(NOT directories)
                continue()
            endif()
            set(search PATHS ${directories} NO_DEFAULT_PATH)
        else()
            set(search HINTS ${system_hint})
        endif()
        if(kind STREQUAL "PROGRAM")
            find_program(candidate NAMES ${dependency_NAMES}
                PATH_SUFFIXES ${dependency_PATH_SUFFIXES} ${search})
        elseif(kind STREQUAL "LIBRARY")
            find_library(candidate NAMES ${dependency_NAMES}
                PATH_SUFFIXES ${dependency_PATH_SUFFIXES} ${search})
        else()
            find_path(candidate NAMES ${dependency_NAMES}
                PATH_SUFFIXES ${dependency_PATH_SUFFIXES} ${search})
        endif()
        if(candidate)
            set(selected "${candidate}")
            if(location STREQUAL "LOCAL")
                set(is_local TRUE)
            endif()
            break()
        endif()
    endforeach()
    unset(candidate CACHE)
    if(NOT selected AND dependency_REQUIRED)
        message(FATAL_ERROR "Required dependency ${output} (${dependency_NAMES}) was not found locally or in the system")
    endif()
    set(${output} "${selected}" CACHE ${cache_type} "Resolved dependency" FORCE)
    set(${output} "${selected}" PARENT_SCOPE)
    set(${output}_LOCAL "${is_local}" PARENT_SCOPE)
endfunction()

macro(cpsc_find_library output)
    cpsc_find_dependency(LIBRARY ${output} NAMES ${ARGN})
endmacro()

macro(cpsc_find_include output name)
    cpsc_find_dependency(PATH ${output} NAMES "${name}")
endmacro()