project(LogService)
cmake_minimum_required(VERSION 2.6)

set(LOGSERVICE_MAJOR_VERSION 2)
set(LOGSERVICE_MINOR_VERSION 8)
set(LOGSERVICE_REVISION_VERSION 0)

set(LOGSERVICE_VERSION ${LOGSERVICE_MAJOR_VERSION}.${LOGSERVICE_MINOR_VERSION}.${LOGSERVICE_REVISION_VERSION})

#-------------- PLATFORM SPECIFIC COMPILATION FLAGS -----------------------
# Requires CMake >= 2.4.7
string(COMPARE EQUAL ${CMAKE_SYSTEM_NAME} "AIX" AIX)
string(COMPARE EQUAL ${CMAKE_SYSTEM_NAME} "Darwin" APPLE)
string(COMPARE EQUAL ${CMAKE_SYSTEM_NAME} "Linux" LINUX)
string(COMPARE EQUAL ${CMAKE_SYSTEM_NAME} "SunOS" SUNOS)
string(COMPARE EQUAL ${CMAKE_SYSTEM_NAME} "FreeBSD" FREEBSD)
 
if(AIX)
  message(STATUS "XXX System name Aix")
  add_definitions(-D__aix__)
elseif(APPLE)
  message(STATUS "XXX System name Darwin")
  add_definitions(-D__darwin__)
elseif(LINUX)
  message(STATUS "XXX System name Linux")
  add_definitions(-D__linux__)
elseif(SUNOS)
  message(STATUS "XXX System name SunOS")
  add_definitions(-D__sunos__)
elseif(FREEBSD)
  message(STATUS "XXX System name FreeBSD")
  add_definitions(-D__freebsd__)
endif()

# installation customization hooks
# manage lib/lib64 install issue
set(LIB_SUFFIX "" CACHE STRING "Define suffix of lib directory name (32/64)")
set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}"
  CACHE INTERNAL "Directory where lib will be installed")
set(BIN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin"
  CACHE INTERNAL "Directory where binary will be installed")
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include"
  CACHE INTERNAL "Directory where include will be installed")
set(MAN_INSTALL_DIR "share/man"
  CACHE INTERNAL "Directory where man pages will be installed")
set(DOC_INSTALL_DIR "share/doc/${PROJECT_NAME}"
  CACHE PATH "Directory where doc will be installed")
set(CMAKE_MOD_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/cmake/Modules"
  CACHE INTERNAL "Directory where cmake modules will be installed")


# --------------------- DEPENDENCIES TOWARDS EXTERNAL PACKAGES -------------
# Path to additional modules (i.e. used by FIND_PACKAGE commands making
# reference to non CMake defined "standard" modules):
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/Cmake)


# --------------------- DEPENDENCIES TOWARDS OmniORB -----------------------
# because x86_64 can't link static libraries to shared libraries
# and from 2.5, this is what is done in DIET
# see http://www.mail-archive.com/cross-lfs%40linuxfromscratch.org/msg00411.html
# You will get these messages on x86_64 any place that libtool tries to use a static (.a) library in a .la, and as it says, it can't link. These "recompile with -fPIC" messages fall into three types -
# (i) recompile the current package with -fPIC
# (ii) fix a broken symlink (I had a dangling symlink for ncurses in my scripts, because of a typo - on x86 libtool couldn't find the .so but took the .a and ran with it, on x86_64 it barfed).
# (iii) convert a Makefile to use .la instead of .a (very uncommon).

# Note: CMAKE_SYSTEM_PROCESSOR return the equivalent of uname -p, note uname -m

if (UNIX AND NOT WIN32)
  find_program(CMAKE_UNAME uname /bin /usr/bin /usr/local/bin)
 if (CMAKE_UNAME)
   exec_program(uname ARGS -m OUTPUT_VARIABLE SYSTEM_PROC_TYPE)
   set(SYSTEM_PROC_TYPE ${SYSTEM_PROC_TYPE} CACHE INTERNAL
     "processor type (i386 and x86_64)")
     if (SYSTEM_PROC_TYPE MATCHES "x86_64|amd64")
     # TODO: Should be done only for libraries build
     add_definitions(-fPIC)
   endif()
 endif()
endif()

# Are libraries dynamic libraries as opposed to library archives ?
# Note: this variable is a cmake internal.
option(BUILD_SHARED_LIBS "Build libraries as shared libraries." ON)


# --------------------- DEPENDENCIES TOWARDS OmniORB -----------------------
find_package(OmniORB)
if (OMNIORB4_FOUND)
  add_definitions(-D__OMNIORB4__)
  if (BUILD_SHARED_LIBS)
    # Link DIET to OmniORB shared libs
    set(OMNIORB4_LIBRARIES ${OMNIORB4_LIBRARIES_sh})
  else ()
    # Link DIET to OmniORB static libs
    set(OMNIORB4_LIBRARIES ${OMNIORB4_LIBRARIES_st})
  endif ()
else ()
  message("omniORB installation was not found. Please provide OMNIORB4_DIR:")
  message("  - through the GUI when working with ccmake, ")
  message("  - as a command line argument when working with cmake e.g. ")
  message("    cmake .. -DOMNIORB4_DIR:PATH=/usr/local/omniORB-4.0.7 ")
  message("Note: the following message is triggered by cmake on the first ")
  message("    undefined necessary PATH variable (e.g.  OMNIORB4_INCLUDE_DIR).")
  message("    Providing OMNIORB4_DIR (as above described) is probably the")
  message("    simplest solution unless you have a really customized/odd")
  message("    omniORB installation...")
  set(OMNIORB4_DIR "" CACHE PATH "Root of omniORB install tree.")
endif ()

# ------------------------- Doxygen documentation ------------------------
option(LOGSERVICE_BUILD_DOXYGEN "Build LogService doxygen." OFF)
if(LOGSERVICE_BUILD_DOXYGEN)
  # create doc target if non-existant
  add_custom_target(doc ALL)
  include(UseDoxygen)
  generate_doxygen(FORMATS ${DOXYGEN_FORMATS})
  # FIXME: now we force man3 and html generation
  # we should check that the following format are generated before install
  install(DIRECTORY ${CMAKE_BINARY_DIR}/doxygen/man/man3
    DESTINATION ${MAN_INSTALL_DIR})
  install(DIRECTORY ${CMAKE_BINARY_DIR}/doxygen/html
    DESTINATION ${DOC_INSTALL_DIR})
endif()

if (UNIX AND NOT WIN32)
  find_program(CMAKE_UNAME uname /bin /usr/bin /usr/local/bin)
  if (CMAKE_UNAME)
    exec_program(uname ARGS -m OUTPUT_VARIABLE SYSTEM_PROC_TYPE)
    set(SYSTEM_PROC_TYPE ${SYSTEM_PROC_TYPE} CACHE INTERNAL 
      "processor type (i386 and x86_64)")
    if (SYSTEM_PROC_TYPE MATCHES "x86_64|amd64")
      # TODO: Should be done only for libraries build
      add_definitions(-fPIC)
    endif()
  endif()
endif()

set (AR_DIR ${CMAKE_MODULE_PATH})
find_package (AR)

add_subdirectory(src)
add_subdirectory(docs/man)

