match.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/sh
  2. # Copyright 2016 The Go Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style
  4. # license that can be found in the LICENSE file.
  5. # Given a source directory, returns the non-test Go files that should
  6. # be built for this target. This implements Go's build constraints in
  7. # a shell script. There is similar code in testsuite/gotest.
  8. set -e
  9. LANG=C
  10. LC_ALL=C
  11. LC_CTYPE=C
  12. export LANG LC_ALL LC_CTYPE
  13. srcdir=""
  14. goarch=""
  15. goos=""
  16. extrafiles=""
  17. cmdlinetag="nosuchtag"
  18. cgotag="cgo"
  19. for arg; do
  20. case "x$arg" in
  21. x--srcdir)
  22. srcdir=$2
  23. shift
  24. shift
  25. ;;
  26. x--srcdir=*)
  27. srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
  28. shift
  29. ;;
  30. x--goarch)
  31. goarch=$2
  32. shift
  33. shift
  34. ;;
  35. x--goarch=*)
  36. goarch=`echo $1 | sed -e 's/^--goarch=//'`
  37. shift
  38. ;;
  39. x--goos)
  40. goos=$2
  41. shift
  42. shift
  43. ;;
  44. x--goos=*)
  45. goos=`echo $1 | sed -e 's/^--goos=//'`
  46. shift
  47. ;;
  48. x--extrafiles)
  49. extrafiles=$2
  50. shift
  51. shift
  52. ;;
  53. x--extrafiles=*)
  54. extrafiles=`echo $1 | sed -e 's/^--extrafiles=//'`
  55. shift
  56. ;;
  57. x--tag)
  58. cmdlinetag=$2
  59. shift
  60. shift
  61. ;;
  62. x--tag=*)
  63. cmdlinetag=`echo $1 | sed -e 's/^--tag=//'`
  64. shift
  65. ;;
  66. x--nocgo)
  67. cgotag="nosuchtag"
  68. shift
  69. ;;
  70. *)
  71. echo 1>&2 "unknown argument $arg"
  72. exit 1
  73. ;;
  74. esac
  75. done
  76. cd $srcdir
  77. gofiles=
  78. for f in *.go; do
  79. case $f in
  80. *_test.go)
  81. ;;
  82. *.go)
  83. gofiles="$gofiles $f"
  84. ;;
  85. esac
  86. done
  87. if test "$gofiles" = ""; then
  88. echo 1>&2 "no non-test .go files in $srcdir"
  89. exit 1
  90. fi
  91. gobuild() {
  92. line=$(echo "$1" | sed -e 's|//go:build ||')
  93. line=$(echo "$line" | sed -e 's/go1\.[0-9]\+/1/g' -e 's/goexperiment\./goexperiment/')
  94. line=" $line "
  95. wrap='[ ()!&|]'
  96. for ones in $goarch $goos $cgotag $cmdlinetag gccgo goexperimentfieldtrack; do
  97. line=$(echo "$line" | sed -e "s/\\(${wrap}\\)${ones}\\(${wrap}\\)/"'\11\2/g')
  98. done
  99. # 386 is a special case since it looks like a number to the shell.
  100. # We need it to be 0 if it's not $goarch.
  101. if test "$goarch" != "386"; then
  102. line=$(echo "$line" | sed -e "s/\\(${wrap}\\)386\\(${wrap}\\)/\10\2/g")
  103. fi
  104. (($line))
  105. }
  106. matched=
  107. for f in $gofiles; do
  108. tag1=`echo $f | sed -e 's/^.*_\([^_]*\).go$/\1/'`
  109. tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*.go$/\1/'`
  110. if test x$tag1 = x$f; then
  111. tag1=
  112. fi
  113. if test x$tag2 = x$f; then
  114. tag2=
  115. fi
  116. case "$tag1" in
  117. "") ;;
  118. $goarch) ;;
  119. $goos) ;;
  120. aix | android | darwin | dragonfly | freebsd | illumos | hurd | ios | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
  121. tag1=nonmatchingtag
  122. ;;
  123. 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
  124. tag1=nonmatchingtag
  125. ;;
  126. *)
  127. # File name like x_amd64_random.go, where tag1=random.
  128. # Don't match based on tag2.
  129. tag2=
  130. ;;
  131. esac
  132. case "$tag2" in
  133. "") ;;
  134. $goarch) ;;
  135. $goos) ;;
  136. aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
  137. tag2=nonmatchingtag
  138. ;;
  139. 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
  140. tag2=nonmatchingtag
  141. ;;
  142. esac
  143. if test x$tag1 = xnonmatchingtag -o x$tag2 = xnonmatchingtag; then
  144. continue
  145. fi
  146. # Check for go:build line
  147. build=$(sed '/^package /q' < $f | grep '^//go:build ' | cat)
  148. if test -n "$build"; then
  149. if $(gobuild "$build"); then
  150. matched="$matched $srcdir/$f"
  151. fi
  152. continue
  153. fi
  154. # No go:build line, check for +build lines.
  155. # Pipe through cat so that `set -e` doesn't affect fgrep.
  156. tags=`sed '/^package /q' < $f | grep '^// *+build ' | cat`
  157. omatch=true
  158. first=true
  159. match=false
  160. for tag in $tags; do
  161. case $tag in
  162. "//")
  163. ;;
  164. "+build" | "//+build")
  165. if test "$first" = "true"; then
  166. first=false
  167. elif test "$match" = "false"; then
  168. omatch=false
  169. fi
  170. match=false
  171. ;;
  172. $goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
  173. match=true
  174. ;;
  175. "!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
  176. ;;
  177. *,*)
  178. cmatch=true
  179. for ctag in `echo $tag | sed -e 's/,/ /g'`; do
  180. case $ctag in
  181. $goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
  182. ;;
  183. "!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
  184. cmatch=false
  185. ;;
  186. "!"*)
  187. ;;
  188. *)
  189. cmatch=false
  190. ;;
  191. esac
  192. done
  193. if test "$cmatch" = "true"; then
  194. match=true
  195. fi
  196. ;;
  197. "!"*)
  198. match=true
  199. ;;
  200. esac
  201. done
  202. if test "$match" = "false" -a "$first" = "false"; then
  203. omatch=false
  204. fi
  205. if test "$omatch" = "true"; then
  206. matched="$matched $srcdir/$f"
  207. fi
  208. done
  209. echo $matched $extrafiles
  210. exit 0