2015-08-09 10:28:31 +08:00
|
|
|
#!/bin/sh
|
|
|
|
CS_URL="$1" # url
|
|
|
|
CS_BRA="$2" # branch name
|
|
|
|
CS_TIP="$3" # tip commit
|
|
|
|
CS_REV="$4" # revert
|
2019-05-09 18:02:27 +08:00
|
|
|
CS_ARCHIVE="$5" # download archived tip
|
2019-04-12 22:13:35 +08:00
|
|
|
CS_DEPTH_CLONE=512
|
2015-08-09 10:28:31 +08:00
|
|
|
|
2019-05-13 16:25:17 +08:00
|
|
|
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() {
|
2019-02-16 01:12:34 +08:00
|
|
|
echo "[capstone] $1"
|
2018-11-05 21:00:33 +08:00
|
|
|
exit 1
|
2018-07-25 23:58:53 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:00:33 +08:00
|
|
|
patch_capstone() {
|
2019-02-16 01:12:34 +08:00
|
|
|
echo "[capstone] Applying patches..."
|
2021-04-25 04:04:51 +08:00
|
|
|
if [ "$CS_BRA" = next ]; then
|
|
|
|
CV=v5
|
|
|
|
else
|
|
|
|
CV=v4
|
|
|
|
fi
|
|
|
|
for patchfile in ../capstone-patches/$CV/*.patch ; do
|
2022-01-07 07:11:53 +08:00
|
|
|
echo "Patch $patchFile"
|
2021-05-12 05:41:47 +08:00
|
|
|
patch -p 1 < "${patchfile}"
|
2018-07-25 23:58:53 +08:00
|
|
|
done
|
2018-11-05 21:00:33 +08:00
|
|
|
}
|
2018-07-25 23:58:53 +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)"
|
2018-07-25 23:58:53 +08:00
|
|
|
fi
|
2018-11-05 21:00:33 +08:00
|
|
|
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
|
|
|
|
}
|
2018-07-25 23:58:53 +08:00
|
|
|
|
2019-05-09 18:02:27 +08:00
|
|
|
download_archive() {
|
|
|
|
echo '[capstone] Downloading capstone snapshot...' >&2
|
2019-05-13 16:25:17 +08:00
|
|
|
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
|
2019-05-09 18:02:27 +08:00
|
|
|
mv "capstone-$CS_TIP" capstone
|
|
|
|
}
|
|
|
|
|
|
|
|
git_clone() {
|
2019-05-13 16:25:17 +08:00
|
|
|
git_assert
|
2019-05-09 18:02:27 +08:00
|
|
|
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
|
2022-01-26 16:49:36 +08:00
|
|
|
if [ -f capstone ]; then
|
|
|
|
rm -f capstone
|
|
|
|
fi
|
2019-05-13 16:25:17 +08:00
|
|
|
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
|
|
|
}
|
2015-08-09 10:28:31 +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"
|
2019-02-11 23:15:23 +08:00
|
|
|
# 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
|
2022-11-06 23:38:07 +08:00
|
|
|
fi
|
2019-02-16 01:12:34 +08:00
|
|
|
git reset --hard "${CS_TIP}"
|
2018-11-05 21:00:33 +08:00
|
|
|
if [ -n "${CS_REV}" ]; then
|
2015-08-09 10:28:31 +08:00
|
|
|
if ! git config user.name ; then
|
2021-04-16 23:56:50 +08:00
|
|
|
git config user.name "radare"
|
|
|
|
git config user.email "radare@radare.org"
|
2015-08-09 10:28:31 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-13 16:25:17 +08:00
|
|
|
### MAIN ###
|
|
|
|
|
2022-01-24 21:38:58 +08:00
|
|
|
pkg-config --cflags capstone > /dev/null 2>&1
|
2022-01-24 08:55:00 +08:00
|
|
|
if [ $? = 0 ]; then
|
2022-01-24 21:38:58 +08:00
|
|
|
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
|
2022-01-24 08:55:00 +08:00
|
|
|
fi
|
|
|
|
|
2019-05-13 16:25:17 +08:00
|
|
|
if [ -n "${CS_ARCHIVE}" ]; then
|
|
|
|
echo "CS_ARCHIVE=${CS_ARCHIVE}"
|
2018-11-05 21:00:33 +08:00
|
|
|
else
|
2019-05-13 16:25:17 +08:00
|
|
|
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
|
|
|
|
2019-05-13 16:25:17 +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
|
2019-05-13 16:25:17 +08:00
|
|
|
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
|
2019-05-13 16:25:17 +08:00
|
|
|
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
|
2015-08-09 10:28:31 +08:00
|
|
|
fi
|
2019-05-13 16:25:17 +08:00
|
|
|
cd capstone || fatal_msg 'Cannot change working directory'
|
|
|
|
patch_capstone
|
|
|
|
cd -
|