Bump to libzip 1.9.2
[platform/upstream/libzip.git] / cmake / FindZstd.cmake
1 # Copyright (C) 2020 Dieter Baron and Thomas Klausner
2 #
3 # The authors can be contacted at <libzip@nih.at>
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 #   notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #   notice, this list of conditions and the following disclaimer in
14 #   the documentation and/or other materials provided with the
15 #   distribution.
16 #
17 # 3. The names of the authors may not be used to endorse or promote
18 #   products derived from this software without specific prior
19 #   written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 #[=======================================================================[.rst:
34 FindZstd
35 -------
36
37 Finds the Zstandard (zstd) library.
38
39 Imported Targets
40 ^^^^^^^^^^^^^^^^
41
42 This module provides the following imported targets, if found:
43
44 ``Zstd::Zstd``
45   The Zstandard library
46
47 Result Variables
48 ^^^^^^^^^^^^^^^^
49
50 This will define the following variables:
51
52 ``Zstd_FOUND``
53   True if the system has the Zstandard library.
54 ``Zstd_VERSION``
55   The version of the Zstandard library which was found.
56 ``Zstd_INCLUDE_DIRS``
57   Include directories needed to use Zstandard.
58 ``Zstd_LIBRARIES``
59   Libraries needed to link to Zstandard.
60
61 Cache Variables
62 ^^^^^^^^^^^^^^^
63
64 The following cache variables may also be set:
65
66 ``Zstd_INCLUDE_DIR``
67   The directory containing ``zstd.h``.
68 ``Zstd_LIBRARY``
69   The path to the Zstandard library.
70
71 #]=======================================================================]
72
73 find_package(PkgConfig)
74 pkg_check_modules(PC_Zstd QUIET zstd)
75
76 find_path(Zstd_INCLUDE_DIR
77   NAMES zstd.h
78   PATHS ${PC_Zstd_INCLUDE_DIRS}
79 )
80 find_library(Zstd_LIBRARY
81   NAMES zstd
82   PATHS ${PC_Zstd_LIBRARY_DIRS}
83 )
84
85 # Extract version information from the header file
86 if(Zstd_INCLUDE_DIR)
87   file(STRINGS ${Zstd_INCLUDE_DIR}/zstd.h _ver_major_line
88     REGEX "^#define ZSTD_VERSION_MAJOR  *[0-9]+"
89     LIMIT_COUNT 1)
90   string(REGEX MATCH "[0-9]+"
91     Zstd_MAJOR_VERSION "${_ver_major_line}")
92   file(STRINGS ${Zstd_INCLUDE_DIR}/zstd.h _ver_minor_line
93     REGEX "^#define ZSTD_VERSION_MINOR  *[0-9]+"
94     LIMIT_COUNT 1)
95   string(REGEX MATCH "[0-9]+"
96     Zstd_MINOR_VERSION "${_ver_minor_line}")
97   file(STRINGS ${Zstd_INCLUDE_DIR}/zstd.h _ver_release_line
98     REGEX "^#define ZSTD_VERSION_RELEASE  *[0-9]+"
99     LIMIT_COUNT 1)
100   string(REGEX MATCH "[0-9]+"
101     Zstd_RELEASE_VERSION "${_ver_release_line}")
102   set(Zstd_VERSION "${Zstd_MAJOR_VERSION}.${Zstd_MINOR_VERSION}.${Zstd_RELEASE_VERSION}")
103   unset(_ver_major_line)
104   unset(_ver_minor_line)
105   unset(_ver_release_line)
106 endif()
107
108 include(FindPackageHandleStandardArgs)
109 find_package_handle_standard_args(Zstd
110   FOUND_VAR Zstd_FOUND
111   REQUIRED_VARS
112     Zstd_LIBRARY
113     Zstd_INCLUDE_DIR
114   VERSION_VAR Zstd_VERSION
115 )
116
117 if(Zstd_FOUND)
118   set(Zstd_LIBRARIES ${Zstd_LIBRARY})
119   set(Zstd_INCLUDE_DIRS ${Zstd_INCLUDE_DIR})
120   set(Zstd_DEFINITIONS ${PC_Zstd_CFLAGS_OTHER})
121 endif()
122
123 if(Zstd_FOUND AND NOT TARGET Zstd::Zstd)
124   add_library(Zstd::Zstd UNKNOWN IMPORTED)
125   set_target_properties(Zstd::Zstd PROPERTIES
126     IMPORTED_LOCATION "${Zstd_LIBRARY}"
127     INTERFACE_COMPILE_OPTIONS "${PC_Zstd_CFLAGS_OTHER}"
128     INTERFACE_INCLUDE_DIRECTORIES "${Zstd_INCLUDE_DIR}"
129   )
130 endif()
131
132 mark_as_advanced(
133   Zstd_INCLUDE_DIR
134   Zstd_LIBRARY
135 )