# Copyright (C) 2007  University of Rostock
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.

# DPWS CMake file - 30.07.2007
#
# Top level cmake file of the dpws core library.
#
# author: S.Feldhorst <sascha.feldhorst@udo.edu>

# Name of the project
PROJECT(WS4D-gSOAP C)

# The required version of cmake. This is needed for full qt4-support.
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.0 FATAL_ERROR)

IF(COMMAND CMAKE_POLICY)
	CMAKE_POLICY(SET CMP0003 NEW)
ENDIF(COMMAND CMAKE_POLICY)

# Sets the default build type to Debug if not already done
IF (NOT CMAKE_BUILD_TYPE)
	SET(CMAKE_BUILD_TYPE
		Debug
		CACHE STRING "Choose the build type, options are: None Debug Release RelWithDebInfo MinSizeRel" FORCE
	)
ENDIF (NOT CMAKE_BUILD_TYPE)

# additional compiler flags for WS4D-gSOAP

IF (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_CROSSCOMPILING)
	SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -funsigned-char -fstrict-aliasing -Wall -Wextra -Wchar-subscripts -Wundef -Wshadow -Wcast-align -Wwrite-strings -Wsign-compare -Wunused -Wno-unused-value -Wpointer-arith -Wredundant-decls -Wmissing-prototypes -Wno-variadic-macros")
ENDIF (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_CROSSCOMPILING)
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")

IF (CMAKE_COMPILER_IS_GNUCC)
	SET(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -ffunction-sections -fdata-sections -Wl,--gc-sections")
ENDIF (CMAKE_COMPILER_IS_GNUCC)

INCLUDE(CMakeDependentOption)

# Read the current build version.
FILE(READ ${CMAKE_SOURCE_DIR}/buildversion VERSION)
MESSAGE(STATUS "Buildversion: ${VERSION}")

# Some cmake specific settings.
SET(CMAKE_COLOR_MAKEFILE ON) # For a colorful makefile output.
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake-modules/ ${CMAKE_MODULE_PATH}) # Include own modules.

IF(CMAKE_TOOLCHAIN_FILE)
	CMAKE_MINIMUM_REQUIRED(VERSION 2.5.0 FATAL_ERROR)
ENDIF(CMAKE_TOOLCHAIN_FILE)


################################################################################
# Check dependencies                                                           #
################################################################################

# required
FIND_PACKAGE(XSLTproc REQUIRED)
FIND_PACKAGE(GnuPatch REQUIRED)
FIND_PACKAGE(GnuSed REQUIRED)

# find gsoap
SET(GSOAP_PATH "/usr/local" CACHE PATH "Path where gSOAP is installed")
FIND_PACKAGE(GSOAP REQUIRED)
SET(GSOAP_PATH ${GSOAP_PATH_FOUND} CACHE PATH "Path where gSOAP is installed" FORCE)

# find host executables
IF(CMAKE_CROSSCOMPILING)
    SET(DPWS_HOSTEXEC_PATH "/usr/local" CACHE PATH "Path where gSOAP is installed")
    FIND_PACKAGE(DPWSHostExecutables REQUIRED)
    SET(DPWS_HOSTEXEC_PATH ${DPWS_HOSTEXEC_PATH_FOUND} CACHE PATH "Path where gSOAP is installed" FORCE)
ENDIF(CMAKE_CROSSCOMPILING)

# optional
FIND_PACKAGE(Threads)
FIND_PACKAGE(LATEX)
FIND_PACKAGE(Doxygen)
FIND_PACKAGE(SystemTap)
FIND_PACKAGE(OpenSSL)

################################################################################
# Compile time configuration options of ws4d-gsoap                             #
################################################################################

IF(CMAKE_CROSSCOMPILING)
    MESSAGE(STATUS "Doing a cross compile CC=${CMAKE_C_COMPILER}")
ENDIF(CMAKE_CROSSCOMPILING)

# Activate IPV6 support (default: off)
OPTION (WITH_IPV6 "Toggle IPv6 support" OFF)

IF(WITH_IPV6)
  ADD_DEFINITIONS ( "-DWITH_IPV6")
ENDIF(WITH_IPV6)

# Activate hosting service (default: on)
OPTION (WITH_HOSTING_SERVICE "Toggle generation of hosting_service" ON)

