CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. cmake_minimum_required(VERSION 2.8.11)
  2. project(Zopfli)
  3. # Check if Zopfli is the top-level project (standalone), or a subproject
  4. set(zopfli_standalone FALSE)
  5. get_directory_property(zopfli_parent_directory PARENT_DIRECTORY)
  6. if(zopfli_parent_directory STREQUAL "")
  7. set(zopfli_standalone TRUE)
  8. endif()
  9. unset(zopfli_parent_directory)
  10. #
  11. # Options
  12. #
  13. # ZOPFLI_BUILD_SHARED controls if Zopfli libraries are built as shared or
  14. # static
  15. #
  16. # It defaults to the value of BUILD_SHARED_LIBS if set, and in most cases
  17. # that should be used instead. The purpose of ZOPFLI_BUILD_SHARED is to allow
  18. # overriding it when built as a subproject.
  19. set(zopfli_shared_default OFF)
  20. if(DEFINED BUILD_SHARED_LIBS)
  21. set(zopfli_shared_default ${BUILD_SHARED_LIBS})
  22. endif()
  23. option(ZOPFLI_BUILD_SHARED "Build Zopfli with shared libraries" ${zopfli_shared_default})
  24. unset(zopfli_shared_default)
  25. # ZOPFLI_BUILD_INSTALL controls if Zopfli adds an install target to the build
  26. #
  27. # When built standalone or as a shared library subproject, the default is ON,
  28. # and for static library subproject the default is OFF.
  29. if(zopfli_standalone OR ZOPFLI_BUILD_SHARED)
  30. option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" ON)
  31. else()
  32. option(ZOPFLI_BUILD_INSTALL "Add Zopfli install target" OFF)
  33. endif()
  34. # ZOPFLI_DEFAULT_RELEASE enables changing empty build type to Release
  35. #
  36. # Make based single-configuration generators default to an empty build type,
  37. # which might be surprising, but could be useful if you want full control over
  38. # compiler and linker flags. When ZOPFLI_DEFAULT_RELEASE is ON, change an
  39. # empty default build type to Release.
  40. option(ZOPFLI_DEFAULT_RELEASE "If CMAKE_BUILD_TYPE is empty, default to Release" ON)
  41. if(zopfli_standalone AND ZOPFLI_DEFAULT_RELEASE)
  42. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  43. message(STATUS "CMAKE_BUILD_TYPE empty, defaulting to Release")
  44. set(CMAKE_BUILD_TYPE Release)
  45. endif()
  46. endif()
  47. #
  48. # Library version
  49. #
  50. set(ZOPFLI_VERSION_MAJOR 1)
  51. set(ZOPFLI_VERSION_MINOR 0)
  52. set(ZOPFLI_VERSION_PATCH 3)
  53. set(ZOPFLI_VERSION ${ZOPFLI_VERSION_MAJOR}.${ZOPFLI_VERSION_MINOR}.${ZOPFLI_VERSION_PATCH})
  54. if(ZOPFLI_BUILD_SHARED)
  55. set(zopfli_library_type SHARED)
  56. else()
  57. set(zopfli_library_type STATIC)
  58. endif()
  59. include(GNUInstallDirs)
  60. #
  61. # libzopfli
  62. #
  63. add_library(libzopfli ${zopfli_library_type}
  64. src/zopfli/blocksplitter.c
  65. src/zopfli/cache.c
  66. src/zopfli/deflate.c
  67. src/zopfli/gzip_container.c
  68. src/zopfli/hash.c
  69. src/zopfli/katajainen.c
  70. src/zopfli/lz77.c
  71. src/zopfli/squeeze.c
  72. src/zopfli/tree.c
  73. src/zopfli/util.c
  74. src/zopfli/zlib_container.c
  75. src/zopfli/zopfli_lib.c
  76. )
  77. target_include_directories(libzopfli
  78. INTERFACE
  79. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopfli>
  80. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  81. )
  82. set_target_properties(libzopfli PROPERTIES
  83. OUTPUT_NAME zopfli
  84. VERSION ${ZOPFLI_VERSION}
  85. SOVERSION ${ZOPFLI_VERSION_MAJOR}
  86. )
  87. if(UNIX AND NOT (BEOS OR HAIKU))
  88. target_link_libraries(libzopfli m)
  89. endif()
  90. #
  91. # libzopflipng
  92. #
  93. add_library(libzopflipng ${zopfli_library_type}
  94. src/zopflipng/zopflipng_lib.cc
  95. src/zopflipng/lodepng/lodepng.cpp
  96. src/zopflipng/lodepng/lodepng_util.cpp
  97. )
  98. target_link_libraries(libzopflipng libzopfli)
  99. target_include_directories(libzopflipng
  100. INTERFACE
  101. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/zopflipng>
  102. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  103. )
  104. set_target_properties(libzopflipng PROPERTIES
  105. OUTPUT_NAME zopflipng
  106. VERSION ${ZOPFLI_VERSION}
  107. SOVERSION ${ZOPFLI_VERSION_MAJOR}
  108. )
  109. # MSVC does not export symbols by default when building a DLL, this is a
  110. # workaround for recent versions of CMake
  111. if(MSVC AND ZOPFLI_BUILD_SHARED)
  112. if(CMAKE_VERSION VERSION_LESS 3.4)
  113. message(WARNING "Automatic export of all symbols to DLL not supported until CMake 3.4")
  114. else()
  115. set_target_properties(libzopfli PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
  116. set_target_properties(libzopflipng PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
  117. endif()
  118. endif()
  119. #
  120. # zopfli
  121. #
  122. add_executable(zopfli src/zopfli/zopfli_bin.c)
  123. target_link_libraries(zopfli libzopfli)
  124. if(MSVC)
  125. target_compile_definitions(zopfli PRIVATE _CRT_SECURE_NO_WARNINGS)
  126. endif()
  127. #
  128. # zopflipng
  129. #
  130. add_executable(zopflipng src/zopflipng/zopflipng_bin.cc)
  131. target_link_libraries(zopflipng libzopflipng)
  132. if(MSVC)
  133. target_compile_definitions(zopflipng PRIVATE _CRT_SECURE_NO_WARNINGS)
  134. endif()
  135. # Create aliases
  136. #
  137. # Makes targets available to projects using Zopfli as a subproject using the
  138. # same names as in the config file package.
  139. if(NOT CMAKE_VERSION VERSION_LESS 3.0)
  140. add_library(Zopfli::libzopfli ALIAS libzopfli)
  141. add_library(Zopfli::libzopflipng ALIAS libzopflipng)
  142. add_executable(Zopfli::zopfli ALIAS zopfli)
  143. add_executable(Zopfli::zopflipng ALIAS zopflipng)
  144. endif()
  145. #
  146. # Install
  147. #
  148. if(ZOPFLI_BUILD_INSTALL)
  149. # Install binaries, libraries, and headers
  150. install(TARGETS libzopfli libzopflipng zopfli zopflipng
  151. EXPORT ZopfliTargets
  152. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  153. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  154. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  155. )
  156. install(FILES src/zopfli/zopfli.h src/zopflipng/zopflipng_lib.h
  157. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  158. )
  159. # Install config file package
  160. #
  161. # This allows CMake based projects to use the installed libraries with
  162. # find_package(Zopfli).
  163. if(NOT CMAKE_VERSION VERSION_LESS 3.0)
  164. include(CMakePackageConfigHelpers)
  165. write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake
  166. VERSION ${ZOPFLI_VERSION}
  167. COMPATIBILITY SameMajorVersion
  168. )
  169. # Since we have no dependencies, use export file directly as config file
  170. install(EXPORT ZopfliTargets
  171. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli
  172. NAMESPACE Zopfli::
  173. FILE ZopfliConfig.cmake
  174. )
  175. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ZopfliConfigVersion.cmake
  176. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zopfli
  177. )
  178. endif()
  179. endif()