blob: 1770241ac918bdfae997f01e48a4fffa4fe876b9 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001# - a gcrypt-config module for CMake
2#
3# Usage:
4# gcrypt_check(<PREFIX> [REQUIRED] <MODULE>)
5# checks if gcrypt is avialable
6#
7# When the 'REQUIRED' argument was set, macros will fail with an error
8# when gcrypt could not be found.
9#
10# It sets the following variables:
11# GCRYPT_CONFIG_FOUND ... true if libgcrypt-config works on the system
12# GCRYPT_CONFIG_EXECUTABLE ... pathname of the libgcrypt-config program
13# <PREFIX>_FOUND ... set to 1 if libgcrypt exist
14# <PREFIX>_LIBRARIES ... the libraries
15# <PREFIX>_CFLAGS ... all required cflags
16# <PREFIX>_ALGORITHMS ... the algorithms that this libgcrypt supports
17# <PREFIX>_VERSION ... gcrypt's version
18#
19# Examples:
20# gcrypt_check (GCRYPT gcrypt)
21# Check if a version of gcrypt is available, issues a warning
22# if not.
23#
24# gcrypt_check (GCRYPT REQUIRED gcrypt)
25# Check if a version of gcrypt is available and fails
26# if not.
27#
28# gcrypt_check (GCRYPT gcrypt>=1.4)
29# requires at least version 1.4 of gcrypt and defines e.g.
30# GCRYPT_VERSION=1.4.4. Issues a warning if a lower version
31# is available only.
32#
33# gcrypt_check (GCRYPT REQUIRED gcrypt>=1.4.4)
34# requires at least version 1.4.4 of gcrypt and fails if
35# only gcrypt 1.4.3 or lower is available only.
36#
37
38# Copyright (C) 2010 Werner Dittmann <werner.dittmann@t-online.de>
39#
40# Redistribution and use, with or without modification, are permitted
41# provided that the following conditions are met:
42#
43# 1. Redistributions must retain the above copyright notice, this
44# list of conditions and the following disclaimer.
45# 2. The name of the author may not be used to endorse or promote
46# products derived from this software without specific prior
47# written permission.
48#
49# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
53# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
55# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
57# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
58# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
59# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60#
61# This is a much edited and simplified variant of the original UsePkgConfig.cmake
62# from Enrico Scholz
63# Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
64#
65
66### Common stuff ####
67set(GCR_CONFIG_VERSION 1)
68set(GCR_CONFIG_FOUND 0)
69
70find_program(GCR_CONFIG_EXECUTABLE NAMES libgcrypt-config --version DOC "libgcrypt-config executable")
71mark_as_advanced(GCR_CONFIG_EXECUTABLE)
72
73if(GCR_CONFIG_EXECUTABLE)
74 set(GCR_CONFIG_FOUND 1)
75endif(GCR_CONFIG_EXECUTABLE)
76
77
78# Unsets the given variables
79macro(_gcrconfig_unset var)
80 set(${var} "" CACHE INTERNAL "")
81endmacro(_gcrconfig_unset)
82
83macro(_gcrconfig_set var value)
84 set(${var} ${value} CACHE INTERNAL "")
85endmacro(_gcrconfig_set)
86
87# Invokes libgcrypt-config, cleans up the result and sets variables
88macro(_gcrconfig_invoke _gcrlist _prefix _varname _regexp)
89 set(_gcrconfig_invoke_result)
90
91 execute_process(
92 COMMAND ${GCR_CONFIG_EXECUTABLE} ${ARGN}
93 OUTPUT_VARIABLE _gcrconfig_invoke_result
94 RESULT_VARIABLE _gcrconfig_failed)
95
96 if (_gcrconfig_failed)
97 set(_gcrconfig_${_varname} "")
98 _gcrconfig_unset(${_prefix}_${_varname})
99 else(_gcrconfig_failed)
100 string(REGEX REPLACE "[\r\n]" " " _gcrconfig_invoke_result "${_gcrconfig_invoke_result}")
101 string(REGEX REPLACE " +$" "" _gcrconfig_invoke_result "${_gcrconfig_invoke_result}")
102
103 if (NOT ${_regexp} STREQUAL "")
104 string(REGEX REPLACE "${_regexp}" " " _gcrconfig_invoke_result "${_gcrconfig_invoke_result}")
105 endif(NOT ${_regexp} STREQUAL "")
106
107 separate_arguments(_gcrconfig_invoke_result)
108
109 #message(STATUS " ${_varname} ... ${_gcrconfig_invoke_result}")
110 set(_gcrconfig_${_varname} ${_gcrconfig_invoke_result})
111 _gcrconfig_set(${_prefix}_${_varname} "${_gcrconfig_invoke_result}")
112 endif(_gcrconfig_failed)
113endmacro(_gcrconfig_invoke)
114
115macro(_gcrconfig_invoke_dyn _gcrlist _prefix _varname cleanup_regexp)
116 _gcrconfig_invoke("${_gcrlist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
117endmacro(_gcrconfig_invoke_dyn)
118
119# Splits given arguments into options and a package list
120macro(_gcrconfig_parse_options _result _is_req)
121 set(${_is_req} 0)
122
123 foreach(_gcr ${ARGN})
124 if (_gcr STREQUAL "REQUIRED")
125 set(${_is_req} 1)
126 endif (_gcr STREQUAL "REQUIRED")
127 endforeach(_gcr ${ARGN})
128
129 set(${_result} ${ARGN})
130 list(REMOVE_ITEM ${_result} "REQUIRED")
131endmacro(_gcrconfig_parse_options)
132
133###
134macro(_gcr_check_modules_internal _is_required _is_silent _prefix)
135 _gcrconfig_unset(${_prefix}_FOUND)
136 _gcrconfig_unset(${_prefix}_VERSION)
137 _gcrconfig_unset(${_prefix}_PREFIX)
138 _gcrconfig_unset(${_prefix}_LIBDIR)
139 _gcrconfig_unset(${_prefix}_LIBRARIES)
140 _gcrconfig_unset(${_prefix}_CFLAGS)
141 _gcrconfig_unset(${_prefix}_ALGORITHMS)
142
143 # create a better addressable variable of the modules and calculate its size
144 set(_gcr_check_modules_list ${ARGN})
145 list(LENGTH _gcr_check_modules_list _gcr_check_modules_cnt)
146
147 if(GCR_CONFIG_EXECUTABLE)
148 # give out status message telling checked module
149 if (NOT ${_is_silent})
150 message(STATUS "checking for module '${_gcr_check_modules_list}'")
151 endif(NOT ${_is_silent})
152
153 # iterate through module list and check whether they exist and match the required version
154 foreach (_gcr_check_modules_gcr ${_gcr_check_modules_list})
155
156 # check whether version is given
157 if (_gcr_check_modules_gcr MATCHES ".*(>=|=|<=).*")
158 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _gcr_check_modules_gcr_name "${_gcr_check_modules_gcr}")
159 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _gcr_check_modules_gcr_op "${_gcr_check_modules_gcr}")
160 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _gcr_check_modules_gcr_ver "${_gcr_check_modules_gcr}")
161 else(_gcr_check_modules_gcr MATCHES ".*(>=|=|<=).*")
162 set(_gcr_check_modules_gcr_name "${_gcr_check_modules_gcr}")
163 set(_gcr_check_modules_gcr_op)
164 set(_gcr_check_modules_gcr_ver)
165 endif(_gcr_check_modules_gcr MATCHES ".*(>=|=|<=).*")
166
167 set(_gcr_check_prefix "${_prefix}")
168
169 _gcrconfig_invoke(${_gcr_check_modules_gcr_name} "${_gcr_check_prefix}" VERSION "" --version )
170# _gcrconfig_invoke(${_gcr_check_modules_gcr_name} "${_gcr_check_prefix}" PREFIX "" --prefix )
171 _gcrconfig_invoke(${_gcr_check_modules_gcr_name} "${_gcr_check_prefix}" LIBRARIES "" --libs )
172 _gcrconfig_invoke(${_gcr_check_modules_gcr_name} "${_gcr_check_prefix}" CFLAGS "" --cflags )
173 _gcrconfig_invoke(${_gcr_check_modules_gcr_name} "${_gcr_check_prefix}" ALGORITHMS "" --algorithms )
174
175 message(STATUS " found ${_gcr_check_modules_gcr}, version ${_gcrconfig_VERSION}")
176 # handle the operands
177 set(_gcr_wrong_version 0)
178 if (_gcr_check_modules_gcr_op STREQUAL ">=")
179 if(_gcr_check_modules_gcr_ver VERSION_EQUAL _gcrconfig_VERSION)
180 message(STATUS " gcrypt wrong version: required: ${_gcr_check_modules_gcr_op}${_gcr_check_modules_gcr_ver}, found: ${_gcrconfig_VERSION}")
181 set(_gcr_wrong_version 1)
182 endif()
183
184 if(_gcrconfig_VERSION VERSION_LESS _gcr_check_modules_gcr_ver )
185 message(STATUS " gcrypt wrong version: required: ${_gcr_check_modules_gcr_op}${_gcr_check_modules_gcr_ver}, found: ${_gcrconfig_VERSION}")
186 set(_gcr_wrong_version 1)
187 endif()
188 endif(_gcr_check_modules_gcr_op STREQUAL ">=")
189
190 if (_gcr_check_modules_gcr_op STREQUAL "=")
191 if(_gcr_check_modules_gcr_ver VERSION_EQUAL _gcrconfig_VERSION)
192 message(STATUS " gcrypt wrong version: required: ${_gcr_check_modules_gcr_op}${_gcr_check_modules_gcr_ver}, found: ${_gcrconfig_VERSION}")
193 set(_gcr_wrong_version 1)
194 endif()
195 endif(_gcr_check_modules_gcr_op STREQUAL "=")
196
197 if (_gcr_check_modules_gcr_op STREQUAL "<=")
198 if(_gcr_check_modules_gcr_ver VERSION_EQUAL _gcrconfig_VERSION)
199 message(STATUS " gcrypt wrong version: required: ${_gcr_check_modules_gcr_op}${_gcr_check_modules_gcr_ver}, found: ${_gcrconfig_VERSION}")
200 set(_gcr_wrong_version 1)
201 endif()
202
203 if(_gcrconfig_VERSION VERSION_GREATER _gcr_check_modules_gcr_ver)
204 message(STATUS " gcrypt wrong version: required: ${_gcr_check_modules_gcr_op}${_gcr_check_modules_gcr_ver}, found: ${_gcrconfig_VERSION}")
205 set(_gcr_wrong_version 1)
206 endif()
207 endif(_gcr_check_modules_gcr_op STREQUAL "<=")
208 if (${_is_required} AND _gcr_wrong_version)
209 message(FATAL_ERROR "")
210 endif()
211
212 endforeach(_gcr_check_modules_gcr)
213 _gcrconfig_set(${_prefix}_FOUND 1)
214
215 else(GCR_CONFIG_EXECUTABLE)
216 if (${_is_required})
217 message(FATAL_ERROR "libgcrypt-config tool not found")
218 endif (${_is_required})
219 endif(GCR_CONFIG_EXECUTABLE)
220endmacro(_gcr_check_modules_internal)
221
222###
223### User visible macro starts here
224###
225
226###
227macro(gcr_check _prefix _module0)
228 # check cached value
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500229 if (NOT DEFINED __gcr_config_checked_${_prefix} OR __gcr_config_checked_${_prefix} LESS ${GCR_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
Alexandre Lision51140e12013-12-02 10:54:09 -0500230 _gcrconfig_parse_options (_gcr_modules _gcr_is_required "${_module0}" ${ARGN})
231 _gcr_check_modules_internal("${_gcr_is_required}" 0 "${_prefix}" ${_gcr_modules})
232
233 _gcrconfig_set(__gcr_config_checked_${_prefix} ${GCR_CONFIG_VERSION})
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500234 endif(NOT DEFINED __gcr_config_checked_${_prefix} OR __gcr_config_checked_${_prefix} LESS ${GCR_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
Alexandre Lision51140e12013-12-02 10:54:09 -0500235endmacro(gcr_check)
236
237###
238
239### Local Variables:
240### mode: cmake
241### End: