# Set the system name to one of Android, Darwin, FreeBSD, Linux, or Windows
SET(CMAKE_SYSTEM_NAME @cmake_system_name@)
SET(CMAKE_SYSTEM_PROCESSOR @arch@)
SET(CMAKE_BUILD_TYPE @release_type@)

SET(STATIC ON)
SET(UNBOUND_STATIC ON)
SET(ARCH "default")

SET(BUILD_TESTS @build_tests@)
SET(TREZOR_DEBUG @build_tests@)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH @prefix@ /usr)

SET(ENV{PKG_CONFIG_PATH} @prefix@/lib/pkgconfig)

SET(Readline_ROOT_DIR @prefix@)
SET(Readline_INCLUDE_DIR @prefix@/include)
SET(Readline_LIBRARY @prefix@/lib/libreadline.a)
SET(Terminfo_LIBRARY @prefix@/lib/libtinfo.a)

SET(LIBUSB-1.0_LIBRARY @prefix@/lib/libusb-1.0.a)
SET(LIBUDEV_LIBRARY @prefix@/lib/libudev.a)

SET(Protobuf_FOUND 1)
SET(Protobuf_PROTOC_EXECUTABLE @prefix@/native/bin/protoc CACHE FILEPATH "Path to the native protoc")
SET(Protobuf_INCLUDE_DIR @prefix@/include CACHE PATH "Protobuf include dir")
SET(Protobuf_INCLUDE_DIRS @prefix@/include CACHE PATH "Protobuf include dir")
SET(Protobuf_LIBRARY @prefix@/lib/libprotobuf.a CACHE FILEPATH "Protobuf library")

SET(Boost_IGNORE_SYSTEM_PATH ON)
SET(BOOST_ROOT @prefix@)
SET(BOOST_INCLUDEDIR @prefix@/include)
SET(BOOST_LIBRARYDIR @prefix@/lib)
SET(Boost_IGNORE_SYSTEM_PATHS_DEFAULT OFF)
SET(Boost_NO_SYSTEM_PATHS ON)
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_STATIC_RUNTIME ON)

SET(OPENSSL_ROOT_DIR @prefix@)
SET(ARCHITECTURE @arch@)
SET(TARGET_OS @host_os@)

# for libraries and headers in the target directories
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # Find programs on host
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) # Find libs in target
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) # Find includes in target

function(split_program input_string program arguments)
  # CMake < 3.19 only understands setting CMAKE_<LANG>_COMPILER using "<program>" "<arguments>".
  # https://cmake.org/cmake/help/v3.19/variable/CMAKE_LANG_COMPILER.html#variable:CMAKE_%3CLANG%3E_COMPILER

  string(FIND "${input_string}" " " space_index)

  if(space_index EQUAL -1)
      set(${program} "${input_string}" PARENT_SCOPE)
      set(${arguments} "" PARENT_SCOPE)
  else()
      string(SUBSTRING "${input_string}" 0 ${space_index} _program)
      math(EXPR args_index "${space_index} + 1")
      string(SUBSTRING "${input_string}" ${args_index} -1 _arguments)

      set(${program} "${_program}" PARENT_SCOPE)
      set(${arguments} "${_arguments}" PARENT_SCOPE)
  endif()
endfunction()

