github.com/astaguna/popon-core@v0.0.0-20231019235610-96e42d76a5ff/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.19.8/misc/ios/clangwrap.sh
    13  GO_VERSION_REQUIRED="1.19.8"
    14  
    15  BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
    16  cd ${BASE_DIR}
    17  
    18  # The location of the final build products
    19  BUILD_DIR="${BASE_DIR}/build/darwin"
    20  TEMP_DIR="${BUILD_DIR}/tmp"
    21  
    22  # Clean previous output
    23  rm -rf "${BUILD_DIR}"
    24  
    25  mkdir -p ${TEMP_DIR}
    26  if [[ $? != 0 ]]; then
    27    echo "FAILURE: mkdir -p ${TEMP_DIR}"
    28    exit 1
    29  fi
    30  
    31  # Ensure go is installed
    32  which go 2>&1 > /dev/null
    33  if [[ $? != 0 ]]; then
    34    echo "Go is not installed in the path, aborting"
    35    exit 1
    36  fi
    37  
    38  # Exporting these seems necessary for subcommands to pick them up.
    39  export GOPATH=${TEMP_DIR}/go-darwin-build
    40  export PATH=${GOPATH}/bin:${PATH}
    41  
    42  # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
    43  rm -rf ${GOPATH}
    44  
    45  TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/astaguna/popon-core
    46  
    47  mkdir -p ${GOPATH}
    48  if [[ $? != 0 ]]; then
    49    echo "FAILURE: mkdir -p ${GOPATH}"
    50    exit 1
    51  fi
    52  
    53  # Symlink the current source directory into GOPATH, so that we're building the
    54  # code in this local repo, rather than pulling from Github and building that.
    55  mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
    56  if [[ $? != 0 ]]; then
    57    echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
    58    exit 1
    59  fi
    60  ln -s "${BASE_DIR}/.." "${GOPATH}/src/github.com/astaguna/popon-core"
    61  if [[ $? != 0 ]]; then
    62    echo "ln -s ../.. ${GOPATH}/src/github.com/astaguna/popon-core"
    63    exit 1
    64  fi
    65  
    66  # Check Go version
    67  GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
    68  if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
    69    echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
    70    exit 1
    71  fi
    72  
    73  prepare_build () {
    74  
    75    # Ensure BUILD* variables reflect the tunnel-core repo
    76    cd ${TUNNEL_CORE_SRC_DIR}
    77  
    78    BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
    79    BUILDREPO=$(git config --get remote.origin.url)
    80    BUILDREV=$(git rev-parse --short HEAD)
    81    GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
    82  
    83    cd ${GOPATH}/src/github.com/astaguna/popon-core/ClientLibrary
    84  
    85    LDFLAGS="\
    86    -s \
    87    -w \
    88    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
    89    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
    90    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
    91    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
    92    "
    93  
    94    echo "Variables for ldflags:"
    95    echo " Build date: ${BUILDDATE}"
    96    echo " Build repo: ${BUILDREPO}"
    97    echo " Build revision: ${BUILDREV}"
    98    echo " Go version: ${GOVERSION}"
    99    echo ""
   100  
   101  }
   102  
   103  
   104  build_for_ios () {
   105  
   106    IOS_BUILD_DIR="${BUILD_DIR}/ios"
   107    rm -rf "${IOS_BUILD_DIR}"
   108  
   109    prepare_build darwin
   110  
   111    # As of Go 1.15, "ios/arm" is no longer supported: https://golang.org/doc/go1.15#darwin
   112  
   113    CC=${BASE_DIR}/clangwrap.sh \
   114    CXX=${BASE_DIR}/clangwrap.sh \
   115    CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
   116    CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
   117    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
   118  
   119  }
   120  
   121  build_for_macos () {
   122  
   123    MACOS_BUILD_DIR="${BUILD_DIR}/macos"
   124    rm -rf "${MACOS_BUILD_DIR}"
   125  
   126    prepare_build darwin
   127  
   128    # i386 is deprecated for macOS and does not link
   129    #TARGET_ARCH=386
   130    #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
   131  
   132    TARGET_ARCH=amd64
   133    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
   134  
   135  }
   136  
   137  cleanup () {
   138    # Remove temporary build artifacts
   139    rm -rf ${TEMP_DIR}
   140  }
   141  
   142  
   143  TARGET=$1
   144  case $TARGET in
   145    macos)
   146      echo "..Building for MacOS"
   147      build_for_macos
   148      if [ $? != 0 ]; then
   149        exit $?
   150      fi
   151  
   152      ;;
   153    ios)
   154      echo "..Building for iOS"
   155      build_for_ios
   156      if [ $? != 0 ]; then
   157        exit $?
   158      fi
   159  
   160      ;;
   161    all)
   162      echo "..Building all"
   163      build_for_ios
   164      if [ $? != 0 ]; then
   165        exit $?
   166      fi
   167  
   168      build_for_macos
   169      if [ $? != 0 ]; then
   170        exit $?
   171      fi
   172  
   173      ;;
   174    *)
   175      echo "..invalid target"
   176      exit 1
   177  
   178      ;;
   179  
   180  esac
   181  
   182  cleanup
   183  echo "BUILD DONE"