You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
13 KiB

  1. #!/bin/sh
  2. # Copyright 2017 Roger Shimizu <rogershimizu@gmail.com>
  3. #
  4. # This is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. help_usage() {
  9. cat << EOT
  10. Build shadowsocks-libev and its dependencies
  11. Usage:
  12. $(basename $0) [--help|-h] [lib|bin|all]
  13. --help|-h Show this usage.
  14. kcp Build kcptun package (with its dependencies) only.
  15. lib Build library packages only.
  16. bin Build binary packages only.
  17. However, you need the libraries built previously, in current working directory.
  18. For advanced user only.
  19. all Build both binary and library packages (default).
  20. The safe choice for everyone.
  21. Please run this script in a clean place.
  22. e.g.
  23. mkdir -p ~/build-area
  24. cd ~/build-area
  25. ln -s $(readlink -f $0) .
  26. ./$(basename $0)
  27. EOT
  28. exit
  29. }
  30. help_lib() {
  31. cat << EOT
  32. Failed to install required library:
  33. $1
  34. You can try to fix it by:
  35. $0 lib
  36. EOT
  37. exit
  38. }
  39. apt_init() {
  40. DEPS="$1"
  41. DEPS_BPO="$2"
  42. DEPS_EXTRA="$3"
  43. if [ -n "$DEPS_BPO" ]; then
  44. BPO=${OSVER}-backports
  45. case "$OSID" in
  46. debian)
  47. REPO=http://deb.debian.org/debian
  48. ;;
  49. ubuntu)
  50. REPO=http://archive.ubuntu.com/ubuntu
  51. ;;
  52. esac
  53. sudo sh -c "printf \"deb $REPO ${OSVER}-backports main\" > /etc/apt/sources.list.d/${OSVER}-backports.list"
  54. [ -n "$DEPS_EXTRA" -a "$DEPS_EXTRA" = "sloppy" ] &&
  55. sudo sh -c "printf \"\\ndeb $REPO ${OSVER}-backports-sloppy main\" >> /etc/apt/sources.list.d/${OSVER}-backports.list"
  56. sudo apt-get update
  57. sudo apt-get install --no-install-recommends -y -t $BPO $DEPS_BPO
  58. [ -n "$DEPS_EXTRA" -a "$DEPS_EXTRA" = "sloppy" ] &&
  59. sudo apt-get install --no-install-recommends -y -t ${BPO}-sloppy $DEPS_BPO
  60. else
  61. sudo apt-get update
  62. fi
  63. sudo apt-get install -y $DEPS
  64. }
  65. # Cleanup
  66. apt_clean() {
  67. sudo apt-get purge -y $DEPS $DEPS_BPO debhelper \
  68. libbloom-dev libcork-dev libcorkipset-dev libmbedtls-dev libsodium-dev
  69. sudo apt-get purge -y libcork-build-deps libcorkipset-build-deps \
  70. libsodium-build-deps mbedtls-build-deps
  71. sudo apt-get purge -y libbloom-build-deps
  72. sudo apt-get purge -y simple-obfs-build-deps
  73. sudo apt-get purge -y shadowsocks-libev-build-deps
  74. if [ $BUILD_KCP -eq 1 ]; then
  75. sudo apt-get purge -y golang-github-klauspost-reedsolomon-build-deps \
  76. golang-github-xtaci-kcp-build-deps golang-github-xtaci-smux-build-deps \
  77. kcptun-build-deps
  78. sudo apt-get purge -y golang-github-urfave-cli-build-deps
  79. sudo apt-get purge -y golang-github-golang-snappy-build-deps \
  80. dh-golang-build-deps golang-github-pkg-errors-build-deps
  81. sudo apt-get purge -y golang-github-klauspost-reedsolomon-dev \
  82. golang-github-xtaci-kcp-dev golang-github-xtaci-smux-dev
  83. sudo apt-get purge -y golang-github-urfave-cli-dev
  84. sudo apt-get purge -y golang-github-pkg-errors-dev \
  85. golang-github-golang-snappy-dev dh-golang
  86. fi
  87. sudo apt-get autoremove -y
  88. }
  89. gbp_build() {
  90. REPO=$1
  91. BRANCH=$2
  92. PROJECT_NAME=$(basename $1|sed s/\.git$//)
  93. gbp clone --pristine-tar $REPO
  94. cd $PROJECT_NAME
  95. [ -n "$BRANCH" ] && git checkout $BRANCH
  96. [ -n "$BRANCH" -a "$BRANCH" = "trusty" ] && # try to rebase the trusty patch
  97. if ! git rebase master; then git rebase --abort; git rebase debian; fi
  98. [ -n "$DEPS_BPO" ] && BPO_REPO="-t ${OSVER}-backports"
  99. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y $BPO_REPO"
  100. rm -f ${PROJECT_NAME}-build-deps_*.deb
  101. gbp buildpackage -us -uc --git-ignore-branch --git-pristine-tar
  102. git clean -fdx
  103. git reset --hard HEAD
  104. cd -
  105. }
  106. git_build() {
  107. REPO=$1
  108. BRANCH=$2
  109. PROJECT_NAME=$(basename $1|sed s/\.git$//)
  110. git clone $REPO
  111. cd $PROJECT_NAME
  112. [ -n "$BRANCH" ] && git checkout $BRANCH
  113. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  114. rm ${PROJECT_NAME}-build-deps_*.deb
  115. gbp buildpackage -us -uc --git-ignore-branch
  116. git clean -fdx
  117. git reset --hard HEAD
  118. cd -
  119. }
  120. dsc_build() {
  121. DSC=$1
  122. DSC_FILE=$(basename $1)
  123. dget -ux $DSC
  124. PROJECT_NAME=$(grep ^Source: $DSC_FILE|cut -d" " -f2)
  125. echo cd ${PROJECT_NAME}-*
  126. cd ${PROJECT_NAME}-*
  127. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  128. rm ${PROJECT_NAME}-build-deps_*.deb
  129. dpkg-buildpackage -us -uc
  130. cd -
  131. }
  132. # Build and install libcork deb
  133. build_install_libcork() {
  134. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  135. BRANCH=$1
  136. if [ $BUILD_LIB -eq 1 ]; then
  137. gbp_build https://github.com/rogers0/libcork $BRANCH
  138. else
  139. ls libcork-dev_*.deb libcork16_*.deb 2>&1 > /dev/null ||
  140. help_lib "libcork-dev libcork16"
  141. fi
  142. sudo dpkg -i libcork-dev_*.deb libcork16_*.deb
  143. fi
  144. }
  145. # Build and install libcorkipset deb
  146. build_install_libcorkipset() {
  147. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  148. BRANCH=$1
  149. if [ $BUILD_LIB -eq 1 ]; then
  150. gbp_build https://github.com/rogers0/libcorkipset $BRANCH
  151. else
  152. ls libcorkipset-dev_*.deb libcorkipset1_*.deb 2>&1 > /dev/null ||
  153. help_lib "libcorkipset-dev libcorkipset1"
  154. fi
  155. sudo dpkg -i libcorkipset-dev_*.deb libcorkipset1_*.deb
  156. fi
  157. }
  158. # Build libmbedtls deb
  159. build_install_libmbedtls() {
  160. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  161. BRANCH=$1
  162. if [ $BUILD_LIB -eq 1 ]; then
  163. gbp_build https://anonscm.debian.org/git/collab-maint/mbedtls.git $BRANCH
  164. else
  165. ls libmbed*.deb 2>&1 > /dev/null ||
  166. help_lib libmbedtls
  167. fi
  168. sudo dpkg -i libmbed*.deb
  169. fi
  170. }
  171. # Build libsodium deb
  172. build_install_libsodium() {
  173. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  174. if [ $BUILD_LIB -eq 1 ]; then
  175. git clone https://github.com/gcsideal/debian-libsodium.git libsodium
  176. cd libsodium; LIBSODIUM=$(dpkg-parsechangelog --show-field Version); cd -
  177. dget -ud http://deb.debian.org/debian/pool/main/libs/libsodium/libsodium_${LIBSODIUM}.dsc
  178. DHVER=$(dpkg -l debhelper|grep debhelper|awk '{print $3}'|head -n1)
  179. cd libsodium
  180. if dpkg --compare-versions $DHVER lt 10; then
  181. sed -i 's/debhelper ( >= 10)/debhelper (>= 9), dh-autoreconf/' debian/control;
  182. echo 9 > debian/compat;
  183. dch -D unstable -l~bpo~ "Rebuild as backports"
  184. git add -u;
  185. git commit -m "Patch to work with ubuntu"
  186. fi
  187. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  188. rm libsodium-build-deps_*.deb
  189. gbp buildpackage -us -uc --git-ignore-branch --git-tarball-dir=.. --git-export-dir=.. --git-overlay
  190. cd -
  191. else
  192. ls libsodium*.deb 2>&1 > /dev/null ||
  193. help_lib libsodium
  194. fi
  195. sudo dpkg -i libsodium*.deb
  196. fi
  197. }
  198. # Build libbloom deb
  199. build_install_libbloom() {
  200. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  201. BRANCH=$1
  202. if [ $BUILD_LIB -eq 1 ]; then
  203. gbp_build https://anonscm.debian.org/git/collab-maint/libbloom.git $BRANCH
  204. else
  205. ls libbloom-dev_*.deb libbloom1_*.deb 2>&1 > /dev/null ||
  206. help_lib "libbloom-dev libbloom1"
  207. fi
  208. sudo dpkg -i libbloom-dev_*.deb libbloom1_*.deb
  209. fi
  210. }
  211. # Add patch to work on system with debhelper 9 only
  212. patch_sslibev_dh9() {
  213. if [ $BUILD_BIN -eq 1 ]; then
  214. BRANCH=$1
  215. gbp clone --pristine-tar https://anonscm.debian.org/git/collab-maint/shadowsocks-libev.git
  216. cd shadowsocks-libev
  217. [ -n "$BRANCH" ] && git checkout $BRANCH
  218. sed -i 's/dh $@/dh $@ --with systemd,autoreconf/' debian/rules
  219. sed -i 's/debhelper (>= 10)/debhelper (>= 9), dh-systemd, dh-autoreconf/' debian/control
  220. echo 9 > debian/compat
  221. dch -D unstable -l~bpo~ "Rebuild as backports"
  222. git add -u
  223. git commit -m "Patch to work with ubuntu trusty (14.04)"
  224. cd -
  225. fi
  226. }
  227. # Build and install shadowsocks-libev deb
  228. build_install_sslibev() {
  229. if [ $BUILD_BIN -eq 1 ]; then
  230. BRANCH=$1
  231. gbp_build https://anonscm.debian.org/git/collab-maint/shadowsocks-libev.git $BRANCH
  232. sudo dpkg -i shadowsocks-libev_*.deb
  233. sudo apt-get install -fy
  234. fi
  235. }
  236. # Build and install simple-obfs
  237. build_install_simpleobfs() {
  238. if [ $BUILD_BIN -eq 1 ]; then
  239. BRANCH=$1
  240. git_build https://anonscm.debian.org/git/collab-maint/simple-obfs.git $BRANCH
  241. sudo dpkg -i simple-obfs_*.deb
  242. sudo apt-get install -fy
  243. fi
  244. }
  245. # Build and install dh-golang deb
  246. build_install_dhgolang() {
  247. if [ $BUILD_KCP -eq 1 ]; then
  248. BRANCH=$1
  249. gbp_build https://anonscm.debian.org/cgit/pkg-go/packages/dh-golang.git $BRANCH
  250. sudo dpkg -i dh-golang_*.deb
  251. sudo apt-get install -fy
  252. fi
  253. }
  254. # Build and install golang-github-klauspost-reedsolomon deb
  255. build_install_reedsolomondev() {
  256. if [ $BUILD_KCP -eq 1 ]; then
  257. BRANCH=$1
  258. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-klauspost-reedsolomon.git $BRANCH
  259. sudo dpkg -i golang-github-klauspost-reedsolomon-dev_*.deb
  260. sudo apt-get install -fy
  261. fi
  262. }
  263. # Build and install golang-github-pkg-errors deb
  264. build_install_errorsdev() {
  265. if [ $BUILD_KCP -eq 1 ]; then
  266. BRANCH=$1
  267. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-pkg-errors.git $BRANCH
  268. sudo dpkg -i golang-github-pkg-errors-dev_*.deb
  269. sudo apt-get install -fy
  270. fi
  271. }
  272. # Add patch to work on system with xenial
  273. patch_urfaveclidev_xenial() {
  274. if [ $BUILD_KCP -eq 1 ]; then
  275. BRANCH=$1
  276. gbp clone --pristine-tar https://anonscm.debian.org/git/pkg-go/packages/golang-github-urfave-cli.git
  277. cd golang-github-urfave-cli
  278. [ -n "$BRANCH" ] && git checkout $BRANCH
  279. sed -i 's/golang-github-burntsushi-toml-dev/golang-toml-dev/; s/golang-gopkg-yaml.v2-dev/golang-yaml.v2-dev/' debian/control
  280. dch -D unstable -l~bpo~ "Rebuild as backports"
  281. git add -u
  282. git commit -m "Patch to work with ubuntu xenial (16.04)"
  283. cd -
  284. fi
  285. }
  286. # Build and install golang-github-urfave-cli-dev deb
  287. build_install_urfaveclidev() {
  288. if [ $BUILD_KCP -eq 1 ]; then
  289. BRANCH=$1
  290. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-urfave-cli.git $BRANCH
  291. sudo dpkg -i build-area/golang-github-urfave-cli-dev_*.deb
  292. sudo apt-get install -fy
  293. fi
  294. }
  295. # Build and install golang-github-golang-snappy deb
  296. build_install_snappydev() {
  297. if [ $BUILD_KCP -eq 1 ]; then
  298. BRANCH=$1
  299. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-golang-snappy.git $BRANCH
  300. sudo dpkg -i golang-github-golang-snappy-dev_*.deb
  301. sudo apt-get install -fy
  302. fi
  303. }
  304. # Build and install golang-github-xtaci-kcp deb
  305. build_install_kcpdev() {
  306. if [ $BUILD_KCP -eq 1 ]; then
  307. BRANCH=$1
  308. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-xtaci-kcp.git $BRANCH
  309. sudo dpkg -i golang-github-xtaci-kcp-dev_*.deb
  310. sudo apt-get install -fy
  311. fi
  312. }
  313. # Build and install golang-github-xtaci-smux deb
  314. build_install_smuxdev() {
  315. if [ $BUILD_KCP -eq 1 ]; then
  316. BRANCH=$1
  317. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-xtaci-smux.git $BRANCH
  318. sudo dpkg -i golang-github-xtaci-smux-dev_*.deb
  319. sudo apt-get install -fy
  320. fi
  321. }
  322. # Build and install kcptun deb
  323. build_install_kcptun() {
  324. if [ $BUILD_KCP -eq 1 ]; then
  325. BRANCH=$1
  326. gbp_build https://anonscm.debian.org/git/pkg-go/packages/kcptun.git $BRANCH
  327. sudo dpkg -i kcptun_*.deb
  328. sudo apt-get install -fy
  329. fi
  330. }
  331. export XZ_DEFAULTS=--memlimit=128MiB
  332. OSID=$(grep ^ID= /etc/os-release|cut -d= -f2)
  333. OSVER=$(lsb_release -cs)
  334. BUILD_KCP=0
  335. BUILD_LIB=0
  336. BUILD_BIN=0
  337. case "$1" in
  338. --help|-h)
  339. help_usage
  340. ;;
  341. kcp)
  342. BUILD_KCP=1
  343. ;;
  344. lib)
  345. BUILD_LIB=1
  346. ;;
  347. bin)
  348. BUILD_BIN=1
  349. ;;
  350. all|"")
  351. BUILD_LIB=1
  352. BUILD_BIN=1
  353. ;;
  354. *)
  355. echo Parameter error, exiting ...
  356. exit
  357. esac
  358. # Exit if in a git repo
  359. [ -d .git ] && help_usage
  360. case "$OSVER" in
  361. jessie)
  362. BPO="debhelper libbloom-dev libsodium-dev"
  363. BPOEXTRA=sloppy
  364. ;;
  365. stretch)
  366. BPO=libbloom-dev
  367. ;;
  368. xenial)
  369. BPO=debhelper
  370. ;;
  371. esac
  372. apt_init "git-buildpackage pristine-tar equivs" "$BPO" $BPOEXTRA
  373. [ $BUILD_KCP -eq 1 ] && case "$OSVER" in
  374. wheezy|precise|trusty)
  375. echo Sorry, your system $OSID/$OSVER is not supported.
  376. ;;
  377. jessie)
  378. build_install_urfaveclidev
  379. build_install_reedsolomondev
  380. build_install_kcpdev
  381. build_install_smuxdev
  382. build_install_kcptun
  383. ;;
  384. stretch|unstable|sid|yakkety|zesty)
  385. build_install_reedsolomondev
  386. build_install_kcpdev
  387. build_install_smuxdev
  388. build_install_kcptun
  389. ;;
  390. xenial)
  391. build_install_dhgolang debian/jessie-backports
  392. build_install_reedsolomondev
  393. build_install_errorsdev
  394. patch_urfaveclidev_xenial
  395. build_install_urfaveclidev
  396. build_install_snappydev debian/jessie-backports
  397. build_install_kcpdev
  398. build_install_smuxdev
  399. build_install_kcptun
  400. ;;
  401. esac
  402. [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ] && case "$OSVER" in
  403. wheezy|precise)
  404. echo Sorry, your system $OSID/$OSVER is not supported.
  405. ;;
  406. jessie|stretch|buster|testing|unstable|sid)
  407. build_install_sslibev
  408. ;;
  409. zesty)
  410. build_install_libsodium
  411. build_install_libbloom
  412. build_install_sslibev
  413. build_install_simpleobfs
  414. ;;
  415. xenial|yakkety)
  416. build_install_libcork debian
  417. build_install_libcorkipset debian
  418. build_install_libsodium
  419. build_install_libbloom
  420. build_install_sslibev
  421. build_install_simpleobfs
  422. ;;
  423. trusty)
  424. build_install_libcork trusty
  425. build_install_libcorkipset trusty
  426. build_install_libmbedtls debian/jessie-backports
  427. build_install_libsodium
  428. build_install_libbloom trusty
  429. patch_sslibev_dh9
  430. build_install_sslibev
  431. build_install_simpleobfs trusty
  432. ;;
  433. *)
  434. echo Your system $OSID/$OSVER is not supported yet.
  435. echo Please report issue:
  436. echo " https://github.com/shadowsocks/shadowsocks-libev/issues/new"
  437. ;;
  438. esac
  439. apt_clean