#!/bin/bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
TOOL_NAME="memscope"
ARCH=$(uname -m)
VERSION="26.0.0"
RUN_FILE="mindstudio-memscope_${VERSION}_${ARCH}.run"
BUILD_DIR="$(cd "$(dirname "$0")" && pwd)"
TEMP_DIR="/tmp/${TOOL_NAME}_build_$$"
PYTHON_DIR="../python"
BIN_DIR="../output/bin"
LIB64_DIR="../output/lib64"
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_debug() {
echo -e "${BLUE}[DEBUG]${NC} $1"
}
check_source_dirs() {
local missing_dirs=()
[ ! -d "$PYTHON_DIR" ] && missing_dirs+=("$PYTHON_DIR")
[ ! -d "$BIN_DIR" ] && missing_dirs+=("$BIN_DIR")
[ ! -d "$LIB64_DIR" ] && missing_dirs+=("$LIB64_DIR")
if [ ${#missing_dirs[@]} -gt 0 ]; then
log_error "The following source directories are missing:"
for dir in "${missing_dirs[@]}"; do
echo " $dir"
done
return 1
fi
return 0
}
create_temp_dir() {
mkdir -p "$TEMP_DIR"
mkdir -p "$TEMP_DIR/payload"
}
generate_file_hashes() {
local payload_dir="$TEMP_DIR/payload/msmemscope"
local hash_file="$payload_dir/file_checksums.sha256"
log_info "Generating file checksums..."
if ! command -v sha256sum >/dev/null 2>&1; then
log_warn "sha256sum not available, skipping checksum generation"
return 0
fi
cd "$payload_dir"
find . -type f ! -name "file_checksums.sha256" ! -name "version.txt" -exec sha256sum {} \; > "$hash_file"
local overall_hash=$(sha256sum "$hash_file" | cut -d' ' -f1)
echo "build_hash: $overall_hash" >> "version.txt"
echo "integrity_check: enabled" >> "version.txt"
cd - > /dev/null
log_info "File checksums generated with overall hash: ${overall_hash:0:16}..."
}
copy_artifacts() {
log_info "Copying artifacts to temporary directory..."
mkdir -p "$TEMP_DIR/payload/msmemscope"
if command -v rsync >/dev/null 2>&1; then
rsync -av "$PYTHON_DIR/" "$TEMP_DIR/payload/msmemscope/python/" --exclude="*.pyc" --exclude="__pycache__"
rsync -av "$BIN_DIR/" "$TEMP_DIR/payload/msmemscope/bin/"
rsync -av "$LIB64_DIR/" "$TEMP_DIR/payload/msmemscope/lib64/"
else
mkdir -p "$TEMP_DIR/payload/msmemscope"
cp -r "$PYTHON_DIR" "$TEMP_DIR/payload/msmemscope/"
cp -r "$BIN_DIR" "$TEMP_DIR/payload/msmemscope/"
cp -r "$LIB64_DIR" "$TEMP_DIR/payload/msmemscope/"
fi
log_info "Copying _msmemscope.so to python directory..."
if [ -f "$TEMP_DIR/payload/msmemscope/lib64/_msmemscope.so" ]; then
cp "$TEMP_DIR/payload/msmemscope/lib64/_msmemscope.so" "$TEMP_DIR/payload/msmemscope/python/msmemscope/"
log_info "Copied _msmemscope.so to python/msmemscope/"
else
log_warn "_msmemscope.so not found in lib64 directory"
fi
echo "version: $VERSION" > "$TEMP_DIR/payload/msmemscope/version.txt"
echo "build_date: $(date '+%Y-%m-%d %H:%M:%S')" >> "$TEMP_DIR/payload/msmemscope/version.txt"
generate_file_hashes
}
create_install_script() {
cat > "$TEMP_DIR/install.sh" << 'EOF'
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
TOOL_NAME="msmemscope"
DEFAULT_INSTALL_PATH="."
VERSION="VERSION_PLACEHOLDER"
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_debug() {
echo -e "${BLUE}[DEBUG]${NC} $1"
}
verify_installation_integrity() {
local install_path="$1"
local install_dir="$install_path/msmemscope"
local checksum_file="$install_dir/file_checksums.sha256"
log_info "Verifying installation integrity..."
if [ ! -f "$checksum_file" ]; then
log_warn "Checksum file not found, skipping integrity verification"
return 0
fi
if ! command -v sha256sum >/dev/null 2>&1; then
log_warn "sha256sum not available, skipping integrity verification"
return 0
fi
cd "$install_dir"
if sha256sum -c file_checksums.sha256 --quiet > /dev/null 2>&1; then
log_info "✓ All files passed integrity verification"
cd - > /dev/null
return 0
else
log_error "✗ Integrity verification failed! Some files are corrupted or modified."
log_error "Run 'sha256sum -c file_checksums.sha256' for details."
cd - > /dev/null
return 1
fi
}
check_disk_space() {
local install_path="$1"
local required_space=100
local available_space=$(df -m "$install_path" | awk 'NR==2 {print $4}')
if [ "$available_space" -lt "$required_space" ]; then
log_error "Insufficient disk space! Required: ${required_space}MB, Available: ${available_space}MB"
return 1
fi
log_info "Disk space check passed: ${available_space}MB available"
return 0
}
validate_install_path() {
local install_path="$1"
if [ -z "$install_path" ]; then
log_error "Install path cannot be empty"
return 1
fi
if [[ "$install_path" != /* ]]; then
log_error "Install path must be an absolute path: $install_path"
return 1
fi
if [ -e "$install_path" ]; then
if [ ! -d "$install_path" ]; then
log_error "Install path exists but is not a directory: $install_path"
return 1
fi
else
log_error "Install directory does not exist: $install_path"
return 1
fi
if [ ! -w "$install_path" ]; then
log_error "No write permission for directory: $install_path"
return 1
fi
if [[ "$install_path" =~ [\|\<\>\"\'] ]]; then
log_error "Install path contains invalid characters"
return 1
fi
return 0
}
set_file_permissions() {
local install_dir="$1/msmemscope"
chmod 750 "$install_dir"
find "$install_dir" -mindepth 1 -type d -exec chmod 750 {} \;
if [ -d "$install_dir/bin" ]; then
find "$install_dir/bin" -type f -exec chmod 750 {} \;
fi
if [ -d "$install_dir/lib64" ]; then
find "$install_dir/lib64" -name "*.so" -type f -exec chmod 640 {} \;
fi
if [ -d "$install_dir/python/msmemscope" ]; then
find "$install_dir/python/msmemscope" -name "*.py" -type f -exec chmod 640 {} \;
fi
local set_env_script="$install_dir/set_env.sh"
local uninstall_script="$install_dir/uninstall.sh"
local version_file="$install_dir/version.txt"
[ -f "$set_env_script" ] && chmod 750 "$set_env_script"
[ -f "$uninstall_script" ] && chmod 750 "$uninstall_script"
[ -f "$version_file" ] && chmod 640 "$version_file"
}
create_set_env_script() {
local install_path="$1"
local set_env_script="$install_path/msmemscope/set_env.sh"
log_info "Creating environment setup script..."
cat > "$set_env_script" << 'SETENV_EOF'
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
echo "Setting up msmemscope environment..."
if [ -d "$SCRIPT_DIR/python" ]; then
local new_pythonpath=""
if [ -n "$PYTHONPATH" ]; then
IFS=':' read -ra paths <<< "$PYTHONPATH"
for path in "${paths[@]}"; do
if [[ "$path" != *"msmemscope"* ]]; then
if [ -z "$new_pythonpath" ]; then
new_pythonpath="$path"
else
new_pythonpath="$new_pythonpath:$path"
fi
fi
done
fi
if [ -z "$new_pythonpath" ]; then
export PYTHONPATH="$SCRIPT_DIR/python"
else
export PYTHONPATH="$SCRIPT_DIR/python:$new_pythonpath"
fi
echo "✓ Added to PYTHONPATH (forced to front): $SCRIPT_DIR/python"
fi
if [ -d "$SCRIPT_DIR/bin" ]; then
local new_path=""
if [ -n "$PATH" ]; then
IFS=':' read -ra paths <<< "$PATH"
for path in "${paths[@]}"; do
if [[ "$path" != *"msmemscope"* ]]; then
if [ -z "$new_path" ]; then
new_path="$path"
else
new_path="$new_path:$path"
fi
fi
done
fi
if [ -z "$new_path" ]; then
export PATH="$SCRIPT_DIR/bin"
else
export PATH="$SCRIPT_DIR/bin:$new_path"
fi
echo "✓ Added to PATH (forced to front): $SCRIPT_DIR/bin"
fi
echo "msmemscope environment setup completed"
echo ""
echo "Environment verification:"
echo "PYTHONPATH starts with: $(echo $PYTHONPATH | cut -d: -f1)"
echo "PATH starts with: $(echo $PATH | cut -d: -f1)"
SETENV_EOF
log_info "Environment setup script created: $set_env_script"
}
perform_installation() {
local install_path="$1"
local is_upgrade="$2"
local action="${is_upgrade:-Installing}"
log_info "${action} to: $install_path/msmemscope"
mkdir -p "$install_path"
log_info "Extracting files..."
PAYLOAD_START=$(awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' "$0")
local write_flag=false
if [ ! -w "$install_path" ]; then
log_info "Installation directory not writable, temporarily adding write permission"
chmod u+w "$install_path"
write_flag=true
fi
tail -n +$PAYLOAD_START "$0" | tar -xz -C "$install_path"
if [ "$write_flag" = true ]; then
log_info "Restoring installation directory permissions"
chmod u-w "$install_path"
fi
if ! verify_installation_integrity "$install_path"; then
log_error "Installation integrity check failed"
exit 1
fi
set_file_permissions "$install_path"
create_set_env_script "$install_path"
local cann_uninstall_path="$(dirname "$install_path")/cann_uninstall.sh"
if [ -f "$cann_uninstall_path" ]; then
log_info "Found cann_uninstall.sh at $cann_uninstall_path, configuring integration..."
local script_right=$(stat -c '%a' "$cann_uninstall_path")
chmod u+w "$cann_uninstall_path"
sed -i "/uninstall_package \"share\/info\/msmemscope\"/d" "$cann_uninstall_path"
sed -i "/^exit /i uninstall_package \"share\/info\/msmemscope\"" "$cann_uninstall_path"
chmod $script_right "$cann_uninstall_path"
log_info "Registered uninstallation logic to cann_uninstall.sh"
local base_dir=$(dirname "$cann_uninstall_path")
local share_info_dir="$base_dir/share/info/msmemscope"
mkdir -p "$share_info_dir"
log_info "Created directory: $share_info_dir"
cat > "$share_info_dir/version.info" << VERSION_EOF
[PACKAGE]
Name=mindstudio-memscope
Version=$VERSION
VERSION_EOF
log_info "Created version.info in $share_info_dir"
cat > "$share_info_dir/uninstall.sh" << 'CANN_UNINSTALL_EOF'
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
CANN_INSTALL_DIR=../../..
MEMSCOPE_INSTALL_DIR="$CANN_INSTALL_DIR/tools/msmemscope"
log_info "Uninstalling msmemscope from $MEMSCOPE_INSTALL_DIR"
if [ -d "$MEMSCOPE_INSTALL_DIR" ]; then
MEMSCOPE_PARENT_DIR=$(dirname "$MEMSCOPE_INSTALL_DIR")
MEMSCOPE_PARENT_PERM=$(stat -c '%a' "$MEMSCOPE_PARENT_DIR" 2>/dev/null || echo "")
if [ -n "$MEMSCOPE_PARENT_PERM" ] && [ ! -w "$MEMSCOPE_PARENT_DIR" ]; then
chmod u+w "$MEMSCOPE_PARENT_DIR"
fi
rm -rf "$MEMSCOPE_INSTALL_DIR"
if [ -n "$MEMSCOPE_PARENT_PERM" ] && [ ! -w "$MEMSCOPE_PARENT_DIR" ] 2>/dev/null; then
chmod "$MEMSCOPE_PARENT_PERM" "$MEMSCOPE_PARENT_DIR"
fi
log_info "Removed msmemscope installation directory"
fi
INSTALL_PARENT_DIR="$CANN_INSTALL_DIR"
if [ -f "$INSTALL_PARENT_DIR/cann_uninstall.sh" ]; then
CANN_UNINSTALL_PERM=$(stat -c '%a' "$INSTALL_PARENT_DIR/cann_uninstall.sh" 2>/dev/null || echo "")
if [ -n "$CANN_UNINSTALL_PERM" ] && [ ! -w "$INSTALL_PARENT_DIR/cann_uninstall.sh" ]; then
chmod u+w "$INSTALL_PARENT_DIR/cann_uninstall.sh"
fi
sed -i "/uninstall_package \"share\/info\/msmemscope\"/d" "$INSTALL_PARENT_DIR/cann_uninstall.sh"
if [ -n "$CANN_UNINSTALL_PERM" ]; then
chmod "$CANN_UNINSTALL_PERM" "$INSTALL_PARENT_DIR/cann_uninstall.sh"
fi
log_info "Removed uninstallation registration from cann_uninstall.sh"
fi
share_info_dir="$INSTALL_PARENT_DIR/share/info/msmemscope"
if [ -d "$share_info_dir" ]; then
SHARE_INFO_PARENT_DIR=$(dirname "$share_info_dir")
SHARE_INFO_PARENT_PERM=$(stat -c '%a' "$SHARE_INFO_PARENT_DIR" 2>/dev/null || echo "")
if [ -n "$SHARE_INFO_PARENT_PERM" ] && [ ! -w "$SHARE_INFO_PARENT_DIR" ]; then
chmod u+w "$SHARE_INFO_PARENT_DIR"
fi
rm -rf "$share_info_dir"
if [ -n "$SHARE_INFO_PARENT_PERM" ] && [ ! -w "$SHARE_INFO_PARENT_DIR" ] 2>/dev/null; then
chmod "$SHARE_INFO_PARENT_PERM" "$SHARE_INFO_PARENT_DIR"
fi
log_info "Removed share/info/msmemscope directory"
fi
log_info "msmemscope uninstallation completed successfully"
CANN_UNINSTALL_EOF
chmod +x "$share_info_dir/uninstall.sh"
log_info "Created uninstall.sh in $share_info_dir"
fi
log_info "File ${is_upgrade:-installation} completed"
}
create_uninstall_script() {
local install_path="$1"
cat > "$install_path/msmemscope/uninstall.sh" << 'UNINSTALL_EOF'
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
if [ -n "${BASH_SOURCE[0]}" ]; then
SCRIPT_PATH="${BASH_SOURCE[0]}"
else
SCRIPT_PATH="$0"
fi
INSTALL_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
check_installation_integrity() {
if [ ! -f "$INSTALL_DIR/version.txt" ]; then
log_error "Installation directory is incomplete or corrupted"
return 1
fi
local required_dirs=("bin" "python" "lib64")
for dir in "${required_dirs[@]}"; do
if [ ! -d "$INSTALL_DIR/$dir" ]; then
log_error "Missing required directory: $dir"
return 1
fi
done
return 0
}
confirm_uninstall() {
echo "=============================================="
echo " Uninstall msmemscope"
echo "=============================================="
echo "Installation directory: $INSTALL_DIR"
echo "Version: $(grep 'version:' $INSTALL_DIR/version.txt 2>/dev/null | cut -d' ' -f2 || echo 'Unknown')"
echo ""
echo -e "${YELLOW}Warning: This operation will permanently delete ALL files and subdirectories${NC}"
echo -e "${YELLOW}within the installation directory, including any user-created content!${NC}"
echo ""
echo -e "${RED}The following directory and ALL its contents will be deleted:${NC}"
echo -e "${RED} $INSTALL_DIR${NC}"
echo ""
read -p "Are you sure you want to uninstall? (y/N): " confirm
case "$confirm" in
[yY]|[yY][eE][sS])
return 0
;;
*)
echo "Uninstall cancelled"
exit 0
;;
esac
}
check_running_processes() {
log_info "Checking for processes using the installation directory..."
if command -v lsof >/dev/null 2>&1; then
if lsof +D "$INSTALL_DIR" 2>/dev/null | grep -q "."; then
log_warn "Found processes using the installation directory:"
lsof +D "$INSTALL_DIR" 2>/dev/null | head -10
return 1
fi
fi
if command -v fuser >/dev/null 2>&1; then
if fuser "$INSTALL_DIR" 2>/dev/null; then
log_warn "Found processes using the installation directory"
return 1
fi
fi
return 0
}
cleanup_environment_variables() {
local install_dir="$INSTALL_DIR"
if [ -n "$PYTHONPATH" ]; then
local new_pythonpath=""
IFS=':' read -ra paths <<< "$PYTHONPATH"
for path in "${paths[@]}"; do
if [[ "$path" != *"$install_dir"* ]]; then
if [ -z "$new_pythonpath" ]; then
new_pythonpath="$path"
else
new_pythonpath="$new_pythonpath:$path"
fi
fi
done
if [ -z "$new_pythonpath" ]; then
unset PYTHONPATH
log_info "PYTHONPATH unset"
else
export PYTHONPATH="$new_pythonpath"
log_info "PYTHONPATH cleaned"
fi
fi
if [ -n "$PATH" ]; then
local new_path=""
IFS=':' read -ra paths <<< "$PATH"
for path in "${paths[@]}"; do
if [[ "$path" != *"$install_dir"* ]]; then
if [ -z "$new_path" ]; then
new_path="$path"
else
new_path="$new_path:$path"
fi
fi
done
export PATH="$new_path"
log_info "PATH cleaned"
fi
log_info "Environment variables cleanup completed"
}
perform_uninstall() {
log_info "Starting uninstallation..."
if ! check_running_processes; then
log_warn "Processes are using the installation directory"
read -p "Force uninstall anyway? (y/N): " force_uninstall
case "$force_uninstall" in
[yY]|[yY][eE][sS])
log_warn "Forcing uninstall..."
;;
*)
echo "Uninstall cancelled"
exit 1
;;
esac
fi
log_info "Cleaning up environment variables..."
cleanup_environment_variables
log_info "Removing installation directory: $INSTALL_DIR"
rm -rf "$INSTALL_DIR"
log_info "Uninstallation completed successfully"
}
main() {
echo "=============================================="
echo " msmemscope Uninstaller"
echo "=============================================="
if ! check_installation_integrity; then
log_error "Installation directory is incomplete, manual cleanup may be required"
exit 1
fi
confirm_uninstall
perform_uninstall
}
main "$@"
UNINSTALL_EOF
log_info "Uninstall script created: $install_path/msmemscope/uninstall.sh"
}
show_installation_info() {
local install_path="$1"
local is_upgrade="$2"
local normalized_path=$(cd "$install_path" && pwd)
local action="${is_upgrade:-Installation}"
echo ""
echo "=============================================="
echo " $TOOL_NAME ${action} Complete"
echo "=============================================="
echo "Installation path: $normalized_path/msmemscope"
echo "Version: $(grep 'version:' "$install_path/msmemscope/version.txt" | cut -d' ' -f2)"
echo "Integrity: $(grep 'integrity_check:' "$install_path/msmemscope/version.txt" | cut -d' ' -f2 || echo 'unknown')"
echo "Install time: $(date '+%Y-%m-%d %H:%M:%S')"
echo ""
echo "Environment setup:"
echo " To set up environment variables, run: source $normalized_path/msmemscope/set_env.sh"
echo ""
if [ -n "$is_upgrade" ]; then
log_info "Upgrade completed successfully"
else
log_info "Installation completed successfully"
fi
}
install_main() {
local install_path=""
local is_upgrade=false
while [[ $# -gt 0 ]]; do
case $1 in
--install-path=*)
install_path="${1#*=}"
shift
;;
--upgrade)
is_upgrade=true
shift
;;
*)
log_warn "Unknown parameter: $1"
shift
;;
esac
done
log_info "Starting installation to: $install_path"
if [ -z "$install_path" ]; then
install_path="$DEFAULT_INSTALL_PATH"
if [[ "$install_path" != /* ]]; then
install_path="$(pwd)/$install_path"
fi
install_path=$(cd "$install_path" && pwd)
log_info "Using installation path: $install_path"
fi
validate_install_path "$install_path" || exit 1
if [ -f "$install_path/cann_uninstall.sh" ]; then
log_info "Found cann_uninstall.sh at $install_path, appending '/tools' to installation path"
install_path="$install_path/tools"
log_info "Updated installation path: $install_path"
fi
check_disk_space "$install_path" || exit 1
if [ -d "$install_path/msmemscope" ] && [ -f "$install_path/msmemscope/version.txt" ]; then
if [ "$is_upgrade" = true ]; then
log_info "Starting upgrade process..."
else
log_warn "Existing installation detected at: $install_path/msmemscope";
log_info "Current version: $(cat "$install_path/msmemscope/version.txt" 2>/dev/null || echo "unknown")";
log_info "Automatically upgrading to version: $VERSION";
log_info "Starting upgrade process..."
is_upgrade=true
fi
elif [ "$is_upgrade" = true ]; then
log_error "Target directory is not a valid installation for upgrade.";
log_error "A valid installation directory must contain: $install_path/msmemscope/version.txt";
log_error "Please check the installation path and ensure you're pointing to an existing MSMemScope installation."
exit 1
else
log_info "Starting fresh installation..."
fi
perform_installation "$install_path" "$([ "$is_upgrade" = true ] && echo "Upgrading")"
create_uninstall_script "$install_path"
show_installation_info "$install_path" "$([ "$is_upgrade" = true ] && echo "Upgrade")"
}
upgrade_main() {
local install_path=""
while [[ $# -gt 0 ]]; do
case $1 in
--install-path=*)
install_path="${1#*=}"
shift
;;
*)
log_warn "Unknown parameter: $1"
shift
;;
esac
done
if [ -z "$install_path" ]; then
log_error "Upgrade requires --install-path parameter"
echo "Usage: $0 --upgrade --install-path=<path>"
exit 1
fi
if [ ! -d "$install_path" ]; then
log_error "Target installation directory does not exist: $install_path"
exit 1
fi
if [ ! -f "$install_path/msmemscope/version.txt" ]; then
log_error "Target directory is not a valid $TOOL_NAME installation."
log_error "A valid installation directory must contain: $install_path/msmemscope/version.txt"
log_error "Please check the installation path and ensure you're pointing to an existing MSMemScope installation."
exit 1
fi
log_info "Starting upgrade for: $install_path"
install_main --install-path="$install_path" --upgrade
}
show_version() {
echo "=============================================="
echo " $TOOL_NAME Version Info"
echo "=============================================="
if [ -f "version.txt" ]; then
cat "version.txt"
else
local payload_start=$(awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' "$0")
if [ -n "$payload_start" ]; then
tail -n +$payload_start "$0" | tar -xz -O "msmemscope/version.txt" 2>/dev/null || \
echo "Version: $VERSION (build $(date '+%Y%m%d'))"
else
echo "Version: $VERSION (build $(date '+%Y%m%d'))"
fi
fi
echo ""
}
show_help() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --install Install the tool"
echo " --install-path=PATH Specify installation path (must be absolute)"
echo " --upgrade Upgrade an existing installation"
echo " --uninstall Uninstall the tool"
echo " --version Show version information"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " $0 --install # Install to default path"
echo " $0 --install --install-path=/usr/local/msmemscope"
echo " $0 --upgrade --install-path=/opt/msmemscope"
echo " $0 --uninstall --install-path=/opt/msmemscope"
echo " $0 --version # Show version"
echo " $0 --help"
echo ""
}
uninstall_main() {
local install_path=""
while [[ $# -gt 0 ]]; do
case $1 in
--install-path=*)
install_path="${1#*=}"
shift
;;
*)
log_warn "Unknown parameter: $1"
shift
;;
esac
done
if [ -z "$install_path" ]; then
log_error "Uninstall requires --install-path parameter"
echo "Usage: $0 --uninstall --install-path=<path>"
exit 1
fi
if [ -f "$install_path/cann_uninstall.sh" ]; then
log_info "Detected CANN root directory, appending 'tools' to install path"
install_path="$install_path/tools"
fi
if [ ! -d "$install_path" ]; then
log_error "Target installation directory does not exist: $install_path"
exit 1
fi
local uninstall_script="$install_path/msmemscope/uninstall.sh"
if [ ! -f "$uninstall_script" ]; then
log_error "Target directory is not a valid $TOOL_NAME installation."
log_error "A valid installation directory must contain: $uninstall_script"
log_error "Please check the installation path and ensure you're pointing to an existing MSMemScope installation."
exit 1
fi
log_info "Starting uninstallation for: $install_path"
echo -e "y\ny" | bash "$uninstall_script"
local cann_uninstall_path="$install_path/cann_uninstall.sh"
if [ -f "$cann_uninstall_path" ]; then
log_info "Removing uninstall registration from cann_uninstall.sh"
sed -i "/uninstall_package \"share\/info\/msmemscope\"/d" "$cann_uninstall_path"
log_info "Uninstall registration removed from cann_uninstall.sh"
fi
local share_info_dir="$install_path/share/info/msmemscope"
if [ -d "$share_info_dir" ]; then
log_info "Removing share/info/msmemscope directory"
rm -rf "$share_info_dir"
log_info "share/info/msmemscope directory removed"
fi
log_info "Uninstallation completed successfully"
if [ $? -eq 0 ]; then
log_info "Uninstallation completed successfully"
else
log_error "Uninstallation failed"
exit 1
fi
}
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi
case "$1" in
--install)
shift
install_main "$@"
;;
--upgrade)
shift
upgrade_main "$@"
;;
--uninstall)
shift
uninstall_main "$@"
;;
--version|-v)
show_version
;;
--help|-h)
show_help
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
main "$@"
exit 0
__PAYLOAD_BELOW__
EOF
}
create_run_file() {
log_info "Creating run file: $RUN_FILE"
cp "$TEMP_DIR/install.sh" "$BUILD_DIR/$RUN_FILE"
sed -i "s/VERSION_PLACEHOLDER/$VERSION/g" "$BUILD_DIR/$RUN_FILE"
log_info "Packaging payload..."
cd "$TEMP_DIR/payload"
tar -cz . >> "$BUILD_DIR/$RUN_FILE"
cd - > /dev/null
chmod 740 "$BUILD_DIR/$RUN_FILE"
if [ -f "$BUILD_DIR/$RUN_FILE" ] && [ -x "$BUILD_DIR/$RUN_FILE" ]; then
log_info "Run file created successfully: $BUILD_DIR/$RUN_FILE"
else
log_error "Failed to create run file"
return 1
fi
}
cleanup() {
if [ -d "$TEMP_DIR" ]; then
log_info "Cleaning up temporary files..."
rm -rf "$TEMP_DIR"
fi
}
show_build_info() {
local run_file_size=$(du -h "$RUN_FILE" | cut -f1)
local run_file_path="$BUILD_DIR/$RUN_FILE"
local version_info=""
local integrity_info=""
if [ -f "$TEMP_DIR/payload/msmemscope/version.txt" ]; then
version_info=$(grep "version:" "$TEMP_DIR/payload/msmemscope/version.txt" | cut -d' ' -f2)
integrity_info=$(grep "integrity_check:" "$TEMP_DIR/payload/msmemscope/version.txt" | cut -d' ' -f2)
fi
echo ""
echo "=============================================="
echo " $TOOL_NAME Run Package Built"
echo "=============================================="
echo "File: $RUN_FILE"
echo "Size: $run_file_size"
echo "Package: $(basename "$RUN_FILE")"
echo "Version: ${version_info:-1.0.0}"
echo "Integrity Check: ${integrity_info:-enabled}"
echo ""
echo "Usage instructions:"
echo " Install: bash $RUN_FILE --install [--install-path=/path]"
echo " Upgrade: bash $RUN_FILE --upgrade --install-path=/path"
echo " Version: bash $RUN_FILE --version"
echo " Help: bash $RUN_FILE --help"
echo ""
echo "After installation:"
echo " To set up environment: source <install-path>/msmemscope/set_env.sh"
echo ""
log_info "Build process completed successfully"
}
main() {
if [ -n "$BUILD_VERSION" ]; then
VERSION="$BUILD_VERSION"
fi
while [[ $# -gt 0 ]]; do
case $1 in
--build-version=*)
if [ -z "$BUILD_VERSION" ]; then
VERSION="${1#*=}"
fi
shift
;;
--build-version)
if [ -z "$BUILD_VERSION" ]; then
VERSION="$2"
fi
shift 2
;;
*)
log_warn "Unknown parameter: $1"
shift
;;
esac
done
RUN_FILE="mindstudio-memscope_${VERSION}_${ARCH}.run"
log_info "Starting build process for $RUN_FILE ..."
if ! check_source_dirs; then
log_error "Source directory check failed, build aborted"
exit 1
fi
create_temp_dir
trap cleanup EXIT
copy_artifacts
create_install_script
if ! create_run_file; then
log_error "Failed to create run file"
exit 1
fi
show_build_info
}
main "$@"