set(DEPENDS_PATH "@prefix@/@deps_dir@")
set(NATIVEPREFIX "@prefix@/@tool_dir@")

set(TARBALL_DIR "@use_tarballs@")

set(OS "@platform_os@")
set(CMAKE_SYSTEM_PROCESSOR @host_cpu@)
set(CPU "@use_cpu@")
set(PLATFORM "@target_platform@")

# set CORE_SYSTEM_NAME and CMAKE_SYSTEM_NAME (sets CMAKE_CROSSCOMPILING)
if(OS STREQUAL linux)
  set(CMAKE_SYSTEM_NAME Linux)
  set(CORE_SYSTEM_NAME linux)
  set(CORE_PLATFORM_NAME @target_platform@)
  set(APP_RENDER_SYSTEM @app_rendersystem@ CACHE STRING "Render system to use: \"gl\" or \"gles\"")
  if(PLATFORM STREQUAL webos)
    set(TOOLCHAIN @use_toolchain@)
    set(HOST @use_host@)
  endif()
elseif(OS STREQUAL android)
  set(CMAKE_SYSTEM_NAME Android)
  set(CORE_SYSTEM_NAME android)
elseif(OS STREQUAL osx)
  set(CMAKE_SYSTEM_NAME Darwin)
  set(CORE_SYSTEM_NAME osx)
elseif(OS STREQUAL darwin_embedded)
  set(CMAKE_SYSTEM_NAME Darwin)
  set(CORE_SYSTEM_NAME darwin_embedded)
  if(PLATFORM STREQUAL appletvos)
    set(CORE_PLATFORM_NAME tvos)
  else()
    set(CORE_PLATFORM_NAME ios)
  endif()
endif()

if(CORE_SYSTEM_NAME STREQUAL darwin_embedded)
  # Necessary to build the main Application (but not other dependencies)
  # with Xcode (and a bundle with Makefiles) (https://cmake.org/Bug/view.php?id=15329)
  if(NOT PROJECT_SOURCE_DIR MATCHES "tools/depends")
    message(STATUS "Toolchain enabled ${CORE_PLATFORM_NAME} bundle for project ${PROJECT_NAME}")
    set(CMAKE_MACOSX_BUNDLE YES)
    set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
    # Need to set this attribute to "" in order to
    # completely disable code signing
    # see: https://gitlab.kitware.com/cmake/cmake/issues/19112
    set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "")
    set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
    if(CORE_PLATFORM_NAME STREQUAL tvos)
      set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "3")
      set(CMAKE_XCODE_ATTRIBUTE_TVOS_DEPLOYMENT_TARGET 12.0)
    else()
      set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
      set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET 11.0)
    endif()
  endif()
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  set(CMAKE_OSX_SYSROOT @use_sdk_path@)
  set(CMAKE_XCODE_ATTRIBUTE_ARCHS ${CPU})
