################################################################################
# Autogenerated by build_tools/bazel_to_cmake/bazel_to_cmake.py from           #
# runtime/src/iree/hal/local/elf/BUILD.bazel                                   #
#                                                                              #
# Use iree_cmake_extra_content from iree/build_defs.oss.bzl to add arbitrary   #
# CMake-only content.                                                          #
#                                                                              #
# To disable autogeneration for this file entirely, delete this header.        #
################################################################################

# Disable on platforms/architectures where ELF is not supported.
if(EMSCRIPTEN)
  return()
endif()

iree_add_all_subdirs()

iree_cc_library(
  NAME
    elf_module
  HDRS
    "elf_module.h"
    "elf_types.h"
    "fatelf.h"
  SRCS
    "elf_module.c"
    "fatelf.c"
  DEPS
    ::arch
    ::platform
    iree::base
  PUBLIC
)

iree_cc_binary(
  NAME
    elf_module_test_binary
  SRCS
    "elf_module_test_main.c"
  DEPS
    ::elf_module
    iree::base
    iree::base::internal::cpu
    iree::hal::local::elf::testdata::elementwise_mul
    iree::hal::local::executable_environment
    iree::hal::local::executable_library
)

iree_native_test(
  NAME
    "elf_module_test"
  SRC
    ::elf_module_test_binary
)

iree_cc_library(
  NAME
    arch
  HDRS
    "arch.h"
  SRCS
    "arch/arm_32.c"
    "arch/arm_64.c"
    "arch/riscv.c"
    "arch/x86_32.c"
    "arch/x86_64.c"
    "elf_types.h"
  DEPS
    iree::base
  PUBLIC
)

iree_cc_library(
  NAME
    platform
  HDRS
    "platform.h"
  SRCS
    "platform/apple.c"
    "platform/generic.c"
    "platform/linux.c"
    "platform/windows.c"
  DEPS
    iree::base
    iree::base::internal::memory
  PUBLIC
)

### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###

# TODO(*): figure out how to make this work on Bazel+Windows.
if(MSVC)
  # CMake + MASM does not work well and CMake ends up passing all our C/C++
  # flags confusing MASM. We invoke MASM directly (ml.exe/ml64.exe) to keep it
  # quiet.
  if(MSVC_C_ARCHITECTURE_ID MATCHES 64 OR MSVC_CXX_ARCHITECTURE_ID MATCHES 64)
    set_source_files_properties(
      arch/x86_64_msvc.asm
      PROPERTIES
      LANGUAGE ASM_MASM
    )
    target_sources(iree_hal_local_elf_arch PRIVATE "arch/x86_64_msvc.obj")
    add_custom_command(
      OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/arch/x86_64_msvc.obj
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/arch/x86_64_msvc.asm
      COMMAND ml64 /nologo /Zi /c /Fo ${CMAKE_CURRENT_BINARY_DIR}/arch/x86_64_msvc.obj ${CMAKE_CURRENT_SOURCE_DIR}/arch/x86_64_msvc.asm
      VERBATIM
    )
  endif()
endif()
