Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ string(REGEX REPLACE ${VERSION_REGEX} "\\1" VERSION_STRING "${VERSION_STRING}")
project(libdeflate
LANGUAGES C
VERSION ${VERSION_STRING})
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(LIBDEFLATE_IS_TOP_LEVEL_PROJECT ON)
else()
set(LIBDEFLATE_IS_TOP_LEVEL_PROJECT OFF)
endif()

# Include the CMake modules required by the top-level directory.
include(CMakePackageConfigHelpers)
Expand All @@ -48,6 +53,8 @@ option(LIBDEFLATE_BUILD_TESTS "Build the test programs" OFF)
option(LIBDEFLATE_USE_SHARED_LIB
"Link the libdeflate-gzip and test programs to the shared library instead
of the static library" OFF)
option(LIBDEFLATE_INSTALL "Install all targets built" ${LIBDEFLATE_IS_TOP_LEVEL_PROJECT})

if(APPLE)
option(LIBDEFLATE_APPLE_FRAMEWORK "Build as Apple Framework" OFF)
endif()
Expand Down Expand Up @@ -310,17 +317,19 @@ if(LIBDEFLATE_BUILD_SHARED_LIB)
endif()

# Install the static and/or shared library.
install(TARGETS ${LIB_TARGETS}
EXPORT libdeflate_exported_targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT runtime OPTIONAL
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if(LIBDEFLATE_INSTALL)
install(TARGETS ${LIB_TARGETS}
EXPORT libdeflate_exported_targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT runtime OPTIONAL
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()

# Install resource files to Resources/ directory in the framework
if(LIBDEFLATE_APPLE_FRAMEWORK)
if(LIBDEFLATE_INSTALL AND LIBDEFLATE_APPLE_FRAMEWORK)
install(FILES ${LIBDEFLATE_RESOURCES}
DESTINATION "${CMAKE_INSTALL_LIBDIR}/libdeflate_static.framework/Resources"
)
Expand All @@ -342,21 +351,25 @@ else()
set(CMAKE_PKGCONFIG_LIBDIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
endif()
configure_file(libdeflate.pc.in libdeflate.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdeflate.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
if(LIBDEFLATE_INSTALL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdeflate.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()

# Generate a "libdeflate-targets.cmake" file in the build tree that can be
# included by outside projects to import targets from the build tree.
export(EXPORT libdeflate_exported_targets
export(TARGETS ${LIB_TARGETS}
NAMESPACE libdeflate::
FILE libdeflate-targets.cmake)

# Generate and install a separate "libdeflate-targets.cmake" file that can be
# included by outside projects to import targets from the installation tree.
install(EXPORT libdeflate_exported_targets
NAMESPACE libdeflate::
FILE libdeflate-targets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libdeflate)
if(LIBDEFLATE_INSTALL)
install(EXPORT libdeflate_exported_targets
NAMESPACE libdeflate::
FILE libdeflate-targets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libdeflate)
endif()

# Generate and install the CMake package version and config files.
write_basic_package_version_file(libdeflate-config-version.cmake
Expand All @@ -366,9 +379,11 @@ configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/libdeflate-config.cmake.in
libdeflate-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libdeflate)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdeflate-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/libdeflate-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libdeflate)
if(LIBDEFLATE_INSTALL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdeflate-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/libdeflate-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libdeflate)
endif()

# Build the programs subdirectory if needed.
if(LIBDEFLATE_BUILD_GZIP OR LIBDEFLATE_BUILD_TESTS)
Expand Down
20 changes: 13 additions & 7 deletions programs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ endif()
if(LIBDEFLATE_BUILD_GZIP)
add_executable(libdeflate-gzip gzip.c)
target_link_libraries(libdeflate-gzip PRIVATE libdeflate_prog_utils)
install(TARGETS libdeflate-gzip DESTINATION ${CMAKE_INSTALL_BINDIR})
if(LIBDEFLATE_INSTALL)
install(TARGETS libdeflate-gzip DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14")
# Install libdeflate-gunzip as a hard link to libdeflate-gzip.
# Fall back to a copy if hard links are unsupported.
Expand All @@ -53,17 +55,21 @@ if(LIBDEFLATE_BUILD_GZIP)
# when ${CMAKE_INSTALL_FULL_BINDIR} includes a drive letter. But that
# is fine since DESTDIR is unsupported on Windows anyway, according to
# the CMake documentation.
set(GZIP "${CMAKE_INSTALL_FULL_BINDIR}/libdeflate-gzip${CMAKE_EXECUTABLE_SUFFIX}")
set(GUNZIP "${CMAKE_INSTALL_FULL_BINDIR}/libdeflate-gunzip${CMAKE_EXECUTABLE_SUFFIX}")
install(CODE "message(\"-- Installing: \$ENV{DESTDIR}${GUNZIP}\")")
install(CODE "file(CREATE_LINK \"\$ENV{DESTDIR}${GZIP}\"
\"\$ENV{DESTDIR}${GUNZIP}\" COPY_ON_ERROR)")
if(LIBDEFLATE_INSTALL)
set(GZIP "${CMAKE_INSTALL_FULL_BINDIR}/libdeflate-gzip${CMAKE_EXECUTABLE_SUFFIX}")
set(GUNZIP "${CMAKE_INSTALL_FULL_BINDIR}/libdeflate-gunzip${CMAKE_EXECUTABLE_SUFFIX}")
install(CODE "message(\"-- Installing: \$ENV{DESTDIR}${GUNZIP}\")")
install(CODE "file(CREATE_LINK \"\$ENV{DESTDIR}${GZIP}\"
\"\$ENV{DESTDIR}${GUNZIP}\" COPY_ON_ERROR)")
endif()
else()
# The cmake version is too old to support file(CREATE_LINK).
# Just compile gzip.c again to build libdeflate-gunzip.
add_executable(libdeflate-gunzip gzip.c)
target_link_libraries(libdeflate-gunzip PRIVATE libdeflate_prog_utils)
install(TARGETS libdeflate-gunzip DESTINATION ${CMAKE_INSTALL_BINDIR})
if(LIBDEFLATE_INSTALL)
install(TARGETS libdeflate-gunzip DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()
endif()

Expand Down