build_trojan-go.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. #
  3. # This is a Shell script for build multi-architectures trojan-go binary file
  4. #
  5. # Supported architectures: amd64, arm32v6, arm32v7, arm64v8, i386, ppc64le, s390x
  6. #
  7. # Copyright (C) 2020 Teddysun <[email protected]>
  8. #
  9. # Reference URL:
  10. # https://github.com/p4gefau1t/trojan-go
  11. cur_dir="$(pwd)"
  12. COMMANDS=( git go )
  13. for CMD in "${COMMANDS[@]}"; do
  14. if [ ! "$(command -v "${CMD}")" ]; then
  15. echo "${CMD} is not installed, please install it and try again" && exit 1
  16. fi
  17. done
  18. cd ${cur_dir}
  19. version=$(wget --no-check-certificate -qO- https://api.github.com/repos/p4gefau1t/trojan-go/tags | grep 'name' | cut -d\" -f4 | head -1)
  20. echo "git clone -b ${version} https://github.com/p4gefau1t/trojan-go.git"
  21. git clone -b ${version} https://github.com/p4gefau1t/trojan-go.git
  22. cd trojan-go || exit 2
  23. PACKAGE_NAME="github.com/p4gefau1t/trojan-go"
  24. VERSION="$(git describe)"
  25. COMMIT="$(git rev-parse HEAD)"
  26. VAR_SETTING=""
  27. VAR_SETTING="${VAR_SETTING} -X ${PACKAGE_NAME}/constant.Version=${VERSION}"
  28. VAR_SETTING="${VAR_SETTING} -X ${PACKAGE_NAME}/constant.Commit=${COMMIT}"
  29. go get -d -v
  30. LDFLAGS="-s -w ${VAR_SETTING}"
  31. ARCHS=( 386 amd64 arm arm64 ppc64le s390x )
  32. ARMS=( 6 7 )
  33. for ARCH in ${ARCHS[@]}; do
  34. if [ "${ARCH}" = "arm" ]; then
  35. for V in ${ARMS[@]}; do
  36. echo "Building trojan-go_linux_${ARCH}${V}"
  37. env CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GOARM=${V} go build -v -tags "full" -ldflags "${LDFLAGS}" -o ${cur_dir}/trojan-go_linux_${ARCH}${V}
  38. done
  39. else
  40. echo "Building trojan-go_linux_${ARCH}"
  41. env CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -v -tags "full" -ldflags "${LDFLAGS}" -o ${cur_dir}/trojan-go_linux_${ARCH}
  42. fi
  43. done
  44. chmod +x ${cur_dir}/trojan-go_linux_*
  45. # clean up
  46. cd ${cur_dir} && rm -fr trojan-go