project(kdepim)

# where to look first for cmake modules. This line must be the first one or cmake will use the system's FindFoo.cmake
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")


############### Build Options ###############

option(KDEPIM_BUILD_EXAMPLES "Build the kdepim example applications." FALSE)
option(KDEPIM_ENTERPRISE_BUILD "Enable features specific to the enterprise branch, which are normally disabled. Also, it disables many components not needed for Kontact such as the Kolab client." FALSE)
option(KDEPIM_ONLY_KLEO "Only build Kleopatra. This option will disable KDEPIM_BUILD_EVERYTHING and KDEPIM_ENTERPRISE_BUILD." FALSE)

# if KDEPIM_ENTERPRISE_BUILD is defined, KDEPIM_ONLY_KLEO is disabled
if(KDEPIM_ENTERPRISE_BUILD)
  set(KDEPIM_ONLY_KLEO FALSE)
  set(KDEPIM_BUILD_EVERYTHING FALSE)
  message(STATUS "Enterprise build is enabled.")
else(KDEPIM_ENTERPRISE_BUILD)
  # if KDEPIM_ONLY_KLEO is defined, KDEPIM_BUILD_EVERYTHING is disabled.
  if(KDEPIM_ONLY_KLEO)
    set(KDEPIM_BUILD_EVERYTHING FALSE)
    set(KDEPIM_DEFINITIONS "-DHAVE_CONFIG_H=1")
    message(STATUS "Only libkleo and Kleopatra will be built.")
  endif(KDEPIM_ONLY_KLEO)
  # Default
    set(KDEPIM_BUILD_EVERYTHING TRUE)
    message(STATUS "Enterprise build is disabled.")
endif(KDEPIM_ENTERPRISE_BUILD)

# config-enterprise.h is needed for both ENTERPRISE_BUILD and BUILD_EVERYTHING
configure_file(config-enterprise.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-enterprise.h )

############### generate kdepim-version.h ###############
# Support for the SVN revision number in kdepim-version.h
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn")
  if (NOT KdeSubversion_FOUND)
    find_package(KdeSubversion)
  endif (NOT KdeSubversion_FOUND)
  if (KdeSubversion_FOUND)
    KdeSubversion_WC_INFO(${PROJECT_SOURCE_DIR} KDEPIM)
    string(SUBSTRING "${KDEPIM_WC_LAST_CHANGED_DATE}" 0 10 KDEPIM_WC_LAST_CHANGED_DATE)
    set(kdepim_svn_revision "svn-${KDEPIM_WC_REVISION}")
    set(kdepim_svn_last_change "${KDEPIM_WC_LAST_CHANGED_DATE}")
  endif (KdeSubversion_FOUND)
