github.com/astaguna/popon-core@v0.0.0-20231019235610-96e42d76a5ff/ClientLibrary/make.bash (about)

     1  #!/usr/bin/env bash
     2  
     3  set -e -u -x
     4  
     5  if [ ! -f make.bash ]; then
     6    echo "make.bash must be run from $GOPATH/src/github.com/astaguna/popon-core/ClientLibrary"
     7    exit 1
     8  fi
     9  
    10  # $2, if specified, is go build tags
    11  if [ -z ${2+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$2"; fi
    12  
    13  export GOCACHE=/tmp
    14  
    15  BUILD_DIR=build
    16  
    17  if [ ! -d ${BUILD_DIR} ]; then
    18    mkdir ${BUILD_DIR}
    19  fi
    20  
    21  prepare_build () {
    22  
    23    BUILDDATE=$(date --iso-8601=seconds)
    24    BUILDREPO=$(git config --get remote.origin.url)
    25    BUILDREV=$(git rev-parse --short HEAD)
    26    GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
    27  
    28    LDFLAGS="\
    29    -s \
    30    -w \
    31    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
    32    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
    33    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
    34    -X github.com/astaguna/popon-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
    35    "
    36  
    37    echo "Variables for ldflags:"
    38    echo " Build date: ${BUILDDATE}"
    39    echo " Build repo: ${BUILDREPO}"
    40    echo " Build revision: ${BUILDREV}"
    41    echo " Go version: ${GOVERSION}"
    42    echo ""
    43  
    44  }
    45  
    46  
    47  build_for_android () {
    48  
    49    TARGET_OS=android
    50    OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
    51  
    52    prepare_build android
    53  
    54    TARGET_ARCH=arm
    55    ARMV=7
    56  
    57    CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang" \
    58    CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang++" \
    59    GOARM=${ARMV} \
    60    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}${ARMV}/libpsiphontunnel.so" PsiphonTunnel.go
    61  
    62  
    63    TARGET_ARCH=arm64
    64  
    65    CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang" \
    66    CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang++" \
    67    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
    68  
    69  }
    70  
    71  
    72  build_for_linux () {
    73  
    74    TARGET_OS=linux
    75    OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
    76  
    77    prepare_build linux
    78  
    79    TARGET_ARCH=386
    80    # TODO: is "CFLAGS=-m32" required?
    81    CFLAGS=-m32 \
    82    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
    83  
    84  
    85    TARGET_ARCH=amd64
    86    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
    87  
    88  }
    89  
    90  
    91  build_for_windows () {
    92  
    93    TARGET_OS=windows
    94    OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
    95  
    96    prepare_build windows
    97  
    98    TARGET_ARCH=386
    99  
   100    CGO_ENABLED=1 \
   101    CGO_LDFLAGS="-static-libgcc -L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
   102    CC=/usr/bin/i686-w64-mingw32-gcc \
   103    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/psiphontunnel.dll" PsiphonTunnel.go
   104  
   105  
   106    TARGET_ARCH=amd64
   107  
   108    CGO_ENABLED=1 \
   109    CGO_LDFLAGS="-static-libgcc -L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
   110    CC=/usr/bin/x86_64-w64-mingw32-gcc \
   111    GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/psiphontunnel.dll" PsiphonTunnel.go
   112  
   113  }
   114  
   115  
   116  build_for_ios () {
   117  
   118    echo "To build for iOS please use build-darwin.sh"
   119  
   120  }
   121  
   122  
   123  build_for_macos () {
   124  
   125    echo "To build for macos please use build-darwin.sh"
   126  
   127  }
   128  
   129  
   130  TARGET=$1
   131  case $TARGET in
   132    windows)
   133      echo "..Building for Windows"
   134      build_for_windows
   135      exit $?
   136  
   137      ;;
   138    linux)
   139      echo "..Building for Linux"
   140      build_for_linux
   141      exit $?
   142  
   143      ;;
   144    macos)
   145      echo "..Building for MacOS"
   146      build_for_macos
   147      exit $?
   148  
   149      ;;
   150    android)
   151      echo "..Building for Android"
   152      build_for_android
   153      exit $?
   154  
   155      ;;
   156    ios)
   157      echo "..Building for iOS"
   158      build_for_ios
   159      exit $?
   160  
   161      ;;
   162    all)
   163      echo "..Building all"
   164      build_for_windows
   165      if [ $? != 0 ]; then
   166        exit $?
   167      fi
   168  
   169      build_for_linux
   170      if [ $? != 0 ]; then
   171        exit $?
   172      fi
   173  
   174      build_for_macos
   175      if [ $? != 0 ]; then
   176        exit $?
   177      fi
   178  
   179      build_for_android
   180      if [ $? != 0 ]; then
   181        exit $?
   182      fi
   183  
   184      build_for_ios
   185      if [ $? != 0 ]; then
   186        exit $?
   187      fi
   188  
   189      ;;
   190    *)
   191      echo "..invalid target"
   192      exit 1
   193  
   194  
   195      ;;
   196  
   197  esac
   198  
   199  echo "BUILD DONE"