#-------------------------------------------------------------------
# This file is part of the CMake build system for OGRE
#     (Object-oriented Graphics Rendering Engine)
# For the latest info, see http://www.ogre3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------

######################################################################
# OGRE SAMPLES BUILD SYSTEM
######################################################################

cmake_minimum_required(VERSION 2.6.2)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
cmake_policy(SET CMP0003 NEW)

# CMake 2.8.2 has a bug that creates unusable Xcode projects when using ARCHS_STANDARD_32_BIT
# to specify both armv6 and armv7.
if(OGRE_BUILD_PLATFORM_APPLE_IOS AND (CMAKE_VERSION VERSION_EQUAL 2.8.2) AND (CMAKE_GENERATOR STREQUAL "Xcode"))
	message(FATAL_ERROR "CMake 2.8.2 cannot create compatible Xcode projects for iOS, please download the latest version or an older release from http://www.cmake.org/files/")
endif()

# Just debug / release since that's all that's included in SDK. Only release for iOS/OS X
if(APPLE)
  set (CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "" FORCE)
else()
  set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
endif()

# Use relative paths
# Note that up to CMake 2.8.0 on MSVC the .pdbs are still referenced absolutely
# This has been fixed with a patch so I expect that in future we will be able to use this
# to pre-generate the sample projects for the SDK. 
if(WIN32)
  # This seems to break Xcode projects so definitely don't enable on Apple builds
  set(CMAKE_USE_RELATIVE_PATHS true)
  set(CMAKE_SUPPRESS_REGENERATION true)
endif()

project(OGRE)

# Find actual Ogre root
if (WIN32 OR APPLE)
  get_filename_component(OGRE_ROOT_DIR "${OGRE_SOURCE_DIR}" ABSOLUTE)
  set(OGRE_TEMPLATES_DIR "${OGRE_ROOT_DIR}/cmake")
  set(OGRE_WORK_DIR "${OGRE_ROOT_DIR}")
  set(CMAKE_MODULE_PATH "${OGRE_ROOT_DIR}/cmake")
elseif (UNIX)
  get_filename_component(OGRE_ROOT_DIR "${OGRE_SOURCE_DIR}/../.." ABSOLUTE)
  set(OGRE_TEMPLATES_DIR "${OGRE_ROOT_DIR}/lib/OGRE/cmake")
  set(OGRE_WORK_DIR "${OGRE_ROOT_DIR}")
  set(CMAKE_MODULE_PATH "${OGRE_ROOT_DIR}/lib/OGRE/cmake")
endif ()

if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  #  By default, install to the Ogre SDK root
  SET(CMAKE_INSTALL_PREFIX
   "${OGRE_ROOT_DIR}" CACHE PATH "OGRE install prefix" FORCE
  )
endif ()

# Include necessary submodules
set(OGRE_SDK_BUILD TRUE)
set(OGRE_LIB_TYPE @OGRE_LIB_TYPE@)
set(OGRE_STATIC @OGRE_STATIC@)
include(OgreConfigTargets)
set(CMAKE_PREFIX_PATH "${OGRE_ROOT_DIR}")
set(OGRE_INSTALL_SAMPLES FALSE)
if (NOT APPLE)
  # Create debug libraries with _d postfix
  set(CMAKE_DEBUG_POSTFIX "_d")
