-
Notifications
You must be signed in to change notification settings - Fork 100
/
FindZSTD.cmake
190 lines (154 loc) · 5.36 KB
/
FindZSTD.cmake
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# This module is a place holder for an official FindZSTD.cmake from
# kitware, when it arrives. As of June 2019, there is not yet a good
# find module that can reliably create a properly namespaced target for
# use in target_link_libraries. We'll write on here, and maybe it'll
# become the standard, maybe not. If this one gets replaced, so be it.
#
# Kyle Bentley
# Torch Technologies
# kyle.bentley@torchtechnologies.com
#
#[=======================================================================[.rst:
FindZSTD
---------
Find ZSTD include dirs and libraries
Use this module by invoking find_package with the form::
find_package(ZSTD
[version] [EXACT] # Minimum or EXACT version e.g. 1.4.0
[REQUIRED] # Fail with error if zstd is not found
)
IMPORTED Targets
^^^^^^^^^^^^^^^^
``ZSTD::zstd``
This module defines IMPORTED target ZSTD::zstd, if ZTD has been found.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``ZSTD_FOUND``
True if the system has the ZSTD library.
``ZSTD_VERSION``
The version of the ZSTD library which was found.
``ZSTD_INCLUDE_DIRS``
Include directories needed to use ZSTD.
``ZSTD_LIBRARIES``
Libraries needed to link to ZSTD.
``ZSTD_LIBRARY_DIRS``
ZSTD library directories.
Cache Variables
^^^^^^^^^^^^^^^
The following cache variables may also be set:
``ZSTD_INCLUDE_DIR``
The directory containing ``zstd.h``.
``ZSTD_LIBRARY``
The path to the z standard library.
Hints
^^^^^
Instead of explicitly setting the cache variables, the following variables
may be provided to tell this module where to look.
``ZSTD_ROOT``
Preferred installation prefix.
``ZSTD_INCLUDEDIR``
Preferred include directory e.g. <prefix>/include
``ZSTD_LIBRARYDIR``
Preferred library directory e.g. <prefix>/lib
``SYSTEM_LIBRARY_PATHS``
Paths appended to all include and lib searches.
#]=======================================================================]
mark_as_advanced(
ZSTD_INCLUDE_DIR
ZSTD_LIBRARY
)
# Append ZSTD_ROOT or $ENV{ZSTD_ROOT} if set (prioritize the direct cmake var)
set(_ZSTD_ROOT_SEARCH_DIR "")
if(ZSTD_ROOT)
list(APPEND _ZSTD_ROOT_SEARCH_DIR ${ZSTD_ROOT})
else()
set(_ENV_ZSTD_ROOT $ENV{ZSTD_ROOT})
if(_ENV_ZSTD_ROOT)
list(APPEND _ZSTD_ROOT_SEARCH_DIR ${_ENV_ZSTD_ROOT})
endif()
endif()
# Additionally try and use pkconfig to find libzstd
find_package(PkgConfig QUIET)
pkg_check_modules(PC_ZSTD QUIET libzstd)
# ------------------------------------------------------------------------
# Search for zstd include DIR
# ------------------------------------------------------------------------
set(_ZSTD_INCLUDE_SEARCH_DIRS "")
list(APPEND _ZSTD_INCLUDE_SEARCH_DIRS
${ZSTD_INCLUDEDIR}
${_ZSTD_ROOT_SEARCH_DIR}
${PC_ZSTD_INCLUDE_DIRS}
${SYSTEM_LIBRARY_PATHS}
)
# Look for a standard zstd header file.
find_path(ZSTD_INCLUDE_DIR zstd.h
PATHS ${_ZSTD_INCLUDE_SEARCH_DIRS}
PATH_SUFFIXES include
)
if(EXISTS "${ZSTD_INCLUDE_DIR}/zstd.h")
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
_ZSTD_VERSION_MAJOR REGEX "^#define ZSTD_VERSION_MAJOR")
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_MAJOR "${_ZSTD_VERSION_MAJOR}")
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
_ZSTD_VERSION_MINOR REGEX "^#define ZSTD_VERSION_MINOR")
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_MINOR "${_ZSTD_VERSION_MINOR}")
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
_ZSTD_VERSION_RELEASE REGEX "^#define ZSTD_VERSION_RELEASE")
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_RELEASE "${_ZSTD_VERSION_RELEASE}")
set(ZSTD_VERSION ${ZSTD_VERSION_MAJOR}.${ZSTD_VERSION_MINOR}.${ZSTD_VERSION_RELEASE})
endif()
# ------------------------------------------------------------------------
# Search for zstd lib DIR
# ------------------------------------------------------------------------
set(_ZSTD_LIBRARYDIR_SEARCH_DIRS "")
list(APPEND _ZSTD_LIBRARYDIR_SEARCH_DIRS
${ZSTD_LIBRARYDIR}
${_ZSTD_ROOT_SEARCH_DIR}
${PC_ZSTD_LIBRARY_DIRS}
${SYSTEM_LIBRARY_PATHS}
)
# Static library setup
if(UNIX AND ZSTD_USE_STATIC_LIBS)
set(_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
endif()
set(ZSTD_PATH_SUFFIXES
lib
lib64
)
find_library(ZSTD_LIBRARY zstd
PATHS ${_ZSTD_LIBRARYDIR_SEARCH_DIRS}
PATH_SUFFIXES ${ZSTD_PATH_SUFFIXES}
)
if(UNIX AND ZSTD_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
endif()
# ------------------------------------------------------------------------
# Cache and set ZSTD_FOUND
# ------------------------------------------------------------------------
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZSTD
FOUND_VAR ZSTD_FOUND
REQUIRED_VARS
ZSTD_LIBRARY
ZSTD_INCLUDE_DIR
VERSION_VAR ZSTD_VERSION
)
if(ZSTD_FOUND)
set(ZSTD_LIBRARIES ${ZSTD_LIBRARY})
set(ZSTD_INCLUDE_DIRS ${ZSTD_INCLUDE_DIR})
set(ZSTD_DEFINITIONS ${PC_ZSTD_CFLAGS_OTHER})
get_filename_component(ZSTD_LIBRARY_DIRS ${ZSTD_LIBRARY} DIRECTORY)
if(NOT TARGET ZSTD::zstd)
add_library(ZSTD::zstd UNKNOWN IMPORTED)
set_target_properties(ZSTD::zstd PROPERTIES
IMPORTED_LOCATION "${ZSTD_LIBRARIES}"
INTERFACE_COMPILE_DEFINITIONS "${ZSTD_DEFINITIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${ZSTD_INCLUDE_DIRS}"
)
endif()
elseif(ZSTD_FIND_REQUIRED)
message(FATAL_ERROR "Unable to find Z Standard library")
endif()