cmake_minimum_required(VERSION 3.10)

#
# Sample CMakeLists.txt for building an application using AGX.
#
project(agxBuild)


# Check so that the setup_env environment file has been executed:
if ("$ENV{AGX_DIR}"  STREQUAL "")
  message(WARNING "\n*** The environment initialization file setup_env.bat/bash has not been executed\n*** Errors might occur during runtime!")
endif()

if (APPLE)
  set(CMAKE_MACOSX_RPATH 1)
endif()


if (NOT MSVC)
  # On non-windows, fallback to release libraries if debug version is not available
  set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Debug Release)
endif()


#
# Set this to the path of the installed agx version. For example set(AGX_INSTALL_DIRECTORY "C:/Program Files/Algoryx/AgX 2.10.0.0")
# By default it will look for AGX in the same directory as this CMakeLists.txt
set(AGX_INSTALL_DIRECTORY ${agxBuild_SOURCE_DIR})

# Get flags that were used when AGX was built.
include(${AGX_INSTALL_DIRECTORY}/AGX_build_settings.txt)


# Make build system 2.4.7 compliant
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

set (AGX_DEBUG_POSTFIX d)
set(CMAKE_DEBUG_POSTFIX ${AGX_DEBUG_POSTFIX} )

#
# Check for architecture
#
set (AGX_BUILD_ARCHITECTURE "")
if (CMAKE_SIZEOF_VOID_P MATCHES "8")
   set(AGX_64BIT_ARCHITECTURE 1)
else()
endif ()

if (MSVC)
  if (AGX_64BIT_ARCHITECTURE)
    set (AGX_BUILD_ARCHITECTURE x64)
  else()
    set (AGX_BUILD_ARCHITECTURE x86)
  endif()
endif(MSVC)

file(TO_NATIVE_PATH ${AGX_INSTALL_DIRECTORY}/include INCLUDE_PATH)

# Set the INCLUDE environment variable that will help CMake locate AgX, OSG etc with find_package(AGX)
set(ENV{INCLUDE} "$ENV{INCLUDE};${INCLUDE_PATH}")

file(TO_NATIVE_PATH ${AGX_INSTALL_DIRECTORY}/lib/${AGX_BUILD_ARCHITECTURE} LIB_PATH)
set(ENV{LIB} "$ENV{LIB};${LIB_PATH}")

# Need to be set, either as an environment variable or in CMake
# Here we take the current directory (of this file)
set (AGX_DIR ${AGX_INSTALL_DIRECTORY})

#
# Output directories, to support out-of-source builds, use _BINARY_DIR here.
#

#
# Setup CMake and environment
#
set( CMAKE_MODULE_PATH "${AGX_INSTALL_DIRECTORY}/CMakeModules;${CMAKE_MODULE_PATH}")

#
#
#
include(AGXUtils)
include(AGX${CMAKE_SYSTEM_NAME}Specific)
include(AGXCompilerFlags )

# To make some macros compatible
set(AGX_DYNAMIC TRUE)
set( AGX_BINARY_DIR ${agxBuild_BINARY_DIR})

if (MSVC)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${agxBuild_BINARY_DIR})
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${agxBuild_BINARY_DIR})
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${agxBuild_BINARY_DIR})
endif(MSVC)

#Libraries for debug has a postfix to separate them from release
set(CMAKE_DEBUG_POSTFIX "d" )

#
# Build Options
#
option(AGX_CMAKE_VERBOSE "Enable to get verbose information from the CMake scripts" ON)


if (WIN32)
  # Exclude CMakeLists.txt from .vcproj
  set(CMAKE_SUPPRESS_REGENERATION "1")
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

option(AGX_BUILD_FMI_TUTORIALS "Set to ON to build C++ FMI tutorials" OFF)
option(AGX_ENABLE_TESTS "Set to ON to enable tests." OFF)

if(AGX_ENABLE_TESTS)
  include(CTest)
  enable_testing()
endif()

# Now locate AGX
find_package(AGX REQUIRED)

if (NOT AGX_FOUND)
  message(FATAL_ERROR "Unable to locate AGX headers and libraries. Cannot proceed.")
endif()

if (AGX_USE_OSG)
  find_package(OSG REQUIRED)
  if (NOT OSG_FOUND)
    message("Could not find OSG!")
  else ()
    include_directories (${OSG_INCLUDE_DIR})
    link_directories(${OSG_LIBRARY_DIR})
  endif ()
endif ()

#
# Include and link directories.
#
include_directories (
  ${AGX_INCLUDE_DIR}
)

link_directories(
  ${AGX_LIBRARY_DIR}
)


# We want paths embedded in binaries (e.g tutorials) that are built on OSes that supports
# this feature so that required libraries are found more easily.
# CMake will embed paths by default in the build tree, this block is needed to also
# have paths in the install tree.
#
set(CMAKE_SKIP_RPATH FALSE)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${AGX_LIBRARY_DIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)


# We can only build tutorials and agxviewer if we have osg
if (AGX_USE_OSG)
  add_subdirectory(tutorials)
  add_subdirectory(applications/agxViewer)
  add_subdirectory(agxOSG)
endif()

if (AGX_USE_ROS2)
  add_subdirectory(agxROS2)
endif()

if(AGX_BUILD_FMI_TUTORIALS)
  add_subdirectory(data/FmiModules/Tutorial/CppModule/ExternalBuild)
endif()
