if ("${CMAKE_SYSTEM_NAME}" STREQUAL "" OR "${CMAKE_SYSTEM_NAME}" STREQUAL ${CMAKE_HOST_SYSTEM_NAME})
#     SET(CMAKE_SYSTEM_NAME ESP8266-RTOS)
    message(FATAL_ERROR "Unknown CMAKE_SYSTEM_NAME given: ${CMAKE_SYSTEM_NAME}")
endif()

if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "")
    message(FATAL_ERROR "Unknown CMAKE_SYSTEM_VERSION given: ${CMAKE_SYSTEM_VERSION}")
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

IF(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
    SET(USER_HOME $ENV{HOME})
    SET(HOST_EXECUTABLE_PREFIX "")
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
    SET(USER_HOME $ENV{USERPROFILE})
    SET(HOST_EXECUTABLE_SUFFIX ".exe")
else()
    message(FATAL_ERROR Unsupported build platform.)
endif()

IF(${CMAKE_VERSION} VERSION_LESS 2.8.0)
    INCLUDE(CMakeForceCompiler)
    CMAKE_FORCE_C_COMPILER(${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-gcc${TOOL_EXECUTABLE_SUFFIX} GNU)
    CMAKE_FORCE_ASM_COMPILER(${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-gcc${TOOL_EXECUTABLE_SUFFIX} GNU)
    CMAKE_FORCE_CXX_COMPILER(${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-g++${TOOL_EXECUTABLE_SUFFIX} GNU)
ELSE()
#     SET(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
    SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-gcc${TOOL_EXECUTABLE_SUFFIX})
    SET(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-gcc${TOOL_EXECUTABLE_SUFFIX})
    SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-g++${TOOL_EXECUTABLE_SUFFIX})
ENDIF()

SET(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-objcopy${TOOL_EXECUTABLE_SUFFIX} CACHE INTERNAL "objcopy tool")
SET(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-objdump${TOOL_EXECUTABLE_SUFFIX} CACHE INTERNAL "objdump tool")
SET(CMAKE_SIZE ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-size${TOOL_EXECUTABLE_SUFFIX} CACHE INTERNAL "size tool")
SET(CMAKE_DEBUGER ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-gdb${TOOL_EXECUTABLE_SUFFIX} CACHE INTERNAL "debuger")
SET(CMAKE_CPPFILT ${TOOLCHAIN_PREFIX}/bin/xtensa-lx106-elf-c++filt${TOOL_EXECUTABLE_SUFFIX} CACHE INTERNAL "C++filt")

SET(CCFLAGS  "-I${TOOLCHAIN_PREFIX}/xtensa-lx106-elf/include -g -Wpointer-arith -Wundef -Werror -Wl,-EL -DICACHE_FLASH -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -ffunction-sections -fdata-sections")
SET(LDFLAGS "-nostdlib -Wl,--no-check-sections -Wl,-static -Wl,--gc-sections -u call_user_start")
SET(CMAKE_C_FLAGS "${CCFLAGS}" CACHE INTERNAL "c compiler flags release")
SET(CMAKE_CXX_FLAGS "${CCFLAGS}" CACHE INTERNAL "cxx compiler flags release")
SET(CMAKE_ASM_FLAGS "" CACHE INTERNAL "asm compiler flags release")
SET(CMAKE_EXE_LINKER_FLAGS "${LDFLAGS}" CACHE INTERNAL "linker flags release")

SET(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb" CACHE INTERNAL "c compiler flags debug")
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb" CACHE INTERNAL "cxx compiler flags debug")
SET(CMAKE_ASM_FLAGS_DEBUG "-g" CACHE INTERNAL "asm compiler flags debug")
SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "" CACHE INTERNAL "linker flags debug")

SET(CMAKE_C_FLAGS_RELEASE "-Os" CACHE INTERNAL "c compiler flags release")
SET(CMAKE_CXX_FLAGS_RELEASE "-Os" CACHE INTERNAL "cxx compiler flags release")
SET(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm compiler flags release")
SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "" CACHE INTERNAL "linker flags release")

SET(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> -Wl,--start-group <OBJECTS> <LINK_LIBRARIES> -Wl,--end-group" CACHE STRING "C linker invocation")
SET(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> -Wl,--start-group <OBJECTS> <LINK_LIBRARIES> -Wl,--end-group" CACHE STRING "CXX linker invocation")

