# ----------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 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.
# ----------------------------------------------------------------------------------------------------------
macro(replace_cur_major_minor_ver)
string(REPLACE CUR_MAJOR_MINOR_VER "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_VERSION_MAJOR_MINOR}" depend "${depend}")
endmacro()
# 设置包和版本号
function(set_package name)
cmake_parse_arguments(VERSION "" "VERSION" "" ${ARGN})
set(VERSION "${VERSION_VERSION}")
if(NOT name)
message(FATAL_ERROR "The name parameter is not set in set_package.")
endif()
if(NOT VERSION)
message(FATAL_ERROR "The VERSION parameter is not set in set_package(${name}).")
endif()
string(REGEX MATCH "^([0-9]+\\.[0-9]+)" VERSION_MAJOR_MINOR "${VERSION}")
list(APPEND CANN_VERSION_PACKAGES "${name}")
set(CANN_VERSION_PACKAGES "${CANN_VERSION_PACKAGES}" PARENT_SCOPE)
set(CANN_VERSION_CURRENT_PACKAGE "${name}" PARENT_SCOPE)
set(CANN_VERSION_${name}_VERSION "${VERSION}" PARENT_SCOPE)
set(CANN_VERSION_${name}_VERSION_MAJOR_MINOR "${VERSION_MAJOR_MINOR}" PARENT_SCOPE)
set(CANN_VERSION_${name}_BUILD_DEPS PARENT_SCOPE)
set(CANN_VERSION_${name}_RUN_DEPS PARENT_SCOPE)
endfunction()
# 设置构建依赖
function(set_build_dependencies pkg_name depend)
if(NOT CANN_VERSION_CURRENT_PACKAGE)
message(FATAL_ERROR "The set_package must be invoked first.")
endif()
if(NOT pkg_name)
message(FATAL_ERROR "The pkg_name parameter is not set in set_build_dependencies.")
endif()
if(NOT depend)
message(FATAL_ERROR "The depend parameter is not set in set_build_dependencies.")
endif()
replace_cur_major_minor_ver()
list(APPEND CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS "${pkg_name}" "${depend}")
set(CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS}" PARENT_SCOPE)
endfunction()
# 设置运行依赖
function(set_run_dependencies pkg_name depend)
if(NOT CANN_VERSION_CURRENT_PACKAGE)
message(FATAL_ERROR "The set_package must be invoked first.")
endif()
if(NOT pkg_name)
message(FATAL_ERROR "The pkg_name parameter is not set in set_run_dependencies.")
endif()
if(NOT depend)
message(FATAL_ERROR "The depend parameter is not set in set_run_dependencies.")
endif()
replace_cur_major_minor_ver()
list(APPEND CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS "${pkg_name}" "${depend}")
set(CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS}" PARENT_SCOPE)
endfunction()
# 检查构建依赖
function(check_pkg_build_deps pkg_name)
execute_process(
COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_build_dependencies.py "${ASCEND_CANN_PACKAGE_PATH}" ${CANN_VERSION_${pkg_name}_BUILD_DEPS}
RESULT_VARIABLE result
)
if(result)
message(FATAL_ERROR "Check ${pkg_name} build dependencies failed!")
endif()
endfunction()
# 添加生成version.info的目标
# 目标名格式为:version_${包名}_info
function(add_version_info_targets)
message(STATUS "Generating version.info....")
foreach(pkg_name ${CANN_VERSION_PACKAGES})
execute_process(
COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_version_info.py
--output ${CMAKE_BINARY_DIR}/version.${pkg_name}.info
"${CANN_VERSION_${pkg_name}_VERSION}" ${CANN_VERSION_${pkg_name}_RUN_DEPS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE result
)
add_custom_target(version_${pkg_name}_info ALL DEPENDS ${CMAKE_BINARY_DIR}/version.${pkg_name}.info)
endforeach()
endfunction()