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.

480 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. git add -u;
  182. git commit -m "Patch to work with ubuntu trusty (14.04)"
  183. fi
  184. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  185. rm libsodium-build-deps_*.deb
  186. gbp buildpackage -us -uc --git-ignore-branch --git-tarball-dir=.. --git-export-dir=.. --git-overlay
  187. cd -
  188. else
  189. ls libsodium*.deb 2>&1 > /dev/null ||
  190. help_lib libsodium
  191. fi
  192. sudo dpkg -i libsodium*.deb
  193. fi
  194. }
  195. # Build libbloom deb
  196. build_install_libbloom() {
  197. if [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ]; then
  198. BRANCH=$1
  199. if [ $BUILD_LIB -eq 1 ]; then
  200. gbp_build https://anonscm.debian.org/git/collab-maint/libbloom.git $BRANCH
  201. else
  202. ls libbloom-dev_*.deb libbloom1_*.deb 2>&1 > /dev/null ||
  203. help_lib "libbloom-dev libbloom1"
  204. fi
  205. sudo dpkg -i libbloom-dev_*.deb libbloom1_*.deb
  206. fi
  207. }
  208. # Add patch to work on system with debhelper 9 only
  209. patch_sslibev_dh9() {
  210. if [ $BUILD_BIN -eq 1 ]; then
  211. BRANCH=$1
  212. gbp clone --pristine-tar https://anonscm.debian.org/git/collab-maint/shadowsocks-libev.git
  213. cd shadowsocks-libev
  214. [ -n "$BRANCH" ] && git checkout $BRANCH
  215. sed -i 's/dh $@/dh $@ --with systemd,autoreconf/' debian/rules
  216. sed -i 's/debhelper (>= 10)/debhelper (>= 9), dh-systemd, dh-autoreconf/' debian/control
  217. echo 9 > debian/compat
  218. git add -u
  219. git commit -m "Patch to work with ubuntu trusty (14.04)"
  220. cd -
  221. fi
  222. }
  223. # Build and install shadowsocks-libev deb
  224. build_install_sslibev() {
  225. if [ $BUILD_BIN -eq 1 ]; then
  226. BRANCH=$1
  227. gbp_build https://anonscm.debian.org/git/collab-maint/shadowsocks-libev.git $BRANCH
  228. sudo dpkg -i shadowsocks-libev_*.deb
  229. sudo apt-get install -fy
  230. fi
  231. }
  232. # Build and install simple-obfs
  233. build_install_simpleobfs() {
  234. if [ $BUILD_BIN -eq 1 ]; then
  235. BRANCH=$1
  236. git_build https://anonscm.debian.org/git/collab-maint/simple-obfs.git $BRANCH
  237. sudo dpkg -i simple-obfs_*.deb
  238. sudo apt-get install -fy
  239. fi
  240. }
  241. # Build and install dh-golang deb
  242. build_install_dhgolang() {
  243. if [ $BUILD_KCP -eq 1 ]; then
  244. BRANCH=$1
  245. gbp_build https://anonscm.debian.org/cgit/pkg-go/packages/dh-golang.git $BRANCH
  246. sudo dpkg -i dh-golang_*.deb
  247. sudo apt-get install -fy
  248. fi
  249. }
  250. # Build and install golang-github-klauspost-reedsolomon deb
  251. build_install_reedsolomondev() {
  252. if [ $BUILD_KCP -eq 1 ]; then
  253. BRANCH=$1
  254. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-klauspost-reedsolomon.git $BRANCH
  255. sudo dpkg -i golang-github-klauspost-reedsolomon-dev_*.deb
  256. sudo apt-get install -fy
  257. fi
  258. }
  259. # Build and install golang-github-pkg-errors deb
  260. build_install_errorsdev() {
  261. if [ $BUILD_KCP -eq 1 ]; then
  262. BRANCH=$1
  263. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-pkg-errors.git $BRANCH
  264. sudo dpkg -i golang-github-pkg-errors-dev_*.deb
  265. sudo apt-get install -fy
  266. fi
  267. }
  268. # Add patch to work on system with xenial
  269. patch_urfaveclidev_xenial() {
  270. if [ $BUILD_KCP -eq 1 ]; then
  271. BRANCH=$1
  272. gbp clone --pristine-tar https://anonscm.debian.org/git/pkg-go/packages/golang-github-urfave-cli.git
  273. cd golang-github-urfave-cli
  274. [ -n "$BRANCH" ] && git checkout $BRANCH
  275. sed -i 's/golang-github-burntsushi-toml-dev/golang-toml-dev/; s/golang-gopkg-yaml.v2-dev/golang-yaml.v2-dev/' debian/control
  276. git add -u
  277. git commit -m "Patch to work with ubuntu xenial (16.04)"
  278. cd -
  279. fi
  280. }
  281. # Build and install golang-github-urfave-cli-dev deb
  282. build_install_urfaveclidev() {
  283. if [ $BUILD_KCP -eq 1 ]; then
  284. BRANCH=$1
  285. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-urfave-cli.git $BRANCH
  286. sudo dpkg -i build-area/golang-github-urfave-cli-dev_*.deb
  287. sudo apt-get install -fy
  288. fi
  289. }
  290. # Build and install golang-github-golang-snappy deb
  291. build_install_snappydev() {
  292. if [ $BUILD_KCP -eq 1 ]; then
  293. BRANCH=$1
  294. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-golang-snappy.git $BRANCH
  295. sudo dpkg -i golang-github-golang-snappy-dev_*.deb
  296. sudo apt-get install -fy
  297. fi
  298. }
  299. # Build and install golang-github-xtaci-kcp deb
  300. build_install_kcpdev() {
  301. if [ $BUILD_KCP -eq 1 ]; then
  302. BRANCH=$1
  303. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-xtaci-kcp.git $BRANCH
  304. sudo dpkg -i golang-github-xtaci-kcp-dev_*.deb
  305. sudo apt-get install -fy
  306. fi
  307. }
  308. # Build and install golang-github-xtaci-smux deb
  309. build_install_smuxdev() {
  310. if [ $BUILD_KCP -eq 1 ]; then
  311. BRANCH=$1
  312. gbp_build https://anonscm.debian.org/git/pkg-go/packages/golang-github-xtaci-smux.git $BRANCH
  313. sudo dpkg -i golang-github-xtaci-smux-dev_*.deb
  314. sudo apt-get install -fy
  315. fi
  316. }
  317. # Build and install kcptun deb
  318. build_install_kcptun() {
  319. if [ $BUILD_KCP -eq 1 ]; then
  320. BRANCH=$1
  321. gbp_build https://anonscm.debian.org/git/pkg-go/packages/kcptun.git $BRANCH
  322. sudo dpkg -i kcptun_*.deb
  323. sudo apt-get install -fy
  324. fi
  325. }
  326. export XZ_DEFAULTS=--memlimit=128MiB
  327. OSID=$(grep ^ID= /etc/os-release|cut -d= -f2)
  328. OSVER=$(lsb_release -cs)
  329. BUILD_KCP=0
  330. BUILD_LIB=0
  331. BUILD_BIN=0
  332. case "$1" in
  333. --help|-h)
  334. help_usage
  335. ;;
  336. kcp)
  337. BUILD_KCP=1
  338. ;;
  339. lib)
  340. BUILD_LIB=1
  341. ;;
  342. bin)
  343. BUILD_BIN=1
  344. ;;
  345. all|"")
  346. BUILD_LIB=1
  347. BUILD_BIN=1
  348. ;;
  349. *)
  350. echo Parameter error, exiting ...
  351. exit
  352. esac
  353. # Exit if in a git repo
  354. [ -d .git ] && help_usage
  355. case "$OSVER" in
  356. jessie)
  357. BPO="debhelper libbloom-dev"
  358. BPOEXTRA=sloppy
  359. ;;
  360. stretch)
  361. BPO=libbloom-dev
  362. ;;
  363. xenial)
  364. BPO=debhelper
  365. ;;
  366. esac
  367. apt_init "git-buildpackage pristine-tar equivs" "$BPO" $BPOEXTRA
  368. [ $BUILD_KCP -eq 1 ] && case "$OSVER" in
  369. wheezy|precise|trusty)
  370. echo Sorry, your system $OSID/$OSVER is not supported.
  371. ;;
  372. jessie)
  373. build_install_urfaveclidev
  374. build_install_reedsolomondev
  375. build_install_kcpdev
  376. build_install_smuxdev
  377. build_install_kcptun
  378. ;;
  379. stretch|unstable|sid|yakkety|zesty)
  380. build_install_reedsolomondev
  381. build_install_kcpdev
  382. build_install_smuxdev
  383. build_install_kcptun
  384. ;;
  385. xenial)
  386. build_install_dhgolang debian/jessie-backports
  387. build_install_reedsolomondev
  388. build_install_errorsdev
  389. patch_urfaveclidev_xenial
  390. build_install_urfaveclidev
  391. build_install_snappydev debian/jessie-backports
  392. build_install_kcpdev
  393. build_install_smuxdev
  394. build_install_kcptun
  395. ;;
  396. esac
  397. [ $BUILD_LIB -eq 1 -o $BUILD_BIN -eq 1 ] && case "$OSVER" in
  398. wheezy|precise)
  399. echo Sorry, your system $OSID/$OSVER is not supported.
  400. ;;
  401. buster|testing|unstable|sid)
  402. build_install_sslibev
  403. ;;
  404. jessie|stretch)
  405. build_install_libsodium
  406. build_install_sslibev
  407. ;;
  408. zesty)
  409. build_install_libsodium
  410. build_install_libbloom
  411. build_install_sslibev
  412. build_install_simpleobfs
  413. ;;
  414. xenial|yakkety)
  415. build_install_libcork debian
  416. build_install_libcorkipset debian
  417. build_install_libsodium
  418. build_install_libbloom
  419. build_install_sslibev
  420. build_install_simpleobfs
  421. ;;
  422. trusty)
  423. build_install_libcork trusty
  424. build_install_libcorkipset trusty
  425. build_install_libmbedtls debian/jessie-backports
  426. build_install_libsodium
  427. build_install_libbloom trusty
  428. patch_sslibev_dh9
  429. build_install_sslibev
  430. build_install_simpleobfs trusty
  431. ;;
  432. *)
  433. echo Your system $OSID/$OSVER is not supported yet.
  434. echo Please report issue:
  435. echo " https://github.com/shadowsocks/shadowsocks-libev/issues/new"
  436. ;;
  437. esac
  438. apt_clean