Skip to content

Commit c756cd5

Browse files
committed
cachemon issue2: update findZSTD
1 parent 82e76bd commit c756cd5

File tree

2 files changed

+225
-184
lines changed

2 files changed

+225
-184
lines changed

cmake/Modules/FindZSTD.cmake

+30-184
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,41 @@
1-
# This module is a place holder for an official FindZSTD.cmake from
2-
# kitware, when it arrives. As of June 2019, there is not yet a good
3-
# find module that can reliably create a properly namespaced target for
4-
# use in target_link_libraries. We'll write on here, and maybe it'll
5-
# become the standard, maybe not. If this one gets replaced, so be it.
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
62
#
7-
# Kyle Bentley
8-
# Torch Technologies
9-
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
106
#
11-
#[=======================================================================[.rst:
12-
13-
FindZSTD
14-
---------
15-
16-
Find ZSTD include dirs and libraries
17-
18-
Use this module by invoking find_package with the form::
19-
20-
find_package(ZSTD
21-
[version] [EXACT] # Minimum or EXACT version e.g. 1.4.0
22-
[REQUIRED] # Fail with error if zstd is not found
23-
)
24-
25-
IMPORTED Targets
26-
^^^^^^^^^^^^^^^^
27-
28-
``ZSTD::zstd``
29-
This module defines IMPORTED target ZSTD::zstd, if ZTD has been found.
30-
31-
Result Variables
32-
^^^^^^^^^^^^^^^^
33-
34-
This will define the following variables:
35-
36-
``ZSTD_FOUND``
37-
True if the system has the ZSTD library.
38-
``ZSTD_VERSION``
39-
The version of the ZSTD library which was found.
40-
``ZSTD_INCLUDE_DIRS``
41-
Include directories needed to use ZSTD.
42-
``ZSTD_LIBRARIES``
43-
Libraries needed to link to ZSTD.
44-
``ZSTD_LIBRARY_DIRS``
45-
ZSTD library directories.
46-
47-
Cache Variables
48-
^^^^^^^^^^^^^^^
49-
50-
The following cache variables may also be set:
51-
52-
``ZSTD_INCLUDE_DIR``
53-
The directory containing ``zstd.h``.
54-
``ZSTD_LIBRARY``
55-
The path to the z standard library.
56-
57-
Hints
58-
^^^^^
59-
60-
Instead of explicitly setting the cache variables, the following variables
61-
may be provided to tell this module where to look.
62-
63-
``ZSTD_ROOT``
64-
Preferred installation prefix.
65-
``ZSTD_INCLUDEDIR``
66-
Preferred include directory e.g. <prefix>/include
67-
``ZSTD_LIBRARYDIR``
68-
Preferred library directory e.g. <prefix>/lib
69-
``SYSTEM_LIBRARY_PATHS``
70-
Paths appended to all include and lib searches.
71-
72-
#]=======================================================================]
73-
74-
mark_as_advanced(
75-
ZSTD_INCLUDE_DIR
76-
ZSTD_LIBRARY
77-
)
78-
79-
# Append ZSTD_ROOT or $ENV{ZSTD_ROOT} if set (prioritize the direct cmake var)
80-
set(_ZSTD_ROOT_SEARCH_DIR "")
81-
82-
if(ZSTD_ROOT)
83-
list(APPEND _ZSTD_ROOT_SEARCH_DIR ${ZSTD_ROOT})
84-
else()
85-
set(_ENV_ZSTD_ROOT $ENV{ZSTD_ROOT})
86-
if(_ENV_ZSTD_ROOT)
87-
list(APPEND _ZSTD_ROOT_SEARCH_DIR ${_ENV_ZSTD_ROOT})
88-
endif()
89-
endif()
90-
91-
# Additionally try and use pkconfig to find libzstd
92-
93-
find_package(PkgConfig)
94-
pkg_check_modules(PC_ZSTD QUIET libzstd)
95-
96-
# ------------------------------------------------------------------------
97-
# Search for zstd include DIR
98-
# ------------------------------------------------------------------------
99-
100-
set(_ZSTD_INCLUDE_SEARCH_DIRS "")
101-
list(APPEND _ZSTD_INCLUDE_SEARCH_DIRS
102-
${ZSTD_INCLUDEDIR}
103-
${_ZSTD_ROOT_SEARCH_DIR}
104-
${PC_ZSTD_INCLUDE_DIRS}
105-
${SYSTEM_LIBRARY_PATHS}
106-
)
107-
108-
# Look for a standard zstd header file.
109-
find_path(ZSTD_INCLUDE_DIR zstd.h
110-
NO_DEFAULT_PATH
111-
PATHS ${_ZSTD_INCLUDE_SEARCH_DIRS}
112-
PATH_SUFFIXES include
113-
)
114-
115-
if(EXISTS "${ZSTD_INCLUDE_DIR}/zstd.h")
116-
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
117-
_ZSTD_VERSION_MAJOR REGEX "^#define ZSTD_VERSION_MAJOR")
118-
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_MAJOR ${_ZSTD_VERSION_MAJOR})
119-
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
120-
_ZSTD_VERSION_MINOR REGEX "^#define ZSTD_VERSION_MINOR")
121-
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_MINOR ${_ZSTD_VERSION_MINOR} )
122-
file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h"
123-
_ZSTD_VERSION_RELEASE REGEX "^#define ZSTD_VERSION_RELEASE")
124-
string(REGEX MATCH "[0-9]+" ZSTD_VERSION_RELEASE ${_ZSTD_VERSION_RELEASE} )
125-
set(ZSTD_VERSION ${ZSTD_VERSION_MAJOR}.${ZSTD_VERSION_MINOR}.${ZSTD_VERSION_RELEASE})
126-
endif()
127-
128-
# ------------------------------------------------------------------------
129-
# Search for zstd lib DIR
130-
# ------------------------------------------------------------------------
131-
132-
set(_ZSTD_LIBRARYDIR_SEARCH_DIRS "")
133-
list(APPEND _ZSTD_LIBRARYDIR_SEARCH_DIRS
134-
${ZSTD_LIBRARYDIR}
135-
${_ZSTD_ROOT_SEARCH_DIR}
136-
${PC_ZSTD_LIBRARY_DIRS}
137-
${SYSTEM_LIBRARY_PATHS}
138-
)
139-
140-
# Static library setup
141-
if(UNIX AND ZSTD_USE_STATIC_LIBS)
142-
set(_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
143-
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
144-
endif()
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14514

146-
set(ZSTD_PATH_SUFFIXES
147-
lib
148-
lib64
149-
)
15+
#
16+
# - Try to find Facebook zstd library
17+
# This will define
18+
# ZSTD_FOUND
19+
# ZSTD_INCLUDE_DIR
20+
# ZSTD_LIBRARY
21+
#
15022

151-
find_library(ZSTD_LIBRARY zstd
152-
NO_DEFAULT_PATH
153-
PATHS ${_ZSTD_LIBRARYDIR_SEARCH_DIRS}
154-
PATH_SUFFIXES ${ZSTD_PATH_SUFFIXES}
155-
)
23+
find_path(ZSTD_INCLUDE_DIR NAMES zstd.h)
15624

157-
if(UNIX AND ZSTD_USE_STATIC_LIBS)
158-
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
159-
unset(_ZSTD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
160-
endif()
25+
find_library(ZSTD_LIBRARY_DEBUG NAMES zstdd zstd_staticd)
26+
find_library(ZSTD_LIBRARY_RELEASE NAMES zstd zstd_static)
16127

162-
# ------------------------------------------------------------------------
163-
# Cache and set ZSTD_FOUND
164-
# ------------------------------------------------------------------------
28+
include(SelectLibraryConfigurations)
29+
SELECT_LIBRARY_CONFIGURATIONS(ZSTD)
16530

16631
include(FindPackageHandleStandardArgs)
167-
find_package_handle_standard_args(ZSTD
168-
FOUND_VAR ZSTD_FOUND
169-
REQUIRED_VARS
170-
ZSTD_LIBRARY
171-
ZSTD_INCLUDE_DIR
172-
VERSION_VAR ZSTD_VERSION
173-
)
174-
175-
if(ZSTD_FOUND)
176-
set(ZSTD_LIBRARIES ${ZSTD_LIBRARY})
177-
set(ZSTD_INCLUDE_DIRS ${ZSTD_INCLUDE_DIR})
178-
set(ZSTD_DEFINITIONS ${PC_ZSTD_CFLAGS_OTHER})
179-
180-
get_filename_component(ZSTD_LIBRARY_DIRS ${ZSTD_LIBRARY} DIRECTORY)
32+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
33+
ZSTD DEFAULT_MSG
34+
ZSTD_LIBRARY ZSTD_INCLUDE_DIR
35+
)
18136

182-
if(NOT TARGET ZSTD::zstd)
183-
add_library(ZSTD::zstd UNKNOWN IMPORTED)
184-
set_target_properties(ZSTD::zstd PROPERTIES
185-
IMPORTED_LOCATION "${ZSTD_LIBRARIES}"
186-
INTERFACE_COMPILE_DEFINITIONS "${ZSTD_DEFINITIONS}"
187-
INTERFACE_INCLUDE_DIRECTORIES "${ZSTD_INCLUDE_DIRS}"
188-
)
189-
endif()
190-
elseif(ZSTD_FIND_REQUIRED)
191-
message(FATAL_ERROR "Unable to find Z Standard library")
37+
if (ZSTD_FOUND)
38+
message(STATUS "Found Zstd: ${ZSTD_LIBRARY}")
19239
endif()
19340

194-
195-
41+
mark_as_advanced(ZSTD_INCLUDE_DIR ZSTD_LIBRARY)

0 commit comments

Comments
 (0)