github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/MobileLibrary/iOS/build-psiphon-framework.sh (about) 1 #!/usr/bin/env bash 2 3 set -e -u -x 4 5 if [ -z ${1+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$1"; fi 6 7 # Modify this value as we use newer Go versions. 8 GO_VERSION_REQUIRED="1.17.13" 9 10 # At this time, psiphon-tunnel-core doesn't support modules 11 export GO111MODULE=off 12 13 # Reset the PATH to macOS default. This is mainly so we don't execute the wrong 14 # gomobile executable. 15 PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin 16 17 # $GOROOT/bin allows build automation to provide various Go versions dynamically. 18 # As gomobile would be installed at $GOPATH/bin, there is minimal risk that 19 # adding $GOROOT/bin will run an unexpected gomobile binary. 20 PATH=$GOROOT/bin:$PATH 21 22 BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P) 23 cd "${BASE_DIR}" 24 25 # The location of the final framework build 26 BUILD_DIR="${BASE_DIR}/build" 27 28 # Ensure go is installed 29 which go 2>&1 > /dev/null 30 if [[ $? != 0 ]]; then 31 echo "Go is not installed in the path, aborting" 32 exit 1 33 fi 34 35 UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/ 36 37 # Exporting these seems necessary for subcommands to pick them up. 38 export GOPATH=${PWD}/go-ios-build 39 export PATH=${GOPATH}/bin:${PATH} 40 41 # The GOPATH we're using is temporary, so make sure there isn't one from a previous run. 42 rm -rf "${GOPATH}" 43 44 GOMOBILE_PINNED_REV=ce6a79cf6a13dd77095a6f8dbee5f39848fa7da1 45 GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile 46 47 TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core 48 49 PATH=${PATH}:${GOPATH}/bin 50 51 mkdir -p "${GOPATH}" 52 if [[ $? != 0 ]]; then 53 echo "FAILURE: mkdir -p ${GOPATH}" 54 exit 1 55 fi 56 57 # Symlink the current source directory into GOPATH, so that we're building the 58 # code in this local repo, rather than pulling from Github and building that. 59 mkdir -p "${GOPATH}/src/github.com/Psiphon-Labs" 60 if [[ $? != 0 ]]; then 61 echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs" 62 exit 1 63 fi 64 ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core" 65 if [[ $? != 0 ]]; then 66 echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core" 67 exit 1 68 fi 69 70 # Builds Psi.framework library for the given platform. 71 # 72 # - Parameter 1: `gomobile bind` -target option value 73 # - Parameter 2: Variable name where gomobile output will be set to. 74 function gomobile_build_for_platform() { 75 76 # Possible values are "ios" and "simulator" 77 local TARGETS=$1 78 79 echo "Build library for targets ${TARGETS}" 80 81 local GOBIND_OUT="${BUILD_DIR}/gobind-framework/Psi.xcframework" 82 83 # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693 84 # CGO_CFLAGS_ALLOW is to workaround https://github.com/golang/go/issues/23742 in Go 1.9.4 85 CGO_CFLAGS_ALLOW="-fmodules|-fblocks" "${GOPATH}"/bin/gomobile bind -v -x \ 86 -target "${TARGETS}" \ 87 -iosversion "10.0" \ 88 -prefix Go \ 89 -tags="${BUILD_TAGS}" \ 90 -ldflags="${LDFLAGS}" \ 91 -o "${GOBIND_OUT}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi 92 93 rc=$?; if [[ $rc != 0 ]]; then 94 echo "FAILURE: gomobile bind failed". 95 exit $rc 96 fi 97 98 99 # Sets parameter $2 to value of GOBIND_OUT. 100 eval "$2=${GOBIND_OUT}" 101 } 102 103 # 104 # Check Go version 105 # 106 107 GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p') 108 if [[ ${GO_VERSION} != "${GO_VERSION_REQUIRED}" ]]; then 109 echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}" 110 exit 1 111 fi 112 113 # 114 # Get and install gomobile, using our pinned revision 115 # 116 117 go get -u golang.org/x/mobile/cmd/gomobile 118 cd "${GOPATH}"/src/golang.org/x/mobile/cmd/gomobile 119 git checkout master 120 git checkout -b pinned ${GOMOBILE_PINNED_REV} 121 122 # Patch gomobile to edit a command that assumes modules 123 mv init.go init.go.orig 124 sed -e 's/golang.org\/x\/mobile\/cmd\/gobind@latest/golang.org\/x\/mobile\/cmd\/gobind/g' init.go.orig > init.go 125 126 127 go install 128 "${GOPATH}"/bin/gomobile init -v -x 129 if [[ $? != 0 ]]; then 130 echo "FAILURE: ${GOPATH}/bin/gomobile init" 131 exit 1 132 fi 133 134 # Ensure BUILD* variables reflect the tunnel-core repo 135 cd "${TUNNEL_CORE_SRC_DIR}" 136 137 BUILDINFOFILE="${BASE_DIR}/psiphon-tunnel-core_buildinfo.txt" 138 BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z) 139 BUILDREPO=$(git config --get remote.origin.url) 140 BUILDREV=$(git rev-parse --short HEAD) 141 GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1') 142 GOMOBILEVERSION=$("${GOPATH}"/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1') 143 144 # see DEPENDENCIES comment in MobileLibrary/Android/make.bash 145 cd "${GOPATH}"/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi 146 DEPENDENCIES=$(echo -n "{" && GOOS=darwin go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | GOOS=darwin xargs go list -tags "${BUILD_TAGS}" -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'cd $GOPATH/src/$0 && if echo -n "$0" | grep -vEq "^github.com/Psiphon-Labs/psiphon-tunnel-core/" ; then echo -n "\"$0\":\"$(git rev-parse --short HEAD)\"," ; fi' pkg | sed 's/,$//' | tr -d '\n' && echo -n "}") 147 148 LDFLAGS="\ 149 -s \ 150 -w \ 151 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \ 152 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \ 153 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \ 154 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \ 155 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=${GOMOBILEVERSION} \ 156 -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=${DEPENDENCIES} \ 157 " 158 159 echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > "$BUILDINFOFILE" 160 161 echo "" 162 echo "Variables for ldflags:" 163 echo " Build date: ${BUILDDATE}" 164 echo " Build repo: ${BUILDREPO}" 165 echo " Build revision: ${BUILDREV}" 166 echo " Go version: ${GOVERSION}" 167 echo " Gomobile version: ${GOMOBILEVERSION}" 168 echo "" 169 170 171 # 172 # Clean previous output 173 # 174 rm -rf "${BUILD_DIR}" 175 176 177 # 178 # Builds Psi.xcframework 179 # 180 IOS_PSI_FRAMEWORK="" 181 gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK 182 183 echo "$IOS_PSI_FRAMEWORK" 184 185 # 186 # Copies gobind output Psi.xcframework to the Xcode project 187 # 188 189 rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework" 190 cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel" 191 192 # 193 # Build PsiphonTunnel framework for iOS. 194 # 195 196 IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive" 197 198 xcodebuild clean archive \ 199 -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \ 200 -scheme "PsiphonTunnel" \ 201 -configuration "Release" \ 202 -sdk iphoneos \ 203 -archivePath "${IOS_ARCHIVE}" \ 204 CODE_SIGN_IDENTITY="" \ 205 CODE_SIGNING_REQUIRED="NO" \ 206 CODE_SIGN_ENTITLEMENTS="" \ 207 CODE_SIGNING_ALLOWED="NO" \ 208 STRIP_BITCODE_FROM_COPIED_FILES="NO" \ 209 BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \ 210 ONLY_ACTIVE_ARCH="NO" \ 211 SKIP_INSTALL="NO" \ 212 EXCLUDED_ARCHS="armv7" 213 214 215 # Build PsiphonTunnel framework for simulator. 216 # 217 # Note: 218 # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386". 219 # - Excludes ARM Macs: EXCLUDED_ARCHS="arm64". 220 221 SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive" 222 223 xcodebuild clean archive \ 224 -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \ 225 -scheme "PsiphonTunnel" \ 226 -configuration "Release" \ 227 -sdk iphonesimulator \ 228 -archivePath "${SIMULATOR_ARCHIVE}" \ 229 CODE_SIGN_IDENTITY="" \ 230 CODE_SIGNING_REQUIRED="NO" \ 231 CODE_SIGN_ENTITLEMENTS="" \ 232 CODE_SIGNING_ALLOWED="NO" \ 233 STRIP_BITCODE_FROM_COPIED_FILES="NO" \ 234 BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \ 235 ONLY_ACTIVE_ARCH="NO" \ 236 SKIP_INSTALL="NO" \ 237 EXCLUDED_ARCHS="arm64 i386" 238 239 # 240 # Bundling the generated frameworks into a single PsiphonTunnel.xcframework 241 # 242 243 xcodebuild -create-xcframework \ 244 -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \ 245 -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \ 246 -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \ 247 -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \ 248 -output "${BUILD_DIR}/PsiphonTunnel.xcframework" 249 250 251 # Jenkins loses symlinks from the framework directory, which results in a build 252 # artifact that is invalid to use in an App Store app. Instead, we will zip the 253 # resulting build and use that as the artifact. 254 cd "${BUILD_DIR}" 255 256 zip --recurse-paths --symlinks build.zip ./PsiphonTunnel.xcframework --exclude "*.DS_Store" 257 258 echo "BUILD DONE"