]> git.xonotic.org Git - xonotic/darkplaces.git/blob - buildsys/module/FindPNG.cmake
cmake: Initial working implementation of cmake build system
[xonotic/darkplaces.git] / buildsys / module / FindPNG.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindPNG
6 -------
7
8 Find libpng, the official reference library for the PNG image format.
9
10 Imported targets
11 ^^^^^^^^^^^^^^^^
12
13 This module defines the following :prop_tgt:`IMPORTED` target:
14
15 ``PNG::PNG``
16   The libpng library, if found.
17
18 Result variables
19 ^^^^^^^^^^^^^^^^
20
21 This module will set the following variables in your project:
22
23 ``PNG_INCLUDE_DIRS``
24   where to find png.h, etc.
25 ``PNG_LIBRARIES``
26   the libraries to link against to use PNG.
27 ``PNG_DEFINITIONS``
28   You should add_definitions(${PNG_DEFINITIONS}) before compiling code
29   that includes png library files.
30 ``PNG_FOUND``
31   If false, do not try to use PNG.
32 ``PNG_VERSION_STRING``
33   the version of the PNG library found (since CMake 2.8.8)
34
35 Obsolete variables
36 ^^^^^^^^^^^^^^^^^^
37
38 The following variables may also be set, for backwards compatibility:
39
40 ``PNG_LIBRARY``
41   where to find the PNG library.
42 ``PNG_INCLUDE_DIR``
43   where to find the PNG headers (same as PNG_INCLUDE_DIRS)
44
45 Since PNG depends on the ZLib compression library, none of the above
46 will be defined unless ZLib can be found.
47 #]=======================================================================]
48
49 if(PNG_FIND_QUIETLY)
50   set(_FIND_ZLIB_ARG QUIET)
51 endif()
52 find_package(ZLIB ${_FIND_ZLIB_ARG})
53
54 if(ZLIB_FOUND)
55   find_path(PNG_PNG_INCLUDE_DIR png.h PATH_SUFFIXES include/libpng)
56
57   list(APPEND PNG_NAMES png libpng)
58   unset(PNG_NAMES_DEBUG)
59   set(_PNG_VERSION_SUFFIXES 17 16 15 14 12)
60   if (PNG_FIND_VERSION MATCHES "^([0-9]+)\\.([0-9]+)(\\..*)?$")
61     set(_PNG_VERSION_SUFFIX_MIN "${CMAKE_MATCH_1}${CMAKE_MATCH_2}")
62     if (PNG_FIND_VERSION_EXACT)
63       set(_PNG_VERSION_SUFFIXES ${_PNG_VERSION_SUFFIX_MIN})
64     else ()
65       string(REGEX REPLACE
66           "${_PNG_VERSION_SUFFIX_MIN}.*" "${_PNG_VERSION_SUFFIX_MIN}"
67           _PNG_VERSION_SUFFIXES "${_PNG_VERSION_SUFFIXES}")
68     endif ()
69     unset(_PNG_VERSION_SUFFIX_MIN)
70   endif ()
71   foreach(v IN LISTS _PNG_VERSION_SUFFIXES)
72     list(APPEND PNG_NAMES png${v} libpng${v} libpng${v}_static)
73     list(APPEND PNG_NAMES_DEBUG png${v}d libpng${v}d libpng${v}_staticd)
74   endforeach()
75   unset(_PNG_VERSION_SUFFIXES)
76   # For compatibility with versions prior to this multi-config search, honor
77   # any PNG_LIBRARY that is already specified and skip the search.
78   if(NOT PNG_LIBRARY)
79     find_library(PNG_LIBRARY_RELEASE NAMES ${PNG_NAMES})
80     find_library(PNG_LIBRARY_DEBUG NAMES ${PNG_NAMES_DEBUG})
81     include(SelectLibraryConfigurations)
82     select_library_configurations(PNG)
83     mark_as_advanced(PNG_LIBRARY_RELEASE PNG_LIBRARY_DEBUG)
84   endif()
85   unset(PNG_NAMES)
86   unset(PNG_NAMES_DEBUG)
87
88   # Set by select_library_configurations(), but we want the one from
89   # find_package_handle_standard_args() below.
90   unset(PNG_FOUND)
91
92   if (PNG_LIBRARY AND PNG_PNG_INCLUDE_DIR)
93       # png.h includes zlib.h. Sigh.
94       set(PNG_INCLUDE_DIRS ${PNG_PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} )
95       set(PNG_INCLUDE_DIR ${PNG_INCLUDE_DIRS} ) # for backward compatibility
96       set(PNG_LIBRARIES ${PNG_LIBRARY} ${ZLIB_LIBRARY})
97       if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
98          ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
99         list(APPEND PNG_LIBRARIES m)
100       endif()
101
102       if (CYGWIN)
103         if(BUILD_SHARED_LIBS)
104            # No need to define PNG_USE_DLL here, because it's default for Cygwin.
105         else()
106           set (PNG_DEFINITIONS -DPNG_STATIC)
107           set(_PNG_COMPILE_DEFINITIONS PNG_STATIC)
108         endif()
109       endif ()
110
111       if(NOT TARGET PNG::PNG)
112         add_library(PNG::PNG UNKNOWN IMPORTED)
113         set_target_properties(PNG::PNG PROPERTIES
114           INTERFACE_COMPILE_DEFINITIONS "${_PNG_COMPILE_DEFINITIONS}"
115           INTERFACE_INCLUDE_DIRECTORIES "${PNG_INCLUDE_DIRS}"
116           INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
117         if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND
118            ("${PNG_LIBRARY}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))
119           set_property(TARGET PNG::PNG APPEND PROPERTY
120             INTERFACE_LINK_LIBRARIES m)
121         endif()
122
123         if(EXISTS "${PNG_LIBRARY}")
124           set_target_properties(PNG::PNG PROPERTIES
125             IMPORTED_LINK_INTERFACE_LANGUAGES "C"
126             IMPORTED_LOCATION "${PNG_LIBRARY}")
127         endif()
128         if(EXISTS "${PNG_LIBRARY_RELEASE}")
129           set_property(TARGET PNG::PNG APPEND PROPERTY
130             IMPORTED_CONFIGURATIONS RELEASE)
131           set_target_properties(PNG::PNG PROPERTIES
132             IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
133             IMPORTED_LOCATION_RELEASE "${PNG_LIBRARY_RELEASE}")
134         endif()
135         if(EXISTS "${PNG_LIBRARY_DEBUG}")
136           set_property(TARGET PNG::PNG APPEND PROPERTY
137             IMPORTED_CONFIGURATIONS DEBUG)
138           set_target_properties(PNG::PNG PROPERTIES
139             IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
140             IMPORTED_LOCATION_DEBUG "${PNG_LIBRARY_DEBUG}")
141         endif()
142       endif()
143
144       unset(_PNG_COMPILE_DEFINITIONS)
145   endif ()
146
147   if (PNG_PNG_INCLUDE_DIR AND EXISTS "${PNG_PNG_INCLUDE_DIR}/png.h")
148       file(STRINGS "${PNG_PNG_INCLUDE_DIR}/png.h" png_version_str REGEX "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\".+\"")
149
150       string(REGEX REPLACE "^#define[ \t]+PNG_LIBPNG_VER_STRING[ \t]+\"([^\"]+)\".*" "\\1" PNG_VERSION_STRING "${png_version_str}")
151       unset(png_version_str)
152   endif ()
153 endif()
154
155 include(FindPackageHandleStandardArgs)
156 find_package_handle_standard_args(PNG
157                                   REQUIRED_VARS PNG_LIBRARY PNG_PNG_INCLUDE_DIR
158                                   VERSION_VAR PNG_VERSION_STRING)
159
160 mark_as_advanced(PNG_PNG_INCLUDE_DIR PNG_LIBRARY )