# Define the minimum cmake version required
cmake_minimum_required(VERSION "3.10.2" FATAL_ERROR)

include(CMakeDependentOption)

set(CMAKE_CONFIGURATION_TYPES Debug;Release CACHE STRING "" FORCE)

# Modules tools
Project(Tools)

# Hide (and set to FALSE) OIV_LOCAL_PROJECT option in OIV_DEV_BUILD mode
cmake_dependent_option(OIV_LOCAL_PROJECT "generate local project ready to distrib" TRUE "NOT OIV_DEV_BUILD" FALSE)

if(OIV_LOCAL_PROJECT)
  set(CMAKE_SUPPRESS_REGENERATION TRUE)
  set(CMAKE_USE_RELATIVE_PATH TRUE)
else()
  if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
    message(FATAL_ERROR "CMake generation is not allowed within the
      source directory! Remove the CMakeCache.txt file and try again from another folder, e.g.:

      rm CMakeCache.txt
      mkdir -p build/{debug/release}
      cd build/debug
      cmake  ../..
      ")
  endif()
endif()

# Detect if we are running Cmake starting from tools directory or from
# main OivSuite directory
if(NOT OIV_DEV_BUILD AND ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
  # Here we have ran CMake directly in the tools directory
  set(OIV_COMMON_CMAKE_DIR ${CMAKE_SOURCE_DIR}/../source/cmake)
  include(${OIV_COMMON_CMAKE_DIR}/FindGit.cmake)
  include(${OIV_COMMON_CMAKE_DIR}/InternalBuildKey.cmake)
  include(${OIV_COMMON_CMAKE_DIR}/GPUDetection.cmake)

  if(OIV_INTERNAL_BUILD_KEY)
    message(STATUS "SHA1 build key: ${OIV_INTERNAL_BUILD_KEY}")
  endif()

  # ensure we find the OpenInventorConfig.cmake file
  list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR}/../source/cmake/config)
  find_package(OpenInventor REQUIRED)

  # First try to find OIV and the modules
  include(${OIV_COMMON_CMAKE_DIR}/UsefulMacros.cmake)
  include(${OIV_COMMON_CMAKE_DIR}/DefaultCompilerFlags.cmake)

  # Enable _SCL_SECURE_NO_WARNINGS to avoid warning C4996 on Windows
  if(MSVC)
    add_definitions(-D_SCL_SECURE_NO_WARNINGS)

    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${RUNTIME_OUTPUT_PATH})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${RUNTIME_OUTPUT_PATH})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${RUNTIME_OUTPUT_PATH})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${RUNTIME_OUTPUT_PATH})
  endif(MSVC)

  configure_file(${CMAKE_SOURCE_DIR}/../source/cmake/RelativeProjectWindows.cmake.in  ${CMAKE_BINARY_DIR}/../source/cmake/RelativeProjectWindows.cmake @ONLY)

  set(OIV_TOOLKIT_BUILD TRUE)
endif()

include(${OIV_COMMON_CMAKE_DIR}/CMakePolicy.cmake NO_POLICY_SCOPE)

if(WIN32)
  add_definitions(-DQT_DLL)
endif()

include_directories(${OIV_INCLUDE_DIR})

add_subdirectory(source)
