|
| 1 | +# =================================================================== |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# =================================================================== |
| 19 | + |
| 20 | +cmake_minimum_required(VERSION 3.12) |
| 21 | + |
| 22 | +#.rst: |
| 23 | +# FindUnbound |
| 24 | +# ----------- |
| 25 | +# |
| 26 | +# Find the Unbound library and headers. |
| 27 | +# |
| 28 | +# IMPORTED Targets |
| 29 | +# ^^^^^^^^^^^^^^^^ |
| 30 | +# |
| 31 | +# This module defines :prop_tgt:`IMPORTED` target ``Unbound::Unbound``, if |
| 32 | +# libunbound has been found. |
| 33 | +# |
| 34 | +# Result Variables |
| 35 | +# ^^^^^^^^^^^^^^^^ |
| 36 | +# |
| 37 | +# This module defines the following variables: |
| 38 | +# |
| 39 | +# :: |
| 40 | +# |
| 41 | +# Unbound_FOUND - True if libunbound was found. |
| 42 | +# UNBOUND_VERSION - The version of libunbound found (x.y.z). |
| 43 | +# UNBOUND_INCLUDE_DIR - Where to find unbound.h. |
| 44 | +# UNBOUND_LIBRARY - Linker switches to use with ld to link the library. |
| 45 | +# |
| 46 | +# :: |
| 47 | +# |
| 48 | +# UNBOUND_PC_REQUIRES - The name of the pkg-config module for libunbound; |
| 49 | +# empty if pkg-config was not used to find the library. |
| 50 | +# UNBOUND_PC_LIBS - The link libraries for pkg-config, if |
| 51 | +# UNBOUND_PC_REQUUIRES is not set. |
| 52 | +# |
| 53 | +# Hints |
| 54 | +# ^^^^^ |
| 55 | +# |
| 56 | +# A user may set ``Unbound_ROOT`` to tell this module where to look. |
| 57 | + |
| 58 | +include(FindPackageHandleStandardArgs) |
| 59 | +include(GNUInstallDirs) |
| 60 | +find_package(PkgConfig QUIET) |
| 61 | + |
| 62 | +# Save the PKG_CONFIG_PATH environment variable |
| 63 | +if(PKG_CONFIG_FOUND) |
| 64 | + set(_pkg_config_path $ENV{PKG_CONFIG_PATH}) |
| 65 | +endif() |
| 66 | + |
| 67 | +set(Unbound_FOUND FALSE) |
| 68 | +if(DEFINED Unbound_ROOT) |
| 69 | + # Search in the provided root path |
| 70 | + set(_root_search PATHS ${Unbound_ROOT} NO_DEFAULT_PATH) |
| 71 | + list(APPEND _UNBOUND_SEARCHES _root_search) |
| 72 | + |
| 73 | + # Set the PKG_CONFIG_PATH environment variable |
| 74 | + if(PKG_CONFIG_FOUND) |
| 75 | + find_path(_pcfile NAMES "libunbound.pc" |
| 76 | + ${_root_search} |
| 77 | + PATH_SUFFIXES |
| 78 | + "lib/pkgconfig" |
| 79 | + "${CMAKE_INSTALL_LIBDIR}/pkgconfig" |
| 80 | + "share/pkgconfig" |
| 81 | + "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") |
| 82 | + if(_pcfile AND EXISTS "${_pcfile}/libunbound.pc") |
| 83 | + set(ENV{PKG_CONFIG_PATH} ${_pcfile}) |
| 84 | + endif() |
| 85 | + endif() |
| 86 | +endif() |
| 87 | + |
| 88 | +# Search in default paths |
| 89 | +set(_default_search) |
| 90 | +list(APPEND _UNBOUND_SEARCHES _default_search) |
| 91 | + |
| 92 | +# Try pkg-config first |
| 93 | +if(PKG_CONFIG_FOUND) |
| 94 | + pkg_search_module(UNBOUND QUIET IMPORTED_TARGET libunbound) |
| 95 | + if(UNBOUND_FOUND) |
| 96 | + find_package_handle_standard_args(Unbound |
| 97 | + REQUIRED_VARS UNBOUND_LINK_LIBRARIES UNBOUND_INCLUDEDIR |
| 98 | + VERSION_VAR UNBOUND_VERSION) |
| 99 | + if(Unbound_FOUND) |
| 100 | + add_library(Unbound::Unbound ALIAS PkgConfig::UNBOUND) |
| 101 | + set(UNBOUND_INCLUDE_DIR ${UNBOUND_INCLUDEDIR}) |
| 102 | + set(UNBOUND_LIBRARY ${UNBOUND_LINK_LIBRARIES}) |
| 103 | + set(UNBOUND_PC_REQUIRES libunbound) |
| 104 | + set(UNBOUND_PC_LIBS) |
| 105 | + endif() |
| 106 | + endif() |
| 107 | +endif() |
| 108 | + |
| 109 | +if(NOT Unbound_FOUND) |
| 110 | + # Try each search configuration |
| 111 | + foreach(search ${_UNBOUND_SEARCHES}) |
| 112 | + find_path(UNBOUND_INCLUDE_DIR NAMES "unbound.h" ${${search}} |
| 113 | + PATH_SUFFIXES "include" "${CMAKE_INSTALL_INCLUDEDIR}") |
| 114 | + if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h") |
| 115 | + # Use the first successful search to find the libraries |
| 116 | + if(NOT DEFINED libsearch) |
| 117 | + set(libsearch ${search}) |
| 118 | + endif() |
| 119 | + endif() |
| 120 | + endforeach() |
| 121 | + |
| 122 | + find_library(UNBOUND_LIBRARY NAMES "unbound" NAMES_PER_DIR ${${libsearch}} |
| 123 | + PATH_SUFFIXES "lib" "${CMAKE_INSTALL_LIBDIR}") |
| 124 | + |
| 125 | + # Extract the version number from the header |
| 126 | + if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h") |
| 127 | + file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _major |
| 128 | + REGEX "^ *# *define +UNBOUND_VERSION_MAJOR +[0-9]+.*$") |
| 129 | + file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _minor |
| 130 | + REGEX "^ *# *define +UNBOUND_VERSION_MINOR +[0-9]+.*$") |
| 131 | + file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _micro |
| 132 | + REGEX "^ *# *define +UNBOUND_VERSION_MICRO +[0-9]+.*$") |
| 133 | + string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _major ${_major}) |
| 134 | + string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _minor ${_minor}) |
| 135 | + string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _micro ${_micro}) |
| 136 | + set(UNBOUND_VERSION "${_major}.${_minor}.${_micro}") |
| 137 | + endif() |
| 138 | + |
| 139 | + find_package_handle_standard_args(Unbound |
| 140 | + REQUIRED_VARS UNBOUND_LIBRARY UNBOUND_INCLUDE_DIR |
| 141 | + VERSION_VAR UNBOUND_VERSION) |
| 142 | + if(Unbound_FOUND) |
| 143 | + add_library(Unbound::Unbound UNKNOWN IMPORTED) |
| 144 | + set_target_properties(Unbound::Unbound PROPERTIES |
| 145 | + INTERFACE_INCLUDE_DIRECTORIES "${UNBOUND_INCLUDE_DIR}" |
| 146 | + IMPORTED_LOCATION "${UNBOUND_LIBRARY}") |
| 147 | + set(UNBOUND_PC_REQUIRES) |
| 148 | + set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}") |
| 149 | + endif() |
| 150 | +endif() |
| 151 | + |
| 152 | +mark_as_advanced(UNBOUND_INCLUDE_DIR) |
| 153 | +mark_as_advanced(UNBOUND_LIBRARY) |
| 154 | + |
| 155 | +# Restore the PKG_CONFIG_PATH environment variable |
| 156 | +if(PKG_CONFIG_FOUND) |
| 157 | + set(ENV{PKG_CONFIG_PATH} ${_pkg_config_path}) |
| 158 | +endif() |
0 commit comments