# Cross-compilation loader for Windows
#
# This file is passed to cmake on the command line via
# -DCMAKE_TOOLCHAIN_FILE.

# replace deprecated CRT functions with security-enhanced functions by defining following preprocessor macros
# WIN_CXX_SECURE_OVERLOAD_STANDARD_OPTIONS is for strcpy, strcmp, and so on.
# WIN_CXX_SECURE_OVERLOAD_MEMORY_OPTIONS is for memcpy, memmove, and so on.
set(WIN_CXX_SECURE_OVERLOAD_STANDARD_OPTIONS "/D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 /D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1 /D_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=1")
set(WIN_CXX_SECURE_OVERLOAD_MEMORY_OPTIONS "/D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_MEMORY=1 /D_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES_MEMORY=1")
set(WIN_CXX_SECURE_OVERLOAD_OPTIONS "${WIN_CXX_SECURE_OVERLOAD_STANDARD_OPTIONS} ${WIN_CXX_SECURE_OVERLOAD_MEMORY_OPTIONS}")

# Debug Information format
# /Zi produces a program database(PDB) that contains type information and symbolic debugging information.
# for more details, see https://msdn.microsoft.com/en-US/library/958x11bc.aspx
set(WIN_DEBUG_INFORMATION_FORMAT "/Zi")

# specify the run-time library
# for more details, see https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
set(WIN_USE_RUNTIME_LIBRARY_RELEASE "/MD")
set(WIN_USE_RUNTIME_LIBRARY_DEBUG "/MDd")

# specify the kind of exception handling used by the compiler.
# for more details, see https://msdn.microsoft.com/en-us/library/1deeycx5.aspx
set(WIN_EXCEPTION_HANDLING_MODEL "/EHsc")

# To link POCO statically, the code using POCO must be compiled with 
# preprocessor symbol POCO_STATIC
if(NOT FORCE_SHAREDLIB)
    set(WIN_PREPROCESSOR_POCO_STATIC "/DPOCO_STATIC")
else()
    set(WIN_PREPROCESSOR_EASYHTTPCPP_DLL "/DEASYHTTPCPP_DLL")
    if(ENABLE_TESTS)
        set(WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS "/DEASYHTTPCPP_DLL_TESTS")
    endif()
endif()

set(CMAKE_C_FLAGS_DEBUG "/Od ${WIN_DEBUG_INFORMATION_FORMAT} ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_DEBUG} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "C compile flags debug" FORCE)
set(CMAKE_C_FLAGS_RELEASE "/O2 /DNDEBUG ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "C compile flags release" FORCE)
set(CMAKE_C_FLAGS_MINSIZEREL "/Os /DNDEBUG ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "C compile flags minsizerel" FORCE)
set(CMAKE_C_FLAGS_RELWITHDEBINFO "/O2 /DNDEBUG ${WIN_DEBUG_INFORMATION_FORMAT} ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "C compile flags relwithdebinfo" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "/Od ${WIN_DEBUG_INFORMATION_FORMAT} ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_DEBUG} ${WIN_CXX_SECURE_OVERLOAD_OPTIONS} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "CXX compile flags debug" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "/O2 /DNDEBUG ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_CXX_SECURE_OVERLOAD_OPTIONS} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "CXX compile flags release" FORCE)
set(CMAKE_CXX_FLAGS_MINSIZEREL "/Os /DNDEBUG ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_CXX_SECURE_OVERLOAD_OPTIONS} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "CXX compile flags minsizerel" FORCE)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/O2 /DNDEBUG ${WIN_DEBUG_INFORMATION_FORMAT} ${WIN_EXCEPTION_HANDLING_MODEL} ${WIN_USE_RUNTIME_LIBRARY_RELEASE} ${WIN_CXX_SECURE_OVERLOAD_OPTIONS} ${WIN_PREPROCESSOR_POCO_STATIC} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL} ${WIN_PREPROCESSOR_EASYHTTPCPP_DLL_TESTS}" CACHE STRING "CXX compile flags relwithdebinfo" FORCE)

# reset compile flags
unset(CMAKE_C_FLAGS CACHE)
unset(CMAKE_CXX_FLAGS CACHE)

# FIXME : If treat C4365 as warning level4, compile for google test fails.
set(WIN_COMPILER_WARNING_C4365 "/w44365")
set(CMAKE_C_FLAGS "/W4" CACHE STRING "C compile flags" FORCE)
set(CMAKE_CXX_FLAGS "/W4" CACHE STRING "CXX compile flags" FORCE)

# Add a d postfix to the debug libraries
if(NOT FORCE_SHAREDLIB)
    set(STATIC_POSTFIX "md" CACHE STRING "Set static library postfix" FORCE)
else()
    set(STATIC_POSTFIX "" CACHE STRING "Set static library postfix" FORCE)
endif()

set(CMAKE_DEBUG_POSTFIX "${STATIC_POSTFIX}d" CACHE STRING "Set Debug library postfix" FORCE)
set(CMAKE_RELEASE_POSTFIX "${STATIC_POSTFIX}" CACHE STRING "Set Release library postfix" FORCE)
set(CMAKE_MINSIZEREL_POSTFIX "${STATIC_POSTFIX}" CACHE STRING "Set MinSizeRel library postfix" FORCE)
set(CMAKE_RELWITHDEBINFO_POSTFIX "${STATIC_POSTFIX}" CACHE STRING "Set RelWithDebInfo library postfix" FORCE)
