From 211d3aa017bbb275b1df939726d5b513c6cfe725 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Mon, 20 Aug 2018 08:49:35 +0200 Subject: [PATCH 01/16] Set minimum CMake version to 2.8.11 This is a compromise between getting useful features and supporting operating systems still in use. https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Life-Cycle-Considerations --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d20e390..309ba2b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 2.8.11) project(Zopfli) @@ -11,7 +11,7 @@ endif() if(MSVC) add_definitions(/D_CRT_SECURE_NO_WARNINGS) endif() - + set(zopflilib_src src/zopfli/blocksplitter.c src/zopfli/cache.c From 763fd0ed69129a86b1f28031de8d37a9573a8b03 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Mon, 20 Aug 2018 09:38:33 +0200 Subject: [PATCH 02/16] Add install target --- CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 309ba2b1..33fd9b23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8.11) project(Zopfli) option(BUILD_SHARED_LIBS "Build Zopfli with shared libraries" OFF) +option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) @@ -86,3 +87,18 @@ target_link_libraries(zopfli libzopfli) # add_executable(zopflipng src/zopflipng/zopflipng_bin.cc) target_link_libraries(zopflipng libzopflipng) + +# +# Install +# +if(ZOPFLI_BUILD_INSTALL) + include(GNUInstallDirs) + install(TARGETS libzopfli libzopflipng zopfli zopflipng + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) + install(FILES include/zopfli.h include/zopflipng_lib.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +endif() From d4181abfcf2ad782950d6395ec21875fc19ca6dc Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Mon, 20 Aug 2018 09:55:18 +0200 Subject: [PATCH 03/16] Add variables for library version Define the library version in one place. If the minimum CMake version is ever updated to 3.0, this could be added to the project command. --- CMakeLists.txt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 33fd9b23..e765e3cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,14 @@ cmake_minimum_required(VERSION 2.8.11) project(Zopfli) +# +# Library version +# +set(ZOPFLI_VERSION_MAJOR 1) +set(ZOPFLI_VERSION_MINOR 0) +set(ZOPFLI_VERSION_PATCH 2) +set(ZOPFLI_VERSION "${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH}") + option(BUILD_SHARED_LIBS "Build Zopfli with shared libraries" OFF) option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) @@ -55,8 +63,8 @@ add_library(libzopfli ) set_target_properties(libzopfli PROPERTIES OUTPUT_NAME zopfli - VERSION 1.0.2 - SOVERSION 1 + VERSION ${ZOPFLI_VERSION} + SOVERSION ${ZOPFLI_VERSION_MAJOR} ) if(UNIX) target_link_libraries(libzopfli m) @@ -72,8 +80,8 @@ add_library(libzopflipng ) set_target_properties(libzopflipng PROPERTIES OUTPUT_NAME zopflipng - VERSION 1.0.2 - SOVERSION 1 + VERSION ${ZOPFLI_VERSION} + SOVERSION ${ZOPFLI_VERSION_MAJOR} ) # From 349646891a48c85e4bd5a97d18e55a0a8b91c2b4 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Mon, 20 Aug 2018 10:50:42 +0200 Subject: [PATCH 04/16] Add include directory to library targets When using zopfli as a subproject, users can do: add_subdirectory(zopfli) target_link_libraries(my_target libzopfli) --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e765e3cd..c9e1c063 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ endif() add_library(libzopfli $ ) +target_include_directories(libzopfli INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli") set_target_properties(libzopfli PROPERTIES OUTPUT_NAME zopfli VERSION ${ZOPFLI_VERSION} @@ -78,6 +79,7 @@ add_library(libzopflipng ${lodepng_src} $ ) +target_include_directories(libzopflipng INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/zopflipng") set_target_properties(libzopflipng PROPERTIES OUTPUT_NAME zopflipng VERSION ${ZOPFLI_VERSION} From 3f183571553151a4f5b326f84cfc0980743b33a9 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 23 Aug 2018 08:50:16 +0200 Subject: [PATCH 05/16] Do not force Release build For Makefile generators, the default build type (CMAKE_BUILD_TYPE empty) uses CMAKE_C_FLAGS and by extension CFLAGS. Instead of forcing the build type in this case, print a status message letting the user know. --- CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9e1c063..b19564fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,10 @@ set(ZOPFLI_VERSION "${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VER option(BUILD_SHARED_LIBS "Build Zopfli with shared libraries" OFF) option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) +if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Zopfli build type is default (CMAKE_BUILD_TYPE empty)") + endif() endif() if(MSVC) From c77fff5566ea0762e27620323fbd34958584d6b4 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 23 Aug 2018 10:08:10 +0200 Subject: [PATCH 06/16] Move MSVC specific define to targets --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b19564fa..a0b07b72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,6 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) endif() endif() -if(MSVC) - add_definitions(/D_CRT_SECURE_NO_WARNINGS) -endif() - set(zopflilib_src src/zopfli/blocksplitter.c src/zopfli/cache.c @@ -93,12 +89,18 @@ set_target_properties(libzopflipng PROPERTIES # add_executable(zopfli src/zopfli/zopfli_bin.c) target_link_libraries(zopfli libzopfli) +if(MSVC) + target_compile_definitions(zopfli PRIVATE _CRT_SECURE_NO_WARNINGS) +endif() # # zopflipng # add_executable(zopflipng src/zopflipng/zopflipng_bin.cc) target_link_libraries(zopflipng libzopflipng) +if(MSVC) + target_compile_definitions(zopflipng PRIVATE _CRT_SECURE_NO_WARNINGS) +endif() # # Install From ff021b2b63ec45ba64a6c4164dcb11c06ae742e5 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 23 Aug 2018 11:23:40 +0200 Subject: [PATCH 07/16] Add ZOPFLI_BUILD_SHARED option This replaces BUILD_SHARED_LIBS, which should not be inadvertently set by Zopfli if built as a subproject. Instead we use BUILD_SHARED_LIBS as the default value, if set. Also do not add install target for Zopfli by default when built as a static library subproject. --- CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0b07b72..76ae56f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,33 @@ cmake_minimum_required(VERSION 2.8.11) project(Zopfli) +# Check if being built standalone +set(zopfli_standalone FALSE) +get_directory_property(zopfli_parent_directory PARENT_DIRECTORY) +if("${zopfli_parent_directory}" STREQUAL "") + set(zopfli_standalone TRUE) +endif() +unset(zopfli_parent_directory) + +# +# Options +# + +# Default to BUILD_SHARED_LIBS if set, but allow overriding it +set(zopfli_shared_default OFF) +if(DEFINED BUILD_SHARED_LIBS) + set(zopfli_shared_default ${BUILD_SHARED_LIBS}) +endif() +option(ZOPFLI_BUILD_SHARED "Build Zopfli with shared libraries" ${zopfli_shared_default}) +unset(zopfli_shared_default) + +# If standalone or shared subproject, default to building install +if(zopfli_standalone OR ZOPFLI_BUILD_SHARED) + option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) +else() + option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" OFF) +endif() + # # Library version # @@ -10,15 +37,18 @@ set(ZOPFLI_VERSION_MINOR 0) set(ZOPFLI_VERSION_PATCH 2) set(ZOPFLI_VERSION "${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH}") -option(BUILD_SHARED_LIBS "Build Zopfli with shared libraries" OFF) -option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) - -if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) +if(zopfli_standalone) if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Zopfli build type is default (CMAKE_BUILD_TYPE empty)") endif() endif() +if(ZOPFLI_BUILD_SHARED) + set(zopfli_library_type SHARED) +else() + set(zopfli_library_type STATIC) +endif() + set(zopflilib_src src/zopfli/blocksplitter.c src/zopfli/cache.c @@ -49,14 +79,14 @@ set (lodepng_src add_library(zopflilib_obj OBJECT ${zopflilib_src} ) -if(BUILD_SHARED_LIBS) +if(ZOPFLI_BUILD_SHARED) set_property(TARGET zopflilib_obj PROPERTY POSITION_INDEPENDENT_CODE ON) endif() # # libzopfli # -add_library(libzopfli +add_library(libzopfli ${zopfli_library_type} $ ) target_include_directories(libzopfli INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli") @@ -72,7 +102,7 @@ endif() # # libzopflipng # -add_library(libzopflipng +add_library(libzopflipng ${zopfli_library_type} ${zopflipnglib_src} ${lodepng_src} $ From c75b874dc203f4837653cc374cbbb54e58768a86 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 23 Aug 2018 21:09:48 +0200 Subject: [PATCH 08/16] Add workaround for MSVC building DLL Recent versions of CMake support exporting all symbols when building a DLL. Enable this feature as a workaround for not explicitly exporting the public symbols, and warn on older version of CMake. --- CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 76ae56f9..3e2e7323 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,17 @@ set_target_properties(libzopflipng PROPERTIES SOVERSION ${ZOPFLI_VERSION_MAJOR} ) +# MSVC does not export symbols by default when building a DLL, this is a +# workaround for recent versions of CMake +if(MSVC AND ZOPFLI_BUILD_SHARED) + if(CMAKE_VERSION VERSION_LESS 3.4) + message(WARNING "Automatic export of all symbols to DLL not supported until CMake 3.4") + else() + set_target_properties(libzopfli PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) + set_target_properties(libzopflipng PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) + endif() +endif() + # # zopfli # From f651da870a71d0f43bdc7ff3f3aebcf4b1b220c1 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Sun, 26 Aug 2018 15:21:24 +0200 Subject: [PATCH 09/16] Avoid (unlikely) double expansion --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e2e7323..16dddde1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(Zopfli) # Check if being built standalone set(zopfli_standalone FALSE) get_directory_property(zopfli_parent_directory PARENT_DIRECTORY) -if("${zopfli_parent_directory}" STREQUAL "") +if(zopfli_parent_directory STREQUAL "") set(zopfli_standalone TRUE) endif() unset(zopfli_parent_directory) @@ -68,7 +68,7 @@ set(zopflipnglib_src src/zopflipng/zopflipng_lib.cc ) -set (lodepng_src +set(lodepng_src src/zopflipng/lodepng/lodepng.cpp src/zopflipng/lodepng/lodepng_util.cpp ) From b0b3b7a8b9847b46ed289c1d8c4e6941a26b83d6 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Sun, 26 Aug 2018 15:28:54 +0200 Subject: [PATCH 10/16] Do not link with libm on BeOS and Haiku --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16dddde1..16cf6d11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,8 +95,8 @@ set_target_properties(libzopfli PROPERTIES VERSION ${ZOPFLI_VERSION} SOVERSION ${ZOPFLI_VERSION_MAJOR} ) -if(UNIX) - target_link_libraries(libzopfli m) +if(UNIX AND NOT (BEOS OR HAIKU)) + target_link_libraries(libzopfli PRIVATE m) endif() # From b97dd0e95c92c416352fd7c2bf5b849fe2f75900 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 31 Aug 2018 07:39:08 +0200 Subject: [PATCH 11/16] Make comments more informative --- CMakeLists.txt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16cf6d11..6cfe0617 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.11) project(Zopfli) -# Check if being built standalone +# Check if Zopfli is the top-level project (standalone), or a subproject set(zopfli_standalone FALSE) get_directory_property(zopfli_parent_directory PARENT_DIRECTORY) if(zopfli_parent_directory STREQUAL "") @@ -14,7 +14,12 @@ unset(zopfli_parent_directory) # Options # -# Default to BUILD_SHARED_LIBS if set, but allow overriding it +# ZOPFLI_BUILD_SHARED controls if Zopfli libraries are built as shared or +# static +# +# It defaults to the value of BUILD_SHARED_LIBS if set, and in most cases +# that should be used instead. The purpose of ZOPFLI_BUILD_SHARED is to allow +# overriding it when built as a subproject. set(zopfli_shared_default OFF) if(DEFINED BUILD_SHARED_LIBS) set(zopfli_shared_default ${BUILD_SHARED_LIBS}) @@ -22,7 +27,10 @@ endif() option(ZOPFLI_BUILD_SHARED "Build Zopfli with shared libraries" ${zopfli_shared_default}) unset(zopfli_shared_default) -# If standalone or shared subproject, default to building install +# ZOPFLI_BUILD_INSTALL controls if Zopfli adds an install target to the build +# +# When built standalone or as a shared library subproject, the default is ON, +# and for static library subproject the default is OFF. if(zopfli_standalone OR ZOPFLI_BUILD_SHARED) option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON) else() From 8856e5dfcbf647ef048934cc86a6cb2591270a13 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 31 Aug 2018 07:43:53 +0200 Subject: [PATCH 12/16] Link libzopflipng with libzopfli Avoids duplicating the zopfli code and simplifies the build a bit. Also move source file listings to the respective add_library and add_executable calls so we don't have extra variables. --- CMakeLists.txt | 44 +++++++++++++------------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cfe0617..5505eba4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,10 @@ else() set(zopfli_library_type STATIC) endif() -set(zopflilib_src +# +# libzopfli +# +add_library(libzopfli ${zopfli_library_type} src/zopfli/blocksplitter.c src/zopfli/cache.c src/zopfli/deflate.c @@ -71,33 +74,9 @@ set(zopflilib_src src/zopfli/zlib_container.c src/zopfli/zopfli_lib.c ) - -set(zopflipnglib_src - src/zopflipng/zopflipng_lib.cc -) - -set(lodepng_src - src/zopflipng/lodepng/lodepng.cpp - src/zopflipng/lodepng/lodepng_util.cpp -) - -# -# libzopfli object files shared by both libraries -# -add_library(zopflilib_obj OBJECT - ${zopflilib_src} -) -if(ZOPFLI_BUILD_SHARED) - set_property(TARGET zopflilib_obj PROPERTY POSITION_INDEPENDENT_CODE ON) -endif() - -# -# libzopfli -# -add_library(libzopfli ${zopfli_library_type} - $ +target_include_directories(libzopfli + INTERFACE $ ) -target_include_directories(libzopfli INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli") set_target_properties(libzopfli PROPERTIES OUTPUT_NAME zopfli VERSION ${ZOPFLI_VERSION} @@ -111,11 +90,14 @@ endif() # libzopflipng # add_library(libzopflipng ${zopfli_library_type} - ${zopflipnglib_src} - ${lodepng_src} - $ + src/zopflipng/zopflipng_lib.cc + src/zopflipng/lodepng/lodepng.cpp + src/zopflipng/lodepng/lodepng_util.cpp +) +target_link_libraries(libzopflipng libzopfli) +target_include_directories(libzopflipng + INTERFACE $ ) -target_include_directories(libzopflipng INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/zopflipng") set_target_properties(libzopflipng PROPERTIES OUTPUT_NAME zopflipng VERSION ${ZOPFLI_VERSION} From b61f099e015427268290f119c4add6e857820caf Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 31 Aug 2018 07:47:22 +0200 Subject: [PATCH 13/16] Remove PRIVATE from target_link_libraries Not supported in CMake 2.8.11. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5505eba4..01b3d16c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,7 @@ set_target_properties(libzopfli PROPERTIES SOVERSION ${ZOPFLI_VERSION_MAJOR} ) if(UNIX AND NOT (BEOS OR HAIKU)) - target_link_libraries(libzopfli PRIVATE m) + target_link_libraries(libzopfli m) endif() # From f4261f201c55d7e69204fdc8d732f7a5098c8d25 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 31 Aug 2018 07:48:22 +0200 Subject: [PATCH 14/16] Fix paths to include files in install --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01b3d16c..9537b7bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,7 +143,7 @@ if(ZOPFLI_BUILD_INSTALL) LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) - install(FILES include/zopfli.h include/zopflipng_lib.h + install(FILES src/zopfli/zopfli.h src/zopflipng/zopflipng_lib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) endif() From f01b29e3c57a2f541fcda85428c9f59aeffa7a31 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 31 Aug 2018 08:24:47 +0200 Subject: [PATCH 15/16] Set CMAKE_BUILD_TYPE to Release if empty Make based single-configuration generators default to an empty build type, if that is the case set it to Release instead. Also add an option to disable this behavior in case someone needs the default empty build type. --- CMakeLists.txt | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9537b7bf..3116e0fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,21 @@ else() option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" OFF) endif() +# ZOPFLI_DEFAULT_RELEASE enables changing empty build type to Release +# +# Make based single-configuration generators default to an empty build type, +# which might be surprising, but could be useful if you want full control over +# compiler and linker flags. When ZOPFLI_DEFAULT_RELEASE is ON, change an +# empty default build type to Release. +option(ZOPFLI_DEFAULT_RELEASE "If CMAKE_BUILD_TYPE is empty, default to Release" ON) + +if(zopfli_standalone AND ZOPFLI_DEFAULT_RELEASE) + if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "CMAKE_BUILD_TYPE empty, defaulting to Release") + set(CMAKE_BUILD_TYPE Release) + endif() +endif() + # # Library version # @@ -45,12 +60,6 @@ set(ZOPFLI_VERSION_MINOR 0) set(ZOPFLI_VERSION_PATCH 2) set(ZOPFLI_VERSION "${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH}") -if(zopfli_standalone) - if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message(STATUS "Zopfli build type is default (CMAKE_BUILD_TYPE empty)") - endif() -endif() - if(ZOPFLI_BUILD_SHARED) set(zopfli_library_type SHARED) else() From e99ba0ffa09a5133466b5708fd8e766f2de27567 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Sun, 2 Sep 2018 23:43:48 +0200 Subject: [PATCH 16/16] Add config file package to install Install config files that allow other CMake based projects to use Zopfli with find_package(Zopfli). Also add aliases, so targets are available with the same names when built as a subproject. This allows the "Modern CMake" usage pattern: find_package(Zopfli REQUIRED) # or add_subdirectory(Zopfli) ... target_link_libraries(my_target PRIVATE Zopfli::libzopfli) These are only enabled for CMake 3.0+. While strictly speaking much of the functionality is available in earlier versions, the namespace support is not. --- CMakeLists.txt | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3116e0fb..97d4f73a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,7 @@ endif() set(ZOPFLI_VERSION_MAJOR 1) set(ZOPFLI_VERSION_MINOR 0) set(ZOPFLI_VERSION_PATCH 2) -set(ZOPFLI_VERSION "${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH}") +set(ZOPFLI_VERSION ${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH}) if(ZOPFLI_BUILD_SHARED) set(zopfli_library_type SHARED) @@ -66,6 +66,8 @@ else() set(zopfli_library_type STATIC) endif() +include(GNUInstallDirs) + # # libzopfli # @@ -84,7 +86,9 @@ add_library(libzopfli ${zopfli_library_type} src/zopfli/zopfli_lib.c ) target_include_directories(libzopfli - INTERFACE $ + INTERFACE + $ + $ ) set_target_properties(libzopfli PROPERTIES OUTPUT_NAME zopfli @@ -105,7 +109,9 @@ add_library(libzopflipng ${zopfli_library_type} ) target_link_libraries(libzopflipng libzopfli) target_include_directories(libzopflipng - INTERFACE $ + INTERFACE + $ + $ ) set_target_properties(libzopflipng PROPERTIES OUTPUT_NAME zopflipng @@ -142,12 +148,24 @@ if(MSVC) target_compile_definitions(zopflipng PRIVATE _CRT_SECURE_NO_WARNINGS) endif() +# Create aliases +# +# Makes targets available to projects using Zopfli as a subproject using the +# same names as in the config file package. +if(NOT CMAKE_VERSION VERSION_LESS 3.0) + add_library(Zopfli::libzopfli ALIAS libzopfli) + add_library(Zopfli::libzopflipng ALIAS libzopflipng) + add_executable(Zopfli::zopfli ALIAS zopfli) + add_executable(Zopfli::zopflipng ALIAS zopflipng) +endif() + # # Install # if(ZOPFLI_BUILD_INSTALL) - include(GNUInstallDirs) + # Install binaries, libraries, and headers install(TARGETS libzopfli libzopflipng zopfli zopflipng + EXPORT ZopfliTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -155,4 +173,25 @@ if(ZOPFLI_BUILD_INSTALL) install(FILES src/zopfli/zopfli.h src/zopflipng/zopflipng_lib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) + + # Install config file package + # + # This allows CMake based projects to use the installed libraries with + # find_package(Zopfli). + if(NOT CMAKE_VERSION VERSION_LESS 3.0) + include(CMakePackageConfigHelpers) + write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake + VERSION ${ZOPFLI_VERSION} + COMPATIBILITY SameMajorVersion + ) + # Since we have no dependencies, use export file directly as config file + install(EXPORT ZopfliTargets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli + NAMESPACE Zopfli:: + FILE ZopfliConfig.cmake + ) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli + ) + endif() endif()