endif (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn")

configure_file(kdepim-version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/kdepim-version.h)

############### search packages used by KDE ###############

find_package(KDE4 REQUIRED)
include(KDE4Defaults)
include(MacroLibrary)
include(CheckIncludeFiles)
include(MacroBoolTo01)
find_package(KdepimLibs 4.4.61 REQUIRED)

### remove the following when requiring kdelibs 4.5 ###
if ( NOT KDE4_KCMUTILS_LIBS )
  set( KDE4_KCMUTILS_LIBS ${KDE4_KUTILS_LIBS} )
endif()
if ( NOT KDE4_KEMOTICONS_LIBS )
  set( KDE4_KEMOTICONS_LIBS ${KDE4_KUTILS_LIBS} )
endif()
### end of 4.4 compat code ###

############### Load the CTest options ###############
# Set a timeout value of 5 minutes per test
set(DART_TESTING_TIMEOUT 300)

# CTestCustom.cmake has to be in the CTEST_BINARY_DIR.
# in the KDE build system, this is the same as CMAKE_BINARY_DIR.
configure_file(${CMAKE_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_BINARY_DIR}/CTestCustom.cmake)


############### search Boost ###############

find_package(Boost 1.34.0)
macro_log_feature(Boost_FOUND "Boost" "Boost C++ Libraries" "http://www.boost.org" TRUE "1.34.0" "Required by several critical KDEPIM apps.")

# Kleopatra needs to know if the topological.hpp header exists (part of Boost_graph).
find_path(Boost_TOPOLOGICAL_SORT_DIR NAMES boost/graph/topological_sort.hpp PATHS ${Boost_INCLUDE_DIRS})
macro_log_feature(Boost_TOPOLOGICAL_SORT_DIR "The Boost Topological_sort header" "topological_sort.hpp (part of Boost_graph)" "http://www.boost.org/" FALSE "" "Necessary to build kleopatra")


############### Windows specific ###############

if(WIN32)
  # detect oxygen icon dir at configure time based on KDEDIRS - there may be different package installation locations
  execute_process(COMMAND "${KDE4_KDECONFIG_EXECUTABLE}" --path icon OUTPUT_VARIABLE _dir ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  file(TO_CMAKE_PATH "${_dir}" __dir)
  find_path(KDE4_ICON_DIR oxygen PATHS
    ${__dir}
  )
  message(STATUS "using oxygen application icons from ${KDE4_ICON_DIR}")
else(WIN32)
    set (KDE4_ICON_DIR  ${CMAKE_INSTALL_PREFIX}/share/icons)
endif(WIN32)


############### ONLY_KLEO ###############

# The KDEPIM_ONLY_KLEO option is true
if(KDEPIM_ONLY_KLEO)
  find_package(QGpgme)
  macro_log_feature(QGPGME_FOUND "QGpgME" "The QGpgME library" "http://www.kde.org" TRUE "" "QGpgME is required to build Kleopatra.")

  add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS} ${KDEPIM_DEFINITIONS} -DQT3_SUPPORT_WARNINGS)
  include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES} ${KDEPIMLIBS_INCLUDE_DIRS} ${QT_QTDBUS_INCLUDE_DIR})
  include(kleopatra/ConfigureChecks.cmake)

  add_subdirectory(libkleo)
  if (Boost_TOPOLOGICAL_SORT_DIR)
    macro_optional_add_subdirectory(kleopatra)
  endif (Boost_TOPOLOGICAL_SORT_DIR)

else(KDEPIM_ONLY_KLEO)

# Otherwise...
############### Find the stuff we need ###############

  find_package(Akonadi QUIET NO_MODULE 1.3.60)
  macro_log_feature(Akonadi_FOUND "Akonadi" "Akonadi server libraries" "http://pim.kde.org/akonadi" TRUE "" "Akonadi is required to build KDEPIM")

  find_package(ZLIB)
  macro_log_feature(ZLIB_FOUND "ZLib" "The Zlib compression library" "http://www.zlib.net" TRUE "" "")

  find_package(Strigi)
  macro_log_feature(STRIGI_FOUND "Strigi" "Index metadata of files" "http://strigi.sourceforge.net" TRUE "" "")

  find_package(QGpgme)
  macro_log_feature(QGPGME_FOUND "QGpgME" "The QGpgME library" "http://www.kde.org" FALSE "" "QGpgME is required to build KMail, KOrganizer and Kleopatra")

  set(SDO_MIN_VERSION 0.2)
  set(SOPRANO_MIN_VERSION 2.3.70)

  find_package(SharedDesktopOntologies ${SDO_MIN_VERSION})
  macro_log_feature(SHAREDDESKTOPONTOLOGIES_FOUND "Shared desktop ontologies" "Desktop ontologies" "http://oscaf.sourceforge.net" TRUE "${SDO_MIN_VERSION}" "Ontologies necessary for the Nepomuk semantic desktop.")

  find_package(Soprano)
  macro_log_feature(Soprano_FOUND "Soprano" "Semantic Desktop Storing" "http://soprano.sourceforge.net" TRUE "${SOPRANO_MIN_VERSION}" "Soprano is needed for Nepomuk")
  include(SopranoAddOntology)

  find_package(Grantlee)
  macro_log_feature(Grantlee_FOUND "Grantlee" "The Grantlee Template System" "http://www.grantlee.org" FALSE "" "Templating and theming for KJots." )

  find_package(Nepomuk)
  macro_log_feature(Nepomuk_FOUND "Nepomuk" "The Nepomuk libraries" "http://www.kde.org" TRUE "" "Nepomuk extends the search and tagging functionalities in KMail and Akonadi")

  find_program(XSLTPROC_EXECUTABLE xsltproc)
  macro_log_feature(XSLTPROC_EXECUTABLE "xsltproc" "The command line XSLT processor from libxslt" "http://xmlsoft.org/XSLT/" TRUE "" "Required to generate the D-Bus interfaces.")

############### Desktop vs. Mobile options ##############

