已合并
适配ops-cv静态包构建 #1174
适配ops-cv静态包构建 #1174
已合并
jialimin1创建于 7月27日
3 个文件变更+244-36
@@ -1291,7 +1291,9 @@ build_static_lib() {
1291 if [[ ${#UNITS[@]} -eq 0 ]]; then1291 if [[ ${#UNITS[@]} -eq 0 ]]; then
1292 UNITS+=("ascend910b")1292 UNITS+=("ascend910b")
1293 fi1293 fi
1294- cmake --build . --target opapi_cv_static -- ${VERBOSE} -j $THREAD_NUM1294+ if grep -wq "opapi_cv_static" <<< "${all_targets}"; then
1295+ cmake --build . --target opapi_cv_static -- ${VERBOSE} -j $THREAD_NUM
1296+ fi
1295 local jit_command=""1297 local jit_command=""
1296 if [[ "$ENABLE_JIT" == "TRUE" ]]; then1298 if [[ "$ENABLE_JIT" == "TRUE" ]]; then
1297 jit_command="-j"1299 jit_command="-j"
@@ -1302,7 +1304,9 @@ build_static_lib() {
1302 python3 "${BASE_PATH}/scripts/util/build_opp_kernel_static.py" StaticCompile -s ${unit} -b "${BUILD_PATH}" -n=0 -a=${ARCH_INFO} ${jit_command}1304 python3 "${BASE_PATH}/scripts/util/build_opp_kernel_static.py" StaticCompile -s ${unit} -b "${BUILD_PATH}" -n=0 -a=${ARCH_INFO} ${jit_command}
1303 done1305 done
1304 cd "${BUILD_PATH}" && cmake ${CMAKE_ARGS} ..1306 cd "${BUILD_PATH}" && cmake ${CMAKE_ARGS} ..
1305- cmake --build . --target cann_cv_static -- ${VERBOSE} -j $THREAD_NUM1307+ if grep -wq "cann_cv_static" <<< "${all_targets}"; then
1308+ cmake --build . --target cann_cv_static -- ${VERBOSE} -j $THREAD_NUM
1309+ fi
1306 echo "Build static lib success!"1310 echo "Build static lib success!"
1307}1311}
1308 1312 
@@ -1831,7 +1835,6 @@ package_static() {
1831 1835 
1832 # Get filename of *.run file and set new directory name1836 # Get filename of *.run file and set new directory name
1833 local run_file=$(basename "${run_files[0]}")1837 local run_file=$(basename "${run_files[0]}")
1834- echo "Found .run file: $run_file"
1835 if [[ "$run_file" != *"ops-cv"* ]]; then1838 if [[ "$run_file" != *"ops-cv"* ]]; then
1836 echo "Error: Filename '$run_file' does not contain 'ops-cv'"1839 echo "Error: Filename '$run_file' does not contain 'ops-cv'"
1837 return 11840 return 1
@@ -1842,8 +1845,7 @@ package_static() {
1842 # Check weather $BUILD_PATH/static_library_files directory exists and not empty1845 # Check weather $BUILD_PATH/static_library_files directory exists and not empty
1843 local static_files_dir="$BUILD_PATH/static_library_files"1846 local static_files_dir="$BUILD_PATH/static_library_files"
1844 if [ ! -d "$static_files_dir" ]; then1847 if [ ! -d "$static_files_dir" ]; then
1845- echo "Error: Directory $static_files_dir does not exist"1848+ return 0
1846- return 1
1847 fi1849 fi
1848 if [ -z "$(ls -A "$static_files_dir")" ]; then1850 if [ -z "$(ls -A "$static_files_dir")" ]; then
1849 echo "Error: Directory $static_files_dir is empty"1851 echo "Error: Directory $static_files_dir is empty"
@@ -1,18 +1,18 @@
1# ---------------------------------------------------------------------------------------------------------1# ---------------------------------------------------------------------------------------------------------
2# Copyright (c) 2025 Huawei Technologies Co., Ltd.2# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 3+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4# CANN Open Software License Agreement Version 2.0 (the "License").4# CANN Open Software License Agreement Version 2.0 (the "License").
5# Please refer to the License for details. You may not use this file except in compliance with the License.5# Please refer to the License for details. You may not use this file except in compliance with the License.
6-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 6+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 7+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8# See LICENSE in the root of the software repository for the full text of the License.8# See LICENSE in the root of the software repository for the full text of the License.
9# ---------------------------------------------------------------------------------------------------------9# ---------------------------------------------------------------------------------------------------------
10 10 
11if (TARGET ${OPHOST_NAME}_infer_obj OR TARGET ${OPHOST_NAME}_tiling_obj OR TARGET ${OPHOST_NAME}_aicpu_objs)11if (TARGET ${OPHOST_NAME}_infer_obj OR TARGET ${OPHOST_NAME}_tiling_obj OR TARGET ${OPHOST_NAME}_aicpu_objs)
12 add_library(12 add_library(
13 ${OPHOST_NAME}_static STATIC13 ${OPHOST_NAME}_static STATIC
14- $<$<TARGET_EXISTS:${OPHOST_NAME}_infer_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_infer_obj>> 14+ $<$<TARGET_EXISTS:${OPHOST_NAME}_infer_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_infer_obj>>
15- $<$<TARGET_EXISTS:${OPHOST_NAME}_tiling_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_tiling_obj>> 15+ $<$<TARGET_EXISTS:${OPHOST_NAME}_tiling_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_tiling_obj>>
16 $<$<TARGET_EXISTS:${OPHOST_NAME}_aicpu_objs>:$<TARGET_OBJECTS:${OPHOST_NAME}_aicpu_objs>>16 $<$<TARGET_EXISTS:${OPHOST_NAME}_aicpu_objs>:$<TARGET_OBJECTS:${OPHOST_NAME}_aicpu_objs>>
17 $<$<TARGET_EXISTS:${COMMON_NAME}_obj>:$<TARGET_OBJECTS:${COMMON_NAME}_obj>>17 $<$<TARGET_EXISTS:${COMMON_NAME}_obj>:$<TARGET_OBJECTS:${COMMON_NAME}_obj>>
18 $<$<TARGET_EXISTS:opbase_util_objs>:$<TARGET_OBJECTS:opbase_util_objs>>18 $<$<TARGET_EXISTS:opbase_util_objs>:$<TARGET_OBJECTS:opbase_util_objs>>
@@ -60,30 +60,32 @@ if (TARGET ${OPHOST_NAME}_opapi_obj OR TARGET opbuild_gen_aclnn_all)
60 -s ${PROJECT_SOURCE_DIR}/build/${OPAPI_NAME}.txt60 -s ${PROJECT_SOURCE_DIR}/build/${OPAPI_NAME}.txt
61 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp)61 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp)
62endif ()62endif ()
63-add_custom_target(${OPSTATIC_NAME})63+if (TARGET ${OPHOST_NAME}_static OR TARGET ${OPAPI_NAME}_static)
atomgit-bot
atomgit-botatomgit-bot7月27日

🟡 Medium Priority

cmake/static.cmake 中,第 63 行新增的守卫条件使用了 OR:if (TARGET ${OPHOST_NAME}_static OR TARGET ${OPAPI_NAME}_static)。这意味着只要两个组件静态库目标中的任意一个存在,就会创建 cann_cv_static${OPSTATIC_NAME})目标。

但在该目标内部的 PRE_BUILD 自定义命令中(第 78–80 行),无条件地对 两个 .a 文件执行 ar x

如果仅存在其中一个组件库(例如只有 ophost_cv_static 而没有 opapi_cv_static),那么缺失的 .a 文件会导致 ar x 失败,从而使 cmake --build --target cann_cv_static 构建失败。

触发条件:项目配置中仅定义了 ophost_cv_infer_obj / ophost_cv_tiling_obj / ophost_cv_aicpu_objs(创建 ophost_cv_static),但没有定义 ophost_cv_opapi_obj / opbuild_gen_aclnn_all(不创建 opapi_cv_static),或反之。虽然对于 ops-cv 项目通常两者同时存在,但 CMake 代码中的 OR 条件表明作者预期了单边存在的场景,而内部命令没有与之匹配。

建议:将第 63 行的 OR 改为 AND:if (TARGET ${OPHOST_NAME}_static AND TARGET ${OPAPI_NAME}_static),确保两个组件库都存在时才创建合并目标;或者拆分为两个独立的条件块,分别处理仅存在一个组件库的情况。

改动建议
63
- if (TARGET ${OPHOST_NAME}_static OR TARGET ${OPAPI_NAME}_static)
63
+ if (TARGET ${OPHOST_NAME}_static AND TARGET ${OPAPI_NAME}_static)
应用建议
likedislike
不准确?
64-foreach (compute_unit ${ASCEND_COMPUTE_UNIT})64+ add_custom_target(${OPSTATIC_NAME})
65- set(RESOURCE_PATH ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}/aclnnop_resource)65+ foreach (compute_unit ${ASCEND_COMPUTE_UNIT})
66- file(GLOB RESOURCE_CPP ${RESOURCE_PATH}/*.cpp)66+ set(RESOURCE_PATH ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}/aclnnop_resource)
67- if (RESOURCE_CPP)67+ file(GLOB RESOURCE_CPP ${RESOURCE_PATH}/*.cpp)
68- add_library(resource_${compute_unit}_static STATIC ${RESOURCE_CPP})68+ if (RESOURCE_CPP)
69- target_include_directories(resource_${compute_unit}_static PRIVATE69+ add_library(resource_${compute_unit}_static STATIC ${RESOURCE_CPP})
70- ${OPAPI_INCLUDE})70+ target_include_directories(resource_${compute_unit}_static PRIVATE
71- set_target_properties(resource_${compute_unit}_static PROPERTIES71+ ${OPAPI_INCLUDE})
72- ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}72+ set_target_properties(resource_${compute_unit}_static PROPERTIES
73- OUTPUT_NAME resource_static)73+ ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}
74- add_dependencies(${OPSTATIC_NAME} resource_${compute_unit}_static)74+ OUTPUT_NAME resource_static)
75- add_custom_command(TARGET ${OPSTATIC_NAME}75+ add_dependencies(${OPSTATIC_NAME} resource_${compute_unit}_static)
76- PRE_BUILD76+ add_custom_command(TARGET ${OPSTATIC_NAME}
77- COMMAND ${CMAKE_AR} x ${CMAKE_BINARY_DIR}/lib${OPHOST_NAME}_static.a77+ PRE_BUILD
78- COMMAND ${CMAKE_AR} x libresource_static.a78+ COMMAND ${CMAKE_AR} x ${CMAKE_BINARY_DIR}/lib${OPHOST_NAME}_static.a
79- COMMAND ${CMAKE_AR} x ${CMAKE_BINARY_DIR}/lib${OPAPI_NAME}_static.a79+ COMMAND ${CMAKE_AR} x libresource_static.a
80- COMMAND ${CMAKE_AR} qcs lib${OPSTATIC_NAME}.a *.o80+ COMMAND ${CMAKE_AR} x ${CMAKE_BINARY_DIR}/lib${OPAPI_NAME}_static.a
81- COMMAND rm *.o81+ COMMAND ${CMAKE_AR} qcs lib${OPSTATIC_NAME}.a *.o
82- WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit})82+ COMMAND rm *.o
83- if (ENABLE_PACKAGE)83+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit})
84- install(FILES ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}/lib${OPSTATIC_NAME}.a84+ if (ENABLE_PACKAGE)
85- DESTINATION ${CMAKE_BINARY_DIR}/static_library_files/lib6485+ install(FILES ${CMAKE_BINARY_DIR}/bin_tmp/${compute_unit}/lib${OPSTATIC_NAME}.a
86- OPTIONAL)86+ DESTINATION ${CMAKE_BINARY_DIR}/static_library_files/lib64
87+ OPTIONAL)
88+ endif ()
87 endif ()89 endif ()
88- endif ()90+ endforeach ()
89-endforeach ()91+endif ()
@@ -0,0 +1,204 @@
1+#!/bin/bash
2+# ----------------------------------------------------------------------------
3+# Copyright (c) 2025 Huawei Technologies Co., Ltd.
liu-wei
liu-weiliu-wei7月29日

版权不对

likedislike
4+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# ----------------------------------------------------------------------------
11+set -euo pipefail
12+ 
13+SOC="910b"
14+ARCH_INFO=$(uname -m)
15+ 
16+log_info() {
17+ echo "=== $1 ==="
18+}
19+ 
20+log_error() {
21+ echo "Error: $1" >&2
22+}
23+ 
24+usage() {
25+ cat <<EOF
26+Usage: $(basename "$0") [--soc <soc_name>] <packages_dir> <output_dir>
27+ 
28+Decompress multiple cann-ops-cv-static tar.gz packages and merge them.
29+Static library (.a) files will be merged into one.
30+ 
31+Options:
32+ --soc <soc_name> Specify the SoC name (default: 910b)
33+ 
34+Arguments:
35+ packages_dir Directory containing *.tar.gz packages
36+ output_dir Output directory for merged result
37+ 
38+Example:
39+ $(basename "$0") ./build_out ./merged_output
40+ $(basename "$0") --soc 910b ./build_out ./merged_output
41+EOF
42+ exit 1
43+}
44+ 
45+remove_ascend_lower() {
46+ local input="$1"
47+ local lower_input=$(echo "$input" | tr '[:upper:]' '[:lower:]')
48+ local result=${lower_input#ascend}
49+ if [[ "$result" == "910_93" ]]; then
50+ result="A3"
51+ fi
52+ echo "$result"
53+}
54+ 
55+parse_args() {
56+ while [[ $# -gt 0 ]]; do
57+ case "$1" in
58+ --soc)
59+ [[ $# -lt 2 ]] && { log_error "--soc requires a value"; usage; }
60+ SOC=$(remove_ascend_lower "$2")
61+ shift 2
62+ ;;
63+ -h|--help)
64+ usage
65+ ;;
66+ -*)
67+ log_error "unknown option '$1'"
68+ usage
69+ ;;
70+ *)
71+ break
72+ ;;
73+ esac
74+ done
75+ 
76+ [[ $# -ne 2 ]] && usage
77+ 
78+ PACKAGES_DIR="$1"
79+ OUTPUT_DIR="$2"
80+ 
81+ [[ ! -d "$PACKAGES_DIR" ]] && { log_error "packages_dir '$PACKAGES_DIR' does not exist"; exit 1; }
82+ 
83+ PACKAGES_DIR=$(realpath "$PACKAGES_DIR")
84+ mkdir -p "$OUTPUT_DIR"
85+ OUTPUT_DIR=$(realpath "$OUTPUT_DIR")
86+}
87+ 
88+extract_packages() {
89+ TMPDIR="$PACKAGES_DIR/tmp"
90+ rm -rf "$TMPDIR"
91+ mkdir -p "$TMPDIR"
92+ 
93+ log_info "Extracting packages"
94+ PKG_COUNT=0
95+ for pkg in "$PACKAGES_DIR"/*.tar.gz; do
96+ [[ ! -f "$pkg" ]] && continue
97+ echo " Extracting: $(basename "$pkg")"
98+ tar -xzf "$pkg" -C "$TMPDIR"
99+ PKG_COUNT=$((PKG_COUNT + 1))
100+ done
101+ 
102+ [[ $PKG_COUNT -eq 0 ]] && { log_error "no .tar.gz files found in '$PACKAGES_DIR'"; exit 1; }
103+ echo " Total packages: $PKG_COUNT"
104+}
105+ 
106+merge_include() {
107+ local pkg_dir="$1"
108+ local base_dir="$2"
109+ 
110+ [[ -d "$pkg_dir/include" ]] || return 0
111+ cp -rn "$pkg_dir/include/"* "$base_dir/include/" 2>/dev/null || true
112+}
113+ 
114+merge_static_lib() {
115+ local pkg_dir="$1"
116+ local base_dir="$2"
117+ local other_lib="$pkg_dir/lib64/libcann_cv_static.a"
118+ 
119+ [[ -f "$other_lib" ]] || return 0
120+ 
121+ pushd "$pkg_dir/lib64" > /dev/null
122+ 
123+ local pkg_name
124+ pkg_name="$(basename "$pkg_dir")"; pkg_name="${pkg_name##*static-}"; pkg_name="${pkg_name%%_linux*}"
125+ local count=0
126+ 
127+ ar x "$other_lib"
128+ o_files=($(ls *.o))
129+ 
130+ for o_file in "${o_files[@]}"; do
131+ local name=$(basename ${o_file})
132+ local dst="$base_dir/lib64/$o_file"
133+ if [[ ! -f "$dst" ]]; then
134+ cp "$o_file" "$dst"
135+ continue
136+ fi
137+ 
138+ # 非生成的资源代码,直接复制
139+ if [[ "$o_file" == *"op_resource.cpp.o" ]]; then
140+ if [[ $(stat -c%s "$o_file") -gt $(stat -c%s "$dst") ]]; then
141+ cp "$o_file" "$dst"
142+ fi
143+ else
144+ cp "$o_file" "$dst"
145+ fi
146+ done
147+ 
148+ popd > /dev/null
149+}
150+ 
151+merge_packages() {
152+ log_info "Merging packages"
153+ 
154+ PKG_DIRS=()
155+ for dir in "$TMPDIR"/cann-ops-cv-static-*/; do
156+ [[ -d "$dir" ]] && PKG_DIRS+=("$dir")
157+ done
158+ 
159+ if [[ ${#PKG_DIRS[@]} -eq 0 ]]; then
160+ log_error "no cann-ops-cv-static-* directories found after extraction"
161+ exit 1
162+ fi
163+ 
164+ local VERSION
165+ VERSION=$(grep 'set_cann_package' "$(dirname "$0")/../../version.cmake" | sed 's/.*set_cann_package([^ ]* [^ ]* "\([^"]*\)".*/\1/')
166+ local base_name="cann-${SOC}-ops-cv-static-${VERSION}_linux-${ARCH_INFO}"
167+ local base_dir="$TMPDIR/$base_name"
168+ 
169+ rm -rf "$base_dir"
170+ mkdir -p "$base_dir/lib64" "$base_dir/include"
171+ 
172+ for pkg_dir in "${PKG_DIRS[@]}"; do
173+ echo " Merging: $(basename "$pkg_dir")"
174+ merge_include "$pkg_dir" "$base_dir"
175+ merge_static_lib "$pkg_dir" "$base_dir"
176+ done
177+ 
178+ package_result "$base_dir" "$base_name"
179+}
180+ 
181+package_result() {
182+ local base_dir="$1"
183+ local base_name="$2"
184+ 
185+ pushd "$base_dir/lib64" > /dev/null
186+ ar rcs libcann_cv_static.a ./*.o
187+ rm -f *.o
188+ popd > /dev/null
189+ 
190+ pushd "$TMPDIR" > /dev/null
191+ tar -czf "${base_name}.tar.gz" "$base_name"
192+ mv "${base_name}.tar.gz" "$OUTPUT_DIR"
193+ popd > /dev/null
194+ 
195+ log_info "Done: merged output in $OUTPUT_DIR"
196+}
197+ 
198+main() {
199+ parse_args "$@"
200+ extract_packages
201+ merge_packages
202+}
203+ 
204+main "$@"