# Activate life cycle manager service (default: off)
CMAKE_DEPENDENT_OPTION (WITH_LCM "Toggle generation of life cycle manager" OFF "WITH_HOSTING_SERVICE" OFF)

# Activate gSOAP WS-Security support (default: off)
OPTION (WITH_WS_SECURITY "Toggle gSOAP WS-Security support" OFF)

# Activate WS4D-security-shell
OPTION (WITH_WS4D_SECURITY_SHELL "Toggle activation of WS4D-Security-Shell" OFF)
IF(WITH_WS4D_SECURITY_SHELL)
	ADD_DEFINITIONS ( "-DWITH_WS4D_SECURITY_SHELL")
ENDIF(WITH_WS4D_SECURITY_SHELL)

# SytemTap support
IF(DTRACE_EXECUTABLE)
  OPTION (WITH_PROBES "Toggle SystemTap probe support" OFF)
ENDIF(DTRACE_EXECUTABLE)

# DPWS specification version
SET (DPWS_VERSION "2006_02" CACHE
     STRING "DPWS version: 2006_02 (default), 2009_01")

################################################################################
# MAINTAINER_MODE options                                                      #
################################################################################

OPTION (MAINTAINER_MODE "Activate options for maintenance purpose" OFF)

# Show or hide specific variables
IF (MAINTAINER_MODE)
  MARK_AS_ADVANCED(CLEAR
    DART_TESTING_TIMEOUT
    BUILD_TESTING)
ELSE(MAINTAINER_MODE)
  MARK_AS_ADVANCED(FORCE
    DART_TESTING_TIMEOUT
    BUILD_TESTING)
ENDIF (MAINTAINER_MODE)


################################################################################
# ADVANCED_OPTIONS options                                                     #
################################################################################

OPTION (ADVANCED_OPTIONS "Activate advanced options" OFF)

# Abort on ws4d_assert() (default: off)
CMAKE_DEPENDENT_OPTION (ABORT_ON_ASSERT "Abort programm on failed ws4d_assert()" OFF "ADVANCED_OPTIONS" OFF)

# Abort on ws4d_fail() (default: off)
CMAKE_DEPENDENT_OPTION (ABORT_ON_FAIL "Abort programm on failed ws4d_fail()" OFF "ADVANCED_OPTIONS" OFF)

# Use custom trace function ws4d_custom_tracefunc() for tracing (default: off)
CMAKE_DEPENDENT_OPTION (WS4D_CUSTOM_TRACE "Use custom trace function in ws4d_assert(), ws4d_fail() and ws4d_warn()" OFF "ADVANCED_OPTIONS" OFF)

# Trace in all build modes (default: off)
CMAKE_DEPENDENT_OPTION (WS4D_TRACE_ALWAYS "Enabel tracing in ws4d_assert(), ws4d_fail() and ws4d_warn() for all build modes" OFF "ADVANCED_OPTIONS" OFF)

CMAKE_DEPENDENT_OPTION (WITH_WS4D_TOOLS "Toggle generation ws4d-gsoap tools" ON "ADVANCED_OPTIONS" ON)

# Generate documentation (default: on)
CMAKE_DEPENDENT_OPTION (WITH_DOCU "Toggle generation of docu" ON "ADVANCED_OPTIONS" ON)

# Generate PDF documentation (default: on)
CMAKE_DEPENDENT_OPTION (WITH_DOCU_PDF "Toggle generation of pdf docu" ON "ADVANCED_OPTIONS" ON)

# Generate sample project (default: on)
CMAKE_DEPENDENT_OPTION (WITH_DOCU_SAMPLES "Toggle generation of samples in docu directory" ON "ADVANCED_OPTIONS" ON)

# Disable architecture specific functions
CMAKE_DEPENDENT_OPTION (WITH_OTHER_ARCH "Toggle inclusion of architecture specific functions" OFF "ADVANCED_OPTIONS" OFF)

# Alternative io mode (default: none)
SET (ALT_IO_MODE "none" CACHE
     STRING "Alternate io support: none (default), lwip, nb20")

