# This file is part of the Owl Positioning System (OwlPS) project.
# It is subject to the copyright notice and license terms in the
# COPYRIGHT.t2t file found in the top-level directory of this
# distribution and at
#   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
# No part of the OwlPS Project, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
# distributed along with this file, either separately or by replacing
# this notice by the COPYRIGHT.t2t file's contents.

# Make sure boost libraries are installed
find_package(Boost 1.42.0 COMPONENTS program_options)
if (NOT Boost_FOUND)
  message(WARNING
    "OwlPS Positioner dependency missing: Boost")
  return()
endif()
include_directories(${Boost_INCLUDE_DIRS})

# Make sure libclaw_tween is installed
find_package(ClawTween)
if (NOT CLAWTWEEN_FOUND)
  message(WARNING
    "OwlPS Positioner dependency missing: Claw Tween (libclaw_tween)")
  return()
endif()
include_directories(${CLAWTWEEN_INCLUDE_DIR})

set(EXTRA_LIBS ${EXTRA_LIBS} ${Boost_LIBRARIES} ${CLAWTWEEN_LIBRARY} m)

# On Linux and other GNU systems (e.g. GNU/kFreeBSD), we need -lrt for
# clock_gettime()
string(REGEX MATCH "^GNU" GNU_SYSTEM ${CMAKE_SYSTEM_NAME})
if ("${GNU_SYSTEM}" OR ${CMAKE_SYSTEM_NAME} STREQUAL Linux)
  set(EXTRA_LIBS ${EXTRA_LIBS} rt)
endif()

# Enable C++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(OWLPS_POSITIONER_CLASSES_SRC_FILES
  capturepoint.cc
  capturepointsreadercsv.cc
  area.cc
  autocalibration.cc
  autocalibrationline.cc
  autocalibrationmesh.cc
  building.cc
  calibrationrequest.cc
  cartographyalgorithm.cc
  configuration.cc
  csvfilereader.cc
  csvstringreader.cc
  direction.cc
  fbcm.cc
  frbhmbasic.cc
  input.cc
  inputcsv.cc
  inputdatareader.cc
  inputlogcsv.cc
  inputmedium.cc
  inputudpsocket.cc
  interlinknetworks.cc
  measurement.cc
  minmax.cc
  mobile.cc
  mobilesreadercsv.cc
  nss.cc
  output.cc
  outputcsv.cc
  outputnetworksocket.cc
  outputtcpsocketevaal.cc
  outputterminal.cc
  outputudpsocket.cc
  point3d.cc
  posexcept.cc
  positioning.cc
  posutil.cc
  realposition.cc
  referencepoint.cc
  request.cc
  result.cc
  resultlist.cc
  stock.cc
  textfilereader.cc
  textfilewriter.cc
  timestamp.cc
  topologyreadercsv.cc
  trilaterationalgorithm.cc
  userinterface.cc
  waypoint.cc
  wifidevice.cc
  )

set(OWLPS_POSITIONER_SRC_FILES
  owlps-positionerd.cc
  ${OWLPS_POSITIONER_CLASSES_SRC_FILES}
  )

add_executable(owlps-positionerd
  ${OWLPS_POSITIONER_SRC_FILES})
target_link_libraries(owlps-positionerd
  libowlps.so
  ${EXTRA_LIBS})
install(
  TARGETS owlps-positionerd
  DESTINATION bin)

add_executable(owlps-positionerd.semistatic EXCLUDE_FROM_ALL
  ${OWLPS_POSITIONER_SRC_FILES})
target_link_libraries(owlps-positionerd.semistatic
  libowlps.a
  ${EXTRA_LIBS})
# Add the semistatic target to the global list of semistatic targets
list(APPEND OWLPS_SEMISTATIC_TARGETS owlps-positionerd.semistatic)
set(OWLPS_SEMISTATIC_TARGETS ${OWLPS_SEMISTATIC_TARGETS} PARENT_SCOPE)

add_executable(owlps-positionerd.static EXCLUDE_FROM_ALL
  ${OWLPS_POSITIONER_SRC_FILES})
set_target_properties(owlps-positionerd.static
  PROPERTIES LINK_FLAGS -static)
target_link_libraries(owlps-positionerd.static
  libowlps.a
  ${EXTRA_LIBS})
# Add the static target to the global list of static targets
list(APPEND OWLPS_STATIC_TARGETS owlps-positionerd.static)
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)

# Add compile flags for the above targets
set_target_properties(
  owlps-positionerd
  owlps-positionerd.semistatic
  owlps-positionerd.static
  PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})


### Tests ###

