set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_CROSSCOMPILING ON)

set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CROSS_TARGET x86_64-none-elf)
unset(CMAKE_OSX_SYSROOT)
unset(CMAKE_OSX_ARCHITECTURES)

set(CMAKE_ASM_COMPILER_TARGET ${CROSS_TARGET})
set(CMAKE_C_COMPILER_TARGET ${CROSS_TARGET})
set(CMAKE_CXX_COMPILER_TARGET ${CROSS_TARGET})

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS ON)

enable_language(CXX)

unset(CMAKE_AR CACHE)
unset(CMAKE_AR)
set(CMAKE_AR ${CMAKE_CXX_COMPILER_AR})

unset(CMAKE_RANLIB CACHE)
unset(CMAKE_RANLIB)
set(CMAKE_RANLIB ${CMAKE_CXX_COMPILER_RANLIB})

get_filename_component(CROSS_BINUTILS_PATH ${CMAKE_CXX_COMPILER_AR} DIRECTORY)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  find_program(
    CMAKE_OBJCOPY llvm-objcopy
    NAMES llvm-objcopy x86_64-elf-objcopy objcopy
    HINTS ${CROSS_BINUTILS_PATH}
  )
  unset(CMAKE_LINKER CACHE)
  unset(CMAKE_LINKER)
  find_program(
    CMAKE_LINKER ld.lld
    HINTS ${CROSS_BINUTILS_PATH}
  )
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  find_program(
    CMAKE_OBJCOPY x86_64-elf-objcopy
    NAMES x86_64-elf-objcopy objcopy
    HINTS ${CROSS_BINUTILS_PATH}
  )
endif()

set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> -o <TARGET> <LINK_FLAGS> <OBJECTS> <LINK_LIBRARIES>")
set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_LINKER> --shared -o <TARGET> <LINK_FLAGS> <OBJECTS> <LINK_LIBRARIES>")

find_program(TAR tar)
find_program(MKISO xorriso)
if(MKISO)
	set(MKISO ${MKISO} -as mkisofs)
else()
  find_program(MKISO mkisofs)
endif()

find_program(MKFAT mformat)
find_program(MCOPY mcopy)
find_program(MMD mmd)
find_program(QEMU qemu-system-x86_64)
