summaryrefslogtreecommitdiff
blob: a4066300a8831e877635206e00194e11185c365d (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
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindZSTD
--------

Find the native ZSTD includes and library.

IMPORTED Targets
^^^^^^^^^^^^^^^^

This module defines :prop_tgt:`IMPORTED` target ``ZSTD::ZSTD``, if
ZSTD has been found.

Result Variables
^^^^^^^^^^^^^^^^

This module defines the following variables:

::

  ZSTD_INCLUDE_DIRS   - where to find zstd.h, etc.
  ZSTD_LIBRARIES      - List of libraries when using zstd.
  ZSTD_FOUND          - True if zstd found.

::

  ZSTD_VERSION_STRING - The version of zstd found (x.y.z)
  ZSTD_VERSION_MAJOR  - The major version of zstd
  ZSTD_VERSION_MINOR  - The minor version of zstd

  Debug and Release variants are found separately.
#]=======================================================================]

# Standard names to search for
set(ZSTD_NAMES zstd zstd_static)
set(ZSTD_NAMES_DEBUG zstdd zstd_staticd)

find_path(ZSTD_INCLUDE_DIR
          NAMES zstd.h
          PATH_SUFFIXES include)

# Allow ZSTD_LIBRARY to be set manually, as the location of the zstd library
if(NOT ZSTD_LIBRARY)
  find_library(ZSTD_LIBRARY_RELEASE
               NAMES ${ZSTD_NAMES}
               PATH_SUFFIXES lib)
  find_library(ZSTD_LIBRARY_DEBUG
               NAMES ${ZSTD_NAMES_DEBUG}
               PATH_SUFFIXES lib)

  include(SelectLibraryConfigurations)
  select_library_configurations(ZSTD)
endif()

unset(ZSTD_NAMES)
unset(ZSTD_NAMES_DEBUG)

mark_as_advanced(ZSTD_INCLUDE_DIR)

if(ZSTD_INCLUDE_DIR AND EXISTS "${ZSTD_INCLUDE_DIR}/zstd.h")
    file(STRINGS "${ZSTD_INCLUDE_DIR}/zstd.h" ZSTD_H REGEX "^#define ZSTD_VERSION_.*$")

    string(REGEX REPLACE "^.*ZSTD_VERSION_MAJOR  *([0-9]+).*$" "\\1" ZSTD_MAJOR_VERSION "${ZSTD_H}")
    string(REGEX REPLACE "^.*ZSTD_VERSION_MINOR  *([0-9]+).*$" "\\1" ZSTD_MINOR_VERSION "${ZSTD_H}")
    string(REGEX REPLACE "^.*ZSTD_VERSION_RELEASE  *([0-9]+).*$" "\\1" ZSTD_PATCH_VERSION "${ZSTD_H}")
    set(ZSTD_VERSION_STRING "${ZSTD_MAJOR_VERSION}.${ZSTD_MINOR_VERSION}.${ZSTD_PATCH_VERSION}")
endif()

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ZSTD
        REQUIRED_VARS ZSTD_LIBRARY ZSTD_INCLUDE_DIR
        VERSION_VAR ZSTD_VERSION_STRING)

if(ZSTD_FOUND)
    set(ZSTD_INCLUDE_DIRS ${ZSTD_INCLUDE_DIR})

    if(NOT ZSTD_LIBRARIES)
        set(ZSTD_LIBRARIES ${ZSTD_LIBRARY})
    endif()

    if(NOT TARGET ZSTD::ZSTD)
        add_library(ZSTD::ZSTD UNKNOWN IMPORTED)
        set_target_properties(ZSTD::ZSTD PROPERTIES
                INTERFACE_INCLUDE_DIRECTORIES "${ZSTD_INCLUDE_DIRS}")

        if(ZSTD_LIBRARY_RELEASE)
            set_property(TARGET ZSTD::ZSTD APPEND PROPERTY
                    IMPORTED_CONFIGURATIONS RELEASE)
            set_target_properties(ZSTD::ZSTD PROPERTIES
                    IMPORTED_LOCATION_RELEASE "${ZSTD_LIBRARY_RELEASE}")
        endif()

        if(ZSTD_LIBRARY_DEBUG)
            set_property(TARGET ZSTD::ZSTD APPEND PROPERTY
                    IMPORTED_CONFIGURATIONS DEBUG)
            set_target_properties(ZSTD::ZSTD PROPERTIES
                    IMPORTED_LOCATION_DEBUG "${ZSTD_LIBRARY_DEBUG}")
        endif()

        if(NOT ZSTD_LIBRARY_RELEASE AND NOT ZSTD_LIBRARY_DEBUG)
            set_target_properties(ZSTD::ZSTD PROPERTIES
                    IMPORTED_LOCATION_RELEASE "${ZSTD_LIBRARY}")
        endif()
    endif()
endif()