You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
4.3 KiB

  1. #!/bin/sh
  2. nl="
  3. "
  4. log() {
  5. # shellcheck disable=SC2039
  6. local IFS=" "
  7. printf '%s\n' "$*"
  8. }
  9. log_error() {
  10. # shellcheck disable=SC2039
  11. local IFS=" "
  12. printf 'Error: %s\n' "$*" >&2
  13. }
  14. link_contents() {
  15. # shellcheck disable=SC2039
  16. local src="$1" dest="$2" IFS="$nl" dirs dir base
  17. if [ -n "$(find "$src" -mindepth 1 -maxdepth 1 -name "*.go" -not -type d)" ]; then
  18. log_error "$src is already a Go library"
  19. return 1
  20. fi
  21. dirs="$(find "$src" -mindepth 1 -maxdepth 1 -type d)"
  22. for dir in $dirs; do
  23. base="${dir##*/}"
  24. if [ -d "$dest/$base" ]; then
  25. case "$dir" in
  26. *$GO_BUILD_DEPENDS_SRC/$GO_PKG)
  27. log "$GO_PKG is already installed. Please check for circular dependencies."
  28. ;;
  29. *)
  30. link_contents "$src/$base" "$dest/$base"
  31. ;;
  32. esac
  33. else
  34. log "...${src#$GO_BUILD_DEPENDS_SRC}/$base"
  35. ln -sf "$src/$base" "$dest/$base"
  36. fi
  37. done
  38. return 0
  39. }
  40. configure() {
  41. # shellcheck disable=SC2039
  42. local files code testdata gomod pattern extra IFS file dest
  43. cd "$BUILD_DIR" || return 1
  44. files="$(find ./ -path "*/.*" -prune -o -not -type d -print)"
  45. if [ "$GO_INSTALL_ALL" != 1 ]; then
  46. code="$(printf '%s\n' "$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$')"
  47. testdata="$(printf '%s\n' "$files" | grep '/testdata/')"
  48. gomod="$(printf '%s\n' "$files" | grep '/go\.\(mod\|sum\)$')"
  49. for pattern in $GO_INSTALL_EXTRA; do
  50. extra="$(printf '%s\n' "$extra"; printf '%s\n' "$files" | grep -e "$pattern")"
  51. done
  52. files="$(printf '%s\n%s\n%s\n%s\n' "$code" "$testdata" "$gomod" "$extra" | grep -v '^[[:space:]]*$' | sort -u)"
  53. fi
  54. IFS="$nl"
  55. log "Copying files from $BUILD_DIR into $GO_BUILD_DIR/src/$GO_PKG"
  56. mkdir -p "$GO_BUILD_DIR/src"
  57. for file in $files; do
  58. log "${file#./}"
  59. dest="$GO_BUILD_DIR/src/$GO_PKG/${file#./}"
  60. mkdir -p "${dest%/*}"
  61. cp -fpR "$file" "$dest"
  62. done
  63. log
  64. if [ "$GO_SOURCE_ONLY" != 1 ]; then
  65. if [ -d "$GO_BUILD_DEPENDS_SRC" ]; then
  66. log "Symlinking directories from $GO_BUILD_DEPENDS_SRC into $GO_BUILD_DIR/src"
  67. link_contents "$GO_BUILD_DEPENDS_SRC" "$GO_BUILD_DIR/src"
  68. else
  69. log "$GO_BUILD_DEPENDS_SRC does not exist, skipping symlinks"
  70. fi
  71. else
  72. log "Not building binaries, skipping symlinks"
  73. fi
  74. log
  75. return 0
  76. }
  77. build() {
  78. # shellcheck disable=SC2039
  79. local modargs pattern targets retval
  80. cd "$GO_BUILD_DIR" || return 1
  81. if [ -f "$BUILD_DIR/go.mod" ] ; then
  82. mkdir -p "$GO_MOD_CACHE_DIR"
  83. modargs="$GO_MOD_ARGS"
  84. fi
  85. log "Finding targets"
  86. # shellcheck disable=SC2086
  87. targets="$(go list $modargs $GO_BUILD_PKG)"
  88. for pattern in $GO_EXCLUDES; do
  89. targets="$(printf '%s\n' "$targets" | grep -v "$pattern")"
  90. done
  91. log
  92. if [ "$GO_GO_GENERATE" = 1 ]; then
  93. log "Calling go generate"
  94. # shellcheck disable=SC2086
  95. GOOS='' GOARCH='' GO386='' GOARM='' GOMIPS='' GOMIPS64='' \
  96. go generate -v $targets
  97. log
  98. fi
  99. if [ "$GO_SOURCE_ONLY" = 1 ]; then
  100. return 0
  101. fi
  102. log "Building targets"
  103. mkdir -p "$GO_BUILD_DIR/bin" "$GO_BUILD_CACHE_DIR"
  104. # shellcheck disable=SC2086
  105. go install $modargs "$@" $targets
  106. retval="$?"
  107. log
  108. if [ "$retval" -eq 0 ] && [ -z "$(find "$GO_BUILD_BIN_DIR" -maxdepth 0 -type d -not -empty 2>/dev/null)" ]; then
  109. log_error "No binaries were built"
  110. retval=1
  111. fi
  112. if [ "$retval" -ne 0 ]; then
  113. cache_cleanup
  114. fi
  115. return "$retval"
  116. }
  117. install_bin() {
  118. # shellcheck disable=SC2039
  119. local dest="$1"
  120. install -d -m0755 "$dest/$GO_INSTALL_BIN_PATH"
  121. install -m0755 "$GO_BUILD_BIN_DIR"/* "$dest/$GO_INSTALL_BIN_PATH/"
  122. }
  123. install_src() {
  124. # shellcheck disable=SC2039
  125. local dest="$1" dir="${GO_PKG%/*}"
  126. install -d -m0755 "$dest/$GO_BUILD_DEPENDS_PATH/src/$dir"
  127. cp -fpR "$GO_BUILD_DIR/src/$GO_PKG" "$dest/$GO_BUILD_DEPENDS_PATH/src/$dir/"
  128. }
  129. cache_cleanup() {
  130. if ! [ -d "$GO_MOD_CACHE_DIR" ]; then
  131. return 0
  132. fi
  133. # in case go is called without -modcacherw
  134. find "$GO_MOD_CACHE_DIR" -type d -not -perm -u+w -exec chmod u+w '{}' +
  135. if [ -n "$CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE" ]; then
  136. find "$GO_MOD_CACHE_DIR" -type d -not -perm -go+rx -exec chmod go+rx '{}' +
  137. find "$GO_MOD_CACHE_DIR" -not -type d -not -perm -go+r -exec chmod go+r '{}' +
  138. fi
  139. return 0
  140. }
  141. if [ "$#" -lt 1 ]; then
  142. log_error "Missing command"
  143. exit 1
  144. fi
  145. command="$1"
  146. shift 1
  147. case "$command" in
  148. configure)
  149. configure
  150. ;;
  151. build)
  152. build "$@"
  153. ;;
  154. install_bin)
  155. install_bin "$@"
  156. ;;
  157. install_src)
  158. install_src "$@"
  159. ;;
  160. cache_cleanup)
  161. cache_cleanup
  162. ;;
  163. *)
  164. log_error "Invalid command \"$command\""
  165. exit 1
  166. ;;
  167. esac