build_trojan-go.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. git clone https://github.com/p4gefau1t/trojan-go.git
  20. cd trojan-go || exit 2
  21. PACKAGE_NAME="github.com/p4gefau1t/trojan-go"
  22. VERSION="$(git describe)"
  23. COMMIT="$(git rev-parse HEAD)"
  24. VAR_SETTING=""
  25. VAR_SETTING="${VAR_SETTING} -X ${PACKAGE_NAME}/constant.Version=${VERSION}"
  26. VAR_SETTING="${VAR_SETTING} -X ${PACKAGE_NAME}/constant.Commit=${COMMIT}"
  27. go get -d -v
  28. LDFLAGS="-s -w ${VAR_SETTING}"
  29. ARCHS=( 386 amd64 arm arm64 ppc64le s390x )
  30. ARMS=( 6 7 )
  31. for ARCH in ${ARCHS[@]}; do
  32. if [ "${ARCH}" = "arm" ]; then
  33. for V in ${ARMS[@]}; do
  34. echo "Building trojan-go_linux_${ARCH}${V}"
  35. 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}
  36. done
  37. else
  38. echo "Building trojan-go_linux_${ARCH}"
  39. env CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -v -tags "full" -ldflags "${LDFLAGS}" -o ${cur_dir}/trojan-go_linux_${ARCH}
  40. fi
  41. done
  42. chmod +x ${cur_dir}/trojan-go_linux_*
  43. # clean up
  44. cd ${cur_dir} && rm -fr trojan-go