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.

493 lines
13 KiB

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