# 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
    )

    ADD_LIBRARY( png ${png_sources} )
    INSTALL_TARGETS( /lib png )

    # SOME NEEDED DEFINITIONS
    
    add_definitions(-DPNG_CONFIGURE_LIBPNG)
    
    if(MSVC)
      add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
    endif(MSVC)
    
    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, the 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(BUILD_SHARED_LIBS)
        ADD_DEFINITIONS(-DPNG_STATIC)
      ENDIF(BUILD_SHARED_LIBS)
    ENDIF (CYGWIN)

    TARGET_LINK_LIBRARIES( png ${ZLIB_LIBRARIES} )
    IF (UNIX)
      TARGET_LINK_LIBRARIES( png m )
    ENDIF (UNIX)
  ENDIF(ZLIB_FOUND)

  IF(BUILD_TESTING)
    SUBDIRS(tests)
  ENDIF(BUILD_TESTING)

ENDIF(NOT VXL_USING_NATIVE_PNG)
