CMakeLists.txt 13.5 KB
Newer Older
1 2
# CMakeLists.txt

3
# Copyright (C) 2007-2015 Glenn Randers-Pehrson
4 5 6 7 8

# This code is released under the libpng license.
# For conditions of distribution and use, see the disclaimer
# and license in png.h

9
cmake_minimum_required(VERSION 2.4.4)
10 11
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

12 13
set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")

14
project(libpng C)
15
enable_testing()
16

17
set(PNGLIB_MAJOR 1)
18
set(PNGLIB_MINOR 6)
19
set(PNGLIB_RELEASE 19)
20 21
set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
22 23 24

# needed packages
find_package(ZLIB REQUIRED)
25 26
include_directories(${ZLIB_INCLUDE_DIR})

27
if(NOT WIN32)
28 29 30 31 32
  find_library(M_LIBRARY
    NAMES m
    PATHS /usr/lib /usr/local/lib
  )
  if(NOT M_LIBRARY)
33
    message(STATUS "math lib 'libm' not found; floating point support disabled")
34
  endif()
35
else()
36 37
  # not needed on windows
  set(M_LIBRARY "")
38
endif()
39

40
# COMMAND LINE OPTIONS
41 42 43
option(PNG_SHARED "Build shared lib" ON)
option(PNG_STATIC "Build static lib" ON)
option(PNG_TESTS  "Build libpng tests" ON)
44

45
# Many more configuration options could be added here
46 47 48
option(PNG_FRAMEWORK "Build OS X framework" OFF)
option(PNG_DEBUG     "Build with debug output" OFF)
option(PNGARG        "Disable ANSI-C prototypes" OFF)
49

50
# SET LIBNAME
51
set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
52 53 54 55

# to distinguish between debug and release lib
set(CMAKE_DEBUG_POSTFIX "d")

56
# Use the prebuilt pnglibconf.h file from the scripts folder
57 58 59
# TODO: fix this by building with awk; without this no cmake build can be
# configured directly (to do so indirectly use your local awk to build a
# pnglibconf.h in the build directory.)
60 61 62
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
               ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
63

64
# OUR SOURCES
65
set(libpng_public_hdrs
66 67
  png.h
  pngconf.h
68 69 70 71
  ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h
)
set(libpng_sources
  ${libpng_public_hdrs}
72 73
  pngdebug.h
  pnginfo.h
74
  pngpriv.h
75
  pngstruct.h
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  png.c
  pngerror.c
  pngget.c
  pngmem.c
  pngpread.c
  pngread.c
  pngrio.c
  pngrtran.c
  pngrutil.c
  pngset.c
  pngtrans.c
  pngwio.c
  pngwrite.c
  pngwtran.c
  pngwutil.c
91 92
)
set(pngtest_sources
93
  pngtest.c
94
)
95
set(pngvalid_sources
96 97 98 99
  contrib/libtests/pngvalid.c
)
set(pngstest_sources
  contrib/libtests/pngstest.c
100
)
101
# SOME NEEDED DEFINITIONS
102

103
if(MSVC)
104
  add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
105 106
endif(MSVC)

107
if(PNG_DEBUG)
108
  add_definitions(-DPNG_DEBUG)
109
endif()
110 111

# NOW BUILD OUR TARGET
112
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
113

114 115
unset(PNG_LIB_TARGETS)

116
if(PNG_SHARED)
117
  add_library(${PNG_LIB_NAME} SHARED ${libpng_sources})
118
  set(PNG_LIB_TARGETS ${PNG_LIB_NAME})
119 120 121
  if(MSVC)
    # msvc does not append 'lib' - do it here to have consistent name
    set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
122
    set_target_properties(${PNG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
123 124
  endif()
  target_link_libraries(${PNG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY})
125 126
endif()

127
if(PNG_STATIC)
128
  # does not work without changing name
129 130
  set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)
  add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources})
131
  list(APPEND PNG_LIB_TARGETS ${PNG_LIB_NAME_STATIC})
132 133 134 135
  if(MSVC)
    # msvc does not append 'lib' - do it here to have consistent name
    set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
  endif()
