CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(BUILD_ALL)

SET( GINKGO_VERSION 3.8.6)

SET( USE_PATCHED_LIBS        FALSE CACHE BOOL "Use patched libraries")
SET( BUILD_VISUALIZATION_EXT TRUE CACHE BOOL "Build Visualization extention")

SET( USE_SYSTEM_SQLITE       TRUE CACHE BOOL "Build with system SQLite library")
SET( USE_CUSTOM_WX           FALSE CACHE BOOL "Build with custom WX library")
SET( USE_CUSTOM_VTK          FALSE CACHE BOOL "Build with custom VTK library")
SET( USE_CUSTOM_ITK          FALSE CACHE BOOL "Build with custom ITK library")
SET( USE_CUSTOM_DCMTK        FALSE CACHE BOOL "Build with custom DCMTK library")

SET( CUSTOM_PACKAGE          FALSE CACHE BOOL "Custom package")
SET( FORCE_SSE               FALSE CACHE BOOL "Force SSE Flag")
SET( INTERNET_DIST           TRUE CACHE BOOL "Public distribution")

SET( CMAKE_OSX_ARCHITECTURES "i386" CACHE STRING "MacOSX Architectures" FORCE)

SET( GINKGO_VERSION_STR ${GINKGO_VERSION})

#############################################################################################

SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
INCLUDE(SysDefs)

#############################################################################################
include(FindPkgConfig)

include(CheckCXXCompilerFlag)

