# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# select platform configuration setting WAMR_BUILD_TARGET
set(WAMR_BUILD_PLATFORM nuttx)

if(CONFIG_ARCH_ARMV6M)
  set(WAMR_BUILD_TARGET THUMBV6M)
elseif(CONFIG_ARCH_ARMV7A)
  if(CONFIG_ARM_THUMB)
    if(CONFIG_ARCH_FPU)
      set(WAMR_BUILD_TARGET THUMBV7_VFP)
    else()
      set(WAMR_BUILD_TARGET THUMBV7)
    endif()
  else()
    if(CONFIG_ARCH_FPU)
      set(WAMR_BUILD_TARGET ARMV7_VFP)
    else()
      set(WAMR_BUILD_TARGET ARMV7)
    endif()
  endif()
elseif(CONFIG_ARCH_ARMV7M)
  set(WAMR_BUILD_TARGET THUMBV7EM)
elseif(CONFIG_ARCH_ARMV8M)
  set(WAMR_BUILD_TARGET THUMBV8M)
elseif(CONFIG_ARCH_X86)
  set(WAMR_BUILD_TARGET X86_32)
elseif(CONFIG_ARCH_X86_64)
  set(WAMR_BUILD_TARGET X86_64)
elseif(CONFIG_ARCH_XTENSA)
  set(WAMR_BUILD_TARGET XTENSA)
elseif(CONFIG_ARCH_RV64)
  set(WAMR_BUILD_TARGET RISCV64)
elseif(CONFIG_ARCH_RV32)
  set(WAMR_BUILD_TARGET RISCV32)
elseif(CONFIG_ARCH_SIM)
  if(CONFIG_SIM_M32 OR CONFIG_HOST_X86)
    set(WAMR_BUILD_TARGET X86_32)
  elseif(CONFIG_HOST_ARM)
    set(WAMR_BUILD_TARGET ARM)
  elseif(CONFIG_HOST_ARM64)
    set(WAMR_BUILD_TARGET AARCH64)
  else()
    set(WAMR_BUILD_TARGET X86_64)
  endif()
  if(CONFIG_HOST_MACOS)
    add_definitions(-DBH_PLATFORM_DARWIN)
  endif()
endif()

if(CONFIG_INTERPRETERS_WAMR_LOG)
  add_definitions(-DWASM_ENABLE_LOG=1)
else()
  add_definitions(-DWASM_ENABLE_LOG=0)
endif()

if(CONFIG_INTERPRETERS_WAMR_AOT_WORD_ALIGN_READ)
  add_definitions(-DWASM_ENABLE_WORD_ALIGN_READ=1)
else()
  add_definitions(-DWASM_ENABLE_WORD_ALIGN_READ=0)
endif()

if(CONFIG_INTERPRETERS_WAMR_DYNAMIC_AOT_DEBUG)
  add_definitions(-DWASM_ENABLE_DYNAMIC_AOT_DEBUG=1)
else()
  add_definitions(-DWASM_ENABLE_DYNAMIC_AOT_DEBUG=0)
endif()

if(CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE)
  add_definitions(-DWASM_STACK_GUARD_SIZE=0)
else()
  add_definitions(
    -DWASM_STACK_GUARD_SIZE=${CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE})
endif()

if(CONFIG_INTERPRETERS_WAMR_MEMORY_TRACING)
  add_definitions(-DWASM_ENABLE_MEMORY_TRACING=1)
else()
  add_definitions(-DWASM_ENABLE_MEMORY_TRACING=0)
endif()

if(CONFIG_INTERPRETERS_WAMR_SHARED_MEMORY)
  set(WAMR_BUILD_SHARED_MEMORY 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_BULK_MEMORY)
  set(WAMR_BUILD_BULK_MEMORY 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_AOT_STACK_FRAME)
  set(WAMR_BUILD_AOT_STACK_FRAME 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_PERF_PROFILING)
  set(WAMR_BUILD_PERF_PROFILING 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_GC)
  set(WAMR_BUILD_GC 1)
  set(WAMR_BUILD_STRINGREF 1)
  set(WAMR_BUILD_REF_TYPES 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_GC_MANUALLY)
  add_definitions(-DWASM_GC_MANUALLY=1)
