if(NOT _VCPKG_LINUX_TOOLCHAIN)
set(_VCPKG_LINUX_TOOLCHAIN 1)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
    set(CMAKE_CROSSCOMPILING OFF CACHE BOOL "")
endif()
set(CMAKE_SYSTEM_NAME Linux CACHE STRING "")

set(CMAKE_SYSTEM_PROCESSOR armv7l CACHE STRING "")

set(ATMELAVR_INSTALL_DIR "$ENV{HOME}/.platformio/packages/toolchain-atmelavr")
message(STATUS "Setting Atmel AVR toolchain location to ${ATMELAVR_INSTALL_DIR}")

set(ATMELAVR_BIN_DIR "${ATMELAVR_INSTALL_DIR}/bin")
message(STATUS "Setting Atmel AVR binary location to ${ATMELAVR_BIN_DIR}")

# set the C++ compiler.
find_program(AVR-GXX_EXECUTABLE NAMES "avr-g++" PATHS "${ATMELAVR_BIN_DIR}" REQUIRED)
if(NOT AVR-GXX_EXECUTABLE)
    message(SEND_ERROR "avr-g++ not found!")
else()
    message(STATUS "avr-g++ found at ${AVR-GXX_EXECUTABLE}")
endif()
set(CMAKE_CXX_COMPILER "${AVR-GXX_EXECUTABLE}" CACHE STRING "" FORCE)

# set the C compiler.
find_program(AVR-GCC_EXECUTABLE NAMES "avr-gcc" PATHS "${ATMELAVR_BIN_DIR}" REQUIRED)
if(NOT AVR-GCC_EXECUTABLE)
    message(SEND_ERROR "avr-gcc not found!")
else()
    message(STATUS "avr-gcc found at ${AVR-GCC_EXECUTABLE}")
endif()
set(CMAKE_CXX_COMPILER "${AVR-GCC_EXECUTABLE}" CACHE STRING "" FORCE)

# set the linker and AR.
set(CMAKE_LINKER "${ATMELAVR_BIN_DIR}/avr-ld" CACHE STRING "" FORCE)
set(CMAKE_AR "${ATMELAVR_BIN_DIR}/avr-ar" CACHE STRING "" FORCE)

message(STATUS "Cross compiling avr on host x86_64, use cross compiler: ${CMAKE_CXX_COMPILER}/${CMAKE_C_COMPILER}")

get_property( _CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE )
if(NOT _CMAKE_IN_TRY_COMPILE)
    string(APPEND CMAKE_C_FLAGS_INIT " -fPIC ${VCPKG_C_FLAGS} ")
    string(APPEND CMAKE_CXX_FLAGS_INIT " -fPIC ${VCPKG_CXX_FLAGS} ")
    string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " ${VCPKG_C_FLAGS_DEBUG} ")
    string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " ${VCPKG_CXX_FLAGS_DEBUG} ")
    string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " ${VCPKG_C_FLAGS_RELEASE} ")
    string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " ${VCPKG_CXX_FLAGS_RELEASE} ")

    string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ")
    string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ")
    if(VCPKG_CRT_LINKAGE STREQUAL "static")
        string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT "-static ")
        string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "-static ")
    endif()
    string(APPEND CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT " ${VCPKG_LINKER_FLAGS_DEBUG} ")
    string(APPEND CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT " ${VCPKG_LINKER_FLAGS_DEBUG} ")
    string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT " ${VCPKG_LINKER_FLAGS_RELEASE} ")
    string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT " ${VCPKG_LINKER_FLAGS_RELEASE} ")
endif()
endif()
