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.

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