已合并
support local compilation CI build. #537
liu-wei创建于 3月17日
support local compilation CI build. #537
已合并
liu-wei创建于 3月17日
1 个文件变更+283-0
Ascripts/ci/local_build.sh+283-0
@@ -0,0 +1,283 @@
1+#!/bin/bash
2+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
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").
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,
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.
9+# ============================================================================
10+ 
11+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
12+workspace_dir="$(dirname "$(dirname "$SCRIPT_DIR")")"
13+export WORKSPACE=${workspace_dir}
14+export ASCEND_3RD_LIB_PATH=${workspace_dir}/third_party
15+ 
16+ENABLE_OPHOST=false
17+ENABLE_UTEST=false
18+ENABLE_SMOKE=false
19+PR_FILELIST=""
20+ 
21+show_usage() {
22+cat << EOF
23+Usage: $0 [OPTIONS]
24+ 
25+Options:
26+ -h, --help Show this help message and exit
27+ -u Build utest
28+ -s Build smoke test workflow:
29+ - build single op
30+ - build ascend950
31+ - if Ascend 910B: run A2 smoke
32+ -f <file>, --file <file> Specify filelist for build
33+ --jit Enable ophost build
34+ 
35+ Note: Options can be combined.
36+EOF
37+}
38+ 
39+if [ "$#" -eq 0 ]; then
40+ ENABLE_OPHOST=true
41+ ENABLE_UTEST=true
42+ ENABLE_SMOKE=true
43+else
44+ while [ "$#" -gt 0 ]; do
45+ case "$1" in
46+ --jit)
47+ ENABLE_OPHOST=true
48+ shift
49+ ;;
50+ -u)
51+ ENABLE_UTEST=true
52+ shift
53+ ;;
54+ -s)
55+ ENABLE_SMOKE=true
56+ shift
57+ ;;
58+ -f)
59+ if [ -z "$2" ] || [[ "$2" == -* ]]; then
60+ echo "[ERROR] -f requires an argument."
61+ show_usage
62+ exit 1
63+ fi
64+ PR_FILELIST="$2"
65+ shift 2
66+ ;;
67+ --file)
68+ if [[ "$2" == *=* ]]; then
69+ PR_FILELIST="${2#*=}"
70+ shift
71+ elif [ -n "$2" ] && [[ "$2" != -* ]]; then
72+ PR_FILELIST="$2"
73+ shift 2
74+ else
75+ echo "[ERROR] --file requires an argument."
76+ show_usage
77+ exit 1
78+ fi
79+ ;;
80+ -h|--help)
81+ show_usage
82+ exit 0
83+ ;;
84+ *)
85+ echo "Invalid option: $1"
86+ show_usage
87+ exit 1
88+ ;;
89+ esac
90+ done
91+fi
92+ 
93+if [[ "$ENABLE_OPHOST" == "false" && "$ENABLE_UTEST" == "false" && "$ENABLE_SMOKE" == "false" ]]; then
94+ ENABLE_OPHOST=true
95+ ENABLE_UTEST=true
96+ ENABLE_SMOKE=true
97+fi
98+ 
99+get_pkg_name(){
100+ compile_package_name=$(ls "${workspace_dir}/build_out/" | grep -E "*.run$" | head -n1)
101+ echo "${compile_package_name}"
102+ return 0
103+}
104+ 
105+set -e
106+if [[ -z "$PR_FILELIST" ]] || [[ ! -s "$PR_FILELIST" ]]; then
107+ PR_FILELIST="pr_filelist.txt"
108+ TARGET_URL="https://gitcode.com/cann/ops-cv.git"
109+ BASE_BRANCH_NAME=${1:-master} # 默认对比 master 分支
110+ LOCAL_BRANCH=${2:-HEAD} # 默认对比当前本地分支
111+ 
112+ REMOTE_NAME=""
113+ for remote in $(git remote); do
114+ url=$(git remote get-url "$remote" 2>/dev/null)
115+ # 支持 https 和 git 协议,以及去掉末尾的 .git 进行模糊匹配
116+ if [[ "$url" == *"$TARGET_URL"* ]] || [[ "$url" == *"${TARGET_URL%.git}"* ]]; then
117+ REMOTE_NAME="$remote"
118+ break
119+ fi
120+ done
121+ 
122+ # 如果没找到匹配的 URL,默认使用 'origin' (兼容直接 clone 主仓的情况)
123+ if [ -z "$REMOTE_NAME" ]; then
124+ echo "Warning: Specific remote URL not found. Defaulting to 'origin'."
125+ if ! git remote | grep -q "^origin$"; then
126+ echo "Error: No 'origin' remote found either. Please check your remotes."
127+ exit 1
128+ fi
129+ REMOTE_NAME="origin"
130+ fi
131+ 
132+ echo "Detected Remote: $REMOTE_NAME (URL: $(git remote get-url $REMOTE_NAME))"
133+ 
134+ echo "Fetching latest from $REMOTE_NAME..."
135+ git fetch "$REMOTE_NAME" --quiet --prune
136+ 
137+ # 构建远程分支引用名 (例如: origin/master)
138+ REMOTE_REF="${REMOTE_NAME}/${BASE_BRANCH_NAME}"
139+ 
140+ # 检查远程分支是否存在
141+ if ! git rev-parse --verify "$REMOTE_REF" >/dev/null 2>&1; then
142+ echo "Error: Remote branch '$REMOTE_REF' does not exist."
143+ echo "Available branches from $REMOTE_NAME:"
144+ git branch -r --list "${REMOTE_NAME}/*"
145+ exit 1
146+ fi
147+ 
148+ MERGE_BASE_COMMIT=$(git merge-base "$REMOTE_REF" "$LOCAL_BRANCH")
149+ 
150+ if [ -z "$MERGE_BASE_COMMIT" ]; then
151+ echo "Error: Could not find a common ancestor between $REMOTE_REF and $LOCAL_BRANCH."
152+ exit 1
153+ fi
154+ 
155+ TARGET_COMMIT=$(git rev-parse "$LOCAL_BRANCH")
156+ 
157+ echo "Calculating changed files..."
158+ CHANGED_FILES=$(git diff --name-only "$MERGE_BASE_COMMIT" "$TARGET_COMMIT" | sort -u)
159+ 
160+ if [ -z "$CHANGED_FILES" ]; then
161+ echo "[warning] The file for the change cannot be found. Please check whether the code has been committed."
162+ exit 0
163+ fi
164+ 
165+ # 输出结果
166+ echo "$CHANGED_FILES" > $PR_FILELIST
167+ echo "Saved changed files to $PR_FILELIST"
168+else
169+ echo "Using custom file: $PR_FILELIST"
170+fi
171+ 
172+echo "Total: $(wc -l < $PR_FILELIST) files"
173+echo ""
174+echo "Preview:"
175+cat $PR_FILELIST
176+ 
177+export BASE_PATH=$(pwd)
178+export BUILD_PATH="${BASE_PATH}/build"
179+rm -rf $BUILD_PATH
180+rm -rf $BASE_PATH/build_out
181+ 
182+THREAD_NUM=$(grep -c ^processor /proc/cpuinfo)
183+if [[ "$ENABLE_OPHOST" == "true" ]]; then
184+ echo "==============================build jit============================================="
185+ echo "exec cmd: [bash build.sh --pkg --jit -j${THREAD_NUM} --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH}]"
186+ bash build.sh --pkg --jit -j${THREAD_NUM} --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH}
187+fi
188+ 
189+if [[ "$ENABLE_UTEST" == "true" ]]; then
190+ echo "==============================build utest start======================================"
191+ echo "--------------------------build ophost ut start-----------------------------------"
192+ echo "exec cmd: [bash build.sh -u --ophost -f $PR_FILELIST --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}]"
193+ bash build.sh -u --ophost -f "$PR_FILELIST" --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}
194+ echo "--------------------------build opapi ut start------------------------------------"
195+ echo "exec cmd: [bash build.sh -u --opapi -f $PR_FILELIST --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}]"
196+ bash build.sh -u --opapi -f "$PR_FILELIST" --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}
197+ if [ "$BASE_BRANCH_NAME" = "master" ]; then
198+ echo "--------------------------build opgraph ut start-----------------------------------"
199+ echo "exec cmd: [bash build.sh -u --opgraph -f $PR_FILELIST --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}]"
200+ bash build.sh -u --opgraph -f "$PR_FILELIST" --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} -j${THREAD_NUM}
201+ echo "--------------------------build opkernel ut start-----------------------------------"
202+ echo "exec cmd: [bash scripts/ci/check_kernel_ut.sh $PR_FILELIST --no_cov]"
203+ bash scripts/ci/check_kernel_ut.sh $PR_FILELIST --no_cov | tee output.txt
204+ if grep -q "error happened" output.txt; then
205+ echo "[ERROR] Error happened in output check log"
206+ exit 1
207+ fi
208+ fi
209+ rm -rf build && rm -rf build_out
210+ echo "==============================build utest end===================================="
211+fi
212+ 
213+rm -f run_test.log
214+if [[ "$ENABLE_SMOKE" == "true" ]]; then
215+ echo "==============================build single op===================================="
216+ SINGLE_FILE="single.tar.gz"
217+ need_check_example="false"
218+ rm -rf single/*
219+ echo "exec cmd: [bash scripts/ci/check_pkg.sh $PR_FILELIST -j${THREAD_NUM}]"
220+ bash scripts/ci/check_pkg.sh "$PR_FILELIST" -j${THREAD_NUM}
221+ if [[ -f "$SINGLE_FILE" && -s "$SINGLE_FILE" ]]; then
222+ need_check_example="true"
223+ rm single.tar.gz
224+ fi
225+ rm -rf build && rm -rf build_out
226+ 
227+ echo "==============================compile ascend950==================================="
228+ # 获取机器架构
229+ ARCH=$(uname -m)
230+ # 判断是 x86 还是 ARM
231+ if [[ "$ARCH" == "x86_64" ]]; then
232+ echo "exec cmd: [bash scripts/ci/compile_ascend950_pkg.sh $PR_FILELIST -j${THREAD_NUM}]"
233+ bash scripts/ci/compile_ascend950_pkg.sh $PR_FILELIST -j${THREAD_NUM}
234+ elif [[ "$ARCH" == "aarch64" ]]; then
235+ echo "exec cmd: [bash scripts/ci/compile_ascend950_pkg.sh $PR_FILELIST -force_jit -j${THREAD_NUM}]"
236+ bash scripts/ci/compile_ascend950_pkg.sh $PR_FILELIST -force_jit -j${THREAD_NUM}
237+ else
238+ echo "[ERROR] Unsupported architecture: $ARCH"
239+ exit 1
240+ fi
241+ check_res=$?
242+ if [[ $check_res -ne 0 ]]; then
243+ echo "[ERROR] compile ascend950 failed"
244+ exit $check_res
245+ fi
246+ rm -rf build && rm -rf build_out
247+ 
248+ if ! asys info -r=status 2>/dev/null | grep -q "Ascend 910B"; then
249+ echo "[Warning] The current platform does not support smoke tests, skipping A2 smoke"
250+ else
251+ echo "==============================build A2 smoke==================================="
252+ if [[ ${need_check_example} == "true" ]]; then
253+ # 执行受影响的算子
254+ echo "exec cmd: [bash scripts/ci/check_example.sh $PR_FILELIST]"
255+ bash scripts/ci/check_example.sh $PR_FILELIST 2>&1 | tee -a ./run_test.log
256+ if grep -w -e "FAIL" -e "errors" -e "fail" -e "failed" -e "error" -e "ERROR:" -e "Error" -e "error:" "./run_test.log"; then
257+ echo "[ERROR] run test case failed"
258+ exit 1
259+ fi
260+ fi
261+ fi
262+fi
263+ 
264+echo "==============================check experimental===================================="
265+rm -rf build && rm -rf build_out
266+echo "exec cmd: [bash scripts/ci/check_experimental_pkg.sh $PR_FILELIST]"
267+bash scripts/ci/check_experimental_pkg.sh "$PR_FILELIST"
268+ 
269+if [ -f "${workspace_dir}/build_out/"*.run ]; then
270+ compile_package_name=$(get_pkg_name)
271+ if [[ -z "${compile_package_name}" ]]; then
272+ echo "[ERROR] Not find *.run in ${workspace_dir}/build_out !"
273+ exit 1
274+ fi
275+ chmod +x ./build_out/${compile_package_name}
276+ echo "exec cmd: [bash scripts/ci/check_experimental_example.sh $PR_FILELIST]"
277+ echo 'y' | bash ${compile_package_name} --quiet
278+ bash scripts/ci/check_experimental_example.sh $PR_FILELIST 2>&1 | tee -a ./run_test.log
279+ if grep -w -e "FAIL" -e "errors" -e "fail" -e "failed" -e "error" -e "ERROR:" -e "Error" -e "error:" "./run_test.log"; then
280+ echo "[ERROR] run test case failed"
281+ exit 1
282+ fi
283+fi