1#!/bin/sh 2CS_URL="$1" # url 3CS_BRA="$2" # branch name 4CS_TIP="$3" # tip commit 5CS_REV="$4" # revert 6CS_ARCHIVE="$5" # download archived tip 7CS_DEPTH_CLONE=512 8 9git_assert() { 10 git --help > /dev/null 2>&1 11 if [ $? != 0 ]; then 12 echo "ERROR: Cannot find git command in PATH" 13 if [ "$1" = check ]; then 14 return 1 15 fi 16 exit 1 17 fi 18 return 0 19} 20 21fatal_msg() { 22 echo "[capstone] $1" 23 exit 1 24} 25 26patch_capstone() { 27 echo "[capstone] Applying patches..." 28 for patchfile in ../capstone-patches/*.patch ; do 29 yes n | patch -p 1 -i "${patchfile}" 30 done 31} 32 33parse_capstone_tip() { 34 if [ -n "${CS_REV}" ]; then 35 HEAD="$(git rev-parse --verify HEAD^)" 36 else 37 HEAD="$(git rev-parse --verify HEAD)" 38 fi 39 BRANCH="$(git rev-parse --abbrev-ref HEAD)" 40} 41 42download_archive() { 43 echo '[capstone] Downloading capstone snapshot...' >&2 44 wget --help > /dev/null 2>&1 45 if [ $? = 0 ]; then 46 DLBIN="wget -O" 47 else 48 DLBIN="curl -o" 49 fi 50 ${DLBIN} .cs_tmp.zip "$CS_ARCHIVE" || fatal_msg 'Cannot download archived capstone' 51 unzip .cs_tmp.zip || exit 1 52 mv "capstone-$CS_TIP" capstone 53} 54 55git_clone() { 56 git_assert 57 echo '[capstone] Cloning capstone from git...' >&2 58 git clone --quiet --single-branch --branch "${CS_BRA}" \ 59 --depth "$CS_DEPTH_CLONE" "${CS_URL}" capstone \ 60 || fatal_msg 'Cannot clone capstone from git' 61} 62 63get_capstone() { 64 git_clone || fatal_msg 'Clone failed' 65 cd capstone || fatal_msg 'Failed to chdir' 66 parse_capstone_tip 67 cd .. 68} 69 70update_capstone_git() { 71 git checkout "${CS_BRA}" || fatal_msg "Cannot checkout to branch $CS_BRA" 72# if [ -n "${CS_TIP}" ]; then 73# # if our shallow clone not contains CS_TIP, clone until it 74# # contain that commit. 75# cur_depth="$CS_DEPTH_CLONE" 76# until git cat-file -e "${CS_TIP}^${commit}"; do 77# cur_depth=$(( cur_depth + 10 )) 78# git pull --depth="$cur_depth" 79# done 80# git reset --hard "${CS_TIP}" 81# fi 82 git reset --hard "${CS_TIP}" 83 if [ -n "${CS_REV}" ]; then 84 if ! git config user.name ; then 85 git config user.name "radare-travis" 86 git config user.email "radare-travis@foo.com" 87 fi 88 env EDITOR=cat git revert --no-edit "${CS_REV}" 89 fi 90} 91 92### MAIN ### 93 94if [ -n "${CS_ARCHIVE}" ]; then 95 echo "CS_ARCHIVE=${CS_ARCHIVE}" 96else 97 echo 98 echo "Run 'make CS_COMMIT_ARCHIVE=1' to download capstone with wget/curl instead of git" 99 echo 100fi 101 102if [ -z "${CS_ARCHIVE}" ]; then 103 git_assert check 104 if [ $? != 0 ]; then 105 export CS_ARCHIVE="https://github.com/aquynh/capstone/archive/$3.zip" 106 fi 107fi 108 109if [ -d capstone ]; then 110 echo "[capstone] Nothing to do" 111 exit 0 112fi 113if [ -n "${CS_ARCHIVE}" ]; then 114 download_archive 115else 116 git_assert 117 get_capstone 118 # if [ ! -d capstone/.git ]; then update_capstone_git fi 119fi 120cd capstone || fatal_msg 'Cannot change working directory' 121patch_capstone 122cd - 123