Skip to content

Instantly share code, notes, and snippets.

@xmdhs
Last active July 30, 2025 07:51
Show Gist options
  • Save xmdhs/9b03023ea6e2038e968013ae3915ecad to your computer and use it in GitHub Desktop.
Save xmdhs/9b03023ea6e2038e968013ae3915ecad to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
trap 'echo "Error at line $LINENO"; exit 1' ERR
usage() {
cat <<EOF
Usage: ${0##*/} -e <exe> -d <dest> [-p <filter_dir>] [-v]
Options:
-e 可执行文件 (必需)
-d 目标目录 (必需)
-p 过滤目录名,默认: $FILTER_DIR
-v 详细模式
-h 帮助
EOF
exit 1
}
# 参数解析
FILTER_DIR="clang64"; VERBOSE=0
while getopts "e:d:p:vh" opt; do
case $opt in
e) EXE=$(realpath "$OPTARG") ;;
d) DEST_DIR=$(realpath "$OPTARG") ;;
p) FILTER_DIR="$OPTARG" ;;
v) VERBOSE=1 ;;
h|*) usage ;;
esac
done
[[ -z "${EXE:-}" || -z "${DEST_DIR:-}" ]] && usage
mkdir -p "$DEST_DIR"
declare -A VISITED
copy_dll() {
local file="$1"
[[ -z "$file" || "${VISITED[$file]:-}" ]] && return
[[ "$file" != *"/$FILTER_DIR/"* ]] && return
[[ ! -f "$file" ]] && { echo "警告: $file 不存在"; return; }
VISITED["$file"]=1
((VERBOSE)) && echo "复制 $file"
cp -L "$file" "$DEST_DIR/"
# 递归依赖
while read -r dep; do
copy_dll "$dep"
done < <(ldd "$file" | grep -Po '=>\s*\K(/[^ ]+)' || true)
}
# 主程序和依赖
cp -v "$EXE" "$DEST_DIR/"
while read -r dep; do
copy_dll "$dep"
done < <(ldd "$EXE" | grep -Po '=>\s*\K(/[^ ]+)' || true)
echo "完成:所有文件已复制到 $DEST_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment