blob: bf4f141e04a7ab87cb5a4fc0a146bc8c1c74149d [file] [log] [blame]
Ian Lance Taylore0f69f32016-08-06 00:36:33 +00001#!/bin/sh
2
3# Copyright 2016 The Go Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file.
6
7# Given a source directory, returns the non-test Go files that should
8# be built for this target. This implements Go's build constraints in
9# a shell script. There is similar code in testsuite/gotest.
10
11set -e
12
Ian Lance Taylor82b709f2016-08-13 02:52:42 +000013LANG=C
Ian Lance Taylore0f69f32016-08-06 00:36:33 +000014LC_ALL=C
15LC_CTYPE=C
Ian Lance Taylor82b709f2016-08-13 02:52:42 +000016export LANG LC_ALL LC_CTYPE
Ian Lance Taylore0f69f32016-08-06 00:36:33 +000017
18srcdir=""
19goarch=""
20goos=""
21extrafiles=""
22cmdlinetag="nosuchtag"
23cgotag="cgo"
24
25for arg; do
26 case "x$arg" in
27 x--srcdir)
28 srcdir=$2
29 shift
30 shift
31 ;;
32 x--srcdir=*)
33 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
34 shift
35 ;;
36 x--goarch)
37 goarch=$2
38 shift
39 shift
40 ;;
41 x--goarch=*)
42 goarch=`echo $1 | sed -e 's/^--goarch=//'`
43 shift
44 ;;
45 x--goos)
46 goos=$2
47 shift
48 shift
49 ;;
50 x--goos=*)
51 goos=`echo $1 | sed -e 's/^--goos=//'`
52 shift
53 ;;
54 x--extrafiles)
55 extrafiles=$2
56 shift
57 shift
58 ;;
59 x--extrafiles=*)
60 extrafiles=`echo $1 | sed -e 's/^--extrafiles=//'`
61 shift
62 ;;
63 x--tag)
64 cmdlinetag=$2
65 shift
66 shift
67 ;;
68 x--tag=*)
69 cmdlinetag=`echo $1 | sed -e 's/^--tag=//'`
70 shift
71 ;;
72 x--nocgo)
73 cgotag="nosuchtag"
74 shift
75 ;;
76 *)
77 echo 1>&2 "unknown argument $arg"
78 exit 1
79 ;;
80 esac
81done
82
83cd $srcdir
84
85gofiles=
86for f in *.go; do
87 case $f in
88 *_test.go)
89 ;;
90 *.go)
91 gofiles="$gofiles $f"
92 ;;
93 esac
94done
95
96if test "$gofiles" = ""; then
97 echo 1>&2 "no non-test .go files in $srcdir"
98 exit 1
99fi
100
101matched=
102for f in $gofiles; do
103 tag1=`echo $f | sed -e 's/^.*_\([^_]*\).go$/\1/'`
104 tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*.go$/\1/'`
105 if test x$tag1 = x$f; then
106 tag1=
107 fi
108 if test x$tag2 = x$f; then
109 tag2=
110 fi
111
112 case "$tag1" in
113 "") ;;
114 $goarch) ;;
115 $goos) ;;
Ian Lance Taylorcfcbb422020-12-23 09:57:37 -0800116 aix | android | darwin | dragonfly | freebsd | illumos | hurd | ios | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000117 tag1=nonmatchingtag
118 ;;
Maciej W. Rozycki2c5499b2020-08-28 16:05:56 +0100119 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)
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000120 tag1=nonmatchingtag
121 ;;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200122 *)
123 # File name like x_amd64_random.go, where tag1=random.
124 # Don't match based on tag2.
125 tag2=
126 ;;
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000127 esac
128
129 case "$tag2" in
130 "") ;;
131 $goarch) ;;
132 $goos) ;;
Ian Lance Taylorcfcbb422020-12-23 09:57:37 -0800133 aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000134 tag2=nonmatchingtag
135 ;;
Maciej W. Rozycki2c5499b2020-08-28 16:05:56 +0100136 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)
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000137 tag2=nonmatchingtag
138 ;;
139 esac
140
141 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
142 # Pipe through cat so that `set -e` doesn't affect fgrep.
Ian Lance Taylor57cccc82019-09-13 17:07:20 +0000143 tags=`sed '/^package /q' < $f | grep '^// *+build ' | cat`
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000144 omatch=true
145 first=true
146 match=false
147 for tag in $tags; do
148 case $tag in
149 "//")
150 ;;
Ian Lance Tayloraa8901e2019-09-06 18:12:46 +0000151 "+build" | "//+build")
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000152 if test "$first" = "true"; then
153 first=false
154 elif test "$match" = "false"; then
155 omatch=false
156 fi
157 match=false
158 ;;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200159 $goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000160 match=true
161 ;;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200162 "!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000163 ;;
164 *,*)
165 cmatch=true
166 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
167 case $ctag in
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200168 $goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000169 ;;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200170 "!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
Ian Lance Taylore0f69f32016-08-06 00:36:33 +0000171 cmatch=false
172 ;;
173 "!"*)
174 ;;
175 *)
176 cmatch=false
177 ;;
178 esac
179 done
180 if test "$cmatch" = "true"; then
181 match=true
182 fi
183 ;;
184 "!"*)
185 match=true
186 ;;
187 esac
188 done
189
190 if test "$match" = "false" -a "$first" = "false"; then
191 omatch=false
192 fi
193
194 if test "$omatch" = "true"; then
195 matched="$matched $srcdir/$f"
196 fi
197 fi
198done
199
200echo $matched $extrafiles
201
202exit 0