# This file is copyrighted under the BSD-license for buildsystem files of KDE
# copyright 2010, Patrick Spendrin <ps_ml@gmx.de>

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

project (libgif)

cmake_minimum_required(VERSION 2.6)

find_package(X11)

if(WINCE)
    find_package(Wcecompat REQUIRED)
    include_directories(${WCECOMPAT_INCLUDE_DIR})
    set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${WCECOMPAT_INCLUDE_DIR})
    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${WCECOMPAT_LIBRARIES})
endif(WINCE)

# you will need non-standard irixGL for this to work
#find_package(OpenGL)
# I had no way to get this library, so keep that for later
#find_package(LibRle)

option(BUILD_utils "build also some small helper executables to transform gif files into other formats" ON)

#/* Set to an unsigned 32 bit integer available on this system */
set(UINT32 "unsigned long")
if(X11_FOUND)
    set(HAVE_LIBX11 ON)
endif(X11_FOUND)

include(CheckIncludeFile)
include(CheckFunctionExists)

check_include_file("fcntl.h" HAVE_FCNTL_H)
check_include_file("io.h" HAVE_IO_H)
check_include_file("stdarg.h" HAVE_STDARG_H)
check_include_file("gl/gl.h" HAVE_GL_GL_H)
check_include_file("inttypes.h" HAVE_INTTYPES_H)
check_include_file("memory.h" HAVE_MEMORY_H)
check_include_file("rle.h" HAVE_RLE_H)
check_include_file("stdint.h" HAVE_STDINT_H)
check_include_file("stdlib.h" HAVE_STDLIB_H)
check_include_file("strings.h" HAVE_STRINGS_H)
check_include_file("string.h" HAVE_STRING_H)
check_include_file("sys/stat.h" HAVE_SYS_STAT_H)
check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
check_include_file("unistd.h" HAVE_UNISTD_H)
check_include_file("varargs.h" HAVE_VARARGS_H)

check_function_exists("strdup" HAVE_STRDUP)
check_function_exists("vprintf" HAVE_VPRINTF)

set(PACKAGE "giflib")
set(PACKAGE_BUGREPORT "abadger1999@sourceforge.net")
set(PACKAGE_NAME "giflib")
set(PACKAGE_STRING "giflib-4.1.6")
set(PACKAGE_TARNAME "")
set(PACKAGE_VERSION "4.1.6")
#cmakedefine VERSION

configure_file(config.h.cmake config.h)
include_directories(lib ${CMAKE_BINARY_DIR}) 
if(OPENGL_FOUND)
    include_directories(${OPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR}/GL)
endif(OPENGL_FOUND)

add_definitions(-DHAVE_CONFIG_H)
if(MSVC)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)

set(libgif_SRCS 
lib/dev2gif.c
lib/dgif_lib.c
lib/egif_lib.c
lib/gif_err.c
lib/gif_font.c
lib/gif_hash.c
lib/gifalloc.c
lib/qprintf.c
lib/quantize.c
)

add_library(gif SHARED ${libgif_SRCS})

if(WINCE)
    target_link_libraries(gif ${WCECOMPAT_LIBRARIES})
endif(WINCE)

install(TARGETS gif RUNTIME DESTINATION bin
                    LIBRARY DESTINATION lib
                    ARCHIVE DESTINATION lib)
set_target_properties(gif PROPERTIES OUTPUT_NAME giflib4)
if(X11_FOUND)
    target_link_libraries(gif ${X11_X11_LIB})
endif(X11_FOUND)

install(FILES lib/gif_lib.h DESTINATION include)

if(BUILD_utils)
# add a convenience library so that this file is not recompiled for each target
add_library(getarg STATIC lib/getarg.c)

macro(compileUtils)
foreach(srcfile ${ARGN})
    add_executable(${srcfile} util/${srcfile}.c)
    target_link_libraries(${srcfile} gif getarg)
    install(TARGETS ${srcfile} RUNTIME DESTINATION bin
                               LIBRARY DESTINATION lib
                               ARCHIVE DESTINATION lib)
endforeach(srcfile ${ARGN})
endmacro(compileUtils)

compileUtils(
gifinfo
gif2epsn
gif2ps
gif2rgb
gifasm
gifbg
gifclip
gifcomb
giffix
gifflip
gifhisto
gifinter
gifinto
gifovly
gifpos
gifrsize
giftext
gifwedge
raw2gif
rgb2gif
text2gif
gifspnge
giffiltr
icon2gif
gifcolor
gifclrmp
gifrotat
)

if(OPENGL_FOUND)
    compileUtils(
    gif2iris
    )
    target_link_libraries(gif2iris ${OPENGL_gl_LIBRARY})
endif(OPENGL_FOUND)

if(X11_FOUND)
    compileUtils(
    gif2x11
    )
    target_link_libraries(gif2x11 ${X11_X11_LIB})
endif(X11_FOUND)

if(RLE_FOUND)
    compileUtils(
    gif2rle
    rle2gif
    )
    target_link_libraries(gif2rle ${rle_LIBRARIES})
    target_link_libraries(rle2gif ${rle_LIBRARIES})
endif(RLE_FOUND)

#gif2x11_CFLAGS = @X_CFLAGS@ $(AM_CFLAGS)
if(X11_FOUND)
endif(X11_FOUND)

if(OPENGL_FOUND)
endif(OPENGL_FOUND)

if(NOT WIN32)
    target_link_libraries(gifclrmp m)
    target_link_libraries(gifrotat m)
endif(NOT WIN32)
endif(BUILD_utils)


# documentation
install(FILES 
doc/gif2bgi.html
doc/gif2epsn.html
doc/gif2herc.html
doc/gif2iris.html
doc/gif2ps.html
doc/gif2rgb.html
doc/gif2rle.html
doc/gif2x11.html
doc/gifasm.html
doc/gifbg.html
doc/gifburst.html
doc/gifclip.html
doc/gifclrmp.html
doc/gifcolor.html
doc/gifcomb.html
doc/gifcompose.html
doc/giffiltr.html
doc/giffix.html
doc/gifflip.html
doc/gifhisto.html
doc/gifinter.html
doc/gifinto.html
doc/gifovly.html
doc/gifpos.html
doc/gifrotat.html
doc/gifrsize.html
doc/gifspnge.html
doc/giftext.html
doc/gifwedge.html
doc/icon2gif.html
doc/raw2gif.html
doc/rgb2gif.html
doc/rle2gif.html
doc/text2gif.html
doc/GifFileType.png
doc/gif89.txt
doc/gif_lib.html
doc/index.html
doc/liberror.html
doc/lzgif.txt
DESTINATION doc/libungif/html)