#!/bin/bash
set -e
TOP_DIR=""
PKG_PATH=""
RUN_PKG_SAVE_PATH=""
OS_ARCH=$(uname -m)
PKG_NAME=""
SOC=""
OPP_PREFIX="opp"
parse_args() {
local arg
for arg in "$@"; do
case "$arg" in
--soc=*)
SOC="${arg#*=}"
;;
--top_dir=*)
TOP_DIR="${arg#*=}"
;;
--pkg_path=*)
PKG_PATH="${arg#*=}"
;;
--run_pkg_save_path=*)
RUN_PKG_SAVE_PATH="${arg#*=}"
;;
--pkg_name=*)
PKG_NAME="${arg#*=}"
;;
*)
echo "ERROR: Unknown param $arg" >&2
;;
esac
done
}
log() {
echo "$*"
}
die() {
log "ERROR: $*"
exit 1
}
ensure_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
log "Creating directory: $dir"
mkdir -p "$dir" || die "Failed to create directory $dir"
fi
}
ensure_file() {
local file="$1"
[[ -f "$file" ]] || die "Required file not found: $file"
}
log "Starting ops packaging process..."
parse_args "$@"
WORKDIR=${TOP_DIR}/${PKG_PATH}
TEMP_RUN_DIR="./run_files"
HOST_RUN_NAME="host.run"
HOST_EXTRACT_DIR="host"
MAKESELF_TARGET_DIR="build/makeself"
RUNFILE_TARGET_DIR="build/_CPack_Packages/makeself_staging"
PACKAGE_SCRIPT="${WORKDIR}/scripts/package/package.py"
MERGE_SCRIPT="${WORKDIR}/scripts/package/common/py/merge_binary_info_config.py"
PKG_OUTPUT_DIR="build/_CPack_Packages/makeself_staging"
RUN_PACKAGE_SAVE_AB_PATH=${TOP_DIR}/${RUN_PKG_SAVE_PATH}
ARCHIVE_RUN_DIR="${TOP_DIR}/vendor/hisi/build/delivery/${SOC}/"
cd ${WORKDIR}/ || exit
ensure_dir "$TEMP_RUN_DIR"
cd "$TEMP_RUN_DIR"
log "Working in temporary directory: $(pwd)"
counter=1
find "$RUN_PACKAGE_SAVE_AB_PATH" -name "cann-*custom_operator_group*.run" -type f | while read -r runfile; do
parent_dir=$(basename "$(dirname "$runfile")")
new_name="kernel_${parent_dir}_${counter}.run"
cp -v "$runfile" "./$new_name" || die "Failed to copy $runfile"
log "Copied $runfile -> $new_name"
((counter++))
done
ensure_dir "../$MAKESELF_TARGET_DIR"
cp -rf "$TOP_DIR"/open_source/makeself/* "../$MAKESELF_TARGET_DIR" || die "Failed to copy $MAKESELF_TARGET_DIR"
[[ -n "$(find . -name 'kernel_*.run' -type f 2>/dev/null)" ]] || \
die "No .run files found in $RUN_PACKAGE_SAVE_AB_PATH"
cd $RUN_PACKAGE_SAVE_AB_PATH
host_file_name=$(find . -type f -name "*.run" | grep -v "custom")
host_file_name="${host_file_name#./}"
cd "${WORKDIR}"
cd "${TEMP_RUN_DIR}"
host_run_src="$RUN_PACKAGE_SAVE_AB_PATH/$host_file_name"
if [ -e $host_run_src ]; then
cp -v $host_run_src "./$HOST_RUN_NAME" || die "Failed to copy host run file"
log "Copied host run file: $host_run_src -> $HOST_RUN_NAME"
else
log "Host run file not found: $host_run_src"
fi
if [[ ! -x "./$HOST_RUN_NAME" ]]; then
chmod +x "./$HOST_RUN_NAME" || die "Cannot make $HOST_RUN_NAME executable"
fi
log "Extracting $HOST_RUN_NAME to ./$HOST_EXTRACT_DIR"
"./$HOST_RUN_NAME" --extract="$HOST_EXTRACT_DIR" --noexec || \
die "Failed to extract $HOST_RUN_NAME"
rm -f "./$HOST_RUN_NAME"
log "Removed $HOST_RUN_NAME after extraction"
ensure_dir "./$HOST_EXTRACT_DIR"
first_run=true
shopt -s nullglob
for runfile in kernel_*.run; do
[[ -f "$runfile" ]] || continue
if [[ ! -x "$runfile" ]]; then
chmod +x "$runfile" || die "Cannot make $runfile executable"
fi
base_name="${runfile%.run}"
extract_dir="./$base_name"
log "Processing $runfile -> extracting to $extract_dir"
"./$runfile" --extract="$extract_dir" --noexec || \
die "Failed to extract $runfile"
PARENT_DIR="${extract_dir}/packages/vendors"
full_path=$(find "$PARENT_DIR" -maxdepth 1 -type d -name "custom_*math" | head -n 1)
if [ -n "$full_path" ]; then
kernel_dir_name=$(basename "$full_path")
echo "kernel_dir_name is $target_dir_name"
else
echo "Not find kernel_dir"
continue
fi
kernel_src_dir=$extract_dir/packages/vendors/$kernel_dir_name/op_impl/ai_core/tbe/kernel/${SOC}
config_src_dir=$extract_dir/packages/vendors/$kernel_dir_name/op_impl/ai_core/tbe/kernel/config/${SOC}
[[ -d $kernel_src_dir ]] || log "Kernel source dir not found: "$kernel_src_dir
[[ -d $config_src_dir ]] || log "Config source dir not found: "$config_src_dir
if [[ ! -d $kernel_src_dir || ! -d "$config_src_dir" ]]; then
continue
fi
ascend_dir=$SOC
if $first_run; then
target_kernel_dir="$HOST_EXTRACT_DIR/$OPP_PREFIX/built-in/op_impl/ai_core/tbe/kernel/$SOC/$PKG_NAME/"
ensure_dir $target_kernel_dir
cp -rf "$kernel_src_dir"/* "./$target_kernel_dir" || \
die "Failed to copy first kernel files"
log "First run: copied full kernel to $target_kernel_dir"
dest_conf_ascend_first="$HOST_EXTRACT_DIR/$OPP_PREFIX/built-in/op_impl/ai_core/tbe/kernel/config/$SOC/ops_math"
ensure_dir $dest_conf_ascend_first
cp -v $config_src_dir/* $dest_conf_ascend_first/
first_run=false
else
dest_kern_ascend="$HOST_EXTRACT_DIR/$OPP_PREFIX/built-in/op_impl/ai_core/tbe/kernel/$SOC/$PKG_NAME"
ensure_dir "$dest_kern_ascend"
cp -rf "$kernel_src_dir"/* "$dest_kern_ascend"/ || \
die "Failed to copy kernel ascend files"
src_conf_ascend=$config_src_dir
dest_conf_ascend="$HOST_EXTRACT_DIR/$OPP_PREFIX/built-in/op_impl/ai_core/tbe/kernel/config/$SOC/ops_math"
for json_file in "$src_conf_ascend"/*.json; do
[[ -f "$json_file" ]] || continue
json_basename=$(basename "$json_file")
if [[ "$json_basename" == "binary_info_config.json" ]]; then
target_json="$dest_conf_ascend/binary_info_config.json"
log "Executing merge_binary_info_config.py to merge final package..."
python3 "$MERGE_SCRIPT" \
--base-file=$json_file \
--update-file=$target_json \
--output-file=binary_info_config.json
mv -f binary_info_config.json $target_json
chmod 550 "$target_json"
else
cp -f "$json_file" "$dest_conf_ascend"/ || \
die "Warning: failed to copy $json_file"
fi
done
fi
done
filelist_src_path="$HOST_EXTRACT_DIR/share/info/ops_math/script/filelist.csv"
rm $filelist_src_path
ensure_dir "../$RUNFILE_TARGET_DIR"
cp -rf "$HOST_EXTRACT_DIR"/* "../$RUNFILE_TARGET_DIR"/ || \
die "Failed to copy host content to makeself directory"
log "Host content copied to $RUNFILE_TARGET_DIR"
cd "../" || echo "Failed to go back to workdir"
log "Executing package.py to generate final package..."
python3 "$PACKAGE_SCRIPT" \
--pkg_name "$PKG_NAME" \
--makeself_dir "${WORKDIR}/$MAKESELF_TARGET_DIR" \
--pkg-output-dir "${WORKDIR}/$PKG_OUTPUT_DIR" \
--independent_pkg \
--chip_name "$SOC" \
--os_arch linux-"$OS_ARCH"
log "Packaging completed successfully!"
ensure_dir "$ARCHIVE_RUN_DIR"
cp -r "$PKG_OUTPUT_DIR"/*.run $ARCHIVE_RUN_DIR