summaryrefslogtreecommitdiffstats
path: root/cmake/Modules/FindGnuPG.cmake
blob: ed92027bc261b69beace12023f186bea6275699c (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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) 2018 Ribose Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#.rst:
# FindGnuPG
# -----------
#
# Find GnuPG executables.
#
# Imported targets
# ^^^^^^^^^^^^^^^^
#
# This module defines the following :prop_tgt:`IMPORTED` targets:
#
# ::
#
#   GnuPG::<COMPONENT>     - the component executable that was requested (default is just 'gpg')
#
## Result variables
# ^^^^^^^^^^^^^^^^
#
# This module always defines the following variables:
#
# ::
#
#   GNUPG_VERSION          - version that was found
#
# Depending on components requested, this module will also define variables like:
#
# ::
#
#   GPG_EXECUTABLE         - path to the gpg executable
#   <COMPONENT>_EXECUTABLE - path to the component executable
#

# helper that will call <utility_name> --version and extract the version string
function(_get_gpg_version utility_name exe_path var_prefix)
  execute_process(
    COMMAND "${exe_path}" --version
    OUTPUT_VARIABLE version
    RESULT_VARIABLE exit_code
    ERROR_QUIET
  )
  if (NOT exit_code)
    string(REGEX MATCH "${utility_name} \\(GnuPG\\) (([0-9]+)\\.([0-9]+)\\.([0-9]+))" version "${version}")
    if (CMAKE_MATCH_1)
      set(${var_prefix}_VERSION "${CMAKE_MATCH_1}" PARENT_SCOPE)
    endif()
  endif()
endfunction()

# default to finding gpg
if (NOT GnuPG_FIND_COMPONENTS)
  set(GnuPG_FIND_COMPONENTS gpg)
endif()

foreach(_comp IN LISTS GnuPG_FIND_COMPONENTS)
  # we also check for an executable with the 2 suffix when appropriate
  set(_names "${_comp}")
  if (_comp STREQUAL "gpg" OR _comp STREQUAL "gpgv")
    if (NOT ${GnuPG_FIND_VERSION})
      set(_names "${_comp}2" ${_comp})
    elseif (${GnuPG_FIND_VERSION} VERSION_GREATER_EQUAL 2.2)
      # 2.2+ defaults to gpg/gpgv, but supports gpg2/gpgv2
      set(_names ${_comp} "${_comp}2")
    elseif(${GnuPG_FIND_VERSION} VERSION_GREATER_EQUAL 2.0)
      # 2.0-2.2 or so used a temporary naming of gpg2/gpgv2
      set(_names "${_comp}2" ${_comp})
    endif()
  endif()
  string(TOUPPER "${_comp}" _comp_upper)
  find_program(${_comp_upper}_EXECUTABLE NAMES ${_names})
  unset(_names)
  mark_as_advanced(${_comp_upper}_EXECUTABLE)

  # if we found an executable, check the version
  if (${_comp_upper}_EXECUTABLE)
    _get_gpg_version(${_comp} ${${_comp_upper}_EXECUTABLE} _${_comp})
    if (_${_comp}_VERSION)
      if (NOT GNUPG_VERSION)
        # this is the first component found, so set the version to match
        set(GNUPG_VERSION ${_${_comp}_VERSION})
      endif()
      # see if the version matches the previous components found
      if(_${_comp}_VERSION VERSION_EQUAL ${GNUPG_VERSION} AND NOT TARGET GnuPG::${_comp})
        add_executable(GnuPG::${_comp} IMPORTED GLOBAL)
        set_target_properties(GnuPG::${_comp} PROPERTIES
          IMPORTED_LOCATION "${${_comp_upper}_EXECUTABLE}"
        )
      endif()
    endif()
    unset(_${_comp}_VERSION)
  endif()

  # mark our components as found or not found
  if (TARGET GnuPG::${_comp})
    set(GnuPG_${_comp}_FOUND TRUE)
  else()
    set(GnuPG_${_comp}_FOUND FALSE)
    unset(${_comp_upper}_EXECUTABLE)
  endif()

  if (GnuPG_FIND_REQUIRED_${_comp})
    list(APPEND _GnuPG_REQUIRED_VARS ${_comp_upper}_EXECUTABLE)
  endif()
endforeach()
unset(_comp)
unset(_comp_upper)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GnuPG
  REQUIRED_VARS ${_GnuPG_REQUIRED_VARS}
  VERSION_VAR GNUPG_VERSION
  HANDLE_COMPONENTS
)