cmake_minimum_required(VERSION 3.8)
project(tmo_kernels)
message(STATUS "project name: ${PROJECT_NAME}")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

################################################################################
# Build Evironment
################################################################################
set(BANG_TARGET_CPU_ARCH ${TARGET_CPU_ARCH})
message("-- TARGET_CPU_ARCH=${TARGET_CPU_ARCH}")
set(TARGET_MLU_ARCH ${TARGET_MLU_ARCH})
message("-- TARGET_MLU_ARCH=${TARGET_MLU_ARCH}")
set(NEUWARE_HOME ${NEUWARE_HOME})
message("-- NEUWARE_HOME=${NEUWARE_HOME}")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
  "${CMAKE_SOURCE_DIR}/cmake"
  "${NEUWARE_HOME}/cmake"
  "${NEUWARE_HOME}/cmake/modules"
)

find_package(BANG)
if(NOT BANG_FOUND)
  message(FATAL_ERROR "BANG cannot be found.")
else ()
  if (NOT BANG_CNCC_EXECUTABLE)
    message(FATAL_ERROR "cncc not found, please ensure cncc is in your PATH env or set variable BANG_CNCC_EXECUTABLE from cmake. Otherwise you should check path used by find_program(BANG_CNCC_EXECUTABLE) in FindBANG.cmake")
  endif()
endif()
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/test")
set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -pthread -pipe")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${CMAKE_C_FLAGS} -g3 -O0")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${CMAKE_C_FLAGS} -O3")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -fPIC -std=c++17 -pthread -pipe")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS} -g3 -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS} -O3")

set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -Wl,--gc-sections -fPIC")

set(BANG_CNCC_FLAGS "-Wall -Werror -Wdeprecated-declarations -fPIC -std=c++17 -pthread --target=${TARGET_CPU_ARCH}")

if ( "${_cncc_version}" VERSION_LESS "5.0.0") # [CNNLCORE-19128]
  message(STATUS "Default rounding mode will be rn when computing float numbers, otherwise will be tz when computing int numbers")
  # This compile option was enabled by JIRA: CNNLCORE-12027
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -Xbang-cnas --deprecated-cvt-default-round-mode-rn")
endif()

if(${TARGET_CPU_ARCH} MATCHES ".*x86_64.*")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -mcmodel=large")
endif()

string(TOLOWER ${CMAKE_BUILD_TYPE} _CMAKE_BUILD_TYPE_LOWER)
if(${_CMAKE_BUILD_TYPE_LOWER} MATCHES "debug")
  message(STATUS "Build debug mode")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -g3 -O0")
endif()

if(${_CMAKE_BUILD_TYPE_LOWER} MATCHES "release")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -O3 -DNDEBUG")
endif()

if(${TARGET_MLU_ARCH} MATCHES "CNFATBIN")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS}" "--bang-mlu-arch=mtp_592 --bang-mlu-arch=mtp_613 --no-neuware-version-check")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS}" "--bang-wram-align64")
else()
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS}" "--bang-mlu-arch=${TARGET_MLU_ARCH}")
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS}" "--bang-wram-align64")
endif()

# setup predefined macro for host sources, only for single mlu arch, useful for edge
if (${TARGET_MLU_ARCH} MATCHES "^(m?tp_)?([0-9]+)$")
  # convert mtp_xxx or tp_xxx to xxx
  string(REGEX REPLACE "^(m?tp_)?([0-9]+)$" "\\2" _TARGET_MLU_ARCH ${TARGET_MLU_ARCH})
  add_definitions(-DTARGET_MLU_ARCH=${_TARGET_MLU_ARCH})
  set(BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -DTARGET_MLU_ARCH=${_TARGET_MLU_ARCH}")
endif()

################################################################################
# Neuware Evironment
################################################################################
if(EXISTS ${NEUWARE_HOME})
  include_directories("${NEUWARE_HOME}/include")
  link_directories("${NEUWARE_HOME}/lib64")
  link_directories("${NEUWARE_HOME}/lib")
else()
  message(FATAL_ERROR "NEUWARE cannot be found, refer README.md to prepare NEUWARE_HOME environment.")
endif()

include_directories("${CMAKE_CURRENT_SOURCE_DIR}")

################################################################################
# Build TMO kernels
################################################################################
# aux_source_directory(src DIR_SRCS)
file(GLOB_RECURSE bang_src_files FOLLOW_SYMLINKS "${CMAKE_CURRENT_SOURCE_DIR}/*.mlu")

bang_add_library(tmo_kernels STATIC "${bang_src_files}")
target_link_libraries(tmo_kernels cnnl cnrt cndrv dl)