IF (((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") AND
      (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.3.0)) OR
    ((${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") AND
      (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.3.0)))
  MESSAGE(STATUS "Compiler has full c++11 support")

  # g++ >= 6.0 sets std=c++14 by default
  IF (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
    IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0.0)
      SET(CXX_11_FLAG "-std=c++11")
    ENDIF()
  ELSE()
    SET(CXX_11_FLAG "-std=c++11")
  ENDIF()
  
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  ${CXX_11_FLAG}")
ELSE()
  CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_GNU_CXX11_FLAG)
  IF(HAS_GNU_CXX11_FLAG)
    SET(CXX_11_FLAG "-std=c++11")
  ELSE(HAS_GNU_CXX11_FLAG)
    CHECK_CXX_COMPILER_FLAG("-std=c++0x" HAS_GNU_CXX0X_FLAG)
    IF(HAS_GNU_CXX0X_FLAG)
      SET(CXX_11_FLAG "-std=c++0x")
    ELSE(HAS_GNU_CXX0X_FLAG)
      MESSAGE(WARNING "Don't know how to enable C++11, if you are lucky, they are enabled by default")
    ENDIF(HAS_GNU_CXX0X_FLAG)
  ENDIF(HAS_GNU_CXX11_FLAG)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  ${CXX_11_FLAG}")
ENDIF()

#doesn't work... using custom build
#IF(LINUX)
#FIND_PACKAGE(LIBCURL REQUIRED)
#ENDIF()
SET_CUSTOM_libcurl_PACKAGE()

IF(NOT USE_CUSTOM_WX)
FIND_PACKAGE(wxWidgets 3.0.2 COMPONENTS core base html gl net xml aui adv richtext xrc propgrid REQUIRED)
ELSE()
MESSAGE(STATUS "Using custom wxWidgets")
SET_CUSTOM_wxWidgets_PACKAGE()
ENDIF()

IF(NOT USE_CUSTOM_VTK)
  FIND_PACKAGE(VTK 7.0.0)
  IF (NOT VTK_FOUND)
    FIND_PACKAGE(VTK 6.2.0 REQUIRED)
  ENDIF()
ELSE()
MESSAGE(STATUS "Using custom VTK")
SET_CUSTOM_VTK_PACKAGE()
ENDIF()

IF(NOT USE_CUSTOM_ITK)
FIND_PACKAGE(ITK 4.8.0 REQUIRED COMPONENTS ITKCommon ITKGDCM ITKIOImageBase ITKIOVTK ITKVTK ITKIOGDCM)
ELSE()
MESSAGE(STATUS "Using custom ITK")
SET_CUSTOM_ITK_PACKAGE()
ENDIF()

IF(NOT USE_CUSTOM_DCMTK)
FIND_PACKAGE(DCMTK 3.6.1 REQUIRED)
ELSE()
MESSAGE(STATUS "Using custom DCMTK")
SET_CUSTOM_DCMTK_PACKAGE()
ENDIF()

SET(CMAKE_REQUIRED_INCLUDES ${DCMTK_INCLUDES})
SET(CMAKE_REQUIRED_LIBRARIES ${DCMTK_LIBRARIES})

CHECK_CXX_SOURCE_COMPILES(
"#include <dcmtk/dcmdata/dcitem.h>
#include <dcmtk/dcmdata/dcdeftag.h>

int main(int argc, char *args[])
{
     DcmTagKey tag(DCM_StudyDate);
     DcmElement *test = DcmItem::newDicomElement(tag);
     return test == 0;
}
" DCMTK_newDicomElement_IS_STATIC_MEMBER_OF_DcmItem)

IF(DCMTK_newDicomElement_IS_STATIC_MEMBER_OF_DcmItem)
  ADD_DEFINITIONS(-DDCMTK_POST_20170228)
ENDIF()



IF(NOT USE_SYSTEM_SQLITE)
MESSAGE(STATUS "Using builtin SQLite")
ELSE()
MESSAGE(STATUS "Using system SQLite")
#FIND_PACKAGE(sqlite REQUIRED)

ENDIF()


# require cairo includes
PKG_CHECK_MODULES(CAIRO REQUIRED cairo)
INCLUDE_DIRECTORIES(${CAIRO_INCLUDE_DIRS})
LINK_DIRECTORIES(${CAIRO_LIBRARY_DIRS})

IF(LINUX)
  PKG_CHECK_MODULES(GTK REQUIRED gtk+-2.0)
  INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIRS})
  LINK_DIRECTORIES(${GTK_LIBRARY_DIRS})

  PKG_CHECK_MODULES(LIBCURL REQUIRED libcurl)
  INCLUDE_DIRECTORIES(${LIBCURL_INCLUDE_DIRS})
  LINK_DIRECTORIES(${LIBCURL_LIBRARY_DIRS})

ENDIF()







#############################################################################################

IF(MACOS)
SET(GINKGO_PROJECT "Ginkgo CADx" CACHE PARENT_SCOPE "")
ELSEIF(LINUX AND CUSTOM_PACKAGE)
SET(GINKGO_PROJECT "ginkgocadx-${ARCH}" CACHE PARENT_SCOPE "")
ELSE()
SET(GINKGO_PROJECT "ginkgocadx" CACHE PARENT_SCOPE "")
ENDIF()

SET(GINKGO_PLUGINS "" CACHE PARENT_SCOPE "")

ADD_SUBDIRECTORY(cadxcore)
ADD_SUBDIRECTORY(ginkgocadx)

IF(BUILD_VISUALIZATION_EXT)
ADD_SUBDIRECTORY(visualizator)
ENDIF()

ADD_CUSTOM_TARGET(${PROJECT_NAME} echo)

MESSAGE(STATUS "Building ${GINKGO_PROJECT} ${GINKGO_VERSION_STR}")

ADD_DEPENDENCIES(${PROJECT_NAME} "${GINKGO_PROJECT}")

IF(BUILD_VISUALIZATION_EXT)
ADD_DEPENDENCIES(${PROJECT_NAME} visualizator)
LIST(APPEND GINKGO_PLUGINS "visualizator")
ENDIF()

IF(CUSTOM_PACKAGE)

SET( DEST "${CMAKE_CURRENT_BINARY_DIR}/dist" )
PACKAGE_RULES( "${PROJECT_NAME}" "${ARCH}" "${DEST}" )
ENDIF()

