# Copyright Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier:  MIT

# Owns the `hipblaslt::mxdatagen` STATIC helper. The source and header live
# under this directory, so the target is defined here rather than at the root.
#
# This file is added by the project root via `add_subdirectory(clients/common)`
# only when:
#   * HIPBLASLT_ENABLE_MXDATAGENERATOR is ON, and
#   * at least one consumer subtree is enabled (HIPBLASLT_ENABLE_CLIENT,
#     TENSILELITE_ENABLE_CLIENT, or TENSILELITE_BUILD_TESTING).
#
# Consumers should `target_link_libraries(<tgt> PUBLIC hipblaslt::mxdatagen)`
# (PRIVATE if no further consumer needs the macro). Linking propagates:
#   * the include path for `<mxDataGen.hpp>`
#   * the link to `roc::mxDataGenerator`
#   * the C++20 requirement
#   * the `HIPBLASLT_ENABLE_MXDATAGENERATOR` macro
#   * `hipblaslt::headers`, which carries the in-tree `<hipblaslt/...>` API
#     headers (the source pulls in `<hipblaslt/hipblaslt-export.h>` and
#     `<hipblaslt/hipblaslt-types.h>`).

if(NOT ROCM_LIBS_SUPERBUILD)
    if(HIPBLASLT_ENABLE_THEROCK)
        find_package(mxDataGenerator REQUIRED)
    else()
        # `${PROJECT_SOURCE_DIR}/../../shared/mxdatagenerator` resolves to the
        # sibling `shared/mxdatagenerator` subtree of the project. The binary
        # dir is anchored under the hipblaslt project's build tree to match
        # the upstream subdirectory's existing layout assumptions.
        add_subdirectory(
            "${PROJECT_SOURCE_DIR}/../../shared/mxdatagenerator"
            "${PROJECT_BINARY_DIR}/mxdatagenerator"
        )
    endif()
endif()

add_library(hipblaslt-mxdatagen STATIC
    "${CMAKE_CURRENT_SOURCE_DIR}/src/mxDataGen.cpp"
)
add_library(hipblaslt::mxdatagen ALIAS hipblaslt-mxdatagen)

target_include_directories(hipblaslt-mxdatagen
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_link_libraries(hipblaslt-mxdatagen
    PUBLIC
        hipblaslt::headers
        roc::mxDataGenerator
    PRIVATE
        # `-x hip` (via hip::device's INTERFACE_COMPILE_OPTIONS) makes the
        # translation unit compile through hipcc/clang. mxDataGen.cpp itself
        # does not launch kernels, but the modern ROCm bf16 header it pulls
        # in transitively uses clang-only builtins (e.g. __builtin_elementwise_rint).
        hip::device
)

target_compile_features(hipblaslt-mxdatagen PUBLIC cxx_std_20)
target_compile_definitions(hipblaslt-mxdatagen PUBLIC HIPBLASLT_ENABLE_MXDATAGENERATOR)
set_target_properties(hipblaslt-mxdatagen
    PROPERTIES POSITION_INDEPENDENT_CODE ON
)

# `<hipblaslt/hipblaslt-export.h>` is generated by
# `library/include/CMakeLists.txt`'s `generate_export_header(hipblaslt ...)`
# call when HIPBLASLT_ENABLE_HOST=ON. In a tensilelite-only build that target
# does not exist, so produce the same file from this helper instead. Both
# code paths emit the file to the same location so `hipblaslt::headers`'s
# `${CMAKE_BINARY_DIR}/library/include` interface dir resolves it either way.
if(NOT HIPBLASLT_ENABLE_HOST)
    include(GenerateExportHeader)
    generate_export_header(hipblaslt-mxdatagen
        BASE_NAME hipblaslt
        EXPORT_FILE_NAME "${PROJECT_BINARY_DIR}/library/include/hipblaslt/hipblaslt-export.h"
    )
endif()
