project(Applauncherd)

cmake_minimum_required(VERSION 2.6)
cmake_policy(VERSION 2.6)

#
# NOTE: For verbose build use VERBOSE=1
#

# Default C++-flags. Sub-builds might alter these.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -Wextra -g -O3 -Wl,--as-needed")
# Default C-flags. Sub-builds might alter these.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W -Wall -Wextra -g -O3")

set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")

# Find libdl
find_library(LIBDL NAMES dl)

# Find Qt4
find_package(Qt4 4.6.0 REQUIRED)
include(${QT_USE_FILE})
if (${QTVERSION} VERSION_LESS 4.6.0)
    message(FATAL_ERROR "You need Qt4.6, found ${QTVERSION}.")
endif (${QTVERSION} VERSION_LESS 4.6.0)

# Find libmeegotouch for MBooster and the test programs
include(FindPkgConfig) 
pkg_check_modules(MEEGOTOUCH meegotouch>=0.20 REQUIRED)
include_directories(${MEEGOTOUCH_INCLUDE_DIRS})

# Check if it's possible to compile MBooster with MComponentCache. If so,
# HAVE_MCOMPONENTCACHE is set.
message(STATUS "checking for mcomponentcache.h")
find_file(M_COMPONENT_CACHE NAMES mcomponentcache.h PATHS ${MEEGOTOUCH_INCLUDE_DIRS})
if (EXISTS ${M_COMPONENT_CACHE})
  message(STATUS "  found")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_MCOMPONENTCACHE")
else (EXISTS ${M_COMPONENT_CACHE})
  message(STATUS "  not found")
endif (EXISTS ${M_COMPONENT_CACHE})

# Find creds.h (and libcreds if the header is found)
message(STATUS "checking for creds.h")
find_file(CREDS_H NAMES creds.h PATHS /usr/include/sys)
if (EXISTS ${CREDS_H})
    message(STATUS "  found: " ${CREDS_H})
    message(STATUS "checking for libcreds")
    find_library(LIBCREDS NAMES creds)
    if (EXISTS ${LIBCREDS})
        add_definitions(-DHAVE_CREDS)
        link_libraries(${LIBCREDS})
        message(STATUS "  found: " ${LIBCREDS})
    else (EXISTS ${LIBCREDS})
        message(STATUS "  not found: Platform Security is disabled.")
    endif (EXISTS ${LIBCREDS})
else (EXISTS ${CREDS_H})
    message(STATUS "  not found: Platform Security is disabled.")
endif (EXISTS ${CREDS_H})

# Find aegis_crypto.h (and libaegis_crypto if the header is found)
message(STATUS "checking for aegis_crypto.h")
find_file(AEGIS_CRYPTO_H NAMES aegis_crypto.h PATHS /usr/include/sys)
if (EXISTS ${AEGIS_CRYPTO_H})
    message(STATUS "  found: " ${AEGIS_CRYPTO_H})
    message(STATUS "checking for libaegis_crypto")
    find_library(LIBAEGIS_CRYPTO NAMES aegis_crypto)
    if (EXISTS ${LIBAEGIS_CRYPTO})
        add_definitions(-DHAVE_AEGIS_CRYPTO)
        link_libraries(${LIBAEGIS_CRYPTO})
        message(STATUS "  found: " ${LIBAEGIS_CRYPTO})
    else (EXISTS ${LIBAEGIS_CRYPTO})
        message(STATUS "  not found: Platform Security is disabled.")
    endif (EXISTS ${LIBAEGIS_CRYPTO})
else (EXISTS ${AEGIS_CRYPTO_H})
    message(STATUS "  not found: Platform Security is disabled.")
endif (EXISTS ${AEGIS_CRYPTO_H})

if ($ENV{DISABLE_VERIFICATION})
    add_definitions(-DDISABLE_VERIFICATION)
endif ($ENV{DISABLE_VERIFICATION})

if ($ENV{DEBUG_BUILD})
    add_definitions(-DDEBUG_BUILD)
endif ($ENV{DEBUG_BUILD})

# Set the program name defines. Must be at this level due to unit tests.
add_definitions(-DPROG_NAME_INVOKER="invoker")
add_definitions(-DPROG_NAME_LAUNCHER="applauncherd")
add_definitions(-DPROG_NAME_SINGLE_INSTANCE="single-instance")

