# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Need support for CMAKE_C_COMPILER_TARGET
cmake_minimum_required(VERSION 3.0)

set(CMAKE_SYSTEM_NAME Fuchsia)

set(CMAKE_SYSROOT ${FUCHSIA_SYSROOT})

if(NOT DEFINED FUCHSIA_TOOLCHAIN)
  string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} HOST_SYSTEM_PROCESSOR)
  if(HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
    set(HOST_SYSTEM_PROCESSOR "x64")
  elseif(HOST_SYSTEM_PROCESSOR STREQUAL "aarch64")
    set(HOST_SYSTEM_PROCESSOR "arm64")
  endif()
  string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} HOST_SYSTEM_NAME)
  if(HOST_SYSTEM_NAME STREQUAL "darwin")
    set(HOST_SYSTEM_NAME "mac")
  endif()
  set(FUCHSIA_TOOLCHAIN "${CMAKE_CURRENT_LIST_DIR}/../prebuilt/third_party/clang/${HOST_SYSTEM_NAME}-${HOST_SYSTEM_PROCESSOR}")
endif()

if(NOT DEFINED FUCHSIA_COMPILER_TARGET)
  set(FUCHSIA_COMPILER_TARGET "${FUCHSIA_SYSTEM_PROCESSOR}-fuchsia")
endif()

set(CMAKE_C_COMPILER "${FUCHSIA_TOOLCHAIN}/bin/clang")
set(CMAKE_C_COMPILER_TARGET ${FUCHSIA_COMPILER_TARGET} CACHE STRING "")
set(CMAKE_CXX_COMPILER "${FUCHSIA_TOOLCHAIN}/bin/clang++")
set(CMAKE_CXX_COMPILER_TARGET ${FUCHSIA_COMPILER_TARGET} CACHE STRING "")
set(CMAKE_ASM_COMPILER "${FUCHSIA_TOOLCHAIN}/bin/clang")
set(CMAKE_ASM_COMPILER_TARGET ${FUCHSIA_COMPILER_TARGET} CACHE STRING "")

set(CMAKE_LINKER "${FUCHSIA_TOOLCHAIN}/bin/ld.lld" CACHE PATH "")
set(CMAKE_AR "${FUCHSIA_TOOLCHAIN}/bin/llvm-ar" CACHE PATH "")
set(CMAKE_RANLIB "${FUCHSIA_TOOLCHAIN}/bin/llvm-ranlib" CACHE PATH "")
set(CMAKE_NM "${FUCHSIA_TOOLCHAIN}/bin/llvm-nm" CACHE PATH "")
set(CMAKE_OBJCOPY "${FUCHSIA_TOOLCHAIN}/bin/llvm-objcopy" CACHE PATH "")
set(CMAKE_OBJDUMP "${FUCHSIA_TOOLCHAIN}/bin/llvm-objdump" CACHE PATH "")
set(CMAKE_STRIP "${FUCHSIA_TOOLCHAIN}/bin/llvm-strip" CACHE PATH "")

set(CMAKE_FIND_ROOT_PATH ${FUCHSIA_SYSROOT})

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