#-------------------------------------------------------------------------------
# Use relative paths
# This is mostly to reduce path size for command-line limits on windows
#-------------------------------------------------------------------------------
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 false)
    SET(ROKLEGEND_SHAREDEXT ".dll")
ELSE ()
    SET(ROKLEGEND_SHAREDEXT ".so")
ENDIF ()

IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
    SET(ROKLEGEND_ARCH ${ROKLEGEND_ARCH_64})
    SET(RO_IS_32BIT FALSE)
    SET(RO_IS_64BIT TRUE)
ELSE ()
    SET(ROKLEGEND_ARCH ${ROKLEGEND_ARCH_32})
    SET(RO_IS_32BIT TRUE)
    SET(RO_IS_64BIT FALSE)
ENDIF ()

IF (WIN32)
    SET(ROKLEGEND_LIB_DIR_DBG ${ROKLEGEND_LIB_DIR}/Debug)
    SET(ROKLEGEND_LIB_DIR_REL ${ROKLEGEND_LIB_DIR}/Release)
ELSE ()
    SET(ROKLEGEND_LIB_DIR_DBG ${ROKLEGEND_LIB_DIR})
    SET(ROKLEGEND_LIB_DIR_REL ${ROKLEGEND_LIB_DIR})
ENDIF ()

MACRO(RO_ADD_CXX_FLAG CXX_FLAG_VAR CXX_FLAG)
	STRING(FIND "${${CXX_FLAG_VAR}}" "${CXX_FLAG}" FLAG_POS)
	IF (${FLAG_POS} EQUAL -1)
		SET(${CXX_FLAG_VAR} "${${CXX_FLAG_VAR}} ${CXX_FLAG}")
	ENDIF ()
ENDMACRO()

IF (MSVC)
    # Create debug libraries with _d postfix
    SET(CMAKE_DEBUG_POSTFIX "_d")
    ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
    ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
    ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
    IF (CMAKE_CL_64)
        # Visual Studio bails out on debug builds in 64bit mode unless
        # this flag is set...
        # Enable bigobj
        SET(RO_BIGOBJ_OPTION "/bigobj")
		# Floating point option
		SET(RO_FP_OPTION "/fp:precise")
		# Additional option
		SET(RO_ADDTIONAL_OPTION "/vmg")
		# Precompiled header option
		SET(RO_PCH_OPTION "/Zm1000")
		# Enhanced Instruction Set Option
		SET(RO_EIS_OPTION "/arch:AVX2")
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_RELEASE "${RO_BIGOBJ_OPTION}")
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_RELEASE "${RO_FP_OPTION}")
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_RELEASE "${RO_ADDITIONAL_OPTION}")
		#RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_RELEASE "${RO_EIS_OPTION}")
		##SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RO_BIGOBJ_OPTION} ${RO_FP_OPTION} ${RO_ADDITIONAL_OPTION} ${RO_EIS_OPTION}" CACHE STRING "" FORCE)
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_DEBUG "${RO_BIGOBJ_OPTION}")
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_DEBUG "${RO_FP_OPTION}")
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_DEBUG "${RO_ADDITIONAL_OPTION}")
		#RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_DEBUG "${RO_EIS_OPTION}")
		# Enable intrinsics on MSVC in debug mode
		RO_ADD_CXX_FLAG(CMAKE_CXX_FLAGS_DEBUG "/Oi")
		##SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${RO_BIGOBJ_OPTION} ${RO_FP_OPTION} ${RO_ADDITIONAL_OPTION} ${RO_EIS_OPTION} /Oi" CACHE STRING "" FORCE)
    ENDIF()

    IF ("${MSVC_VERSION}" STREQUAL "1200")
        SET(ROKLEGEND_MSVC "Visual Studio 6.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1300")
        SET(ROKLEGEND_MSVC "Visual Studio 7.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1310")
        SET(ROKLEGEND_MSVC "Visual Studio 7.1")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1400")
        SET(ROKLEGEND_MSVC "Visual Studio 8.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1500")
        SET(ROKLEGEND_MSVC "Visual Studio 9.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1600")
        SET(ROKLEGEND_MSVC "Visual Studio 10.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1700")
        SET(ROKLEGEND_MSVC "Visual Studio 11.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1800")
        SET(ROKLEGEND_MSVC "Visual Studio 12.0")
    ELSEIF ("${MSVC_VERSION}" STREQUAL "1900")
        SET(ROKLEGEND_MSVC "Visual Studio 14.0")
    ELSE ()
        SET(ROKLEGEND_MSVC "Unknown MSVC version ${MSVC_VERSION}")
    ENDIF()
ENDIF ()