# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
include_directories(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) # just add .qrc in sources to get it included in the lib
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# find all sources files, set rules for qt files...
set(CAMITKEXTENSIONGENERATOR_HEADERS CamiTKExtensionGeneratorAPI.h
                               TransformEngine.h 
                               ExtensionGenerator.h
                               CamiTKExtensionModel.h
                               VariantDataModel.h
                               CMakeProjectManager.h)
set(CAMITKEXTENSIONGENERATOR_SOURCES TransformEngine.cpp
                               ExtensionGenerator.cpp
                               CamiTKExtensionModel.cpp
                               VariantDataModel.cpp
                               CMakeProjectManager.cpp
                               resources/templates.qrc)

# Needed for a correct decl export/import on WIN32
add_definitions(-DCOMPILE_CAMITKEXTENSIONGENERATOR_EXPORT)

# prepare the library specific info (SONAME...)
set(EXTENSIONGENERATOR_LIBRARY_PROPERTIES ${EXTENSIONGENERATOR_LIBRARY_PROPERTIES}
    VERSION   "${CAMITK_VERSION_MAJOR}.${CAMITK_VERSION_MINOR}.${CAMITK_VERSION_PATCH}"
    SOVERSION "${CAMITK_VERSION_MAJOR}"
)

# camitkcore only depends on qt
# link_directories(${CAMITK_BUILD_BIN_DIR} ${CAMITK_PUBLIC_LIB_DIR})

# camitkextensiongenerator needs to be SHARED
add_library(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} SHARED ${CAMITKEXTENSIONGENERATOR_SOURCES} ${CAMITKEXTENSIONGENERATOR_HEADERS})

target_compile_definitions(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PRIVATE CAMITK_SHORT_VERSION="${CAMITK_SHORT_VERSION_STRING}")

# linking with Qt
target_link_libraries(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PRIVATE ${CAMITK_QT_LIBRARIES})

# set the library specific info (SONAME...)
set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES ${EXTENSIONGENERATOR_LIBRARY_PROPERTIES} LINK_INTERFACE_LIBRARIES "")
if (WIN32 AND MSVC)
    # property required for camitk file association on windows platform compilation with Visual Studio
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES COMPILE_FLAGS "/Zc:wchar_t-"
                                                                       DEBUG_POSTFIX ${CAMITK_DEBUG_POSTFIX})
endif()

# output directory
if (MSVC)
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CAMITK_BUILD_BIN_DIR}
                                                                    LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_BIN_DIR}
                                                                    LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_BIN_DIR}
    )
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CAMITK_BUILD_BIN_DIR}
                                                                    RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_BIN_DIR}
                                                                    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_BIN_DIR}
    )
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CAMITK_BUILD_BIN_DIR}
                                                                    ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_BIN_DIR}
                                                                    ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_BIN_DIR}                                                        
    )
else()
    # Temporary take multiarch into account for packaging during nested cmake run
    if (CAMITK_HOST_MULTIARCH)
        set(CAMITK_BUILD_PUBLIC_LIB_DIR ${CAMITK_BUILD_PUBLIC_LIB_DIR}/${CAMITK_HOST_MULTIARCH})
    endif()
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_PUBLIC_LIB_DIR}
    )
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_PUBLIC_LIB_DIR}
    )
    set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CAMITK_BUILD_PUBLIC_LIB_DIR}
                                                                    ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CAMITK_BUILD_PUBLIC_LIB_DIR}                                                        
    )
endif()

# no dependencies → no add_dependency required.

# -- headers to install 
#--------------
# installation
#--------------
export_headers(${CAMITKEXTENSIONGENERATOR_HEADERS} 
               COMPONENT ${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
               GROUP libraries
)

# change the output library name (without the prefix 'library')
set_target_properties(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
                       PROPERTIES OUTPUT_NAME ${CAMITK_EXTENSIONLIBRARY_LIB_NAME}
)

# lib installation
install(TARGETS ${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/${CAMITK_SHORT_VERSION_STRING}
        COMPONENT ${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
)

cpack_add_component(${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
                    DISPLAY_NAME ${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME}
                    DESCRIPTION "CamiTK extension generator."
                    REQUIRED
                    GROUP SDK
)

# CDASH XML SUBPROJECTS DESCRIPTION UPDATE
# library-extensionmanager is a corelib as library-camitkcore depends on it (same as library-qtpropertybrowser)
camitk_register_subproject(CORELIB ${CAMITK_EXTENSIONGENERATOR_TARGET_LIB_NAME})

# -- unit testing
camitk_add_subdirectory(testing)