endif ()
# Boost is included
set (BOOST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/boost")

#####################################################################
# Set up the basic build environment
#####################################################################

# Set compiler specific build flags
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
  list(APPEND CMAKE_CXX_FLAGS -msse)
endif ()
if (APPLE AND OGRE_BUILD_PLATFORM_APPLE_IOS)
  # Set build variables
  set(XCODE_ATTRIBUTE_SDKROOT iphoneos)
  if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
    execute_process(COMMAND xcodebuild -version -sdk "${XCODE_ATTRIBUTE_SDKROOT}" Path | head -n 1 OUTPUT_VARIABLE CMAKE_OSX_SYSROOT)
    string(REGEX REPLACE "(\r?\n)+$" "" CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT}")
  else()
    set(CMAKE_OSX_SYSROOT iphoneos)
  endif()

  set(CMAKE_OSX_DEPLOYMENT_TARGET "")
  set(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -framework CoreGraphics -framework QuartzCore -framework UIKit")
  set(XCODE_ATTRIBUTE_GCC_UNROLL_LOOPS "YES")
  set(XCODE_ATTRIBUTE_LLVM_VECTORIZE_LOOPS "YES")
  set(XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
  set(OGRE_BUILD_RENDERSYSTEM_GLES TRUE CACHE BOOL "Forcing OpenGL ES RenderSystem for iOS" FORCE)
  set(OGRE_BUILD_RENDERSYSTEM_GLES2 TRUE CACHE BOOL "Forcing OpenGL ES 2 RenderSystem for iOS" FORCE)
  set(OGRE_STATIC TRUE CACHE BOOL "Forcing static build for iOS" FORCE)
  set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.yourcompany.\${PRODUCT_NAME:rfc1034identifier}")
  set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_INCLUDING_64_BIT)")

  remove_definitions(-msse)
endif ()
if (MSVC)
  add_definitions(/fp:fast)
endif ()

# Find dependencies
find_package(OGRE REQUIRED)
find_package(OIS REQUIRED)

# Need to get the Cg path for OS X post build scripts.
if (APPLE AND NOT OGRE_RenderSystem_GLES2_FOUND AND NOT OGRE_RenderSystem_GLES_FOUND)
  find_package(Cg REQUIRED)
endif()

if (NOT OGRE_BUILD_PLATFORM_APPLE_IOS)
	if (WIN32 OR APPLE)
		set(Boost_USE_STATIC_LIBS TRUE)
	else ()
		# Statically linking boost to a dynamic Ogre build doesn't work on Linux 64bit
		set(Boost_USE_STATIC_LIBS ${OGRE_STATIC})
	endif ()
	if (MINGW)
		# this is probably a bug in CMake: the boost find module tries to look for
		# boost libraries with name libboost_*, but CMake already prefixes library
		# search names with "lib". This is the workaround.
		set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} "")
	endif ()
    set(Boost_ADDITIONAL_VERSIONS "1.54" "1.54.0" "1.53" "1.53.0" "1.52" "1.52.0" "1.51" "1.51.0" "1.50" "1.50.0" "1.49" "1.49.0" "1.48" "1.48.0" "1.47" "1.47.0" "1.46" "1.46.0" "1.45" "1.45.0" "1.44" "1.44.0" "1.42" "1.42.0" "1.41.0" "1.41" "1.40.0" "1.40")
    # Components that need linking (NB does not include header-only components like bind)
    set(OGRE_BOOST_COMPONENTS thread date_time)
    find_package(Boost QUIET)
    if (NOT Boost_FOUND)
        if(Boost_USE_STATIC_LIBS)
            set(Boost_USE_STATIC_LIBS OFF)
        else()
            set(Boost_USE_STATIC_LIBS ON)
        endif()
        find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET)
    endif()    

    if(Boost_FOUND AND Boost_VERSION GREATER 104900)
        set(OGRE_BOOST_COMPONENTS thread date_time system chrono)
        find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET)
    endif()

	# Set up referencing of Boost
	include_directories(${Boost_INCLUDE_DIR})
	add_definitions(-DBOOST_ALL_NO_LIB)
	set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${Boost_LIBRARIES})
else()
	set(BOOST_LIBRARYDIR "${CMAKE_CURRENT_SOURCE_DIR}/boost/lib")
	set(BOOST_INCLUDEDIR "${CMAKE_CURRENT_SOURCE_DIR}/boost/include")
	set(Boost_USE_STATIC_LIBS TRUE)
	set(Boost_NO_SYSTEM_PATHS TRUE)
    set(Boost_USE_MULTITHREADED OFF)

    set(Boost_ADDITIONAL_VERSIONS "1.54" "1.54.0" "1.53" "1.53.0" "1.52" "1.52.0" "1.51" "1.51.0" "1.50" "1.50.0" "1.49" "1.49.0" "1.48" "1.48.0" "1.47" "1.47.0" "1.46" "1.46.0" "1.45" "1.45.0" "1.44" "1.44.0" "1.42" "1.42.0" "1.41.0" "1.41" "1.40.0" "1.40")
    # Components that need linking (NB does not include header-only components like bind)
    set(OGRE_BOOST_COMPONENTS thread date_time)
    find_package(Boost QUIET)
    if (NOT Boost_FOUND)
        if(Boost_USE_STATIC_LIBS)
            set(Boost_USE_STATIC_LIBS OFF)
        else()
            set(Boost_USE_STATIC_LIBS ON)
        endif()
        find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET)
    endif()    

    if(Boost_FOUND AND Boost_VERSION GREATER 104900)
        set(OGRE_BOOST_COMPONENTS thread date_time system chrono)
        find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET)
    endif()

	# Set up referencing of Boost
	include_directories(${Boost_INCLUDE_DIR})
	add_definitions(-DBOOST_ALL_NO_LIB)
	set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${Boost_LIBRARIES})
endif(NOT OGRE_BUILD_PLATFORM_APPLE_IOS)

include_directories(
  ${OGRE_INCLUDE_DIRS}
  ${OGRE_Plugin_PCZSceneManager_INCLUDE_DIRS}
  ${OGRE_Plugin_OctreeZone_INCLUDE_DIRS}
  ${OIS_INCLUDE_DIRS}
  ${CMAKE_CURRENT_SOURCE_DIR}/Common/include
)
if (APPLE)
  if (OGRE_BUILD_PLATFORM_APPLE_IOS)
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/OGRE/iOS")
  else ()
    # Set 10.6 as the base SDK by default
    set(XCODE_ATTRIBUTE_SDKROOT macosx)
    if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
      execute_process(COMMAND xcodebuild -version -sdk "${XCODE_ATTRIBUTE_SDKROOT}" Path | head -n 1 OUTPUT_VARIABLE CMAKE_OSX_SYSROOT)
      string(REGEX REPLACE "(\r?\n)+$" "" CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT}")
    else()
      set(CMAKE_OSX_SYSROOT macosx)
    endif()

    set(CMAKE_OSX_DEPLOYMENT_TARGET 10.6)
    set(CMAKE_OSX_ARCHITECTURES ${ARCHS_STANDARD_32_64_BIT})
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/OGRE/OSX"
      "${CMAKE_CURRENT_SOURCE_DIR}/include/OGRE/RenderSystems/GL"
      "${CMAKE_CURRENT_SOURCE_DIR}/include/OGRE/RenderSystems/GL/OSX")
  endif ()
