set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_CROSSCOMPILING ON)

if (CMAKE_SYSROOT STREQUAL "")
	message(FATAL_ERROR "MinGW sysroot didn't set: please use -DCMAKE_SYSROOT=/path/to/mingw")
endif ()

if (CROSS_ARCH STREQUAL "")
	message(FATAL_ERROR "Please set target architecture: -DCROSS_ARCH=[i686 or x86_64]")
endif ()

set(CMAKE_SYSTEM_PROCESSOR ${CROSS_ARCH})

message(STATUS "Cross compile: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR}")

if (NOT SYSROOT_PREFIX)
	set(SYSROOT_PREFIX ${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32-)
endif ()

set(CMAKE_STAGING_PREFIX ${CMAKE_CURRENT_BINARY_DIR})


set(CMAKE_C_COMPILER ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}g++)
set(CMAKE_CXX_LINK_EXECUTABLE ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}ld)
set(CMAKE_C_LINK_EXECUTABLE ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}ld)
set(CMAKE_RANLIB ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}ranlib)
set($ENV{DLLTOOL} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}dllwrap)
set($ENV{DLLWRAP} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}dlltool)
set($ENV{WINDRES} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}windres)
set($ENV{WINDMC} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}windmc)
set($ENV{NM} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}nm)
set($ENV{ADDR2LINE} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}addr2line)
set($ENV{STRIP} ${CMAKE_SYSROOT}/bin/${SYSROOT_PREFIX}strip)


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

set(FLAG_ARCH "")
if (CROSS_ARCH STREQUAL "i686")
	set(FLAG_ARCH "-m32")
elseif (CROSS_ARCH STREQUAL "x86_64")
	set(FLAG_ARCH "-m64")
endif ()


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++ ${FLAG_ARCH}")
message(STATUS "Flags: ${CMAKE_CXX_FLAGS}")

if (JAVA_HOME)
	set(JAVA_AWT_LIBRARY ${JAVA_HOME}/lib/jawt.lib)
	set(JAVA_JVM_LIBRARY ${JAVA_HOME}/lib/jvm.lib)
	set(JAVA_INCLUDE_PATH ${JAVA_HOME}/include)
	set(JAVA_INCLUDE_PATH2 ${JAVA_HOME}/include/win32)
	set(JAVA_AWT_INCLUDE_PATH ${JAVA_HOME}/include)
endif ()