# 指定cmake的最小版本
cmake_minimum_required(VERSION 2.8)

# 指定工程名称，ImageConverter.sln
project(ImageConverter)

# 配置版本号
set (APP_VERSION_MAJOR 1)
set (APP_VERSION_MINOR 0)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# 配置是否开启SVG的支持
option (SUPPORT_SVG_TYPE "support .svg file" ON)  

# 加入一个配置头文件，用于处理 CMake 对源码的设置
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_SOURCE_DIR}/config.h"
 )

aux_source_directory(. srcs)
source_group("src" FILES ${srcs})

FILE(GLOB_RECURSE coresrcs 
    ./core/*.cpp
)
source_group("src\\core" FILES ${coresrcs})

FILE(GLOB_RECURSE miscsrcs 
    ./misc/*.cpp
)
source_group("src\\misc" FILES ${miscsrcs})

FILE(GLOB_RECURSE coreheaders 
    ./core/*.h
)
source_group("include\\core" FILES ${coreheaders}) 

FILE(GLOB_RECURSE mischeaders 
    ./misc/*.h
)
source_group("include\\misc" FILES ${mischeaders}) 

if(SUPPORT_SVG_TYPE)
FILE(GLOB_RECURSE nanosvgheaders 
    ./modules/nanosvg/src/*.h
    ./modules/nanosvg/example/*.h
)
source_group("modules\\include\\nanosvg" FILES ${nanosvgheaders}) 
include_directories(./modules/nanosvg/src)
include_directories(./modules/nanosvg/example)
add_definitions(-DSUPPORT_SVG_TYPE)
endif(SUPPORT_SVG_TYPE)

# 添加头文件搜索路径
include_directories(./core)
include_directories(./misc)

SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

FIND_PACKAGE(FREEIMAGEX32)
IF(FREEIMAGEX32_FOUND)
    include_directories(${FREEIMAGEX32_INCLUDE_DIR})
    FILE(GLOB_RECURSE freeimageheaders 
        ${FREEIMAGEX32_INCLUDE_DIR}/*.h
    )
    source_group("include\\freeimage" FILES ${freeimageheaders}) 
    if (FREEIMAGEX32_STATICLIB)
        add_executable(ImageConverterForFreeImage 
            ${srcs} 
            ${coresrcs} 
            ${miscsrcs} 
            ${coreheaders} 
            ${mischeaders} 
            ${freeimageheaders}
            ${nanosvgheaders})

        set_target_properties(ImageConverterForFreeImage PROPERTIES COMPILE_DEFINITIONS "SUPPORT_FREEIMAGE")
        target_link_libraries(ImageConverterForFreeImage ${FREEIMAGEX32_STATICLIB})
        add_custom_command(TARGET ImageConverterForFreeImage POST_BUILD        # Adds a post-build event to MyTest
            COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
                "${FREEIMAGEX32_SHAREDLIB}"      # <--this is in-file
                "${PROJECT_SOURCE_DIR}/bin/$<CONFIGURATION>"
                )
    endif(FREEIMAGEX32_STATICLIB)
else(FREEIMAGEX32_FOUND)    
    MESSAGE(status "没有找到FreeImage哦，不生成此工程啦") 
ENDIF(FREEIMAGEX32_FOUND)


# 配置一个可执行文件项目，ImageConverterDemo.vcxproj
add_executable(ImageConverterDemo ${srcs} ${coresrcs} ${miscsrcs} ${coreheaders} ${mischeaders} ${nanosvgheaders})