# specify the cross compiler to be used. Darwin uses clang provided by the SDK.
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  if(ARCHITECTURE STREQUAL "aarch64")
    SET(CLANG_TARGET "arm64-apple-darwin")
    SET(CONF_TRIPLE "arm64-apple-darwin")
    SET(BUILD_TAG "mac-armv8")
    SET(CMAKE_OSX_ARCHITECTURES "arm64")
    set(ARM ON)
    set(ARM_ID "armv8-a")
  else()
    SET(CLANG_TARGET "x86_64-apple-darwin")
    SET(CONF_TRIPLE "x86_64-apple-darwin")
    SET(BUILD_TAG "mac-x64")
    SET(CMAKE_OSX_ARCHITECTURES "x86_64")
  endif()
  SET(_CMAKE_TOOLCHAIN_PREFIX @prefix@/native/bin/${CONF_TRIPLE}-)
  split_program("@CC@" CC_DARWIN CC_DARWIN_ARGS)
  SET(CMAKE_C_COMPILER "${CC_DARWIN}" "${CC_DARWIN_ARGS}")
  SET(CMAKE_C_COMPILER_TARGET ${CLANG_TARGET})
  split_program("@CXX@" CXX_DARWIN CXX_DARWIN_ARGS)
  SET(CMAKE_CXX_COMPILER "${CXX_DARWIN}" "${CXX_DARWIN_ARGS}")
  SET(CMAKE_C_FLAGS "@CFLAGS@")
  SET(CMAKE_CXX_COMPILER_TARGET ${CLANG_TARGET})
  SET(CMAKE_CXX_FLAGS "@CXXFLAGS@")
  SET(CMAKE_EXE_LINKER_FLAGS "@LDFLAGS@")
  SET(CMAKE_MODULE_LINKER_FLAGS "@LDFLAGS@")
  SET(CMAKE_SHARED_LINKER_FLAGS "@LDFLAGS@")
  SET(CMAKE_INSTALL_NAME_TOOL llvm-install-name-tool)
  SET(CMAKE_ASM_COMPILER clang)
  SET(CMAKE_ASM-ATT_COMPILER llvm-as)
  SET(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET})
  SET(CMAKE_ASM-ATT_COMPILER_TARGET ${CLANG_TARGET})
  SET(APPLE True)
  SET(BUILD_64 ON)
  SET(BREW OFF)
  SET(PORT OFF)
  SET(CMAKE_OSX_SYSROOT "@prefix@/native/SDK/")
  SET(CMAKE_OSX_DEPLOYMENT_TARGET "11.0")
  SET(CMAKE_CXX_STANDARD 17)
elseif(TARGET_OS STREQUAL "android")
  SET(ANDROID TRUE)

  split_program("@CC@" CC_ANDROID CC_ANDROID_ARGS)
  SET(CMAKE_C_COMPILER "${CC_ANDROID}" "${CC_ANDROID_ARGS}")

  split_program("@CXX@" CXX_ANDROID CXX_ANDROID_ARGS)
  SET(CMAKE_CXX_COMPILER "${CXX_ANDROID}" "${CXX_ANDROID_ARGS}")
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  split_program("@CC@" CC_FREEBSD CC_FREEBSD_ARGS)
  SET(CMAKE_C_COMPILER "${CC_FREEBSD}" "${CC_FREEBSD_ARGS}")

  split_program("@CXX@" CXX_FREEBSD CXX_FREEBSD_ARGS)
  SET(CMAKE_CXX_COMPILER "${CXX_FREEBSD}" "${CXX_FREEBSD_ARGS}")

  SET(CMAKE_ASM_COMPILER clang)
  SET(CMAKE_ASM-ATT_COMPILER as)
else()
  SET(CMAKE_C_COMPILER @CC@)
  SET(CMAKE_CXX_COMPILER @CXX@)
endif()

if(ARCHITECTURE STREQUAL "arm")
  set(ARCH "armv7-a")
  set(ARM ON)
  set(ARM_ID "armv7-a")
  set(BUILD_64 OFF)
  set(CMAKE_BUILD_TYPE release)
  if(ANDROID)
    set(BUILD_TAG "android-armv7")
  else()
    set(BUILD_TAG "linux-armv7")
  endif()
  set(ARM7)
elseif(ARCHITECTURE STREQUAL "aarch64")
  set(ARCH "armv8-a")
  set(ARM ON)
  set(ARM_ID "armv8-a")
  if(ANDROID)
    set(BUILD_TAG "android-armv8")
  elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(BUILD_TAG "linux-armv8")
  endif()
  set(BUILD_64 ON)
endif()

if(ARCHITECTURE STREQUAL "riscv64")
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(BUILD_TAG "linux-riscv64")
  endif()
  set(ARCH_ID "riscv64")
  set(ARCH "rv64gc")
endif()
if(ARCHITECTURE STREQUAL "loongarch64")
    set(ARCH_ID "loongarch64")
    set(ARCH "loongarch64")
endif()

if(ARCHITECTURE STREQUAL "i686")
  SET(ARCH_ID "i386")
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(BUILD_TAG "linux-x86")
    SET(LINUX_32 ON)
  elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(BUILD_TAG "win-x32")
  endif()
endif()

if(ARCHITECTURE STREQUAL "x86_64")
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(BUILD_TAG "linux-x64")
  elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    set(BUILD_TAG "freebsd-x64")
  elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(BUILD_TAG "win-x64")
  endif()
  SET(ARCH_ID "x86_64")
endif()

#Create a new global cmake flag that indicates building with depends
set (DEPENDS true)
