# -*- mode: cmake -*-
# vi: set ft=cmake :

# Copyright (c) 2012, Willow Garage, Inc.
# Copyright (c) 2017, Toyota Research Institute, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

if(CMAKE_VERSION VERSION_LESS 3.12)
  set(CONFIGURE_DEPENDS_OPTION)
else()
  set(CONFIGURE_DEPENDS_OPTION CONFIGURE_DEPENDS)
endif()

file(GLOB_RECURSE FCL_SOURCE_CODE ${CONFIGURE_DEPENDS_OPTION}
  "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)

add_library(fcl STATIC ${FCL_HEADERS} ${FCL_SOURCE_CODE})

# Be sure to pass to the consumer the set of SIMD used in the compilation
target_compile_options(fcl PUBLIC ${SSE_FLAGS})

set_target_properties(fcl PROPERTIES
  VERSION ${FCL_VERSION}
  SOVERSION ${FCL_ABI_VERSION})

# Hide symbols by default
#################################################
# Macro to check for visibility capability in compiler
# Original idea from: https://gitorious.org/ferric-cmake-stuff/
macro (check_compiler_visibility)
  include (CheckCXXCompilerFlag)
  check_cxx_compiler_flag(-fvisibility=hidden COMPILER_SUPPORTS_VISIBILITY)
endmacro()

if (UNIX)
  check_compiler_visibility()
  if (COMPILER_SUPPORTS_VISIBILITY)
    set_target_properties(fcl
      PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
  endif()
endif()

add_definitions("-DFCL_STATIC_DEFINE")
set_property(TARGET fcl PROPERTY POSITION_INDEPENDENT_CODE ON)

# Use the IMPORTED target from newer versions of ccd-config.cmake if available,
# otherwise fall back to CCD_INCLUDE_DIRS and CCD_LIBRARIES
if(TARGET ccd)
  target_link_libraries(fcl PUBLIC ccd)
else()
  target_include_directories(fcl SYSTEM PUBLIC "${CCD_INCLUDE_DIRS}")
  target_link_libraries(fcl PUBLIC "${CCD_LIBRARIES}")
endif()

# Use the IMPORTED target from newer versions of Eigen3Config.cmake if
# available, otherwise fall back to EIGEN3_INCLUDE_DIRS from older versions of
# Eigen3Config.cmake or EIGEN3_INCLUDE_DIR from FindEigen3.cmake
if(TARGET Eigen3::Eigen)
  # Note that Eigen3::Eigen is an INTERFACE library, so the INCLUDE_DIRECTORIES
  # and INTERFACE_INCLUDE_DIRECTORIES are populated, but nothing is actually
  # linked
  target_link_libraries(fcl PUBLIC Eigen3::Eigen)
elseif(EIGEN3_INCLUDE_DIRS)
  target_include_directories(fcl SYSTEM PUBLIC "${EIGEN3_INCLUDE_DIRS}")
else()
  target_include_directories(fcl SYSTEM PUBLIC "${EIGEN3_INCLUDE_DIR}")
endif()

if(FCL_HAVE_OCTOMAP)
  # Use the IMPORTED target from newer versions of octomap-config.cmake if
  # available, otherwise fall back to OCTOMAP_INCLUDE_DIRS and OCTOMAP_LIBRARIES
  if(TARGET octomap)
    target_link_libraries(fcl PUBLIC octomap)
  elseif(OCTOMAP_INCLUDE_DIRS AND OCTOMAP_LIBRARIES)
    target_include_directories(fcl SYSTEM PUBLIC "${OCTOMAP_INCLUDE_DIRS}")
    target_link_libraries(fcl PUBLIC "${OCTOMAP_LIBRARIES}")
  endif()
endif()

target_include_directories(fcl
  BEFORE
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
