1# CMakeLists.txt
2
3# Copyright (C) 2007,2009-2016 Glenn Randers-Pehrson
4# Written by Christian Ehrlicher, 2007
5# Revised by Roger Lowman, 2009-2010
6# Revised by Clifford Yapp, 2011-2012
7# Revised by Roger Leigh, 2016
8
9# This code is released under the libpng license.
10# For conditions of distribution and use, see the disclaimer
11# and license in png.h
12
13cmake_minimum_required(VERSION 2.8.3)
14cmake_policy(VERSION 2.8.3)
15
16# Set MacOSX @rpath usage globally.
17if (POLICY CMP0020)
18  cmake_policy(SET CMP0020 NEW)
19endif(POLICY CMP0020)
20if (POLICY CMP0042)
21  cmake_policy(SET CMP0042 NEW)
22endif(POLICY CMP0042)
23# Use new variable expansion policy.
24if (POLICY CMP0053)
25  cmake_policy(SET CMP0053 NEW)
26endif(POLICY CMP0053)
27if (POLICY CMP0054)
28  cmake_policy(SET CMP0054 NEW)
29endif(POLICY CMP0054)
30
31set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")
32
33project(libpng C)
34enable_testing()
35
36set(PNGLIB_MAJOR 1)
37set(PNGLIB_MINOR 6)
38set(PNGLIB_RELEASE 22)
39set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
40set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
41
42# needed packages
43find_package(ZLIB REQUIRED)
44include_directories(${ZLIB_INCLUDE_DIR})
45
46if(NOT WIN32)
47  find_library(M_LIBRARY
48    NAMES m
49    PATHS /usr/lib /usr/local/lib
50  )
51  if(NOT M_LIBRARY)
52    message(STATUS "math lib 'libm' not found; floating point support disabled")
53  endif()
54else()
55  # not needed on windows
56  set(M_LIBRARY "")
57endif()
58
59# COMMAND LINE OPTIONS
60option(PNG_SHARED "Build shared lib" ON)
61option(PNG_STATIC "Build static lib" ON)
62option(PNG_TESTS  "Build libpng tests" ON)
63
64# Many more configuration options could be added here
65option(PNG_FRAMEWORK "Build OS X framework" OFF)
66option(PNG_DEBUG     "Build with debug output" OFF)
67option(PNGARG        "Disable ANSI-C prototypes" OFF)
68
69set(PNG_PREFIX "" CACHE STRING "Prefix to add to the API function names")
70set(DFA_XTRA "" CACHE FILEPATH "File containing extra configuration settings")
71
72# SET LIBNAME
73set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
74
75# to distinguish between debug and release lib
76set(CMAKE_DEBUG_POSTFIX "d")
77
78include(CheckCSourceCompiles)
79option(ld-version-script "Enable linker version script" ON)
80if(ld-version-script AND NOT APPLE)
81  # Check if LD supports linker scripts.
82  file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map" "VERS_1 {
83        global: sym;
84        local: *;
85};
86
87VERS_2 {
88        global: sym2;
89                main;
90} VERS_1;
91")
92  set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
93  set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} "-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/conftest.map'")
94  check_c_source_compiles("void sym(void) {}
95void sym2(void) {}
96int main(void) {return 0;}
97" HAVE_LD_VERSION_SCRIPT)
98  if(NOT HAVE_LD_VERSION_SCRIPT)
99    set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE} "-Wl,-M -Wl,${CMAKE_CURRENT_BINARY_DIR}/conftest.map")
100  check_c_source_compiles("void sym(void) {}
101void sym2(void) {}
102int main(void) {return 0;}
103" HAVE_SOLARIS_LD_VERSION_SCRIPT)
104  endif()
105  set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
106  file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map")
107endif()
108
109# Find symbol prefix.  Likely obsolete and unnecessary with recent
110# toolchains (it's not done in many other projects).
111function(symbol_prefix)
112  set(SYMBOL_PREFIX)
113
114  execute_process(COMMAND "${CMAKE_C_COMPILER}" "-E" "-"
115                  INPUT_FILE /dev/null
116                  OUTPUT_VARIABLE OUT
117                  RESULT_VARIABLE STATUS)
118
119  if(CPP_FAIL)
120    message(WARNING "Failed to run the C preprocessor")
121  endif()
122
123  string(REPLACE "\n" ";" OUT "${OUT}")
124  foreach(line ${OUT})
125    string(REGEX MATCH "^PREFIX=" found_match "${line}")
126    if(found_match)
127      STRING(REGEX REPLACE "^PREFIX=(.*\)" "\\1" prefix "${line}")
128      string(REGEX MATCH "__USER_LABEL_PREFIX__" found_match "${prefix}")
129      if(found_match)
130        STRING(REGEX REPLACE "(.*)__USER_LABEL_PREFIX__(.*)" "\\1\\2" prefix "${prefix}")
131      endif()
132      set(SYMBOL_PREFIX "${prefix}")
133    endif()
134  endforeach()
135
136    message(STATUS "Symbol prefix: ${SYMBOL_PREFIX}")
137    set(SYMBOL_PREFIX "${SYMBOL_PREFIX}" PARENT_SCOPE)
138endfunction()
139
140if(UNIX)
141  symbol_prefix()
142endif()
143
144find_program(AWK NAMES gawk awk)
145
146include_directories(${CMAKE_CURRENT_BINARY_DIR})
147
148if(NOT AWK)
149  # No awk available to generate sources; use pre-built pnglibconf.h
150  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
151                 ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
152  add_custom_target(genfiles) # Dummy
153else()
154  include(CMakeParseArguments)
155  # Generate .chk from .out with awk
156  # generate_chk(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]])
157  function(generate_chk)
158    set(options)
159    set(oneValueArgs INPUT OUTPUT)
160    set(multiValueArgs DEPENDS)
161    cmake_parse_arguments(_GC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
162    if (NOT _GC_INPUT)
163      message(FATAL_ERROR "Invalid arguments.  generate_out requires input.")
164    endif()
165    if (NOT _GC_OUTPUT)
166      message(FATAL_ERROR "Invalid arguments.  generate_out requires output.")
167    endif()
168
169    add_custom_command(OUTPUT "${_GC_OUTPUT}"
170                       COMMAND "${CMAKE_COMMAND}"
171                               "-DINPUT=${_GC_INPUT}"
172                               "-DOUTPUT=${_GC_OUTPUT}"
173                               -P "${CMAKE_CURRENT_BINARY_DIR}/scripts/genchk.cmake"
174                       DEPENDS "${_GC_INPUT}" ${_GC_DEPENDS}
175                       WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
176  endfunction()
177
178  # Generate .out from .c with awk
179  # generate_out(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]])
180  function(generate_out)
181    set(options)
182    set(oneValueArgs INPUT OUTPUT)
183    set(multiValueArgs DEPENDS)
184    cmake_parse_arguments(_GO "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
185    if (NOT _GO_INPUT)
186      message(FATAL_ERROR "Invalid arguments.  generate_out requires input.")
187    endif()
188    if (NOT _GO_OUTPUT)
189      message(FATAL_ERROR "Invalid arguments.  generate_out requires output.")
190    endif()
191
192    add_custom_command(OUTPUT "${_GO_OUTPUT}"
193                       COMMAND "${CMAKE_COMMAND}"
194                               "-DINPUT=${_GO_INPUT}"
195                               "-DOUTPUT=${_GO_OUTPUT}"
196                               -P "${CMAKE_CURRENT_BINARY_DIR}/scripts/genout.cmake"
197                       DEPENDS "${_GO_INPUT}" ${_GO_DEPENDS}
198                       WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
199  endfunction()
200
201  # Generate specific source file with awk
202  # generate_source(OUTPUT outputfile [DEPENDS dep1 [dep2...]])
203  function(generate_source)
204    set(options)
205    set(oneValueArgs OUTPUT)
206    set(multiValueArgs DEPENDS)
207    cmake_parse_arguments(_GSO "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
208    if (NOT _GSO_OUTPUT)
209      message(FATAL_ERROR "Invalid arguments.  generate_source requires output.")
210    endif()
211
212    add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_GSO_OUTPUT}"
213                       COMMAND "${CMAKE_COMMAND}"
214                               "-DOUTPUT=${_GSO_OUTPUT}"
215                               -P "${CMAKE_CURRENT_BINARY_DIR}/scripts/gensrc.cmake"
216                       DEPENDS ${_GSO_DEPENDS}
217                       WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
218  endfunction()
219
220  # Copy file
221  function(generate_copy source destination)
222    add_custom_command(OUTPUT "${destination}"
223                       COMMAND "${CMAKE_COMMAND}" -E remove "${destination}"
224                       COMMAND "${CMAKE_COMMAND}" -E copy "${source}"
225                                                          "${destination}"
226                       DEPENDS "${source}")
227  endfunction()
228
229  # Generate scripts/pnglibconf.h
230  generate_source(OUTPUT "scripts/pnglibconf.c"
231                  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
232                          "${CMAKE_CURRENT_SOURCE_DIR}/scripts/options.awk"
233                          "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
234
235  # Generate pnglibconf.c
236  generate_source(OUTPUT "pnglibconf.c"
237                  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
238                          "${CMAKE_CURRENT_SOURCE_DIR}/scripts/options.awk"
239                          "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
240
241  if(PNG_PREFIX)
242    set(PNGLIBCONF_H_EXTRA_DEPENDS
243        "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out"
244        "${CMAKE_CURRENT_SOURCE_DIR}/scripts/macro.lst")
245    set(PNGPREFIX_H_EXTRA_DEPENDS
246        "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out")
247  endif()
248
249  generate_out(INPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c"
250               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out")
251
252  # Generate pnglibconf.h
253  generate_source(OUTPUT "pnglibconf.h"
254                  DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out"
255                          ${PNGLIBCONF_H_EXTRA_DEPENDS})
256
257  generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/intprefix.c"
258               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out"
259               DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h")
260
261  generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/prefix.c"
262               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out"
263               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
264                       "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
265                       "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out")
266
267  # Generate pngprefix.h
268  generate_source(OUTPUT "pngprefix.h"
269                  DEPENDS ${PNGPREFIX_H_EXTRA_DEPENDS})
270
271  generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/sym.c"
272               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out"
273               DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h")
274
275  generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.c"
276               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
277               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
278                       "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
279                       "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt")
280
281  generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/vers.c"
282               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out"
283               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
284                       "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
285                       "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h")
286
287  generate_chk(INPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
288               OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk"
289               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/checksym.awk"
290                       "${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.def")
291
292  add_custom_target(symbol-check DEPENDS
293                    "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk")
294
295  generate_copy("${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out"
296                "${CMAKE_CURRENT_BINARY_DIR}/libpng.sym")
297  generate_copy("${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out"
298                "${CMAKE_CURRENT_BINARY_DIR}/libpng.vers")
299
300  add_custom_target(genvers DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libpng.vers")
301  add_custom_target(gensym DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libpng.sym")
302
303  add_custom_target("genprebuilt"
304                    COMMAND "${CMAKE_COMMAND}"
305                            "-DOUTPUT=scripts/pnglibconf.h.prebuilt"
306                            -P "${CMAKE_CURRENT_BINARY_DIR}/scripts/gensrc.cmake"
307                    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
308
309  # A single target handles generation of all generated files.  If
310  # they are dependend upon separately by multiple targets, this
311  # confuses parallel make (it would require a separate top-level
312  # target for each file to track the dependencies properly).
313  add_custom_target(genfiles DEPENDS
314    "${CMAKE_CURRENT_BINARY_DIR}/libpng.sym"
315    "${CMAKE_CURRENT_BINARY_DIR}/libpng.vers"
316    "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c"
317    "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h"
318    "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out"
319    "${CMAKE_CURRENT_BINARY_DIR}/pngprefix.h"
320    "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out"
321    "${CMAKE_CURRENT_BINARY_DIR}/scripts/pnglibconf.c"
322    "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out"
323    "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out"
324    "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk"
325    "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
326    "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out")
327endif(NOT AWK)
328
329# OUR SOURCES
330set(libpng_public_hdrs
331  png.h
332  pngconf.h
333  "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h"
334)
335set(libpng_private_hdrs
336  pngpriv.h
337  pngdebug.h
338  pnginfo.h
339  pngstruct.h
340)
341if(AWK)
342  list(APPEND libpng_private_hdrs "${CMAKE_CURRENT_BINARY_DIR}/pngprefix.h")
343endif()
344set(libpng_sources
345  ${libpng_public_hdrs}
346  ${libpng_private_hdrs}
347  png.c
348  pngerror.c
349  pngget.c
350  pngmem.c
351  pngpread.c
352  pngread.c
353  pngrio.c
354  pngrtran.c
355  pngrutil.c
356  pngset.c
357  pngtrans.c
358  pngwio.c
359  pngwrite.c
360  pngwtran.c
361  pngwutil.c
362)
363set(pngtest_sources
364  pngtest.c
365)
366set(pngvalid_sources
367  contrib/libtests/pngvalid.c
368)
369set(pngstest_sources
370  contrib/libtests/pngstest.c
371)
372set(pngunknown_sources
373  contrib/libtests/pngunknown.c
374)
375set(pngimage_sources
376  contrib/libtests/pngimage.c
377)
378set(pngfix_sources
379  contrib/tools/pngfix.c
380)
381set(png_fix_itxt_sources
382  contrib/tools/png-fix-itxt.c
383)
384
385if(MSVC)
386  add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
387endif(MSVC)
388
389if(PNG_DEBUG)
390  add_definitions(-DPNG_DEBUG)
391endif()
392
393# NOW BUILD OUR TARGET
394include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
395
396unset(PNG_LIB_TARGETS)
397
398if(PNG_SHARED)
399  add_library(png SHARED ${libpng_sources})
400  set(PNG_LIB_TARGETS png)
401  set_target_properties(png PROPERTIES OUTPUT_NAME ${PNG_LIB_NAME})
402  add_dependencies(png genfiles)
403  if(MSVC)
404    # msvc does not append 'lib' - do it here to have consistent name
405    set_target_properties(png PROPERTIES PREFIX "lib")
406    set_target_properties(png PROPERTIES IMPORT_PREFIX "lib")
407  endif()
408  target_link_libraries(png ${ZLIB_LIBRARY} ${M_LIBRARY})
409
410  if(UNIX AND AWK)
411    if(HAVE_LD_VERSION_SCRIPT)
412      set_target_properties(png PROPERTIES LINK_FLAGS
413        "-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'")
414    elseif(HAVE_SOLARIS_LD_VERSION_SCRIPT)
415      set_target_properties(png PROPERTIES LINK_FLAGS
416        "-Wl,-M -Wl,'${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'")
417    endif()
418  endif()
419endif()
420
421if(PNG_STATIC)
422  # does not work without changing name
423  set(PNG_LIB_NAME_STATIC png_static)
424  add_library(png_static STATIC ${libpng_sources})
425  add_dependencies(png_static genfiles)
426  # MSVC doesn't use a different file extension for shared vs. static
427  # libs.  We are able to change OUTPUT_NAME to remove the _static
428  # for all other platforms.
429  if(NOT MSVC)
430    set_target_properties(png_static PROPERTIES
431      OUTPUT_NAME "${PNG_LIB_NAME}"
432      CLEAN_DIRECT_OUTPUT 1)
433  else()
434    set_target_properties(png_static PROPERTIES
435      OUTPUT_NAME "${PNG_LIB_NAME}_static"
436      CLEAN_DIRECT_OUTPUT 1)
437  endif()
438  list(APPEND PNG_LIB_TARGETS png_static)
439  if(MSVC)
440    # msvc does not append 'lib' - do it here to have consistent name
441    set_target_properties(png_static PROPERTIES PREFIX "lib")
442  endif()
443  target_link_libraries(png_static ${ZLIB_LIBRARY} ${M_LIBRARY})
444endif()
445
446if(PNG_FRAMEWORK)
447  set(PNG_LIB_NAME_FRAMEWORK png_framework)
448  add_library(png_framework SHARED ${libpng_sources})
449  add_dependencies(png_framework genfiles)
450  list(APPEND PNG_LIB_TARGETS png_framework)
451  set_target_properties(png_framework PROPERTIES
452    FRAMEWORK TRUE
453    FRAMEWORK_VERSION ${PNGLIB_VERSION}
454    MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${PNGLIB_MAJOR}.${PNGLIB_MINOR}
455    MACOSX_FRAMEWORK_BUNDLE_VERSION ${PNGLIB_VERSION}
456    MACOSX_FRAMEWORK_IDENTIFIER org.libpng.libpng
457    XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
458    PUBLIC_HEADER "${libpng_public_hdrs}"
459    OUTPUT_NAME png)
460  target_link_libraries(png_framework ${ZLIB_LIBRARY} ${M_LIBRARY})
461endif()
462
463if(NOT PNG_LIB_TARGETS)
464  message(SEND_ERROR
465    "No library variant selected to build. "
466    "Please enable at least one of the following options: "
467    " PNG_STATIC, PNG_SHARED, PNG_FRAMEWORK")
468endif()
469
470if(PNG_SHARED AND WIN32)
471  set_target_properties(png PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
472endif()
473
474function(png_add_test)
475  set(options)
476  set(oneValueArgs NAME COMMAND)
477  set(multiValueArgs OPTIONS FILES)
478  cmake_parse_arguments(_PAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
479
480  if (NOT _PAT_NAME)
481    message(FATAL_ERROR "Invalid arguments.  png_add_test requires name.")
482  endif()
483  if (NOT _PAT_COMMAND)
484    message(FATAL_ERROR "Invalid arguments.  png_add_test requires command.")
485  endif()
486
487  set(TEST_OPTIONS "${_PAT_OPTIONS}")
488  set(TEST_FILES "${_PAT_FILES}")
489
490  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scripts/test.cmake.in"
491                 "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake" @ONLY)
492  if(CMAKE_MAJOR_VERSION GREATER 2) # have generator expressions
493    add_test(NAME "${_PAT_NAME}"
494             COMMAND "${CMAKE_COMMAND}"
495             "-DLIBPNG=$<TARGET_FILE:png>"
496             "-DTEST_COMMAND=$<TARGET_FILE:${_PAT_COMMAND}>"
497             -P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake")
498  else() # old 2.x add_test; limited and won't work well on Windows
499    # Note LIBPNG is a dummy value as there are no generator expressions
500    add_test("${_PAT_NAME}" "${CMAKE_COMMAND}"
501             "-DLIBPNG=${CMAKE_CURRENT_BINARY_DIR}/libpng.so"
502             "-DTEST_COMMAND=./${_PAT_COMMAND}"
503             -P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake")
504  endif()
505endfunction()
506
507if(PNG_TESTS AND PNG_SHARED)
508  # Find test PNG files by globbing, but sort lists to ensure
509  # consistency between different filesystems.
510  file(GLOB PNGSUITE_PNGS "${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/*.png")
511  list(SORT PNGSUITE_PNGS)
512  file(GLOB TEST_PNGS "${CMAKE_CURRENT_SOURCE_DIR}/contrib/testpngs/*.png")
513  list(SORT TEST_PNGS)
514
515  set(PNGTEST_PNG "${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png")
516
517  add_executable(pngtest ${pngtest_sources})
518  target_link_libraries(pngtest png)
519
520  png_add_test(NAME pngtest COMMAND pngtest FILES "${PNGTEST_PNG}")
521
522  add_executable(pngvalid ${pngvalid_sources})
523  target_link_libraries(pngvalid png)
524
525  png_add_test(NAME pngvalid-gamma-16-to-8
526               COMMAND pngvalid OPTIONS --gamma-16-to-8)
527  png_add_test(NAME pngvalid-gamma-alpha-mode
528               COMMAND pngvalid OPTIONS --gamma-alpha-mode)
529  png_add_test(NAME pngvalid-gamma-background
530               COMMAND pngvalid OPTIONS --gamma-background)
531  png_add_test(NAME pngvalid-gamma-expand16-alpha-mode
532               COMMAND pngvalid OPTIONS --gamma-alpha-mode --expand16)
533  png_add_test(NAME pngvalid-gamma-expand16-background
534               COMMAND pngvalid OPTIONS --gamma-background --expand16)
535  png_add_test(NAME pngvalid-gamma-expand16-transform
536               COMMAND pngvalid OPTIONS --gamma-transform --expand16)
537  png_add_test(NAME pngvalid-gamma-sbit
538               COMMAND pngvalid OPTIONS --gamma-sbit)
539  png_add_test(NAME pngvalid-gamma-threshold
540               COMMAND pngvalid OPTIONS --gamma-threshold)
541  png_add_test(NAME pngvalid-gamma-transform
542               COMMAND pngvalid OPTIONS --gamma-transform)
543  png_add_test(NAME pngvalid-progressive-interlace-standard
544               COMMAND pngvalid OPTIONS --standard --progressive-read --interlace)
545  png_add_test(NAME pngvalid-progressive-size
546               COMMAND pngvalid OPTIONS --size --progressive-read)
547  png_add_test(NAME pngvalid-progressive-standard
548               COMMAND pngvalid OPTIONS --standard --progressive-read)
549  png_add_test(NAME pngvalid-standard
550               COMMAND pngvalid OPTIONS --standard)
551  png_add_test(NAME pngvalid-transform
552               COMMAND pngvalid OPTIONS --transform)
553
554  add_executable(pngstest ${pngstest_sources})
555  target_link_libraries(pngstest png)
556
557  foreach(gamma_type 1.8 linear none sRGB)
558    foreach(alpha_type none alpha)
559      set(PNGSTEST_FILES)
560      foreach(test_png ${TEST_PNGS})
561        string(REGEX MATCH ".*-linear[-.].*" TEST_PNG_LINEAR "${test_png}")
562        string(REGEX MATCH ".*-sRGB[-.].*" TEST_PNG_SRGB "${test_png}")
563        string(REGEX MATCH ".*-1.8[-.].*" TEST_PNG_G18 "${test_png}")
564        string(REGEX MATCH ".*-alpha-.*" TEST_PNG_ALPHA "${test_png}")
565
566        set(TEST_PNG_VALID TRUE)
567
568        if(TEST_PNG_ALPHA)
569          if (NOT "${alpha_type}" STREQUAL "alpha")
570            set(TEST_PNG_VALID FALSE)
571          endif()
572        else()
573          if ("${alpha_type}" STREQUAL "alpha")
574            set(TEST_PNG_VALID FALSE)
575          endif()
576        endif()
577
578        if(TEST_PNG_LINEAR)
579          if(NOT "${gamma_type}" STREQUAL "linear")
580            set(TEST_PNG_VALID FALSE)
581          endif()
582        elseif(TEST_PNG_SRGB)
583          if(NOT "${gamma_type}" STREQUAL "sRGB")
584            set(TEST_PNG_VALID FALSE)
585          endif()
586        elseif(TEST_PNG_G18)
587          if(NOT "${gamma_type}" STREQUAL "1.8")
588            set(TEST_PNG_VALID FALSE)
589          endif()
590        else()
591          if(NOT "${gamma_type}" STREQUAL "none")
592            set(TEST_PNG_VALID FALSE)
593          endif()
594        endif()
595
596        if(TEST_PNG_VALID)
597          list(APPEND PNGSTEST_FILES "${test_png}")
598        endif()
599      endforeach()
600      # Should already be sorted, but sort anyway to be certain.
601      list(SORT PNGSTEST_FILES)
602      png_add_test(NAME pngstest-${gamma_type}-${alpha_type}
603                   COMMAND pngstest
604                   OPTIONS --tmpfile "${gamma_type}-${alpha_type}-" --log
605                   FILES ${PNGSTEST_FILES})
606    endforeach()
607  endforeach()
608
609  add_executable(pngunknown ${pngunknown_sources})
610  target_link_libraries(pngunknown png)
611
612  png_add_test(NAME pngunknown-discard COMMAND pngunknown OPTIONS --strict default=discard FILES "${PNGTEST_PNG}")
613  png_add_test(NAME pngunknown-IDAT COMMAND pngunknown OPTIONS --strict default=discard IDAT=save FILES "${PNGTEST_PNG}")
614  png_add_test(NAME pngunknown-if-safe COMMAND pngunknown OPTIONS --strict default=if-safe FILES "${PNGTEST_PNG}")
615  png_add_test(NAME pngunknown-sAPI COMMAND pngunknown OPTIONS --strict bKGD=save cHRM=save gAMA=save all=discard iCCP=save sBIT=save sRGB=save FILES "${PNGTEST_PNG}")
616  png_add_test(NAME pngunknown-save COMMAND pngunknown OPTIONS --strict default=save FILES "${PNGTEST_PNG}")
617  png_add_test(NAME pngunknown-sTER COMMAND pngunknown OPTIONS --strict sTER=if-safe FILES "${PNGTEST_PNG}")
618  png_add_test(NAME pngunknown-vpAg COMMAND pngunknown OPTIONS --strict vpAg=if-safe FILES "${PNGTEST_PNG}")
619
620  add_executable(pngimage ${pngimage_sources})
621  target_link_libraries(pngimage png)
622
623  png_add_test(NAME pngimage-quick COMMAND pngimage OPTIONS --list-combos --log FILES ${PNGSUITE_PNGS})
624  png_add_test(NAME pngimage-full COMMAND pngimage OPTIONS --exhaustive --list-combos --log FILES ${PNGSUITE_PNGS})
625endif()
626
627if(PNG_SHARED)
628  add_executable(pngfix ${pngfix_sources})
629  target_link_libraries(pngfix png)
630  set(PNG_BIN_TARGETS pngfix)
631
632  add_executable(png-fix-itxt ${png_fix_itxt_sources})
633  target_link_libraries(png-fix-itxt ${ZLIB_LIBRARY} ${M_LIBRARY})
634  list(APPEND PNG_BIN_TARGETS png-fix-itxt)
635endif()
636
637# Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
638IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
639  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
640ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
641
642# Set a variable with CMake code which:
643# Creates a symlink from src to dest (if possible) or alternatively
644# copies if different.
645macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
646  FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
647  if(WIN32 AND NOT CYGWIN AND NOT MSYS)
648    ADD_CUSTOM_COMMAND(
649        OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}   ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
650        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
651        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
652        DEPENDS ${PNG_LIB_TARGETS}
653        )
654    ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
655  else(WIN32 AND NOT CYGWIN AND NOT MSYS)
656    get_filename_component(LINK_TARGET "${SRC_FILE}" NAME)
657    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
658    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
659    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
660  endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
661endmacro()
662
663# Create source generation scripts.
664configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/genchk.cmake.in
665               ${CMAKE_CURRENT_BINARY_DIR}/scripts/genchk.cmake @ONLY)
666configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/genout.cmake.in
667               ${CMAKE_CURRENT_BINARY_DIR}/scripts/genout.cmake @ONLY)
668configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/gensrc.cmake.in
669               ${CMAKE_CURRENT_BINARY_DIR}/scripts/gensrc.cmake @ONLY)
670
671
672# libpng is a library so default to 'lib'
673if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
674  set(CMAKE_INSTALL_LIBDIR lib)
675endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
676
677# CREATE PKGCONFIG FILES
678# we use the same files like ./configure, so we have to set its vars
679# Only do this on Windows for Cygwin - the files don't make much sense outside
680# a UNIX look alike
681if(NOT WIN32 OR CYGWIN OR MINGW)
682  set(prefix      ${CMAKE_INSTALL_PREFIX})
683  set(exec_prefix ${CMAKE_INSTALL_PREFIX})
684  set(libdir      ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
685  set(includedir  ${CMAKE_INSTALL_PREFIX}/include)
686  set(LIBS        "-lz -lm")
687  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng.pc.in
688    ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc @ONLY)
689  CREATE_SYMLINK(${PNGLIB_NAME}.pc libpng.pc)
690
691  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng-config.in
692    ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config @ONLY)
693  CREATE_SYMLINK(${PNGLIB_NAME}-config libpng-config)
694endif(NOT WIN32 OR CYGWIN OR MINGW)
695
696# SET UP LINKS
697if(PNG_SHARED)
698  set_target_properties(png PROPERTIES
699#   VERSION 16.${PNGLIB_RELEASE}.1.6.22beta03
700    VERSION 16.${PNGLIB_RELEASE}.0
701    SOVERSION 16
702    CLEAN_DIRECT_OUTPUT 1)
703endif()
704
705# If CMake > 2.4.x, we set a variable used below to export
706# targets to an export file.
707# TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
708if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
709  set(PNG_EXPORT_RULE EXPORT libpng)
710elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
711  set(PNG_EXPORT_RULE EXPORT libpng)
712endif()
713
714# INSTALL
715if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
716  install(TARGETS ${PNG_LIB_TARGETS}
717      ${PNG_EXPORT_RULE}
718      RUNTIME DESTINATION bin
719      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
720      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
721      FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR})
722
723  if(PNG_SHARED)
724    # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
725    if(CYGWIN OR MINGW)
726       get_target_property(BUILD_TARGET_LOCATION png LOCATION_${CMAKE_BUILD_TYPE})
727       CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
728       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
729         DESTINATION ${CMAKE_INSTALL_LIBDIR})
730    endif(CYGWIN OR MINGW)
731
732    if(NOT WIN32)
733      get_target_property(BUILD_TARGET_LOCATION png LOCATION_${CMAKE_BUILD_TYPE})
734      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
735      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
736         DESTINATION ${CMAKE_INSTALL_LIBDIR})
737    endif(NOT WIN32)
738  endif(PNG_SHARED)
739
740  if(PNG_STATIC)
741    if(NOT WIN32 OR CYGWIN OR MINGW)
742      get_target_property(BUILD_TARGET_LOCATION png_static LOCATION_${CMAKE_BUILD_TYPE})
743      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
744      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
745         DESTINATION ${CMAKE_INSTALL_LIBDIR})
746    endif(NOT WIN32 OR CYGWIN OR MINGW)
747 endif()
748endif()
749
750if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
751  install(FILES ${libpng_public_hdrs}   DESTINATION include)
752  install(FILES ${libpng_public_hdrs}   DESTINATION include/${PNGLIB_NAME})
753endif()
754if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
755  if(NOT WIN32 OR CYGWIN OR MINGW)
756    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin)
757    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
758            DESTINATION bin)
759  endif(NOT WIN32 OR CYGWIN OR MINGW)
760endif()
761
762if(NOT SKIP_INSTALL_PROGRAMS AND NOT SKIP_INSTALL_ALL )
763  install(TARGETS ${PNG_BIN_TARGETS}
764      RUNTIME DESTINATION bin)
765endif()
766
767if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
768  # Install man pages
769  if(NOT PNG_MAN_DIR)
770    set(PNG_MAN_DIR "share/man")
771  endif()
772  install(FILES libpng.3 libpngpf.3      DESTINATION ${PNG_MAN_DIR}/man3)
773  install(FILES png.5                    DESTINATION ${PNG_MAN_DIR}/man5)
774  # Install pkg-config files
775  if(NOT WIN32 OR CYGWIN OR MINGW)
776    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
777            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
778    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
779            DESTINATION bin)
780    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
781            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
782    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
783            DESTINATION bin)
784  endif(NOT WIN32 OR CYGWIN OR MINGW)
785endif()
786
787# On versions of CMake that support it, create an export file CMake
788# users can include() to import our targets
789if(PNG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
790  install(EXPORT libpng DESTINATION lib/libpng FILE lib${PNG_LIB_NAME}.cmake)
791endif()
792
793# what's with libpng-manual.txt and all the extra files?
794
795# UNINSTALL
796# do we need this?
797
798# DIST
799# do we need this?
800
801# to create msvc import lib for mingw compiled shared lib
802# pexports libpng.dll > libpng.def
803# lib /def:libpng.def /machine:x86
804
805