endif()
set(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
set(CMAKE_C_COMPILER @CC@)
set(CMAKE_CXX_COMPILER @CXX@)
set(CMAKE_AR @AR@ CACHE FILEPATH "Archiver")
SET(CMAKE_AS @AS@ CACHE FILEPATH "Assembler")
set(CMAKE_LINKER @LD@ CACHE FILEPATH "Linker")
set(CMAKE_NM @NM@ CACHE FILEPATH "Nm")
set(CMAKE_STRIP @STRIP@ CACHE PATH "strip binary" FORCE)
set(CMAKE_OBJDUMP @OBJDUMP@ CACHE FILEPATH "Objdump")
set(CMAKE_RANLIB @RANLIB@ CACHE FILEPATH "Ranlib")

if(NOT "@use_ccache@" STREQUAL "")
  set(CMAKE_CXX_COMPILER_LAUNCHER @CCACHE@)
  set(CMAKE_C_COMPILER_LAUNCHER @CCACHE@)
endif()

# where is the target environment
set(CMAKE_FIND_ROOT_PATH @prefix@/@deps_dir@)
set(CMAKE_LIBRARY_PATH @prefix@/@deps_dir@/lib)
if(NOT "@use_sdk_path@" STREQUAL "")
  list(APPEND CMAKE_FIND_ROOT_PATH @use_sdk_path@ @use_sdk_path@/usr)
endif()
# Currently this is only set to reject android by default
if(NOT "@use_toolchain@" STREQUAL "")
  list(APPEND CMAKE_FIND_ROOT_PATH @use_toolchain@/@use_host@ @use_toolchain@/@use_host@/sysroot @use_toolchain@/@use_host@/sysroot/usr @use_toolchain@/@use_host@/libc @use_toolchain@/lib/@use_host@/sysroot @use_toolchain@/usr @use_toolchain@/sysroot/usr)
  # Explicitly set this as last. This potentially is /usr which can then cause linux
  # cross compilation to search paths that are not relevant to target arch (eg host libs)
  # x86/x86_64 jenkins CI jobs require /usr for libs like iconv
  list(APPEND CMAKE_FIND_ROOT_PATH @use_toolchain@)
  set(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH}:@use_toolchain@/usr/lib/@use_host@:@use_toolchain@/lib/@use_host@")
endif()

# add Android directories and tools
if(CORE_SYSTEM_NAME STREQUAL android)
  set(NDKROOT @use_ndk_path@)
  set(SDKROOT @use_sdk_path@)
  set(TOOLCHAIN @use_toolchain@)
  set(HOST @use_host@)
  list(APPEND CMAKE_LIBRARY_PATH ${TOOLCHAIN}/sysroot/usr/lib/${HOST}/@use_ndk_api@)
  string(REPLACE ":" ";" SDK_BUILDTOOLS_PATH "@build_tools_path@")
endif()

set(CMAKE_ASM_NASM_FLAGS "@platform_asflags@")
set(CMAKE_C_FLAGS "@platform_cflags@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_CXX_FLAGS "@platform_cxxflags@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_C_FLAGS_RELEASE "@platform_cflags_release@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_CXX_FLAGS_RELEASE "@platform_cxxflags_release@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_C_FLAGS_DEBUG "@platform_cflags_debug@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_CXX_FLAGS_DEBUG "@platform_cxxflags_debug@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_CPP_FLAGS "@platform_cflags@ @platform_includes@ -isystem @prefix@/@deps_dir@/include")
set(CMAKE_EXE_LINKER_FLAGS "-L@prefix@/@deps_dir@/lib @platform_ldflags@")
set(ENV{CFLAGS} ${CMAKE_C_FLAGS})
set(ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS})
set(ENV{CPPFLAGS} ${CMAKE_CPP_FLAGS})
set(ENV{LDFLAGS} ${CMAKE_EXE_LINKER_FLAGS})
# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_FRAMEWORK LAST)
set(ENV{PKG_CONFIG_LIBDIR} @prefix@/@deps_dir@/lib/pkgconfig:@prefix@/@deps_dir@/share/pkgconfig)

# Binary Addons
if(NOT CORE_SYSTEM_NAME STREQUAL linux)
  set(ADDONS_PREFER_STATIC_LIBS ON)
endif()

# common usage in autoconf to refer to host arch tool use
set(CC_FOR_BUILD "@CC_FOR_BUILD@")
set(CXX_FOR_BUILD "@CXX_FOR_BUILD@")
set(LD_FOR_BUILD "@LD_FOR_BUILD@")
set(CC_BINARY_FOR_BUILD "@CC_FOR_BUILD@")
set(CXX_BINARY_FOR_BUILD "@CXX_FOR_BUILD@")
set(AR_FOR_BUILD "@AR_FOR_BUILD@")
set(RANLIB_FOR_BUILD "@RANLIB_FOR_BUILD@")
set(AS_FOR_BUILD "@AS_FOR_BUILD@")
set(NM_FOR_BUILD "@NM_FOR_BUILD@")
set(STRIP_FOR_BUILD "@STRIP_FOR_BUILD@")
set(READELF_FOR_BUILD "@READELF_FOR_BUILD@")
set(OBJDUMP_FOR_BUILD "@OBJDUMP_FOR_BUILD@")

# flags for host arch building
set(CFLAGS_FOR_BUILD "@host_includes@ -I@prefix@/@tool_dir@/include")
set(LDFLAGS_FOR_BUILD "@host_includes@ -L@prefix@/@tool_dir@/lib")

# other build tools
find_program(NASM nasm HINTS "${NATIVEPREFIX}/bin" REQUIRED)