IF (OPENSSL_FOUND AND "${ALT_IO_MODE}" STREQUAL "none")
  MESSAGE(STATUS "Found OPENSSL: ${OPENSSL_INCLUDE_DIR}, ${OPENSSL_INCLUDE_DIR}")
  IF(DPWS_COMPILE_FLAGS)
    SET(DPWS_COMPILE_FLAGS "${DPWS_COMPILE_FLAGS} -DWITH_OPENSSL -DWITH_DOM")
  ELSE(DPWS_COMPILE_FLAGS)
    SET(DPWS_COMPILE_FLAGS "-DWITH_OPENSSL -DWITH_DOM")
  ENDIF(DPWS_COMPILE_FLAGS)
  IF(TOOL_COMPILE_FLAGS)
    SET(TOOL_COMPILE_FLAGS "${TOOL_COMPILE_FLAGS} -DWITH_OPENSSL")
  ELSE(TOOL_COMPILE_FLAGS)
    SET(TOOL_COMPILE_FLAGS "-DWITH_OPENSSL")
  ENDIF(TOOL_COMPILE_FLAGS)
  IF(TOOL_LIBS)
    SET(TOOL_LIBS "${TOOL_LIBS} ${OPENSSL_LIBRARIES}")
  ELSE(TOOL_LIBS)
    SET(TOOL_LIBS "${OPENSSL_LIBRARIES}")
  ENDIF(TOOL_LIBS)
ENDIF (OPENSSL_FOUND AND "${ALT_IO_MODE}" STREQUAL "none")

# Show or hide specific variables
IF (ADVANCED_OPTIONS)
  MARK_AS_ADVANCED(CLEAR
    ALT_IO_MODE
    DTRACE_EXECUTABLE
    GNU_SED_EXECUTABLE
    XSLTPROC_EXECUTABLE
    GNU_PATCH_EXECUTABLE)
ELSE(ADVANCED_OPTIONS)
  MARK_AS_ADVANCED(FORCE
    ALT_IO_MODE
    DTRACE_EXECUTABLE
    GNU_SED_EXECUTABLE
    XSLTPROC_EXECUTABLE
    GNU_PATCH_EXECUTABLE)
ENDIF (ADVANCED_OPTIONS)

################################################################################
# Include further cmake files                                                  #
################################################################################

# ws4d-gsoap specific macros
INCLUDE(WS4D-Misc)

# make target to create source package
INCLUDE(PackSourceTar)

# make target to indent all source files
INCLUDE(IndentSources)

# make target to indent all source files
INCLUDE(Uninstall)

################################################################################
# Set paths to build ws4d-gsoap                                                #
################################################################################

# Set the output paths.
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

SET(DPWS_WS_DIR
    ${CMAKE_BINARY_DIR}/WS)

SET(DPWS_INCLUDE_DIR
    ${CMAKE_SOURCE_DIR}/include)

SET(DPWS_GENERATED_INCLUDE_DIR
    ${CMAKE_BINARY_DIR}/include)

SET(DPWS_INCLUDES
    ${DPWS_INCLUDE_DIR}
    ${DPWS_GENERATED_INCLUDE_DIR})

SET(DPWS_SRC_DIR
    ${CMAKE_SOURCE_DIR}/src)

SET(LIBDPWS_SRC_DIR
    ${DPWS_SRC_DIR}/libdpws)

################################################################################
# CTest configuration                                                          #
################################################################################

IF(NOT CMAKE_CROSSCOMPILING)


  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake
                 ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake
                 COPYONLY)

  ENABLE_TESTING()
  INCLUDE(CTest)

  FIND_PACKAGE(Check QUIET)
  IF(NOT FOUND_CHECK)
    MESSAGE(STATUS "Don't do unit tests because check is missing")
  ENDIF(NOT FOUND_CHECK)

  INCLUDE(TestingMacros)

ENDIF(NOT CMAKE_CROSSCOMPILING)

################################################################################
# Process sub directories                                                      #
################################################################################

ADD_SUBDIRECTORY(cmake-modules)
ADD_SUBDIRECTORY(WS)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(bin)
ADD_SUBDIRECTORY(doc)
ADD_SUBDIRECTORY(test)

MARK_AS_ADVANCED(
  EXECUTABLE_OUTPUT_PATH
  LIBRARY_OUTPUT_PATH
  CMAKE_BACKWARDS_COMPATIBILITY)
