1# Copyright 2012 The Go Authors.  All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4
5# These function names are also known to
6# (and are the plan for transitioning to) run.go.
7
8# helper (not known to run.go)
9# group file list by packages and return list of packages
10# each package is a comma-separated list of go files.
11pkgs() {
12	pkglist=$(grep -h '^package ' $* | awk '{print $2}' | sort -u)
13	for p in $pkglist
14	do
15		echo $(grep -l "^package $p\$" $*) | tr ' ' ,
16	done | sort
17}
18
19_match() {
20	case $1 in
21	*,*)
22		#echo >&2 "match comma separated $1"
23		first=$(echo $1 | sed 's/,.*//')
24		rest=$(echo $1 | sed 's/[^,]*,//')
25		if _match $first && _match $rest; then
26			return 0
27		fi
28		return 1
29		;;
30	'!'*)
31		#echo >&2 "match negation $1"
32		neg=$(echo $1 | sed 's/^!//')
33		if _match $neg; then
34			return 1
35		fi
36		return 0
37		;;
38	$GOARCH|$GOOS)
39		#echo >&2 "match GOARCH or GOOS $1"
40		return 0
41		;;
42	esac
43	return 1
44}
45
46# +build aborts execution if the supplied tags don't match,
47# i.e. none of the tags (x or !x) matches GOARCH or GOOS.
48+build() {
49	if (( $# == 0 )); then
50		return
51	fi
52	m=0
53	for tag; do
54		if _match $tag; then
55			m=1
56		fi
57	done
58	if [ $m = 0 ]; then
59		#echo >&2 no match
60		exit 0
61	fi
62	unset m
63}
64
65compile() {
66	$G $D/$F.go
67}
68
69compiledir() {
70	for pkg in $(pkgs $D/$F.dir/*.go)
71	do
72		$G -I . $(echo $pkg | tr , ' ') || return 1
73	done
74}
75
76errorcheckdir() {
77	lastzero=""
78	if [ "$1" = "-0" ]; then
79		lastzero="-0"
80	fi
81	pkgs=$(pkgs $D/$F.dir/*.go)
82	for pkg in $pkgs.last
83	do
84		zero="-0"
85		case $pkg in
86		*.last)
87			pkg=$(echo $pkg |sed 's/\.last$//')
88			zero=$lastzero
89		esac
90		errchk $zero $G -D . -I . -e $(echo $pkg | tr , ' ')
91	done
92}
93
94rundir() {
95	lastfile=""
96	for pkg in $(pkgs $D/$F.dir/*.go)
97	do
98		name=$(echo $pkg | sed 's/\.go.*//; s/.*\///')
99		$G -D . -I . -e $(echo $pkg | tr , ' ') || return 1
100		lastfile=$name
101	done
102	$L -o $A.out -L . $lastfile.$A
103	./$A.out
104}
105
106rundircmpout() {
107	lastfile=""
108	for pkg in $(pkgs $D/$F.dir/*.go)
109	do
110		name=$(echo $pkg | sed 's/\.go.*//; s/.*\///')
111		$G -D . -I . -e $(echo $pkg | tr , ' ') || return 1
112		lastfile=$name
113	done
114	$L -o $A.out -L . $lastfile.$A
115	./$A.out 2>&1 | cmp - $D/$F.out
116}
117
118build() {
119	$G $D/$F.go && $L $F.$A
120}
121
122runoutput() {
123	go run "$D/$F.go" "$@" > tmp.go
124	go run tmp.go
125}
126
127run() {
128	gofiles=""
129	ingo=true
130	while $ingo; do
131		case "$1" in
132		*.go)
133			gofiles="$gofiles $1"
134			shift
135			;;
136		*)
137			ingo=false
138			;;
139		esac
140	done
141
142	$G $D/$F.go $gofiles && $L $F.$A && ./$A.out "$@"
143}
144
145cmpout() {
146	$G $D/$F.go && $L $F.$A && ./$A.out 2>&1 | cmp - $D/$F.out
147}
148
149errorcheck() {
150	zero=""
151	if [ "$1" = "-0" ]; then
152		zero="-0"
153		shift
154	fi
155	errchk $zero $G -e $* $D/$F.go
156}
157
158errorcheckoutput() {
159	zero=""
160	if [ "$1" = "-0" ]; then
161		zero="-0"
162		shift
163	fi
164	go run "$D/$F.go" "$@" > tmp.go
165	errchk $zero $G -e tmp.go
166}
167
168skip() {
169	true
170}
171