# cmake for upper modules

# Required version of CMake
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)

set(SIMD_INSTRUCTION_SET "AVX1" CACHE STRING "choose between SSE4, AVX1 and AVX2")
set_property(CACHE SIMD_INSTRUCTION_SET PROPERTY STRINGS "SSE4" "AVX1" "AVX2")

ADD_DEFINITIONS("-DMSYS_LINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -DMSYS_UNIX_LARGEFILE")

SET(EXTENSION_360_VIDEO false CACHE BOOL "enables 360 extensions")
IF(EXTENSION_360_VIDEO)
	SET(TCH_MTT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
    SET(TCH_360_LIB_PATH "" CACHE PATH "path to 360lib dir")
	IF ("${TCH_360_LIB_PATH}" STREQUAL "")
		message(FATAL_ERROR "provide path TCH_360_LIB_PATH")
	ENDIF()
    add_definitions(-DEXTENSION_360_VIDEO=1)
ENDIF()

IF (NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
	set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
ENDIF()


PROJECT(MTT)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules")

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
  IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
	  IF (SIMD_INSTRUCTION_SET STREQUAL "SSE4")
		message("[INFO] BUILD SSE4")
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -msse4")
	  ELSEIF( SIMD_INSTRUCTION_SET STREQUAL "AVX1" )
		message("[INFO] BUILD AVX1")
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -mavx")
	  ELSEIF( SIMD_INSTRUCTION_SET STREQUAL "AVX2" )
		message("[INFO] BUILD AVX2")
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -mavx2") 
	  ELSE()
		message( FATAL_ERROR "Unknown SIMD type")
	  ENDIF()
   ELSE()
     message("[INFO] BUILD Debug SIMD SSE4")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4")
   ENDIF()

  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wall -Wshadow -Wno-sign-compare -Werror -Wno-unused -Wno-unknown-pragmas -Wno-array-bounds")
  IF (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized")
     IF(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-misleading-indentation")
	 ENDIF()
  ENDIF()
ENDIF()

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W3 /MTd")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W3 /WX /GL")
  set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
  set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
  set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
  #IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
	  IF (SIMD_INSTRUCTION_SET STREQUAL "SSE4")
		message("[INFO] BUILD SSE4")
		set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /MT") 
		set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /MT") 
	  ELSEIF( SIMD_INSTRUCTION_SET STREQUAL "AVX1" )
		message("[INFO] BUILD AVX1")
                set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /arch:AVX /MT")
		set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /arch:AVX /MT") 
	  ELSEIF( SIMD_INSTRUCTION_SET STREQUAL "AVX2" )
		message("[INFO] BUILD AVX2")
		set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /arch:AVX2 /MT") 
		set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /arch:AVX2 /MT") 
	  ELSE()
		message( FATAL_ERROR "Unknown SIMD type")
	  ENDIF()
ENDIF()

# ---------------------------------------------
# generic part starts here 

SET(CMAKE_DEBUG_POSTFIX "_debug") # for library
 
# customisable variables 

# SET(CMAKE_BUILD_TYPE Release CACHE STRING "Build type automatically set to Release, but can be change to Debug.")
SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} CACHE PATH "path for cmake modules (e.g. Qt path)")
MARK_AS_ADVANCED(CMAKE_INSTALL_PREFIX)

# detect configuration
IF(MSVC)
  MESSAGE("[INFO] MSVC:${MSVC_VERSION}")
 IF(MSVC10) # visual 2010
  MESSAGE("[INFO] MSVC10")
 ELSEIF(MSVC12) # visual 2013
  MESSAGE("[INFO] MSVC12")
   SET(CMAKE_GENERATOR_TOOLSET "CTP_Nov2013" CACHE STRING "Platform Toolset" FORCE) # force ctp
 ELSEIF(MSVC14) # visual 2015
  MESSAGE("[INFO] MSVC14")
 ELSE()
  MESSAGE("[INFO] MSVCxx")
 ENDIF()
  SET(COMP ${CMAKE_CXX_COMPILER_ID})

ELSE()
  SET(COMP ${CMAKE_CXX_COMPILER_ID})
# NB: to build with clang, first do a export CC=/usr/bin/clang CXX=/usr/bin/clang++ before cmake
ENDIF()

# make a var of the form: os_compiler_version_tgt
SET(TARGET_DIR "${CMAKE_SYSTEM_NAME}_${COMP}_${CMAKE_CXX_COMPILER_VERSION}_${CMAKE_SYSTEM_PROCESSOR}")

# set default warnings
if(MSVC)
 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  # Disable "anoying" warnings
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244 /wd4267 /wd4305") # disable warning about pod conversion
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996") # disable warning about unsafe parameters function (because msvc warns about its own stuff!)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4425") # disable warning about not supported const expr)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4930") # disable warning about prototyped function not called (was a variable definition intended?))
  #add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  #add_definitions(-D_SCL_SECURE_NO_WARNINGS)

ELSE() # assume gcc or clang
   
ENDIF()

SET(EXECUTABLE_OUTPUT_PATH  ${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_DIR})

 
# Enable solution folders; i.e. grouping of libraries & apps inside MS Visual solution (Use default  location for each library & application, defined in the corresponding CMakeLists.txt file.)
# SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)

MESSAGE("[INFO] target_env=${TARGET_DIR}")
MESSAGE("[INFO] target_dir=${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_DIR}")

# generic part ends here ---------------------------------------------

INCLUDE_DIRECTORIES(lib)
IF (EXTENSION_360_VIDEO)
 INCLUDE_DIRECTORIES(${TCH_360_LIB_PATH})
ENDIF()

ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(app)

IF (EXTENSION_360_VIDEO)
 ADD_SUBDIRECTORY (${TCH_360_LIB_PATH} 360lib)
ENDIF()