endif (APPLE)

if (OGRE_RTShaderSystem_FOUND)
	add_definitions(-DINCLUDE_RTSHADER_SYSTEM)
	ogre_add_component_include_dir(RTShaderSystem)
		# Link to RT Shader System if enabled.
  		set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${OGRE_RTShaderSystem_LIBRARIES})
endif()

if (OGRE_Terrain_FOUND)
    ogre_add_component_include_dir(Terrain)
    set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${OGRE_Terrain_LIBRARIES})
endif ()

if (OGRE_Paging_FOUND)
    ogre_add_component_include_dir(Paging)
    set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${OGRE_Paging_LIBRARIES})
endif ()

if (OGRE_Volume_FOUND)
    ogre_add_component_include_dir(Volume)
    set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${OGRE_Volume_LIBRARIES})
endif ()

if (OGRE_Overlay_FOUND)
    ogre_add_component_include_dir(Overlay)
    set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${OGRE_Overlay_LIBRARIES})
endif ()

if (UNIX AND NOT APPLE)
  # on Linux, shipped samples will still be compiled with CMake, so we need
  # the .cfg files in the build directory.
  configure_file(${OGRE_SOURCE_DIR}/plugins.cfg ${OGRE_BINARY_DIR}/bin/plugins.cfg COPYONLY)
  configure_file(${OGRE_SOURCE_DIR}/resources.cfg ${OGRE_BINARY_DIR}/bin/resources.cfg COPYONLY)
  configure_file(${OGRE_SOURCE_DIR}/samples.cfg ${OGRE_BINARY_DIR}/bin/samples.cfg COPYONLY)
  configure_file(${OGRE_SOURCE_DIR}/quakemap.cfg ${OGRE_BINARY_DIR}/bin/quakemap.cfg COPYONLY)
endif ()

if (MINGW)
  # MinGW also relies on CMake, but we can just output to source dir.
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_definitions(-DDEBUG)
    set(OGRE_BUILD_TYPE "debug")
  else ()
    set(OGRE_BUILD_TYPE "release")
  endif ()
  set(OGRE_ARCHIVE_OUTPUT ${OGRE_SOURCE_DIR}/lib/${OGRE_BUILD_TYPE})
  set(OGRE_LIBRARY_OUTPUT ${OGRE_SOURCE_DIR}/lib/${OGRE_BUILD_TYPE})
  set(OGRE_RUNTIME_OUTPUT ${OGRE_SOURCE_DIR}/bin/${OGRE_BUILD_TYPE})
endif ()


if (OGRE_STATIC)
  # need to link against all plugins
  if (OGRE_Plugin_BSPSceneManager_FOUND)
    include_directories(${OGRE_Plugin_BSPSceneManager_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_Plugin_BSPSceneManager_LIBRARIES})
  endif ()
  if (OGRE_Plugin_CgProgramManager_FOUND)
    include_directories(${OGRE_Plugin_CgProgramManager_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_Plugin_CgProgramManager_LIBRARIES})
  endif ()
  if (OGRE_Plugin_OctreeSceneManager_FOUND)
    include_directories(${OGRE_Plugin_OctreeSceneManager_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_Plugin_OctreeSceneManager_LIBRARIES})
  endif ()
  if (OGRE_Plugin_ParticleFX_FOUND)
    include_directories(${OGRE_Plugin_ParticleFX_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_Plugin_ParticleFX_LIBRARIES})
  endif ()
  if (OGRE_RenderSystem_Direct3D9_FOUND)
    include_directories(${OGRE_RenderSystem_Direct3D9_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_RenderSystem_Direct3D9_LIBRARIES})
  endif ()
  if (OGRE_RenderSystem_Direct3D11_FOUND)
    include_directories(${OGRE_RenderSystem_Direct3D11_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_RenderSystem_Direct3D11_LIBRARIES})
  endif ()
  if (OGRE_RenderSystem_GL_FOUND)
    include_directories(${OGRE_RenderSystem_GL_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_RenderSystem_GL_LIBRARIES})
  endif ()
  if (OGRE_RenderSystem_GLES_FOUND)
    include_directories(${OGRE_RenderSystem_GLES_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_RenderSystem_GLES_LIBRARIES})
  endif ()
  if (OGRE_RenderSystem_GLES2_FOUND)
    include_directories(${OGRE_RenderSystem_GLES2_INCLUDE_DIRS})
    set(OGRE_PLUGIN_LIBRARIES ${OGRE_PLUGIN_LIBRARIES} ${OGRE_RenderSystem_GLES2_LIBRARIES})
  endif ()
endif ()

add_subdirectory(Samples)