136
  target_link_libraries(${PNG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
137 138
endif()

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
if(PNG_FRAMEWORK)
  set(PNG_LIB_NAME_FRAMEWORK ${PNG_LIB_NAME}_framework)
  add_library(${PNG_LIB_NAME_FRAMEWORK} SHARED ${libpng_sources})
  list(APPEND PNG_LIB_TARGETS ${PNG_LIB_NAME_FRAMEWORK})
  set_target_properties(${PNG_LIB_NAME_FRAMEWORK} PROPERTIES
    FRAMEWORK TRUE
    FRAMEWORK_VERSION ${PNGLIB_VERSION}
    MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${PNGLIB_MAJOR}.${PNGLIB_MINOR}
    MACOSX_FRAMEWORK_BUNDLE_VERSION ${PNGLIB_VERSION}
    MACOSX_FRAMEWORK_IDENTIFIER org.libpng.libpng
    XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
    PUBLIC_HEADER "${libpng_public_hdrs}"
    OUTPUT_NAME png)
  target_link_libraries(${PNG_LIB_NAME_FRAMEWORK} ${ZLIB_LIBRARY} ${M_LIBRARY})
endif()

155 156 157
if(NOT PNG_LIB_TARGETS)
  message(SEND_ERROR
    "No library variant selected to build. "
158 159
    "Please enable at least one of the following options: "
    " PNG_STATIC, PNG_SHARED, PNG_FRAMEWORK")
160 161
endif()

162
if(PNG_SHARED AND WIN32)
163
  set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
164
endif()
165

166
if(PNG_TESTS AND PNG_SHARED)
167 168 169
  # does not work with msvc due to png_lib_ver issue
  add_executable(pngtest ${pngtest_sources})
  target_link_libraries(pngtest ${PNG_LIB_NAME})
170
  add_test(pngtest ./pngtest ${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png)
171 172 173 174
  #
  add_executable(pngvalid ${pngvalid_sources})
  target_link_libraries(pngvalid ${PNG_LIB_NAME})
  add_test(pngvalid ./pngvalid)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  add_executable(pngstest ${pngstest_sources})
  target_link_libraries(pngstest ${PNG_LIB_NAME})
  add_test(pngstest ./pngstest
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g01.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g02.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g04.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g16.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c16.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p01.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p02.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p04.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a16.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a16.png
193 194 195
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g01.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g02.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g04.png
196 197 198 199 200
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn2c16.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn3p08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn2c16.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn3p08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbrn2c08.png
201
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn0g16.png
202 203
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn3p08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbyn3p08.png
204
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n0g08.png
205 206 207 208
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n2c08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n3p08.png
    ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp1n3p08.png
  )
209
endif()
210

211 212
# Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
213
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
214 215
ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)

216 217 218
# Set a variable with CMake code which:
# Creates a symlink from src to dest (if possible) or alternatively
# copies if different.
219
macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
220
  FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
221
  if(WIN32 AND NOT CYGWIN AND NOT MSYS)
222 223
    ADD_CUSTOM_COMMAND(
        OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}   ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
224 225 226
        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
        DEPENDS ${PNG_LIB_TARGETS}
227 228
        )
    ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
229
  else(WIN32 AND NOT CYGWIN AND NOT MSYS)
230
    get_filename_component(LINK_TARGET "${SRC_FILE}" NAME)
231
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
232 233
    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
234
  endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
235
endmacro()
236

237
# libpng is a library so default to 'lib'
238
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
239
  set(CMAKE_INSTALL_LIBDIR lib)
240
endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
241 242 243 244 245

# CREATE PKGCONFIG FILES
# we use the same files like ./configure, so we have to set its vars
# Only do this on Windows for Cygwin - the files don't make much sense outside
# a UNIX look alike
246 247 248 249 250 251 252 253 254 255 256 257 258
if(NOT WIN32 OR CYGWIN OR MINGW)
  set(prefix      ${CMAKE_INSTALL_PREFIX})
  set(exec_prefix ${CMAKE_INSTALL_PREFIX})
  set(libdir      ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
  set(includedir  ${CMAKE_INSTALL_PREFIX}/include)
  set(LIBS        "-lz -lm")
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng.pc.in
    ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc @ONLY)
  CREATE_SYMLINK(${PNGLIB_NAME}.pc libpng.pc)

  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng-config.in
    ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config @ONLY)
  CREATE_SYMLINK(${PNGLIB_NAME}-config libpng-config)