else()
  add_definitions(-DWASM_GC_MANUALLY=0)
endif()

if(CONFIG_INTERPRETERS_WAMR_GC_PERF_PROFILING)
  set(WAMR_BUILD_GC_PERF_PROFILING 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_ENABLE_EXCE_HANDLING)
  set(WAMR_BUILD_EXCE_HANDLING 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_TAIL_CALL)
  set(WAMR_BUILD_TAIL_CALL 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_MEMORY_PROFILING)
  set(WAMR_BUILD_MEMORY_PROFILING 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_MULTI_MODULE)
  set(WAMR_BUILD_MULTI_MODULE 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_LIB_PTHREAD_SEMAPHORE)
  set(WAMR_BUILD_LIB_PTHREAD_SEMAPHORE 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_DISABLE_HW_BOUND_CHECK)
  set(WAMR_DISABLE_HW_BOUND_CHECK 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_CUSTOM_NAME_SECTIONS)
  set(WAMR_BUILD_CUSTOM_NAME_SECTION 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL)
  set(WAMR_BUILD_GLOBAL_HEAP_POOL 1)
  math(EXPR _HEAP_SIZE_
       "${CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE} * 1024")
  set(WAMR_BUILD_GLOBAL_HEAP_SIZE ${_HEAP_SIZE_})
endif()

if (CONFIG_INTERPRETERS_WAMR_MEM_ALLOC_WITH_USAGE)
  set(WAMR_BUILD_MEM_ALLOC_WITH_USAGE 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_ENABLE_SPEC_TEST)
  set(WAMR_BUILD_SPEC_TEST 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_REF_TYPES)
  set(WAMR_BUILD_REF_TYPES 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_CLASSIC)
  # include iwasm_interp.cmake
  set(WAMR_BUILD_INTERP 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_FAST)
  # enable iwasm_interp.cmake
  set(WAMR_BUILD_FAST_INTERP 1)
endif()

if((CONFIG_INTERPRETERS_WAMR_FAST OR CONFIG_INTERPRETERS_WAMR_CLASSIC)
   AND CONFIG_INTERPRETERS_WAMR_MINILOADER)
  # enable iwasm_interp.cmake
  set(WAMR_BUILD_MINI_LOADER 1)
else()
  set(WAMR_BUILD_MINI_LOADER 0)
endif()

if(CONFIG_INTERPRETERS_WAMR_AOT)
  # include iwasm_aot.cmake
  set(WAMR_BUILD_AOT 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_DEBUG_INTERP)
  # include debug_engine.cmake
  set(WAMR_BUILD_DEBUG_INTERP 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN)
  # include libc_builtin.cmake
  set(WAMR_BUILD_LIBC_BUILTIN 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_LIBC_WASI)
  # include libc_wasi.cmake
  set(WAMR_BUILD_LIBC_WASI 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_THREAD_MGR)
  # include thread_mgr.cmake
  set(WAMR_BUILD_THREAD_MGR 1)
endif()

if(CONFIG_INTERPRETERS_WAMR_LIB_PTHREAD)
  # include lib_pthread.cmake
  set(WAMR_BUILD_LIB_PTHREAD 1)
endif()

set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)

# enable WAMR build system
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

# NuttX wamr lib compile required: `WAMR_SOURCES` `WAMR_CFLAGS` `WAMR_INCDIRS`
# `WAMR_DEFINITIONS`
set(WAMR_SOURCES ${WAMR_RUNTIME_LIB_SOURCE})
set(WAMR_CFLAGS -Wno-shadow -Wno-unused-variable
                -Wno-int-conversion -Wno-implicit-function-declaration)
get_directory_property(WAMR_INCDIRS INCLUDE_DIRECTORIES)
get_directory_property(WAMR_DEFINITIONS COMPILE_DEFINITIONS)
