# Copyright (C) 2015-2021 |Meso|Star> (contact@meso-star.com)
#
# This software is a computer program whose purpose is to generate files used
# to build the Star-3D library.
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.

cmake_minimum_required(VERSION 3.1)
project(star-3d C)
enable_testing()

set(S3D_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
option(NO_TEST "Disable the test" OFF)

################################################################################
# Check dependencies
################################################################################
find_package(Embree 3.6 REQUIRED)
find_package(RCMake 0.2.2 REQUIRED)
find_package(RSys 0.6 REQUIRED)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
include(rcmake)
include(rcmake_runtime)

include_directories(${EMBREE_INCLUDE_DIRS} ${RSys_INCLUDE_DIR})

if(CMAKE_COMPILER_IS_GNUCC)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
endif()

################################################################################
# Configure and define targets
################################################################################
set(VERSION_MAJOR 0)
set(VERSION_MINOR 8)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

set(S3D_FILES_SRC
  s3d_device.c
  s3d_geometry.c
  s3d_instance.c
  s3d_mesh.c
  s3d_primitive.c
  s3d_scene.c
  s3d_scene_view.c
  s3d_scene_view_closest_point.c
  s3d_scene_view_trace_ray.c
  s3d_shape.c
  s3d_sphere.c)
set(S3D_FILES_INC_API s3d.h)
set(S3D_FILES_INC
  s3d.h
  s3d_backend.h
  s3d_buffer.h
  s3d_device_c.h
  s3d_geometry.h
  s3d_instance.h
  s3d_mesh.h
  s3d_scene_c.h
  s3d_scene_view_c.h
  s3d_shape_c.h
  s3d_sphere.h)
set(S3D_FILES_DOC COPYING.fr COPYING.en README.md)

# Prepend each file in the `S3D_FILES_<SRC|INC>' list by `S3D_SOURCE_DIR'
rcmake_prepend_path(S3D_FILES_SRC ${S3D_SOURCE_DIR})
rcmake_prepend_path(S3D_FILES_INC ${S3D_SOURCE_DIR})
rcmake_prepend_path(S3D_FILES_INC_API ${S3D_SOURCE_DIR})
rcmake_prepend_path(S3D_FILES_DOC ${PROJECT_SOURCE_DIR}/../)

add_library(s3d SHARED ${S3D_FILES_SRC} ${S3D_FILES_INC} ${S3D_FILES_INC_API})
target_link_libraries(s3d RSys ${EMBREE_LIBRARIES})

if(CMAKE_COMPILER_IS_GNUCC)
  target_link_libraries(s3d m)
endif()

set_target_properties(s3d PROPERTIES
  DEFINE_SYMBOL S3D_SHARED_BUILD
  VERSION ${VERSION}
  SOVERSION ${VERSION_MAJOR})

rcmake_setup_devel(s3d Star3D ${VERSION} star/s3d_version.h)

################################################################################
# Add tests
################################################################################
if(NOT NO_TEST)
  function(build_test _name)
    add_executable(${_name}
      ${S3D_SOURCE_DIR}/${_name}.c
      ${S3D_SOURCE_DIR}/test_s3d_utils.h)
    target_link_libraries(${_name} s3d RSys)
    set(_libraries  ${ARGN})
    foreach(_lib ${_libraries})
      target_link_libraries(${_name} ${_lib})
    endforeach()
  endfunction()

  function(register_test _name)
    add_test(${_name} ${ARGN})
  endfunction()

  function(new_test _name)
    build_test(${_name} ${ARGN})
    register_test(${_name} ${_name})
  endfunction()

  new_test(test_s3d_accel_struct_conf)
  new_test(test_s3d_closest_point)
  new_test(test_s3d_device)
  new_test(test_s3d_sampler)
  new_test(test_s3d_sample_sphere)
  new_test(test_s3d_scene)
  new_test(test_s3d_scene_view)
  new_test(test_s3d_scene_view_aabb)
  new_test(test_s3d_seams)
  new_test(test_s3d_shape)
  new_test(test_s3d_sphere)
  new_test(test_s3d_sphere_box)
  new_test(test_s3d_primitive)
  new_test(test_s3d_trace_ray_instance)
  new_test(test_s3d_trace_ray_sphere)

  build_test(test_s3d_sphere_instance)
  register_test(test_s3d_sphere_instance_legacy test_s3d_sphere_instance)
  register_test(test_s3d_sphere_instance_filter test_s3d_sphere_instance filter)

  build_test(test_s3d_trace_ray)
  register_test(test_s3d_trace_ray_legacy test_s3d_trace_ray)
  register_test(test_s3d_trace_ray_filter test_s3d_trace_ray filter)

  rcmake_copy_runtime_libraries(test_s3d_trace_ray)

endif(NOT NO_TEST)

################################################################################
# Define output & install directories
################################################################################
install(TARGETS s3d
  ARCHIVE DESTINATION bin
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)
install(FILES ${S3D_FILES_INC_API} DESTINATION include/star)
install(FILES ${S3D_FILES_DOC} DESTINATION share/doc/star-3d)