option(KDEPIM_MOBILE_UI "Build UI for mobile devices instead of for desktops" FALSE)
if (KDEPIM_MOBILE_UI)
  macro_log_feature(QT_QTDECLARATIVE_FOUND "QtDeclarative" "Qt Declarative (QML) module" "http://qt.nokia.com" TRUE "4.7" "Required for building mobile UI")
  add_definitions( -DKDEPIM_MOBILE_UI )
endif (KDEPIM_MOBILE_UI)

############### Needed commands before building anything ###############

add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS} ${KDEPIM_DEFINITIONS})

include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDEPIMLIBS_INCLUDE_DIRS} ${KDE4_INCLUDES} ${QT_QTDBUS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/libkdepim)

if (NOT KDEPIMLIBS_KRESOURCES_LIBRARY)
  add_definitions( -DKDEPIM_NO_KRESOURCES )
endif (NOT KDEPIMLIBS_KRESOURCES_LIBRARY)


############### Macros ###############

# generates a D-Bus interface description from a KConfigXT file
macro(kcfg_generate_dbus_interface _kcfg _name)
  add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}.xml
    COMMAND ${XSLTPROC_EXECUTABLE} --stringparam interfaceName ${_name}
    ${KDEPIMLIBS_DATA_DIR}/akonadi-kde/kcfg2dbus.xsl
    ${_kcfg}
    > ${CMAKE_CURRENT_BINARY_DIR}/${_name}.xml
    DEPENDS ${KDEPIMLIBS_DATA_DIR}/akonadi-kde/kcfg2dbus.xsl
    ${_kcfg}
    )
endmacro(kcfg_generate_dbus_interface)


############### Code used in several places ###############
# TODO Remove from the top CMakeLists.

# used in knotes, kontact/plugins/knotes, kresources/egroupware, kresources/kolab/knotes, kresources/scalix/knotes, wizard
set(libknotesresources_SRCS
  ${CMAKE_SOURCE_DIR}/knotes/resourcemanager.cpp
  ${CMAKE_SOURCE_DIR}/knotes/resourcenotes.cpp
  ${CMAKE_SOURCE_DIR}/knotes/resourcelocal.cpp
  ${CMAKE_SOURCE_DIR}/knotes/resourcelocalconfig.cpp
)

# used in knotes, kontact/plugins/knotes
set(libknoteseditor_SRCS
  ${CMAKE_SOURCE_DIR}/knotes/knoteedit.cpp
)

# used in kdgantt1, korganizer
set(libkdgantt1_SRCS
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttView.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewEventItem.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewItem.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewItemDrag.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewSubwidgets.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewSummaryItem.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewTaskItem.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewTaskLink.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttViewTaskLinkGroup.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttMinimizeSplitter.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttSemiSizingControl.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttSizingControl.cpp
  ${CMAKE_SOURCE_DIR}/kdgantt1/KDGanttXMLTools.cpp
)


