build-psiphon-framework.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env bash
  2. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  3. cd ${BASE_DIR}
  4. # Ensure go is installed
  5. which go 2>&1 > /dev/null
  6. if [ $? -ne 0 ]; then
  7. echo "Go is not installed in the path, aborting"
  8. exit 1
  9. fi
  10. OUTPUT_DIR=${BASE_DIR}/framework
  11. OUTPUT_FILE=Psi.framework
  12. LIBSSL=${BASE_DIR}/OpenSSL-for-iPhone/lib/libssl.a
  13. LIBCRYPTO=${BASE_DIR}/OpenSSL-for-iPhone/lib/libcrypto.a
  14. OPENSSL_INCLUDE=${BASE_DIR}/OpenSSL-for-iPhone/include/
  15. # Not exporting this breaks go commands later if run via jenkins
  16. export GOPATH=${PWD}/go-ios-build
  17. GOMOBILE_PINNED_REV=8ab5dbbea1dc4713a98b6f1d51de4582a43e3fa8
  18. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  19. IOS_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-ios
  20. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  21. OPENSSL_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Inc/openssl
  22. PATH=${PATH}:${GOPATH}/bin
  23. mkdir -p ${GOPATH}
  24. mkdir -p ${OUTPUT_DIR}
  25. if [ ! -e ${IOS_SRC_DIR} ]; then
  26. echo "iOS source directory (${IOS_SRC_DIR}) not found, creating link"
  27. mkdir -p $(dirname ${IOS_SRC_DIR})
  28. ln -s $(pwd -P) $IOS_SRC_DIR
  29. if [ $? -ne 0 ]; then
  30. echo "..Could not create symlink, aborting"
  31. exit 1
  32. fi
  33. fi
  34. cd OpenSSL-for-iPhone && ./build-libssl.sh; cd -
  35. go get -d -v github.com/Psiphon-Inc/openssl
  36. go get -d -v github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  37. function check_pinned_version() {
  38. echo "Checking for gomobile revision: '${GOMOBILE_PINNED_REV}'"
  39. if [ -e ${GOMOBILE_PATH} ]; then
  40. echo "..Gomobile path found"
  41. cd ${GOMOBILE_PATH}
  42. CURRENT_REVISION=$(git rev-parse HEAD)
  43. if [ ${CURRENT_REVISION} != ${GOMOBILE_PINNED_REV} ]; then
  44. echo "..Current revision '${CURRENT_REVISION}' does not match"
  45. return 1
  46. else
  47. echo "..Current revision matches"
  48. return 0
  49. fi
  50. else
  51. echo "Could not find GOMOBILE_PATH (${GOMOBILE_PATH})"
  52. return 1
  53. fi
  54. }
  55. check_pinned_version
  56. if [ $? -ne 0 ]; then
  57. go get -u golang.org/x/mobile/cmd/gomobile
  58. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  59. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  60. go build
  61. gomobile init -v
  62. check_pinned_version
  63. if [ $? -ne 0 ]; then
  64. echo "gomobile not found, aborting"
  65. exit 1
  66. fi
  67. fi
  68. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  69. BUILDREPO=$(git config --get remote.origin.url)
  70. BUILDREV=$(git rev-parse --short HEAD)
  71. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  72. GOMOBILEVERSION=$(gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  73. LDFLAGS="\
  74. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildDate=${BUILDDATE} \
  75. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRepo=${BUILDREPO} \
  76. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRev=${BUILDREV} \
  77. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.goVersion=${GOVERSION} \
  78. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.gomobileVersion=${GOMOBILEVERSION} \
  79. "
  80. echo ""
  81. echo "Variables for ldflags:"
  82. echo " Build date: ${BUILDDATE}"
  83. echo " Build repo: ${BUILDREPO}"
  84. echo " Build revision: ${BUILDREV}"
  85. echo " Go version: ${GOVERSION}"
  86. echo " Gomobile version: ${GOMOBILEVERSION}"
  87. echo ""
  88. # Patch source files to build on Darwin
  89. LC_ALL=C sed -i -- 's/+build android windows$/+build android windows darwin/' "${TUNNEL_CORE_SRC_DIR}/psiphon/opensslConn.go"
  90. LC_ALL=C sed -i -- 's/+build !android,!windows$/+build !android,!windows,!darwin/' "${TUNNEL_CORE_SRC_DIR}/psiphon/opensslConn_unsupported.go"
  91. IOS_CGO_BUILD_FLAGS='// #cgo darwin CFLAGS: -I'"${OPENSSL_INCLUDE}"'\
  92. // #cgo darwin LDFLAGS:'"${LIBSSL}"'\
  93. // #cgo darwin LDFLAGS:'"${LIBCRYPTO}"''
  94. LC_ALL=C sed -i -- "s|// #cgo pkg-config: libssl|${IOS_CGO_BUILD_FLAGS}|" "${OPENSSL_SRC_DIR}/build.go"
  95. gomobile bind -v -target ios -ldflags="${LDFLAGS}" -o ${OUTPUT_DIR}/${OUTPUT_FILE} github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi