# Copyright (C) 2023 Advanced Micro Devices, Inc.  All rights reserved.
cmake_minimum_required (VERSION 3.0.2)
if (POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()

if (POLICY CMP0077)
  cmake_policy(SET CMP0077 NEW)
endif()

if (POLICY CMP0057)
  cmake_policy(SET CMP0057 NEW)
endif()

if (NOT ("libmetal" IN_LIST BSP_LIBSRC_SUBDIRS))
  message(FATAL_ERROR "This library requires libmetal library in the Board Support Package.")
endif()

option(OPENAMP_WITH_PROXY "Enable proxy services in OpenAMP library" OFF)

set (OPENAMP_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set (OPENAMP_BIN_ROOT "${CMAKE_CURRENT_BINARY_DIR}")

list (APPEND CMAKE_MODULE_PATH
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/platforms")

if (a53)
  set(MACHINE "zynqmp_a53" CACHE STRING "" FORCE)
endif()

string(REGEX MATCH "a72" a72 "${CMAKE_SYSTEM_PROCESSOR}")
if (a72)
  set(MACHINE "zynqmp_a72" CACHE STRING "" FORCE)
endif()

string(REGEX MATCH "a78" a78 "${CMAKE_SYSTEM_PROCESSOR}")
if (a78)
  set(MACHINE "zynqmp_a78" CACHE STRING "" FORCE)
endif()

if (a53 OR a72 OR a78)
  set (CROSS_PREFIX "aarch64-none-elf-" CACHE STRING "" FORCE)
  set (CMAKE_SYSTEM_PROCESSOR "aarch64" CACHE STRING "" FORCE)
endif()

string(REGEX MATCH "r5" r5 "${CMAKE_SYSTEM_PROCESSOR}")
if (r5)
  set(MACHINE "zynqmp_r5" CACHE STRING "" FORCE)
  set (CROSS_PREFIX "armr5-none-eabi-" CACHE STRING "" FORCE)
  set (CMAKE_SYSTEM_PROCESSOR "arm" CACHE STRING "" FORCE)
endif()

string (TOUPPER ${OPENAMP_WITH_PROXY}      PROXY_OPTION_UPPER)
if (${PROXY_OPTION_UPPER} STREQUAL "1" OR ${PROXY_OPTION_UPPER} STREQUAL "TRUE" OR ${PROXY_OPTION_UPPER} STREQUAL "ON")
  message("OPENAMP: define -DUNDEFINE_FILE_OPS=1 TO 1")
  add_definitions(-DUNDEFINE_FILE_OPS=1)
  set (WITH_PROXY=ON)
endif()

set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/build-openamp" CACHE STRING "" FORCE)

include (syscheck)
project (open_amp C)

set (WITH_DOC OFF)

include (CheckIncludeFiles)
include (CheckCSourceCompiles)
include (collect)
include (options)
set_property (GLOBAL PROPERTY "PROJECT_EC_FLAGS" -Wall  -Wextra)
option (WITH_LIBMETAL_FIND "Check Libmetal library can be found" OFF)
include (depends)
enable_testing ()

add_subdirectory (lib)
