github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/ClientLibrary/build-darwin.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -e -u -x
     4  
     5  # $2, if specified, is go build tags
     6  if [ -z ${2+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$2"; fi
     7  
     8  # Modify this value as we use newer Go versions.
     9  # Note:
    10  #   clangwrap.sh needs to be updated when the Go version changes.
    11  #   The last version was:
    12  #   https://github.com/golang/go/blob/go1.17.13/misc/ios/clangwrap.sh
    13  #     - with a patch to lower -mios-version-min to 7.0
    14  GO_VERSION_REQUIRED="1.17.13"
    15  
    16  # At this time, we don't support modules
    17  export GO111MODULE=off
    18  
    19  BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
    20  cd ${BASE_DIR}
    21  
    22  # The location of the final build products
    23  BUILD_DIR="${BASE_DIR}/build/darwin"
    24  TEMP_DIR="${BUILD_DIR}/tmp"
    25  
    26  # Clean previous output
    27  rm -rf "${BUILD_DIR}"
    28  
    29  mkdir -p ${TEMP_DIR}
    30  if [[ $? != 0 ]]; then
    31    echo "FAILURE: mkdir -p ${TEMP_DIR}"
    32    exit 1
    33  fi
    34  
    35  # Ensure go is installed
    36  which go 2>&1 > /dev/null
    37  if [[ $? != 0 ]]; then
    38    echo "Go is not installed in the path, aborting"
    39    exit 1
    40  fi
    41  
    42  # Exporting these seems necessary for subcommands to pick them up.
    43  export GOPATH=${TEMP_DIR}/go-darwin-build
    44  export PATH=${GOPATH}/bin:${PATH}
    45  
    46  # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
    47  rm -rf ${GOPATH}
    48  
    49  TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
    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  # Check Go version
    71  GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
    72  if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
    73    echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
    74    exit 1
    75  fi
    76  
    77  prepare_build () {
    78  
    79    # Ensure BUILD* variables reflect the tunnel-core repo
    80    cd ${TUNNEL_CORE_SRC_DIR}
    81  
    82    BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
    83    BUILDREPO=$(git config --get remote.origin.url)
    84    BUILDREV=$(git rev-parse --short HEAD)
    85    GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
    86  
    87    # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
    88    cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary
    89    DEPENDENCIES=$(echo -n "{" && GOOS=$1 go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | GOOS=$1 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 "}")
    90  
    91    LDFLAGS="\
    92    -s \
    93    -w \
    94    -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
    95    -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
    96    -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
    97    -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
    98    -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
    99    "
   100  
   101    echo "Variables for ldflags:"
   102    echo " Build date: ${BUILDDATE}"
   103    echo " Build repo: ${BUILDREPO}"
   104    echo " Build revision: ${BUILDREV}"
   105    echo " Go version: ${GOVERSION}"
   106    echo " Dependencies: ${DEPENDENCIES}"
   107    echo ""
   108  
   109  }
   110  
   111  
   112  build_for_ios () {
   113  
   114    IOS_BUILD_DIR="${BUILD_DIR}/ios"
   115    rm -rf "${IOS_BUILD_DIR}"
   116  
   117    prepare_build darwin
   118  
   119    # As of Go 1.15, "ios/arm" is no longer supported: https://golang.org/doc/go1.15#darwin
   120  
   121    CC=${BASE_DIR}/clangwrap.sh \
   122    CXX=${BASE_DIR}/clangwrap.sh \
   123    CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
   124    CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
   125    CGO_ENABLED=1 GOOS=ios GOARCH=arm64 go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o ${IOS_BUILD_DIR}/arm64/libpsiphontunnel.a PsiphonTunnel.go
   126  
   127  }
   128  
   129  build_for_macos () {
   130  
   131    MACOS_BUILD_DIR="${BUILD_DIR}/macos"
   132    rm -rf "${MACOS_BUILD_DIR}"
   133  
   134    prepare_build darwin
   135  
   136    # i386 is deprecated for macOS and does not link
   137    #TARGET_ARCH=386
   138    #CGO_ENABLED=1 GOOS=darwin GOARCH="${TARGET_ARCH}" go build -buildmode=c-shared -ldflags "-s ${LDFLAGS}" -tags "${BUILD_TAGS}" -o "${MACOS_BUILD_DIR}/${TARGET_ARCH}/libpsiphontunnel.dylib" PsiphonTunnel.go
   139  
   140    TARGET_ARCH=amd64
   141    CGO_ENABLED=1 GOOS=darwin GOARCH="${TARGET_ARCH}" go build -buildmode=c-shared -ldflags "-s ${LDFLAGS}" -tags "${BUILD_TAGS}" -o "${MACOS_BUILD_DIR}/${TARGET_ARCH}/libpsiphontunnel.dylib" PsiphonTunnel.go
   142  
   143  }
   144  
   145  cleanup () {
   146    # Remove temporary build artifacts
   147    rm -rf ${TEMP_DIR}
   148  }
   149  
   150  
   151  TARGET=$1
   152  case $TARGET in
   153    macos)
   154      echo "..Building for MacOS"
   155      build_for_macos
   156      if [ $? != 0 ]; then
   157        exit $?
   158      fi
   159  
   160      ;;
   161    ios)
   162      echo "..Building for iOS"
   163      build_for_ios
   164      if [ $? != 0 ]; then
   165        exit $?
   166      fi
   167  
   168      ;;
   169    all)
   170      echo "..Building all"
   171      build_for_ios
   172      if [ $? != 0 ]; then
   173        exit $?
   174      fi
   175  
   176      build_for_macos
   177      if [ $? != 0 ]; then
   178        exit $?
   179      fi
   180  
   181      ;;
   182    *)
   183      echo "..invalid target"
   184      exit 1
   185  
   186      ;;
   187  
   188  esac
   189  
   190  cleanup
   191  echo "BUILD DONE"