# Set gconf key definitions. These are used by MonitorBooster to get
# notifications when language / theme changes.
add_definitions(-DMEEGOTOUCH_THEME_GCONF_KEY="/meegotouch/theme/name")
add_definitions(-DMEEGOTOUCH_LANGUAGE_GCONF_KEY="/meegotouch/i18n/language")

# applauncherd will find lib*booster.so from here
add_definitions(-DBOOSTER_PLUGIN_DIR="/usr/lib/applauncherd")

# applauncherd will try to load single-instance using this path
add_definitions(-DSINGLE_INSTANCE_PATH="/usr/bin/single-instance")

# Disable debug logging, only error and warning messages get logged
# Currently effective only for invoker. Launcher part recognizes --debug
# which enables console echoing and debug messages.
add_definitions(-DDEBUG_LOGGING_DISABLED)

# Build with test coverage switch if BUILD_COVERAGE environment variable is set
if ($ENV{BUILD_COVERAGE})
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -DWITH_COVERAGE")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -DWITH_COVERAGE")
    set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} --coverage")
endif ($ENV{BUILD_COVERAGE})

# Sub build: applauncherd
add_subdirectory(src)

# Build tests if BUILD_TESTS environment variable is set
if ($ENV{BUILD_TESTS})
    # Sub build: tests
    # This will build the HelloWorld test library, unit tests, functional tests
    # and performance tests
    add_subdirectory(tests)
endif ($ENV{BUILD_TESTS})

# Target for documentation (make doc)
find_program(Doxygen NAMES doxygen)
if (EXISTS ${Doxygen})
    add_custom_target(doc COMMAND cd doc && ${Doxygen} doxygen-impldoc.conf COMMAND cd doc && ${Doxygen} doxygen-userdoc.conf COMMAND cd doc && ${Doxygen} doxygen-mdeclarativecache.conf)
else (EXISTS ${Doxygen})
    message(STATUS "Doxygen not found: you're not able to build documentation.")
endif (EXISTS ${Doxygen})

# Install README
install(FILES README DESTINATION /usr/share/doc/applauncherd)

# Install html documentation
install(DIRECTORY doc/implementation-documentation DESTINATION /usr/share/doc/applauncherd)
install(DIRECTORY doc/user-documentation DESTINATION /usr/share/doc/applauncherd)
install(DIRECTORY doc/mdeclarativecache-documentation DESTINATION /usr/share/doc/applauncherd)


# Install examples
install(DIRECTORY examples
        DESTINATION /usr/share/doc/applauncherd)

# Install startup script
if ($ENV{DEBUG_BUILD})
    install(PROGRAMS src/scripts/applauncherd-debug DESTINATION /usr/bin/ RENAME applauncherd)
else ($ENV{DEBUG_BUILD})
    install(PROGRAMS src/scripts/applauncherd DESTINATION /usr/bin/)
endif ($ENV{DEBUG_BUILD})

# Install pkg-config and qmake feature files. Note that current packaging policy is such 
# that applauncherd-dev installs only qt-boostable.pc which is intended for "all" 
# non-MeeGo Touch applications. meegotouch-boostable.pc is installed by libmeegotouch-dev 
# so as to make it easier for MeeGo Touch application developers. 
#
# TODO: Should meegotouch-boostable.* be removed completely from here since it's already included
#       in meegotouch-dev ?
install(FILES data/pkgconfig/meegotouch-boostable.pc DESTINATION /usr/lib/pkgconfig)
install(FILES data/mkspecs/features/meegotouch-boostable.prf DESTINATION /usr/share/qt4/mkspecs/features)
install(FILES data/pkgconfig/qt-boostable.pc DESTINATION /usr/lib/pkgconfig)
install(FILES data/mkspecs/features/qt-boostable.prf DESTINATION /usr/share/qt4/mkspecs/features)
install(FILES data/pkgconfig/qdeclarative-boostable.pc DESTINATION /usr/lib/pkgconfig)
install(FILES data/mkspecs/features/qdeclarative-boostable.prf DESTINATION /usr/share/qt4/mkspecs/features)
if ($ENV{MEEGO})
    install(FILES meego/applauncherd.desktop DESTINATION /etc/xdg/autostart)
endif ($ENV{MEEGO})

# Install aegisfs configuration file
install(FILES aegisfs/applauncherd-fs.conf DESTINATION /etc/aegisfs.d)