259
endif(NOT WIN32 OR CYGWIN OR MINGW)
260 261

# SET UP LINKS
262 263
if(PNG_SHARED)
  set_target_properties(${PNG_LIB_NAME} PROPERTIES
264
#   VERSION 16.${PNGLIB_RELEASE}.1.6.19beta02
265 266
    VERSION 16.${PNGLIB_RELEASE}.0
    SOVERSION 16
267
    CLEAN_DIRECT_OUTPUT 1)
268
endif()
269
if(PNG_STATIC)
270 271 272 273
  # MSVC doesn't use a different file extension for shared vs. static
  # libs.  We are able to change OUTPUT_NAME to remove the _static
  # for all other platforms.
  if(NOT MSVC)
274
    set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES
275 276
      OUTPUT_NAME ${PNG_LIB_NAME}
      CLEAN_DIRECT_OUTPUT 1)
277 278 279
  endif()
endif()

280 281 282 283 284 285 286 287 288
# If CMake > 2.4.x, we set a variable used below to export
# targets to an export file.
# TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
  set(PNG_EXPORT_RULE EXPORT libpng)
elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
  set(PNG_EXPORT_RULE EXPORT libpng)
endif()

289 290
# INSTALL
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
291 292 293 294
  install(TARGETS ${PNG_LIB_TARGETS}
      ${PNG_EXPORT_RULE}
      RUNTIME DESTINATION bin
      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
295 296
      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
      FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR})
297

298
  if(PNG_SHARED)
299
    # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
300
    if(CYGWIN OR MINGW)
301
       get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
302
       CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
303 304
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
305
    endif(CYGWIN OR MINGW)
306 307

    if(NOT WIN32)
308
      get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
309
      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
310 311
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
312 313
    endif(NOT WIN32)
  endif(PNG_SHARED)
314

315
  if(PNG_STATIC)
316
    if(NOT WIN32 OR CYGWIN OR MINGW)
317
      get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
318
      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
319 320
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
321
    endif(NOT WIN32 OR CYGWIN OR MINGW)
322
 endif()
323 324 325
endif()

if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
326 327
  install(FILES ${libpng_public_hdrs}   DESTINATION include)
  install(FILES ${libpng_public_hdrs}   DESTINATION include/${PNGLIB_NAME})
328 329
endif()
if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
330 331 332 333 334
  if(NOT WIN32 OR CYGWIN OR MINGW)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
            DESTINATION bin)
  endif(NOT WIN32 OR CYGWIN OR MINGW)
335
endif()
336

337
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
338
  # Install man pages
339 340 341 342 343
  if(NOT PNG_MAN_DIR)
    set(PNG_MAN_DIR "share/man")
  endif()
  install(FILES libpng.3 libpngpf.3      DESTINATION ${PNG_MAN_DIR}/man3)
  install(FILES png.5                    DESTINATION ${PNG_MAN_DIR}/man5)
344
  # Install pkg-config files
345 346 347 348 349 350 351 352 353 354
  if(NOT WIN32 OR CYGWIN OR MINGW)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
            DESTINATION bin)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
            DESTINATION bin)
  endif(NOT WIN32 OR CYGWIN OR MINGW)
355
endif()
356

357 358 359 360 361 362
# On versions of CMake that support it, create an export file CMake
# users can include() to import our targets
if(PNG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
  install(EXPORT libpng DESTINATION lib/libpng FILE lib${PNG_LIB_NAME}.cmake)
endif()

363
# what's with libpng-manual.txt and all the extra files?
364

365
# UNINSTALL
366 367
# do we need this?

368
# DIST
369 370 371 372 373
# do we need this?

# to create msvc import lib for mingw compiled shared lib
# pexports libpng.dll > libpng.def
# lib /def:libpng.def /machine:x86
374