1# Copyright (c) 2018 Ribose Inc. 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions 6# are met: 7# 1. Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# 2. Redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution. 12# 13# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 14# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 15# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS 17# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23# POSSIBILITY OF SUCH DAMAGE. 24 25#.rst: 26# FindGnuPG 27# ----------- 28# 29# Find GnuPG executables. 30# 31# Imported targets 32# ^^^^^^^^^^^^^^^^ 33# 34# This module defines the following :prop_tgt:`IMPORTED` targets: 35# 36# :: 37# 38# GnuPG::<COMPONENT> - the component executable that was requested (default is just 'gpg') 39# 40## Result variables 41# ^^^^^^^^^^^^^^^^ 42# 43# This module always defines the following variables: 44# 45# :: 46# 47# GNUPG_VERSION - version that was found 48# 49# Depending on components requested, this module will also define variables like: 50# 51# :: 52# 53# GPG_EXECUTABLE - path to the gpg executable 54# <COMPONENT>_EXECUTABLE - path to the component executable 55# 56 57# helper that will call <utility_name> --version and extract the version string 58function(_get_gpg_version utility_name exe_path var_prefix) 59 execute_process( 60 COMMAND "${exe_path}" --version 61 OUTPUT_VARIABLE version 62 RESULT_VARIABLE exit_code 63 ERROR_QUIET 64 ) 65 if (NOT exit_code) 66 string(REGEX MATCH "${utility_name} \\(GnuPG\\) (([0-9]+)\\.([0-9]+)\\.([0-9]+))" version "${version}") 67 if (CMAKE_MATCH_1) 68 set(${var_prefix}_VERSION "${CMAKE_MATCH_1}" PARENT_SCOPE) 69 endif() 70 endif() 71endfunction() 72 73# default to finding gpg 74if (NOT GnuPG_FIND_COMPONENTS) 75 set(GnuPG_FIND_COMPONENTS gpg) 76endif() 77 78foreach(_comp IN LISTS GnuPG_FIND_COMPONENTS) 79 # we also check for an executable with the 2 suffix when appropriate 80 set(_names "${_comp}") 81 if (_comp STREQUAL "gpg" OR _comp STREQUAL "gpgv") 82 if (NOT ${GnuPG_FIND_VERSION}) 83 set(_names "${_comp}2" ${_comp}) 84 elseif (${GnuPG_FIND_VERSION} VERSION_GREATER_EQUAL 2.2) 85 # 2.2+ defaults to gpg/gpgv, but supports gpg2/gpgv2 86 set(_names ${_comp} "${_comp}2") 87 elseif(${GnuPG_FIND_VERSION} VERSION_GREATER_EQUAL 2.0) 88 # 2.0-2.2 or so used a temporary naming of gpg2/gpgv2 89 set(_names "${_comp}2" ${_comp}) 90 endif() 91 endif() 92 string(TOUPPER "${_comp}" _comp_upper) 93 find_program(${_comp_upper}_EXECUTABLE NAMES ${_names}) 94 unset(_names) 95 mark_as_advanced(${_comp_upper}_EXECUTABLE) 96 97 # if we found an executable, check the version 98 if (${_comp_upper}_EXECUTABLE) 99 _get_gpg_version(${_comp} ${${_comp_upper}_EXECUTABLE} _${_comp}) 100 if (_${_comp}_VERSION) 101 if (NOT GNUPG_VERSION) 102 # this is the first component found, so set the version to match 103 set(GNUPG_VERSION ${_${_comp}_VERSION}) 104 endif() 105 # see if the version matches the previous components found 106 if(_${_comp}_VERSION VERSION_EQUAL ${GNUPG_VERSION} AND NOT TARGET GnuPG::${_comp}) 107 add_executable(GnuPG::${_comp} IMPORTED GLOBAL) 108 set_target_properties(GnuPG::${_comp} PROPERTIES 109 IMPORTED_LOCATION "${${_comp_upper}_EXECUTABLE}" 110 ) 111 endif() 112 endif() 113 unset(_${_comp}_VERSION) 114 endif() 115 116 # mark our components as found or not found 117 if (TARGET GnuPG::${_comp}) 118 set(GnuPG_${_comp}_FOUND TRUE) 119 else() 120 set(GnuPG_${_comp}_FOUND FALSE) 121 unset(${_comp_upper}_EXECUTABLE) 122 endif() 123 124 if (GnuPG_FIND_REQUIRED_${_comp}) 125 list(APPEND _GnuPG_REQUIRED_VARS ${_comp_upper}_EXECUTABLE) 126 endif() 127endforeach() 128unset(_comp) 129unset(_comp_upper) 130 131include(FindPackageHandleStandardArgs) 132find_package_handle_standard_args(GnuPG 133 REQUIRED_VARS ${_GnuPG_REQUIRED_VARS} 134 VERSION_VAR GNUPG_VERSION 135 HANDLE_COMPONENTS 136) 137 138