if(DEFINED ENV{DEVKITPRO})
	set(DEVKITPRO $ENV{DEVKITPRO})
else()
	message(FATAL_ERROR "Could not find DEVKITPRO in environment")
endif()

if(DEFINED ENV{DEVKITARM})
	set(DEVKITARM $ENV{DEVKITARM})
else()
	set(DEVKITARM ${DEVKITPRO}/devkitARM)
endif()

if(NOT DEFINED UNITY)
	set(UNITY ON)
endif()

set(toolchain_dir ${DEVKITARM}/psp2)
set(toolchain_bin_dir ${toolchain_dir}/bin)
set(cross_prefix ${DEVKITARM}/bin/arm-none-eabi-)
set(inc_flags -I${toolchain_dir}/include)
set(arch_flags "-march=armv7-a -mtune=cortex-a9 -specs=psp2.specs")
set(link_flags "-L${toolchain_dir}/lib")

set(CMAKE_SYSTEM_NAME Generic CACHE INTERNAL "system name")
set(CMAKE_SYSTEM_PROCESSOR arm CACHE INTERNAL "processor")
set(CMAKE_LIBRARY_ARCHITECTURE arm-none-eabi CACHE INTERNAL "abi")
set(CMAKE_AR ${cross_prefix}ar CACHE INTERNAL "archiver")
set(CMAKE_C_COMPILER ${cross_prefix}gcc CACHE INTERNAL "c compiler")
set(CMAKE_CXX_COMPILER ${cross_prefix}g++ CACHE INTERNAL "cxx compiler")
set(CMAKE_ASM_COMPILER ${cross_prefix}gcc CACHE INTERNAL "assembler")
if(NOT UNITY)
	set(common_flags "${arch_flags} -fno-reorder-functions -fno-optimize-sibling-calls ${inc_flags}")
else()
	set(common_flags "${arch_flags} -fno-gcse -fno-tree-vectorize ${inc_flags}")
endif()
set(CMAKE_C_FLAGS ${common_flags} CACHE INTERNAL "c compiler flags")
set(CMAKE_ASM_FLAGS ${common_flags} CACHE INTERNAL "c compiler flags")
set(CMAKE_CXX_FLAGS ${common_flags} CACHE INTERNAL "cxx compiler flags")
set(CMAKE_LINKER ${cross_prefix}ld CACHE INTERNAL "linker")

set(CMAKE_EXE_LINKER_FLAGS ${link_flags} CACHE INTERNAL "exe link flags")
set(CMAKE_MODULE_LINKER_FLAGS ${link_flags} CACHE INTERNAL "module link flags")
set(CMAKE_SHARED_LINKER_FLAGS ${link_flags} CACHE INTERNAL "shared link flags")

set(PKG_CONFIG_EXECUTABLE "/dev/null" CACHE INTERNAL "" FORCE)

set(FIXUP ${toolchain_bin_dir}/psp2-fixup)
set(RAW2C ${DEVKITARM}/bin/raw2c)

set(PSP2 ON)
add_definitions(-DPSP2)

set(CMAKE_C_COMPILER_WORKS 1) # Skip test
