target_enable_png.cmake 1.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
macro(target_enable_png _TARGET_ _SCOPE_)
    if (${ARGC} GREATER 2)
      set(LINK_EXECUTABLE ON)
    else ()
      set(LINK_EXECUTABLE OFF)
    endif ()

    find_file(_PNG_H png.h
      HINTS ENV CPATH /usr/include
      DOC "headerfile png.h for PNG-READER")

12
    if (NOT _PNG_H)
13
      message(FATAL_ERROR "Could not find the PNG header png.h.")
14
15
16
17
18
19
20
21
22
23
24
25
26
    else ()
      file(STRINGS ${_PNG_H} PNG_VERSION_LINE REGEX "#define PNG_LIBPNG_VER_STRING")
      string(REGEX MATCH "\"([0-9]+[.][0-9]+([.][0-9]+)?)\"" PNG_VERSION_LINE ${PNG_VERSION_LINE})
      set(PNG_VERSION ${CMAKE_MATCH_1})

      message(STATUS "Libpng version: ${PNG_VERSION}")
      if (NOT PNG_VERSION VERSION_LESS "1.6" OR NOT PNG_VERSION VERSION_GREATER "1.2")
        message(FATAL_ERROR "Incompatible libpng version. Requires 1.2.x")
      endif ()
    endif ()

    get_filename_component(PNG_PATH ${_PNG_H} PATH)
    target_include_directories(${_TARGET_} ${_SCOPE_} ${PNG_PATH})
27
28
29
30
31
32
33
34
35
36
37
38
39
40

    if (LINK_EXECUTABLE)
      find_library(_PNG_LIB png
        HINTS ENV LIBRARY_PATH
        DOC "The PNG library")
      if (_PNG_LIB)
        target_link_libraries(${_TARGET_} ${_SCOPE_} ${_PNG_LIB})
      else()
        message(FATAL_ERROR "Could not find the PNG library")
      endif()
    endif (LINK_EXECUTABLE)

    target_compile_definitions(${_TARGET_} ${_SCOPE_} HAVE_PNG=1)
endmacro(target_enable_png)