summaryrefslogtreecommitdiff
path: root/src/env.bash
blob: 9f9f0397df0d1e5ec48b45cb159dfe6b8b7e6bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# Set to false if something breaks, to revert back to Makefiles.
# TODO: This variable will go away when the Makefiles do.
USE_GO_TOOL=${USE_GO_TOOL:-true}

# If set to a Windows-style path convert to an MSYS-Unix 
# one using the built-in shell commands.   
if [[ "$GOROOT" == *:* ]]; then
	GOROOT=$(cd "$GOROOT"; pwd)
fi

if [[ "$GOBIN" == *:* ]]; then
	GOBIN=$(cd "$GOBIN"; pwd)
fi

export GOROOT=${GOROOT:-$(cd ..; pwd)}

if ! test -f "$GOROOT"/include/u.h
then
	echo '$GOROOT is not set correctly or not exported: '$GOROOT 1>&2
	exit 1
fi

# Double-check that we're in $GOROOT, for people with multiple Go trees.
# Various aspects of the build cd into $GOROOT-rooted paths,
# making it easy to jump to a different tree and get confused.
DIR1=$(cd ..; pwd)
DIR2=$(cd "$GOROOT"; pwd)
if [ "$DIR1" != "$DIR2" ]; then
	echo 'Suspicious $GOROOT '"$GOROOT"': does not match current directory.' 1>&2
	exit 1
fi

export GOBIN=${GOBIN:-"$GOROOT/bin"}
if [ ! -d "$GOBIN" -a "$GOBIN" != "$GOROOT/bin" ]; then
	echo '$GOBIN is not a directory or does not exist' 1>&2
	echo 'create it or set $GOBIN differently' 1>&2
	exit 1
fi

export OLDPATH=$PATH
export PATH="$GOBIN":$PATH

MAKE=make
if ! make --version 2>/dev/null | grep 'GNU Make' >/dev/null; then
	MAKE=gmake
fi

PROGS="
	ar
	awk
	bash
	bison
	chmod
	cp
	cut
	echo
	egrep
	gcc
	grep
	ls
	$MAKE
	mkdir
	mv
	pwd
	rm
	sed
	sort
	tee
	touch
	tr
	true
	uname
	uniq
"

for i in $PROGS; do
	if ! which $i >/dev/null 2>&1; then
		echo "Cannot find '$i' on search path." 1>&2
		echo "See http://golang.org/doc/install.html#ctools" 1>&2
		exit 1
	fi
done

if bison --version 2>&1 | grep 'bison++' >/dev/null 2>&1; then
	echo "Your system's 'bison' is bison++."
	echo "Go needs the original bison instead." 1>&2
	echo "See http://golang.org/doc/install.html#ctools" 1>&2
	exit 1
fi

# Issue 2020: some users configure bash to default to
#	set -o noclobber
# which makes >x fail if x already exists.  Restore sanity.
set +o noclobber

# Tried to use . <($MAKE ...) here, but it cannot set environment
# variables in the version of bash that ships with OS X.  Amazing.
eval $($MAKE --no-print-directory -f Make.inc go-env | egrep 'GOARCH|GOOS|GOHOSTARCH|GOHOSTOS|GO_ENV|CGO_ENABLED')

# Shell doesn't tell us whether make succeeded,
# so Make.inc generates a fake variable name.
if [ "$MAKE_GO_ENV_WORKED" != 1 ]; then
	echo 'Did not find Go environment variables.' 1>&2
	exit 1
fi
unset MAKE_GO_ENV_WORKED