blob: e56fd2fb35391fedf2027266c772abc3e2b5142b [file] [log] [blame]
Joergen Ibsen211d3aa2018-08-20 08:49:35 +02001cmake_minimum_required(VERSION 2.8.11)
Joergen Ibsen7554e4d2015-05-30 15:21:51 +02002
3project(Zopfli)
4
Joergen Ibsenb97dd0e2018-08-31 07:39:08 +02005# Check if Zopfli is the top-level project (standalone), or a subproject
Joergen Ibsenff021b22018-08-23 11:23:40 +02006set(zopfli_standalone FALSE)
7get_directory_property(zopfli_parent_directory PARENT_DIRECTORY)
Joergen Ibsenf651da82018-08-26 15:21:24 +02008if(zopfli_parent_directory STREQUAL "")
Joergen Ibsenff021b22018-08-23 11:23:40 +02009 set(zopfli_standalone TRUE)
10endif()
11unset(zopfli_parent_directory)
12
13#
14# Options
15#
16
Joergen Ibsenb97dd0e2018-08-31 07:39:08 +020017# ZOPFLI_BUILD_SHARED controls if Zopfli libraries are built as shared or
18# static
19#
20# It defaults to the value of BUILD_SHARED_LIBS if set, and in most cases
21# that should be used instead. The purpose of ZOPFLI_BUILD_SHARED is to allow
22# overriding it when built as a subproject.
Joergen Ibsenff021b22018-08-23 11:23:40 +020023set(zopfli_shared_default OFF)
24if(DEFINED BUILD_SHARED_LIBS)
25 set(zopfli_shared_default ${BUILD_SHARED_LIBS})
26endif()
27option(ZOPFLI_BUILD_SHARED "Build Zopfli with shared libraries" ${zopfli_shared_default})
28unset(zopfli_shared_default)
29
Joergen Ibsenb97dd0e2018-08-31 07:39:08 +020030# ZOPFLI_BUILD_INSTALL controls if Zopfli adds an install target to the build
31#
32# When built standalone or as a shared library subproject, the default is ON,
33# and for static library subproject the default is OFF.
Joergen Ibsenff021b22018-08-23 11:23:40 +020034if(zopfli_standalone OR ZOPFLI_BUILD_SHARED)
35 option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON)
36else()
37 option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" OFF)
38endif()
39
Joergen Ibsenf01b29e2018-08-31 08:24:47 +020040# ZOPFLI_DEFAULT_RELEASE enables changing empty build type to Release
41#
42# Make based single-configuration generators default to an empty build type,
43# which might be surprising, but could be useful if you want full control over
44# compiler and linker flags. When ZOPFLI_DEFAULT_RELEASE is ON, change an
45# empty default build type to Release.
46option(ZOPFLI_DEFAULT_RELEASE "If CMAKE_BUILD_TYPE is empty, default to Release" ON)
47
48if(zopfli_standalone AND ZOPFLI_DEFAULT_RELEASE)
49 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
50 message(STATUS "CMAKE_BUILD_TYPE empty, defaulting to Release")
51 set(CMAKE_BUILD_TYPE Release)
52 endif()
53endif()
54
Joergen Ibsend4181ab2018-08-20 09:55:18 +020055#
56# Library version
57#
58set(ZOPFLI_VERSION_MAJOR 1)
59set(ZOPFLI_VERSION_MINOR 0)
Lodebd64b2f2019-11-27 23:52:30 +010060set(ZOPFLI_VERSION_PATCH 3)
Joergen Ibsene99ba0f2018-09-02 23:43:48 +020061set(ZOPFLI_VERSION ${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH})
Joergen Ibsend4181ab2018-08-20 09:55:18 +020062
Joergen Ibsenff021b22018-08-23 11:23:40 +020063if(ZOPFLI_BUILD_SHARED)
64 set(zopfli_library_type SHARED)
65else()
66 set(zopfli_library_type STATIC)
67endif()
68
Joergen Ibsene99ba0f2018-09-02 23:43:48 +020069include(GNUInstallDirs)
70
Joergen Ibsen8856e5d2018-08-31 07:43:53 +020071#
72# libzopfli
73#
74add_library(libzopfli ${zopfli_library_type}
Joergen Ibsen7554e4d2015-05-30 15:21:51 +020075 src/zopfli/blocksplitter.c
76 src/zopfli/cache.c
77 src/zopfli/deflate.c
78 src/zopfli/gzip_container.c
79 src/zopfli/hash.c
80 src/zopfli/katajainen.c
81 src/zopfli/lz77.c
82 src/zopfli/squeeze.c
83 src/zopfli/tree.c
84 src/zopfli/util.c
85 src/zopfli/zlib_container.c
86 src/zopfli/zopfli_lib.c
87)
Joergen Ibsen8856e5d2018-08-31 07:43:53 +020088target_include_directories(libzopfli
Joergen Ibsene99ba0f2018-09-02 23:43:48 +020089 INTERFACE
90 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli>
91 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
Joergen Ibsen7554e4d2015-05-30 15:21:51 +020092)
Joergen Ibsen7554e4d2015-05-30 15:21:51 +020093set_target_properties(libzopfli PROPERTIES
94 OUTPUT_NAME zopfli
Joergen Ibsend4181ab2018-08-20 09:55:18 +020095 VERSION ${ZOPFLI_VERSION}
96 SOVERSION ${ZOPFLI_VERSION_MAJOR}
Joergen Ibsen7554e4d2015-05-30 15:21:51 +020097)
Joergen Ibsenb0b3b7a2018-08-26 15:28:54 +020098if(UNIX AND NOT (BEOS OR HAIKU))
Joergen Ibsenb61f0992018-08-31 07:47:22 +020099 target_link_libraries(libzopfli m)
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200100endif()
101
102#
103# libzopflipng
104#
Joergen Ibsenff021b22018-08-23 11:23:40 +0200105add_library(libzopflipng ${zopfli_library_type}
Joergen Ibsen8856e5d2018-08-31 07:43:53 +0200106 src/zopflipng/zopflipng_lib.cc
107 src/zopflipng/lodepng/lodepng.cpp
108 src/zopflipng/lodepng/lodepng_util.cpp
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200109)
Joergen Ibsen8856e5d2018-08-31 07:43:53 +0200110target_link_libraries(libzopflipng libzopfli)
111target_include_directories(libzopflipng
Joergen Ibsene99ba0f2018-09-02 23:43:48 +0200112 INTERFACE
113 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopflipng>
114 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
Joergen Ibsen8856e5d2018-08-31 07:43:53 +0200115)
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200116set_target_properties(libzopflipng PROPERTIES
117 OUTPUT_NAME zopflipng
Joergen Ibsend4181ab2018-08-20 09:55:18 +0200118 VERSION ${ZOPFLI_VERSION}
119 SOVERSION ${ZOPFLI_VERSION_MAJOR}
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200120)
121
Joergen Ibsenc75b8742018-08-23 21:09:48 +0200122# MSVC does not export symbols by default when building a DLL, this is a
123# workaround for recent versions of CMake
124if(MSVC AND ZOPFLI_BUILD_SHARED)
125 if(CMAKE_VERSION VERSION_LESS 3.4)
126 message(WARNING "Automatic export of all symbols to DLL not supported until CMake 3.4")
127 else()
128 set_target_properties(libzopfli PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
129 set_target_properties(libzopflipng PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
130 endif()
131endif()
132
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200133#
134# zopfli
135#
136add_executable(zopfli src/zopfli/zopfli_bin.c)
137target_link_libraries(zopfli libzopfli)
Joergen Ibsenc77fff52018-08-23 10:08:10 +0200138if(MSVC)
139 target_compile_definitions(zopfli PRIVATE _CRT_SECURE_NO_WARNINGS)
140endif()
Joergen Ibsen7554e4d2015-05-30 15:21:51 +0200141
142#
143# zopflipng
144#
145add_executable(zopflipng src/zopflipng/zopflipng_bin.cc)
146target_link_libraries(zopflipng libzopflipng)
Joergen Ibsenc77fff52018-08-23 10:08:10 +0200147if(MSVC)
148 target_compile_definitions(zopflipng PRIVATE _CRT_SECURE_NO_WARNINGS)
149endif()
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200150
Joergen Ibsene99ba0f2018-09-02 23:43:48 +0200151# Create aliases
152#
153# Makes targets available to projects using Zopfli as a subproject using the
154# same names as in the config file package.
155if(NOT CMAKE_VERSION VERSION_LESS 3.0)
156 add_library(Zopfli::libzopfli ALIAS libzopfli)
157 add_library(Zopfli::libzopflipng ALIAS libzopflipng)
158 add_executable(Zopfli::zopfli ALIAS zopfli)
159 add_executable(Zopfli::zopflipng ALIAS zopflipng)
160endif()
161
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200162#
163# Install
164#
165if(ZOPFLI_BUILD_INSTALL)
Joergen Ibsene99ba0f2018-09-02 23:43:48 +0200166 # Install binaries, libraries, and headers
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200167 install(TARGETS libzopfli libzopflipng zopfli zopflipng
Joergen Ibsene99ba0f2018-09-02 23:43:48 +0200168 EXPORT ZopfliTargets
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200169 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
170 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
171 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
172 )
Joergen Ibsenf4261f22018-08-31 07:48:22 +0200173 install(FILES src/zopfli/zopfli.h src/zopflipng/zopflipng_lib.h
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200174 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
175 )
Joergen Ibsene99ba0f2018-09-02 23:43:48 +0200176
177 # Install config file package
178 #
179 # This allows CMake based projects to use the installed libraries with
180 # find_package(Zopfli).
181 if(NOT CMAKE_VERSION VERSION_LESS 3.0)
182 include(CMakePackageConfigHelpers)
183 write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake
184 VERSION ${ZOPFLI_VERSION}
185 COMPATIBILITY SameMajorVersion
186 )
187 # Since we have no dependencies, use export file directly as config file
188 install(EXPORT ZopfliTargets
189 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli
190 NAMESPACE Zopfli::
191 FILE ZopfliConfig.cmake
192 )
193 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake
194 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli
195 )
196 endif()
Joergen Ibsen763fd0e2018-08-20 09:38:33 +0200197endif()