radare2/shlr/capstone.sh

147 lines
3.3 KiB
Bash
Raw Normal View History

#!/bin/sh
CS_URL="$1" # url
CS_BRA="$2" # branch name
CS_TIP="$3" # tip commit
CS_REV="$4" # revert
CS_ARCHIVE="$5" # download archived tip
CS_DEPTH_CLONE=512
git_assert() {
git --help > /dev/null 2>&1
if [ $? != 0 ]; then
echo "ERROR: Cannot find git command in PATH"
if [ "$1" = check ]; then
return 1
fi
exit 1
fi
return 0
}
2019-02-14 06:23:48 +08:00
2018-11-05 21:00:33 +08:00
fatal_msg() {
echo "[capstone] $1"
2018-11-05 21:00:33 +08:00
exit 1
}
2018-11-05 21:00:33 +08:00
patch_capstone() {
echo "[capstone] Applying patches..."
if [ "$CS_BRA" = next ]; then
CV=v5
else
CV=v4
fi
for patchfile in ../capstone-patches/$CV/*.patch ; do
echo "Patch $patchFile"
2021-05-12 05:41:47 +08:00
patch -p 1 < "${patchfile}"
done
2018-11-05 21:00:33 +08:00
}
2018-11-05 21:00:33 +08:00
parse_capstone_tip() {
if [ -n "${CS_REV}" ]; then
HEAD="$(git rev-parse --verify HEAD^)"
else
HEAD="$(git rev-parse --verify HEAD)"
fi
2018-11-05 21:00:33 +08:00
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
}
download_archive() {
echo '[capstone] Downloading capstone snapshot...' >&2
wget --help > /dev/null 2>&1
if [ $? = 0 ]; then
DLBIN="wget -O"
else
DLBIN="curl -o"
fi
${DLBIN} .cs_tmp.zip "$CS_ARCHIVE" || fatal_msg 'Cannot download archived capstone'
unzip .cs_tmp.zip || exit 1
mv "capstone-$CS_TIP" capstone
}
git_clone() {
git_assert
echo '[capstone] Cloning capstone from git...' >&2
git clone --quiet --single-branch --branch "${CS_BRA}" \
--depth "$CS_DEPTH_CLONE" "${CS_URL}" capstone \
|| fatal_msg 'Cannot clone capstone from git'
}
get_capstone() {
2021-11-05 08:25:02 +08:00
if [ -d capstone ]; then
return
fi
if [ -f capstone ]; then
rm -f capstone
fi
git_clone || fatal_msg 'Clone failed'
cd capstone || fatal_msg 'Failed to chdir'
parse_capstone_tip
cd ..
2018-11-05 21:00:33 +08:00
}
2018-11-05 21:00:33 +08:00
update_capstone_git() {
2022-04-26 17:49:53 +08:00
cd capstone || fatal_msg 'Failed to chdir'
2018-11-05 21:00:33 +08:00
git checkout "${CS_BRA}" || fatal_msg "Cannot checkout to branch $CS_BRA"
# if [ -n "${CS_TIP}" ]; then
# # if our shallow clone not contains CS_TIP, clone until it
# # contain that commit.
# cur_depth="$CS_DEPTH_CLONE"
# until git cat-file -e "${CS_TIP}^${commit}"; do
# cur_depth=$(( cur_depth + 10 ))
# git pull --depth="$cur_depth"
# done
# git reset --hard "${CS_TIP}"
# fi
2022-11-12 00:32:00 +08:00
if ! git show --oneline "${CS_TIP}" &>/dev/null ; then
git fetch
fi
git reset --hard "${CS_TIP}"
2018-11-05 21:00:33 +08:00
if [ -n "${CS_REV}" ]; then
if ! git config user.name ; then
git config user.name "radare"
git config user.email "radare@radare.org"
fi
2022-11-12 00:32:00 +08:00
export EDITOR=cat git revert --no-edit "${CS_REV}"
2018-11-05 21:00:33 +08:00
fi
2022-04-26 17:49:53 +08:00
cd ..
2018-11-05 21:00:33 +08:00
}
### MAIN ###
pkg-config --cflags capstone > /dev/null 2>&1
if [ $? = 0 ]; then
echo "Warning: You have system-wide capstone installation, but im gonna clone a new copy of it"
echo "Warning: Use ./configure --with-syscapstone # in case you prefer to use system one"
# exit 0
fi
if [ -n "${CS_ARCHIVE}" ]; then
echo "CS_ARCHIVE=${CS_ARCHIVE}"
2018-11-05 21:00:33 +08:00
else
echo
echo "Run 'make CS_COMMIT_ARCHIVE=1' to download capstone with wget/curl instead of git"
echo
fi
2018-11-05 21:00:33 +08:00
if [ -z "${CS_ARCHIVE}" ]; then
git_assert check
if [ $? != 0 ]; then
export CS_ARCHIVE="https://github.com/aquynh/capstone/archive/$3.zip"
2018-11-05 21:00:33 +08:00
fi
fi
2018-11-05 21:00:33 +08:00
2022-11-12 00:32:00 +08:00
if [ -d capstone -a ! -d capstone/.git ] || [ "$(git --git-dir capstone/.git rev-parse --verify HEAD > /dev/null 2>&1)" = "${CS_TIP}" ]; then
echo "[capstone] Nothing to do"
exit 0
fi
if [ -n "${CS_ARCHIVE}" ]; then
download_archive
else
git_assert
get_capstone
2022-04-26 17:49:53 +08:00
[ -d capstone/.git ] && update_capstone_git
fi
cd capstone || fatal_msg 'Cannot change working directory'
patch_capstone
cd -