github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/MobileLibrary/Android/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/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/Android"
     7    exit 1
     8  fi
     9  
    10  # $1, if specified, is go build tags
    11  if [ -z ${1+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$1"; fi
    12  
    13  # At this time, psiphon-tunnel-core doesn't support modules
    14  export GO111MODULE=off
    15  
    16  BUILDINFOFILE="psiphon-tunnel-core_buildinfo.txt"
    17  BUILDDATE=$(date --iso-8601=seconds)
    18  BUILDREPO=$(git config --get remote.origin.url)
    19  BUILDREV=$(git rev-parse --short HEAD)
    20  GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
    21  GOMOBILEVERSION=$(gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
    22  
    23  # DEPENDENCIES
    24  #
    25  # - this script produces a JSON object listing all Go package dependencies,
    26  #   excluding packages under github.com/Psiphon-Labs/psiphon-tunnel-core/
    27  #   (thus also excluding vendored packages) which will all have the same rev
    28  #   as BUILDREV
    29  #
    30  # - starts the string with a `{` and ends with a `}`
    31  #
    32  # - uses the `go list` command and passes it a template string (using the Go
    33  #   template syntax) saying I want all the dependencies of the package in the
    34  #   current directory, printing 1/line via printf
    35  #
    36  # - pipes to `xargs` to run a command on each line output from the first
    37  #   command and uses `go list` with a template string to print the "Import
    38  #   Path" (from just below `$GOPATH/src`) if the package is not part of the
    39  #   standard library
    40  #
    41  # - pipes to `xargs` again, specifiying `pkg` as the placeholder name for each
    42  #   item being operated on (which is the list of non standard library import
    43  #   paths from the previous step); `xargs` runs a bash script (via `-c`) which
    44  #   changes to each import path in sequence, then echoes out, after the
    45  #   exclusion check, `"<import path>":"<subshell output of getting the short
    46  #   git revision>",`
    47  #
    48  # - for non-empty dependency lists, the last command leaves a trailing `,\n` at
    49  #   the end, so use `sed` and `tr` to remove the suffix.
    50  #
    51  DEPENDENCIES=$(cd ../psi && echo -n "{" && GOOS=android go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | GOOS=android 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 "}")
    52  
    53  LDFLAGS="\
    54  -s \
    55  -w \
    56  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
    57  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
    58  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
    59  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
    60  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=$GOMOBILEVERSION \
    61  -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
    62  "
    63  
    64  echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
    65  
    66  echo "Variables for ldflags:"
    67  echo " Build date: ${BUILDDATE}"
    68  echo " Build repo: ${BUILDREPO}"
    69  echo " Build revision: ${BUILDREV}"
    70  echo " Go version: ${GOVERSION}"
    71  echo " Gomobile version: ${GOMOBILEVERSION}"
    72  echo " Dependencies: ${DEPENDENCIES}"
    73  echo ""
    74  
    75  # Note: android/386 is x86, which is used on both x86 and x86_64 Android
    76  # devices. We are excluding the android/amd64, x86_64, ABI as it causes a
    77  # crash in Android x86_64 emulators: "seccomp prevented call to disallowed
    78  # x86_64 system call 22". x86/linux syscall 22 is pipe.
    79  #
    80  # In Android seccomp config, pipe is permitted only for 32-bit platforms:
    81  # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SECCOMP_WHITELIST_APP.TXT#7.
    82  #
    83  # The Go syscall.Pipe on linux(android)/amd64 is the disallowed pipe:
    84  # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_amd64.go#L115-L126
    85  #
    86  # A potential future fix is to use the allowed pipe2,
    87  # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SYSCALLS.TXT#129,
    88  # which is what linux(android)/arm64 uses, for example:
    89  # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_arm64.go#L150-L159.
    90  
    91  gomobile bind -v -x -target=android/arm,android/arm64,android/386 -tags="${BUILD_TAGS}" -ldflags="$LDFLAGS" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
    92  if [ $? != 0 ]; then
    93    echo "..'gomobile bind' failed, exiting"
    94    exit $?
    95  fi
    96  
    97  mkdir -p build-tmp/psi
    98  unzip -o psi.aar -d build-tmp/psi
    99  yes | cp -f PsiphonTunnel/AndroidManifest.xml build-tmp/psi/AndroidManifest.xml
   100  yes | cp -f PsiphonTunnel/libs/armeabi-v7a/libtun2socks.so build-tmp/psi/jni/armeabi-v7a/libtun2socks.so
   101  yes | cp -f PsiphonTunnel/libs/arm64-v8a/libtun2socks.so build-tmp/psi/jni/arm64-v8a/libtun2socks.so
   102  yes | cp -f PsiphonTunnel/libs/x86/libtun2socks.so build-tmp/psi/jni/x86/libtun2socks.so
   103  mkdir -p build-tmp/psi/res/xml
   104  yes | cp -f PsiphonTunnel/ca_psiphon_psiphontunnel_backup_rules.xml build-tmp/psi/res/xml/ca_psiphon_psiphontunnel_backup_rules.xml
   105  
   106  javac -d build-tmp -bootclasspath $ANDROID_HOME/platforms/android-$ANDROID_PLATFORM_VERSION/android.jar -source 1.8 -target 1.8 -classpath build-tmp/psi/classes.jar PsiphonTunnel/PsiphonTunnel.java
   107  if [ $? != 0 ]; then
   108    echo "..'javac' compiling PsiphonTunnel failed, exiting"
   109    exit $?
   110  fi
   111  
   112  cd build-tmp
   113  
   114  jar uf psi/classes.jar ca/psiphon/*.class
   115  if [ $? != 0 ]; then
   116    echo "..'jar' failed to add classes, exiting"
   117    exit $?
   118  fi
   119  
   120  cd -
   121  cd build-tmp/psi
   122  echo -e "-keep class psi.** { *; }\n-keep class ca.psiphon.** { *; }\n"  >> proguard.txt
   123  rm -f ../../ca.psiphon.aar
   124  zip -r ../../ca.psiphon.aar ./
   125  cd -
   126  rm -rf build-tmp
   127  echo "Done"