# ----------------------------------------------------------------------------
# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# ----------------------------------------------------------------------------

set(CMAKE_SKIP_RPATH TRUE)

set(INCLUDE_LIST
        ${HIXL_DIR}/src/hixl
        ${HIXL_DIR}/include
)

add_library(cann_hixl_kernel SHARED
        hixl_batch_transfer.cc
        hixl_sync_transfer_context.cc
        transfer_context_manager.cc
)

target_include_directories(cann_hixl_kernel PRIVATE
        ${INCLUDE_LIST}
        ${HIXL_CODE_DIR}/src/hixl/proxy
)

target_link_libraries(cann_hixl_kernel PRIVATE
        $<BUILD_INTERFACE:intf_pub>
        $<BUILD_INTERFACE:hccl_headers>
        $<BUILD_INTERFACE:slog_headers>
        $<BUILD_INTERFACE:acl_rt_headers>
        $<BUILD_INTERFACE:c_sec_headers>
        $<BUILD_INTERFACE:error_manager_headers>
        $<BUILD_INTERFACE:metadef_headers>
        $<BUILD_INTERFACE:runtime_headers>
        ascend_hal
        aicpu_sharder
        -Wl,--no-as-needed
        slog_dev
        hcomm_proxy_static
        -Wl,--no-as-needed
)

target_compile_options(cann_hixl_kernel PRIVATE -O2)

# 基于ini生成json文件
message(STATUS "HIXL_DIR = ${HIXL_DIR}")
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libcann_hixl_kernel.json
        COMMAND python3 ${HIXL_DIR}/scripts/kernel/parser_ini.py ${HIXL_DIR}/src/ops/hixl_kernel/libcann_hixl_kernel.ini ${CMAKE_CURRENT_BINARY_DIR}/libcann_hixl_kernel.json
)
add_custom_target(hixl_aicpu_kernel_json DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libcann_hixl_kernel.json)
add_dependencies(cann_hixl_kernel hixl_aicpu_kernel_json)

install(FILES
        ${CMAKE_CURRENT_BINARY_DIR}/libcann_hixl_kernel.json
        DESTINATION opp/built-in/op_impl/aicpu/config
        COMPONENT hixl
)

install(TARGETS cann_hixl_kernel
        DESTINATION ${INSTALL_LIBRARY_DIR} ${INSTALL_OPTIONAL})