# v3p/png/CMakeLists.txt

project( png C )

#   This is the png directory of v3p.  Current version is 1.5.10 of March 2012.

include(${VXL_CMAKE_DIR}/FindPNG.cmake)

if(NOT VXL_USING_NATIVE_PNG)

  include( ${VXL_CMAKE_DIR}/FindZLIB.cmake )

  if(ZLIB_FOUND)
    include_directories( ${ZLIB_INCLUDE_DIR} )

    set(PNGLIB_MAJOR 1)
    set(PNGLIB_MINOR 5)
    set(PNGLIB_RELEASE 10)

    # Use the prebuilt pnglibconf.h file from the scripts folder
    # 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.)
    #configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
    #               ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
    #include_directories(${CMAKE_CURRENT_BINARY_DIR})

    set(libpng_public_hdrs
      png.h
      pngconf.h
      #${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h
      pnglibconf.h  #Remove it and use previous line if it is generated by configure_file
    )

    set( png_sources
      ${libpng_public_hdrs}
                                      pngdebug.h
                                      pnginfo.h
                                      pngpriv.h
                                      pngstruct.h
      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
    )

    vxl_add_library(
        LIBRARY_NAME png
        LIBRARY_SOURCES ${png_sources}
        DISABLE_MSVC_MP
    )

    # SOME NEEDED DEFINITIONS
    add_definitions(-DPNG_CONFIGURE_LIBPNG)

    if(MSVC)
      add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
    endif()

    if(PNG_DEBUG)
      add_definitions(-DPNG_DEBUG)
    endif()

    #if(MSVC)
    #  # msvc does not append 'lib' - do it here to have consistent name
    #  set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
    #  set_target_properties(${PNG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
    #endif()

    # With cygwin, there are multiple user configurations possible.
    # From pngconf.h:
    #     'Cygwin' defines/defaults:
    #       PNG_BUILD_DLL -- building the dll
    #       (no define)   -- building an application, linking to the dll
    #       PNG_STATIC    -- building the static lib, or building an application
    #                        which links to the static lib.
    #
    # Since we are here, there is no external png library. Therefore, we
    # build a static or shared based on the vxl build property, and rely
    # on FindPNG.cmake to propagate the appropriate build flags to user
    # code.
    #
    # NB: make sure this logic is consistent with FindPNG.cmake!

    if(CYGWIN)
      # in Cygwin a define is needed by any file including
      # vxl/v3p/png/png.h (which in turn includes pngconf.h)
      if(BUILD_SHARED_LIBS)
        add_definitions(-DPNG_BUILD_DLL)
      else()
        add_definitions(-DPNG_STATIC)
      endif()
    endif()

    target_link_libraries( png ${ZLIB_LIBRARIES} )
    if(UNIX)
      target_link_libraries( png m )
    endif()
  endif()

  if(BUILD_TESTING)
    add_subdirectory(tests)
  endif()

endif()