############### Now, we add the PIM components ###############

  include (kleopatra/ConfigureChecks.cmake)

  # These targets will always be built before anything else.

  add_subdirectory( ontologies ) # must be the first one.
  add_subdirectory(akonadi)
  add_subdirectory(libkdepim)
  add_subdirectory(calendarviews)
  add_subdirectory( incidenceeditors )
  add_subdirectory(libkdepimdbusinterfaces)
  add_subdirectory(libkleo)
  add_subdirectory(libkpgp)
  add_subdirectory(libksieve)
  #add_subdirectory(libstickynotes)
  add_subdirectory(kdgantt1)
  add_subdirectory(icons)
  add_subdirectory(messagecore)
  add_subdirectory(messagelist)
  add_subdirectory(templateparser)

  if(QGPGME_FOUND)
    if (Boost_TOPOLOGICAL_SORT_DIR)
      macro_optional_add_subdirectory(kleopatra)
    endif(Boost_TOPOLOGICAL_SORT_DIR)
    if (QT_QT3SUPPORT_FOUND AND KDEPIMLIBS_KRESOURCES_LIBRARY)
      macro_optional_add_subdirectory(wizards)
    endif (QT_QT3SUPPORT_FOUND AND KDEPIMLIBS_KRESOURCES_LIBRARY)
    # The following components depend on QGpgME.
    set(MESSAGEVIEWER_SUPPORTED TRUE)
    add_subdirectory(messageviewer)
    macro_optional_add_subdirectory(messagecomposer)
    macro_optional_add_subdirectory(kmail)
    if (QT_QTDECLARATIVE_FOUND AND KDEPIM_MOBILE_UI)
      macro_optional_add_subdirectory(mobile)
    endif (QT_QTDECLARATIVE_FOUND AND KDEPIM_MOBILE_UI)

    if(KDEPIM_BUILD_EXAMPLES)
      add_subdirectory(examples)
    endif(KDEPIM_BUILD_EXAMPLES)

    # If kmail is compiled, KMAIL_SUPPORTED is true (used in several places)
    if(BUILD_kmail)
      set(KMAIL_SUPPORTED TRUE)
      add_definitions(-DKMAIL_SUPPORTED)
    endif(BUILD_kmail)

    macro_optional_add_subdirectory(kmailcvt)
    macro_optional_add_subdirectory(ksendemail)
    macro_optional_add_subdirectory(korganizer)

    if (SHAREDDESKTOPONTOLOGIES_VERSION VERSION_GREATER 0.3.59)
      add_subdirectory( nepomuk_email_feeder )
    endif ()
  endif(QGPGME_FOUND)

  if(KDEPIM_BUILD_EVERYTHING AND QT_QT3SUPPORT_FOUND AND KDEPIMLIBS_KRESOURCES_LIBRARY)
    macro_optional_add_subdirectory(knode)
  endif(KDEPIM_BUILD_EVERYTHING AND QT_QT3SUPPORT_FOUND AND KDEPIMLIBS_KRESOURCES_LIBRARY)

  if (QT_QT3SUPPORT_FOUND)
    macro_optional_add_subdirectory(akregator)
  endif (QT_QT3SUPPORT_FOUND)
  macro_optional_add_subdirectory(akonadiconsole)
  macro_optional_add_subdirectory(blogilo)
  macro_optional_add_subdirectory(console)
  if (KDEPIMLIBS_KRESOURCES_LIBRARY AND NOT MINGW)
    macro_optional_add_subdirectory(kalarm)
  endif (KDEPIMLIBS_KRESOURCES_LIBRARY AND NOT MINGW)

  if (Grantlee_FOUND)
    macro_optional_add_subdirectory(kjots)
  endif (Grantlee_FOUND)
  if (KDEPIMLIBS_KRESOURCES_LIBRARY)
    macro_optional_add_subdirectory(knotes)
  endif (KDEPIMLIBS_KRESOURCES_LIBRARY)
  macro_optional_add_subdirectory(strigi-analyzer)

  if(EXISTS ${CMAKE_SOURCE_DIR}/runtime)
    macro_optional_add_subdirectory(runtime)
  endif(EXISTS ${CMAKE_SOURCE_DIR}/runtime)

  if (BUILD_runtime)
    include_directories(${CMAKE_SOURCE_DIR}/runtime ${CMAKE_BINARY_DIR}/runtime)
  endif (BUILD_runtime)

  macro_optional_add_subdirectory(kaddressbook)
  if(Q_WS_X11 AND KDEPIMLIBS_KRESOURCES_LIBRARY)
    macro_optional_add_subdirectory(ktimetracker)
  endif(Q_WS_X11 AND KDEPIMLIBS_KRESOURCES_LIBRARY)

  if (QT_QT3SUPPORT_FOUND)
    macro_optional_add_subdirectory(kontact) # must be the last one.
  endif (QT_QT3SUPPORT_FOUND)

  # These targets depend on optional applications
  if (KDEPIMLIBS_KRESOURCES_LIBRARY)
    add_subdirectory(kresources) # Must be after KAddressbook
  endif (KDEPIMLIBS_KRESOURCES_LIBRARY)
  add_subdirectory(plugins) # Must be after KMail

endif (KDEPIM_ONLY_KLEO)

# doc must be a subdir of kdepim or packagers will kill us
macro_optional_add_subdirectory(doc)


# We really want to encourage users to enable/install QGpgME from kdepimlibs
if(NOT QGPGME_FOUND)
  # Users must be aware that QGpgMe is really needed
  message(STATUS "*** WARNING: QGpgME is not installed on your system ***")
  message(STATUS "*** It is required if you want to use KMail, KOrganizer or Kleopatra ***")
  message(STATUS "*** You are really encouraged to install it ***")
endif(NOT QGPGME_FOUND)
# All done, let's display what we found...
macro_display_feature_log()

