#!/bin/bash
declare -a ALL_INCLUDE_FILES=()
declare -a TODO_QUEUE=()
TARGET_H_FILENAME=""
INCLUDE_TYPES=("--include=*.c" "--include=*.h" "--include=*.cpp" "--include=*.CPP" "--include=*.C" "--include=*.H")
run_build_command() {
local cmd=$1
echo "$cmd"
if ! eval "$cmd"; then
echo "build pkg error"
exit 1
fi
}
execute_run_file() {
local pkg_type=$1
local run_files=(./build_out/*.run)
if [ ! -f "${run_files[0]}" ];then
echo "no run pkg found"
return 1
fi
chmod +x "${run_files[0]}"
cmd=""${run_files[0]}" --install-path="/tmp""
if [ $pkg_type == "builtin" ];then
cmd+=" --full"
else
cmd+=" --force"
fi
echo $cmd
if ! eval "$cmd"; then
echo "execute pkg error"
exit 1
fi
}
is_in_array() {
local elem="$1"
local arr_name="$2"
local -n arr="$arr_name"
for item in "${arr[@]}"; do
if [[ "$item" == "$elem" ]]; then
return 0
fi
done
return 1
}
recursive_find_includes() {
local match_filename=$1
local category_path=$2
local new_files=($(grep -rl "${INCLUDE_TYPES[@]}" "#include.*"$match_filename"" $category_path | sort -u))
for file in "${new_files[@]}"; do
local abs_file=$(realpath --relative-to=$(pwd) "$file")
if ! is_in_array "$abs_file" "ALL_INCLUDE_FILES"; then
ALL_INCLUDE_FILES+=("$abs_file")
TODO_QUEUE+=("$abs_file")
fi
done
if [[ ${#new_files[@]} -eq 0 ]]; then
return
fi
local current_file
while [[ ${#TODO_QUEUE[@]} -gt 0 ]]; do
current_file="${TODO_QUEUE[0]}"
TODO_QUEUE=("${TODO_QUEUE[@]:1}")
local curr_filename=$(basename "$current_file")
recursive_find_includes "$curr_filename" "$category_path"
done
}
set -euo pipefail
current_dir=$(pwd)
variables_cmake="cmake/variables.cmake"
op_category_list=$(
perl -0777 -ne '
if (/set\(OP_CATEGORY_LIST\s*(.*?)\)/s) {
print $1
}' \
"$current_dir/$variables_cmake" \
| grep -oP '"[^"]+"' \
| sed 's/"//g' \
| xargs
)
IFS=' ' read -r -a op_categories <<< "$op_category_list"
builtin_dirs=()
experimental_dirs=()
force_jit="false"
force=""
pr_file="pr_filelist.txt"
THREAD_NUM="-j16"
while [[ $# -gt 0 ]]; do
case "$1" in
-force_jit)
force_jit="true"
if [[ ${2+x} && -n "$2" && "$2" != --* ]]; then
shift
fi
shift
;;
-pr_file)
if [[ ${2+x} && -n "$2" && "$2" != --* ]]; then
pr_file="$2"
shift
else
echo "-pr_file use default value: $pr_file"
fi
shift
;;
--no_force)
force="--no_force"
shift
;;
-j)
if [[ ${2+x} && -n "$2" && "$2" != --* ]]; then
THREAD_NUM="-j$2"
shift
else
echo "-j use default value: 16"
fi
shift
;;
-j*)
THREAD_NUM="$1"
shift
;;
*)
shift
;;
esac
done
for category in "${op_categories[@]}"
do
builtin_category_path="$current_dir/$category"
if [ -d "$builtin_category_path" ]; then
for dir in "$builtin_category_path"/*/
do
dir_name=$(basename "$dir")
if [[ "$dir_name" != *"common"* && "$dir_name" != *"utils"* ]]; then
builtin_dirs+=("$dir_name")
fi
done
fi
exper_category_path="$current_dir/experimental/$category"
if [ -d "$exper_category_path" ]; then
for dir in "$exper_category_path"/*/
do
dir_name=$(basename "$dir")
if [[ "$dir_name" != *"common"* && "$dir_name" != *"utils"* ]]; then
experimental_dirs+=("$dir_name")
fi
done
fi
done
builtin_ops_name=()
experimental_ops_name=()
build_all=${build_all:-"false"}
mapfile -t lines < ${pr_file}
for file_path in "${lines[@]}"
do
file_path=$(echo "$file_path" | xargs)
if [ -z "$file_path" ]; then
continue
fi
if [[ "$file_path" == *.md ]]; then
continue
fi
for category in "${op_categories[@]}"
do
REGEX_COMMON="^$category/(.*/)?[^/]*common[^/]*/.*\.[hH]$"
REGEX_UTIL="^$category/(.*/)?[^/]*utils[^/]*/.*\.[hH]$"
if [[ "$file_path" =~ $REGEX_COMMON || "$file_path" =~ $REGEX_UTIL ]]; then
file_name=$(basename "$file_path")
recursive_find_includes "$file_name" "$current_dir/$category"
break
fi
done
done
for file_path in "${ALL_INCLUDE_FILES[@]}"; do
if ! is_in_array "$file_path" "lines"; then
lines+=("$file_path")
fi
done
for file_path in "${lines[@]}"
do
file_path=$(echo "$file_path" | xargs)
if [ -z "$file_path" ]; then
continue
fi
if [[ "$file_path" == *.md ]]; then
continue
fi
for dir in "${builtin_dirs[@]}"
do
if [[ "$file_path" == *"/$dir/"*"/arch35/"* && "$file_path" != "experimental"* ]]; then
if [[ ! " ${builtin_ops_name[@]} " =~ " $dir " ]]; then
builtin_ops_name+=("$dir")
fi
break
fi
done
for dir in "${experimental_dirs[@]}"
do
if [[ "$file_path" == "experimental/"*"/$dir/"*"/arch35/"* ]]; then
if [[ ! " ${experimental_ops_name[@]} " =~ " $dir " ]]; then
experimental_ops_name+=("$dir")
fi
break
fi
done
if [[ "$file_path" == "common/"* || "$file_path" == "cmake/"* ]]; then
build_all="true"
fi
done
echo "related op: ${builtin_ops_name[*]}"
echo "related experimental op: ${experimental_ops_name[*]}"
echo "need build all: ${build_all}"
a5_soc="ascend950"
if [[ ${#builtin_ops_name[@]} -gt 0 && "$force_jit" = "false" ]]; then
builtin_ops_str=$(IFS=,; echo "${builtin_ops_name[*]}")
build_cmd="bash build.sh --pkg --ops=$builtin_ops_str --soc=$a5_soc ${THREAD_NUM} --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} ${force}"
run_build_command "$build_cmd"
execute_run_file "custom"
fi
if [[ ${#experimental_ops_name[@]} -gt 0 && "$force_jit" = "false" ]]; then
experimental_ops_str=$(IFS=,; echo "${experimental_ops_name[*]}")
build_cmd="bash build.sh --pkg --experimental --ops=$experimental_ops_str --soc=$a5_soc ${THREAD_NUM} --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH} ${force}"
run_build_command "$build_cmd"
execute_run_file "custom"
fi
if [ "${build_all}" == "true" ]; then
build_cmd="bash build.sh --pkg --jit --soc=$a5_soc ${THREAD_NUM} --cann_3rd_lib_path=${ASCEND_3RD_LIB_PATH}"
run_build_command "$build_cmd"
execute_run_file "builtin"
fi
exit 0