# common autoconf build tools
find_program(AUTOCONF autoconf HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(ACLOCAL aclocal HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(AUTOHEADER autoheader HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(AUTOMAKE automake HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(AUTOM4TE autom4te HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(AUTOPOINT autopoint HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(AUTORECONF autoreconf HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(LIBTOOL libtool HINTS "${NATIVEPREFIX}/bin" REQUIRED)
find_program(LIBTOOLIZE libtoolize HINTS "${NATIVEPREFIX}/bin" REQUIRED)

set(ENV{ACLOCAL_PATH} "${DEPENDS_PATH}/share/aclocal:${NATIVEPREFIX}/share/aclocal")
set(ENVPATH "${NATIVEPREFIX}/bin:$ENV{PATH}")

# Dependency build tool config files
find_file(MESON-CROSS "cross-file.meson" PATHS "${DEPENDS_PATH}/share" NO_CMAKE_FIND_ROOT_PATH REQUIRED)
# autoconf config.site
find_file(CONFIG_SITE "config.site" PATHS "${DEPENDS_PATH}/share" NO_CMAKE_FIND_ROOT_PATH REQUIRED)

# Env variables for non cmake target environments
set(PROJECT_TARGETENV "AS=${CMAKE_AS}"
                      "AR=${CMAKE_AR}"
                      "CC=${CMAKE_C_COMPILER}"
                      "CXX=${CMAKE_CXX_COMPILER}"
                      "NM=${CMAKE_NM}"
                      "LD=${CMAKE_LINKER}"
                      "STRIP=${CMAKE_STRIP}"
                      "RANLIB=${CMAKE_RANLIB}"
                      "OBJDUMP=${CMAKE_OBJDUMP}"
                      "CFLAGS=${CMAKE_C_FLAGS}"
                      "CPPFLAGS=${CMAKE_CPP_FLAGS}"
                      "LDFLAGS=${CMAKE_EXE_LINKER_FLAGS}"
                      "PKG_CONFIG_LIBDIR=$ENV{PKG_CONFIG_LIBDIR}"
                      "AUTOM4TE=${AUTOM4TE}"
                      "AUTOMAKE=${AUTOMAKE}"
                      "AUTOCONF=${AUTOCONF}"
                      "AUTORECONF=${AUTORECONF}"
                      "ACLOCAL=${ACLOCAL}"
                      "ACLOCAL_PATH=$ENV{ACLOCAL_PATH}"
                      "AUTOPOINT=${AUTOPOINT}"
                      "AUTOHEADER=${AUTOHEADER}"
                      "LIBTOOL=${LIBTOOL}"
                      "LIBTOOLIZE=${LIBTOOLIZE}"
                      "CONFIG_SITE=${CONFIG_SITE}"
                      )

# Env variables for non cmake host environments
set(PROJECT_BUILDENV CC_FOR_BUILD=${CC_FOR_BUILD}
                     CXX_FOR_BUILD=${CXX_FOR_BUILD}
                     LD_FOR_BUILD=${LD_FOR_BUILD}
                     CC_BINARY_FOR_BUILD=${CC_FOR_BUILD}
                     CXX_BINARY_FOR_BUILD=${CXX_FOR_BUILD}
                     AR_FOR_BUILD=${AR_FOR_BUILD}
                     RANLIB_FOR_BUILD=${RANLIB_FOR_BUILD}
                     AS_FOR_BUILD=${AS_FOR_BUILD}
                     NM_FOR_BUILD=${NM_FOR_BUILD}
                     STRIP_FOR_BUILD=${STRIP_FOR_BUILD}
                     READELF_FOR_BUILD=${READELF_FOR_BUILD}
                     OBJDUMP_FOR_BUILD=${OBJDUMP_FOR_BUILD}
                     CFLAGS_FOR_BUILD=${CFLAGS_FOR_BUILD}
                     LDFLAGS_FOR_BUILD=${LDFLAGS_FOR_BUILD}
                     )

# variable to easily set host/target env for non cmake internal dep builds
set(DEP_BUILDENV ${CMAKE_COMMAND} -E env ${PROJECT_TARGETENV} ${PROJECT_BUILDENV})

set(KODI_DEPENDSBUILD 1)

