2015-08-09 02:28:31 +00:00
|
|
|
#!/bin/sh
|
|
|
|
CS_URL="$1" # url
|
|
|
|
CS_BRA="$2" # branch name
|
|
|
|
CS_TIP="$3" # tip commit
|
|
|
|
CS_REV="$4" # revert
|
2019-05-09 10:02:27 +00:00
|
|
|
CS_ARCHIVE="$5" # download archived tip
|
2019-04-12 14:13:35 +00:00
|
|
|
CS_DEPTH_CLONE=512
|
2015-08-09 02:28:31 +00:00
|
|
|
|
2019-05-13 08:25:17 +00: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-13 22:23:48 +00:00
|
|
|
|
2018-11-05 13:00:33 +00:00
|
|
|
fatal_msg() {
|
2019-02-15 17:12:34 +00:00
|
|
|
echo "[capstone] $1"
|
2018-11-05 13:00:33 +00:00
|
|
|
exit 1
|
2018-07-25 15:58:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 13:00:33 +00:00
|
|
|
patch_capstone() {
|
2019-02-15 17:12:34 +00:00
|
|
|
echo "[capstone] Applying patches..."
|
2021-04-24 20:04:51 +00:00
|
|
|
if [ "$CS_BRA" = next ]; then
|
|
|
|
CV=v5
|
|
|
|
else
|
|
|
|
CV=v4
|
|
|
|
fi
|
|
|
|
for patchfile in ../capstone-patches/$CV/*.patch ; do
|
2019-02-15 17:12:34 +00:00
|
|
|
yes n | patch -p 1 -i "${patchfile}"
|
2018-07-25 15:58:53 +00:00
|
|
|
done
|
2018-11-05 13:00:33 +00:00
|
|
|
}
|
2018-07-25 15:58:53 +00:00
|
|
|
|
2018-11-05 13:00:33 +00: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 15:58:53 +00:00
|
|
|
fi
|
2018-11-05 13:00:33 +00:00
|
|
|
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
|
|
|
|
}
|
2018-07-25 15:58:53 +00:00
|
|
|
|
2019-05-09 10:02:27 +00:00
|
|
|
download_archive() {
|
|
|
|
echo '[capstone] Downloading capstone snapshot...' >&2
|
2019-05-13 08:25:17 +00: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 10:02:27 +00:00
|
|
|
mv "capstone-$CS_TIP" capstone
|
|
|
|
}
|
|
|
|
|
|
|
|
git_clone() {
|
2019-05-13 08:25:17 +00:00
|
|
|
git_assert
|
2019-05-09 10:02:27 +00: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() {
|
2019-05-13 08:25:17 +00:00
|
|
|
git_clone || fatal_msg 'Clone failed'
|
|
|
|
cd capstone || fatal_msg 'Failed to chdir'
|
|
|
|
parse_capstone_tip
|
|
|
|
cd ..
|
2018-11-05 13:00:33 +00:00
|
|
|
}
|
2015-08-09 02:28:31 +00:00
|
|
|
|
2018-11-05 13:00:33 +00:00
|
|
|
update_capstone_git() {
|
|
|
|
git checkout "${CS_BRA}" || fatal_msg "Cannot checkout to branch $CS_BRA"
|
2019-02-11 15:15:23 +00: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
|
2019-02-15 17:12:34 +00:00
|
|
|
git reset --hard "${CS_TIP}"
|
2018-11-05 13:00:33 +00:00
|
|
|
if [ -n "${CS_REV}" ]; then
|
2015-08-09 02:28:31 +00:00
|
|
|
if ! git config user.name ; then
|
2021-04-16 15:56:50 +00:00
|
|
|
git config user.name "radare"
|
|
|
|
git config user.email "radare@radare.org"
|
2015-08-09 02:28:31 +00:00
|
|
|
fi
|
2018-11-05 13:00:33 +00:00
|
|
|
env EDITOR=cat git revert --no-edit "${CS_REV}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-05-13 08:25:17 +00:00
|
|
|
### MAIN ###
|
|
|
|
|
|
|
|
if [ -n "${CS_ARCHIVE}" ]; then
|
|
|
|
echo "CS_ARCHIVE=${CS_ARCHIVE}"
|
2018-11-05 13:00:33 +00:00
|
|
|
else
|
2019-05-13 08:25:17 +00:00
|
|
|
echo
|
|
|
|
echo "Run 'make CS_COMMIT_ARCHIVE=1' to download capstone with wget/curl instead of git"
|
|
|
|
echo
|
|
|
|
fi
|
2018-11-05 13:00:33 +00:00
|
|
|
|
2019-05-13 08:25:17 +00: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 13:00:33 +00:00
|
|
|
fi
|
2019-05-13 08:25:17 +00:00
|
|
|
fi
|
2018-11-05 13:00:33 +00:00
|
|
|
|
2019-05-13 08:25:17 +00:00
|
|
|
if [ -d capstone ]; then
|
|
|
|
echo "[capstone] Nothing to do"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
if [ -n "${CS_ARCHIVE}" ]; then
|
|
|
|
download_archive
|
|
|
|
else
|
|
|
|
git_assert
|
|
|
|
get_capstone
|
|
|
|
# if [ ! -d capstone/.git ]; then update_capstone_git fi
|
2015-08-09 02:28:31 +00:00
|
|
|
fi
|
2019-05-13 08:25:17 +00:00
|
|
|
cd capstone || fatal_msg 'Cannot change working directory'
|
|
|
|
patch_capstone
|
|
|
|
cd -
|