# 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()