• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 2.8.11)
2
3project(Zopfli)
4
5# Check if Zopfli is the top-level project (standalone), or a subproject
6set(zopfli_standalone FALSE)
7get_directory_property(zopfli_parent_directory PARENT_DIRECTORY)
8if(zopfli_parent_directory STREQUAL "")
9  set(zopfli_standalone TRUE)
10endif()
11unset(zopfli_parent_directory)
12
13#
14# Options
15#
16
17# 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.
23set(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
30# 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.
34if(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
40# 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
55#
56# Library version
57#
58set(ZOPFLI_VERSION_MAJOR 1)
59set(ZOPFLI_VERSION_MINOR 0)
60set(ZOPFLI_VERSION_PATCH 3)
61set(ZOPFLI_VERSION ${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH})
62
63if(ZOPFLI_BUILD_SHARED)
64  set(zopfli_library_type SHARED)
65else()
66  set(zopfli_library_type STATIC)
67endif()
68
69include(GNUInstallDirs)
70
71#
72# libzopfli
73#
74add_library(libzopfli ${zopfli_library_type}
75  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)
88target_include_directories(libzopfli
89  INTERFACE
90    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli>
91    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
92)
93set_target_properties(libzopfli PROPERTIES
94  OUTPUT_NAME zopfli
95  VERSION ${ZOPFLI_VERSION}
96  SOVERSION ${ZOPFLI_VERSION_MAJOR}
97)
98if(UNIX AND NOT (BEOS OR HAIKU))
99  target_link_libraries(libzopfli m)
100endif()
101
102#
103# libzopflipng
104#
105add_library(libzopflipng ${zopfli_library_type}
106  src/zopflipng/zopflipng_lib.cc
107  src/zopflipng/lodepng/lodepng.cpp
108  src/zopflipng/lodepng/lodepng_util.cpp
109)
110target_link_libraries(libzopflipng libzopfli)
111target_include_directories(libzopflipng
112  INTERFACE
113    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopflipng>
114    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
115)
116set_target_properties(libzopflipng PROPERTIES
117  OUTPUT_NAME zopflipng
118  VERSION ${ZOPFLI_VERSION}
119  SOVERSION ${ZOPFLI_VERSION_MAJOR}
120)
121
122# 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
133#
134# zopfli
135#
136add_executable(zopfli src/zopfli/zopfli_bin.c)
137target_link_libraries(zopfli libzopfli)
138if(MSVC)
139  target_compile_definitions(zopfli PRIVATE _CRT_SECURE_NO_WARNINGS)
140endif()
141
142#
143# zopflipng
144#
145add_executable(zopflipng src/zopflipng/zopflipng_bin.cc)
146target_link_libraries(zopflipng libzopflipng)
147if(MSVC)
148  target_compile_definitions(zopflipng PRIVATE _CRT_SECURE_NO_WARNINGS)
149endif()
150
151# 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
162#
163# Install
164#
165if(ZOPFLI_BUILD_INSTALL)
166  # Install binaries, libraries, and headers
167  install(TARGETS libzopfli libzopflipng zopfli zopflipng
168    EXPORT ZopfliTargets
169    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
170    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
171    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
172  )
173  install(FILES src/zopfli/zopfli.h src/zopflipng/zopflipng_lib.h
174    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
175  )
176
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()
197endif()
198