if("$ENV{system}" STREQUAL "")
	message(FATAL_ERROR "toolchain.cmake requires the system environment variable")
endif()
if("$ENV{processor}" STREQUAL "")
	message(FATAL_ERROR "toolchain.cmake requires the processor environment variable")
endif()
if("$ENV{cc}" STREQUAL "")
	message(FATAL_ERROR "toolchain.cmake requires the cc environment variable")
endif()
if("$ENV{cxx}" STREQUAL "")
	message(FATAL_ERROR "toolchain.cmake requires the cxx environment variable")
endif()

set(CMAKE_SYSTEM_NAME "$ENV{system}")
set(CMAKE_SYSTEM_PROCESSOR "$ENV{processor}")
set(CMAKE_C_COMPILER "$ENV{cc}")
set(CMAKE_CXX_COMPILER "$ENV{cxx}")
set(CMAKE_ASM_COMPILER "$ENV{cc}")

if(NOT "$ENV{rc}" STREQUAL "")
	set(CMAKE_RC_COMPILER "$ENV{rc}")
endif()

if(NOT "$ENV{find_root}" STREQUAL "")
	set(CMAKE_FIND_ROOT_PATH "$ENV{find_root}")
endif()

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

if("$ENV{system}" STREQUAL "Linux")
	if("$ENV{sysroot}" STREQUAL "")
		message(FATAL_ERROR "toolchain.cmake requires the sysroot environment variable for Linux")
	endif()
	if("$ENV{qemu}" STREQUAL "")
		message(FATAL_ERROR "toolchain.cmake requires the qemu environment variable for Linux")
	endif()
	set(CMAKE_SYSROOT "$ENV{sysroot}/usr")
	set(CMAKE_CROSSCOMPILING_EMULATOR "$ENV{qemu}" "-L" "$ENV{sysroot}")
endif()
