1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find libarchive library and headers.
9 Libarchive is multi-format archive and compression library.
11 The module defines the following variables:
15 LibArchive_FOUND - true if libarchive was found
16 LibArchive_INCLUDE_DIRS - include search path
17 LibArchive_LIBRARIES - libraries to link
18 LibArchive_VERSION - libarchive 3-component version number
20 The module defines the following ``IMPORTED`` targets:
24 LibArchive::LibArchive - target for linking against libarchive
27 Support for new libarchive 3.2 version string format.
29 .. versionadded:: 3.17
30 Provides an imported target.
32 #]=======================================================================]
35 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
37 find_path(LibArchive_INCLUDE_DIR
40 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\LibArchive;InstallPath]/include"
41 DOC "libarchive include directory"
44 find_library(LibArchive_LIBRARY
45 NAMES archive libarchive
47 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\LibArchive;InstallPath]/lib"
48 DOC "libarchive library"
51 mark_as_advanced(LibArchive_INCLUDE_DIR LibArchive_LIBRARY)
53 # Extract the version number from the header.
54 if(LibArchive_INCLUDE_DIR AND EXISTS "${LibArchive_INCLUDE_DIR}/archive.h")
55 # The version string appears in one of three known formats in the header:
56 # #define ARCHIVE_LIBRARY_VERSION "libarchive 2.4.12"
57 # #define ARCHIVE_VERSION_STRING "libarchive 2.8.4"
58 # #define ARCHIVE_VERSION_ONLY_STRING "3.2.0"
60 set(_LibArchive_VERSION_REGEX "^#define[ \t]+ARCHIVE[_A-Z]+VERSION[_A-Z]*[ \t]+\"(libarchive +)?([0-9]+)\\.([0-9]+)\\.([0-9]+)[^\"]*\".*$")
61 file(STRINGS "${LibArchive_INCLUDE_DIR}/archive.h" _LibArchive_VERSION_STRING LIMIT_COUNT 1 REGEX "${_LibArchive_VERSION_REGEX}")
62 if(_LibArchive_VERSION_STRING)
63 string(REGEX REPLACE "${_LibArchive_VERSION_REGEX}" "\\2.\\3.\\4" LibArchive_VERSION "${_LibArchive_VERSION_STRING}")
65 unset(_LibArchive_VERSION_REGEX)
66 unset(_LibArchive_VERSION_STRING)
69 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
70 find_package_handle_standard_args(LibArchive
71 REQUIRED_VARS LibArchive_LIBRARY LibArchive_INCLUDE_DIR
72 VERSION_VAR LibArchive_VERSION
74 unset(LIBARCHIVE_FOUND)
77 set(LibArchive_INCLUDE_DIRS ${LibArchive_INCLUDE_DIR})
78 set(LibArchive_LIBRARIES ${LibArchive_LIBRARY})
80 if (NOT TARGET LibArchive::LibArchive)
81 add_library(LibArchive::LibArchive UNKNOWN IMPORTED)
82 set_target_properties(LibArchive::LibArchive PROPERTIES
83 IMPORTED_LOCATION "${LibArchive_LIBRARY}"
84 INTERFACE_INCLUDE_DIRECTORIES "${LibArchive_INCLUDE_DIR}")