set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
set(TESTS_SRC_FILES
  ${TESTS_DIR}/area_test.hh
  ${TESTS_DIR}/building_test.hh
  ${TESTS_DIR}/calibrationrequest_test.hh
  ${TESTS_DIR}/capturepoint_test.hh
  ${TESTS_DIR}/configuration_test.hh
  ${TESTS_DIR}/csvfilereader_test.hh
  ${TESTS_DIR}/direction_test.hh
  ${TESTS_DIR}/fbcm_test.hh
  ${TESTS_DIR}/frbhmbasic_test.hh
  ${TESTS_DIR}/inputcsv_test.hh
  ${TESTS_DIR}/inputdatareader_test.hh
  ${TESTS_DIR}/inputlogcsv_test.hh
  ${TESTS_DIR}/input_test.hh
  ${TESTS_DIR}/interlinknetworks_test.hh
  ${TESTS_DIR}/measurement_test.hh
  ${TESTS_DIR}/minmax_test.hh
  ${TESTS_DIR}/mobile_test.hh
  ${TESTS_DIR}/nss_test.hh
  ${TESTS_DIR}/outputcsv_test.hh
  ${TESTS_DIR}/outputterminal_test.hh
  ${TESTS_DIR}/output_test.hh
  ${TESTS_DIR}/point3d_test.hh
  ${TESTS_DIR}/positioning_test.hh
  ${TESTS_DIR}/posutil_test.hh
  ${TESTS_DIR}/realposition_test.hh
  ${TESTS_DIR}/referencepoint_test.hh
  ${TESTS_DIR}/request_test.hh
  ${TESTS_DIR}/result_test.hh
  ${TESTS_DIR}/stock_test.hh
  ${TESTS_DIR}/textfilereader_test.hh
  ${TESTS_DIR}/textfilewriter_test.hh
  ${TESTS_DIR}/timestamp_test.hh
  ${TESTS_DIR}/topologyreadercsv_test.hh
  ${TESTS_DIR}/waypoint_test.hh
  ${TESTS_DIR}/wifidevice_test.hh
  )

# Create the test runner's source
set(TEST_POSITIONER_FILE_DEPS
  ${TESTS_SRC_FILES}
  ${TESTS_DIR}/valuetraits.hh
  ${TESTS_DIR}/testutil.hh)
add_custom_command(OUTPUT test_positioner.cc
  COMMAND cxxtestgen --error-printer
  --include=${TESTS_DIR}/valuetraits.hh
  --include=${TESTS_DIR}/testutil.hh -o test_positioner.cc
  ${TESTS_SRC_FILES}
  DEPENDS ${TEST_POSITIONER_FILE_DEPS})


# Compile the test runner
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(test_positioner EXCLUDE_FROM_ALL
  test_positioner.cc
  ${TESTS_DIR}/testutil.cc
  ${TESTS_DIR}/testsetup.cc
  ${OWLPS_POSITIONER_CLASSES_SRC_FILES})
target_link_libraries(test_positioner
  libowlps.a
  ${EXTRA_LIBS})


### Extra targets ###

# Indentation
set(INDENTER astyle --style=gnu --indent=spaces=2 --formatted --recursive)
add_custom_target(indent_positioner
  ${INDENTER} *.cc *.hh tests/*.cc tests/*.hh
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  COMMENT "Reindenting source files"
  VERBATIM)

# cppcheck
set(CODE_CHECKER cppcheck --quiet --enable=all)
add_custom_target(check_positioner
  ${CODE_CHECKER} *.hh *.cc
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  COMMENT "Checking source code")

# Documentation
if (DOXYGEN_FOUND)
  if (NOT DOXYGEN_DOT_FOUND)
    message(WARNING
      "Couldn't find Graphviz: you should install it if you want to generate diagrams in the OwlPS Positioner's HTML documentation")
  endif()
  string(REPLACE " " "\ "
    OWLPS_POSITIONER_SRC_FILES ${OWLPS_POSITIONER_SRC_FILES})
  set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc")
  set(DOXYGEN
    DOXYGEN_INPUT=${DOXYGEN_INPUT} DOXYGEN_OUTPUT_DIRECTORY=${DOXYGEN_OUTPUT_DIRECTORY} OWLPS_VERSION=${OWLPS_VERSION} "${DOXYGEN_EXECUTABLE}")
  set(DOXYFILE Doxyfile)
  add_custom_target(doc_positioner
    ${DOXYGEN} ${DOXYFILE}
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
    COMMENT "Generating Doxygen documentation for OwlPS Positioner in \"${DOXYGEN_OUTPUT_DIRECTORY}\"")
else()
  message(WARNING
    "Couldn't find Doxygen: disabling OwlPS Positioner's documentation target")
endif()
