summaryrefslogtreecommitdiff
path: root/makefiles/gmake/guess_env
blob: 60e039a2129359097c4030783c58ce147a35ac33 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/sh

# bail out fast if we already have a cache file, otherwise building is far too slow!

MAKEFILE_DIR="$3/$4"
if test -f "${MAKEFILE_DIR}/makefiles/gmake/platform.vars"; then
	. "${MAKEFILE_DIR}/makefiles/gmake/platform.vars"
else

	# operating system and major, minor version, more should not be necessary

	UNAME_SYSTEM=`(uname -s) 2>/dev/null`
	UNAME_RELEASE=`(uname -r) 2>/dev/null`
	UNAME_VERSION=`(uname -v) 2>/dev/null`
	UNAME_MACHINE=`(uname -m) 2>/dev/null`

	case "$UNAME_SYSTEM.$UNAME_RELEASE" in
		Linux*)		PLATFORM=LINUX
				OS_MAJOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2`
				
				# LSB-system? Check for lsb-release
				if test -x /usr/bin/lsb_release; then
					dist=`/usr/bin/lsb_release -i | cut -f 2`
					rev=`/usr/bin/lsb_release -r | cut -f 2`
					case "$dist" in
						Ubuntu)
							LINUX_DIST='ubuntu'
							LINUX_REV=$rev
							;;
						
						Debian)
							LINUX_DIST='debian'
							LINUX_REV=`echo $rev | cut -f 1 -d.`
							;;
							
						SUSE*LINUX)
							LINUX_DIST='suse'
							LINUX_REV=`echo $rev | tr -s ' ' '\t' | cut -f 2 -d ' '`
							;;
							
						*)
							LINUX_DIST='unknown'
							LINUX_REV='unknown'
							;;
					esac
				else
					# try the older way with release files in /etc
					
					if test -f /etc/arch-release; then
						LINUX_DIST='arch'
						LINUX_REV='current'
						if test "$OS_MAJOR_VERSION" = "3"; then
							OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2 | cut -d - -f 1`
						fi
					elif test -f /etc/debian_version; then
						LINUX_DIST='debian'
						LINUX_REV=`cat /etc/debian_version | cut -d . -f 1`
					elif test -f /etc/slackware-version; then
						LINUX_DIST='slackware'
						LINUX_REV=`cat /etc/slackware-version | cut -d ' ' -f 2 | cut -d . -f 1,2`
					elif test -f /etc/redhat-release; then
						LINUX_DIST='redhat'
						LINUX_REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*// | cut -f 1 -d .`
					elif test -f /etc/fedora-release; then
						LINUX_DIST='redhat'
						LINUX_REV=`cat /etc/fedora-release | cut -f 3 -d ' '`
					elif test -f /etc/SuSE-release; then
						grep "SUSE Linux Enterprise Server" /etc/SuSE-release
						if test $? = 0; then
							LINUX_DIST='sles'
							LINUX_REV=`grep VERSION /etc/SuSE-release | cut -f 3 -d ' '`
						else
							LINUX_DIST='suse'
							LINUX_REV=`grep VERSION /etc/SuSE-release | cut -f 3 -d ' '`
						fi
					else
						LINUX_DIST='unknown'
						LINUX_REV='unknown'
					fi
				fi
				;;

		FreeBSD*)	PLATFORM=FREEBSD
				OS_MAJOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2 | cut -d - -f 1`
				;;
				
		OpenBSD*)	PLATFORM=OPENBSD
				OS_MAJOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2 | cut -d - -f 1`
				;;

		NetBSD*)	PLATFORM=NETBSD
				OS_MAJOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2`
				;;

		SunOS*)		PLATFORM=SUNOS
				OS_MAJOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $UNAME_RELEASE | cut -d . -f 2`
				;;

		CYGWIN_NT*)	PLATFORM=CYGWIN
				_tmp=`echo $UNAME_SYSTEM | cut -d - -f 2`
				OS_MAJOR_VERSION=`echo $_tmp | cut -d . -f 1`
				OS_MINOR_VERSION=`echo $_tmp | cut -d . -f 2`
				;;

		*)
				PLATFORM=UNKNOWN
				echo "Unknown platform '$UNAME_SYSTEM $UNAME_RELEASE'"
				exit 1
	esac

	# the architecture

	case "$UNAME_MACHINE" in
		i*86*)		ARCH=x86
				;;
		x86_64|amd64)	ARCH=x86_64
				;;
		sun4u)		ARCH=sun4u
				;;

		*)		ARCH=UNKNOWN
				echo "Unknown architecture '$UNAME_MACHINE'"
				exit 1
				
	esac

	# get last line, old 'tail' syntax and POSIX syntax, both exist out there
	if test "x${PLATFORM}" = "xSUNOS"; then
		TAIL1='tail -1'
	else
		TAIL1='tail -n 1'
	fi

	# the compiler and version

	# do we have ccache or distcc in CC, then check the compiler, not the wrapper
	CC=$2
	CC=`echo $CC | sed 's/distcc//g' | sed 's/ccache//g'`

	# what compiler do we have (we can't relly on it's name as it may be a cc link to the binary!)
	(( $CC -v 2>&1 | $TAIL1 ) | grep -i GCC ) 2>/dev/null 1>/dev/null
	if test $? = 0; then
		COMPILER='gcc'
	else
	#	( $CC -V 2>&1 | grep l_cproc_p ) >/dev/null
		( $CC -V 2>&1 | grep "Intel(R) C" | grep "Compiler" ) >/dev/null
		if test $? = 0; then
			COMPILER='icc'
		else
			( $CC -xhelp=readme | head -n 1 | grep 'Sun Studio' ) >/dev/null
			if test $? = 0; then
				COMPILER='spro'
			else
				COMPILER='unknown'
			fi
		fi
	fi

	# version of gcc (GNU C compiler)

	if test $COMPILER = "gcc"; then
		GCC_VERSION=`gcc -dumpversion`
		GCC_MAJOR_VERSION=`echo $GCC_VERSION | cut -d . -f 1`
		GCC_MINOR_VERSION=`echo $GCC_VERSION | cut -d . -f 2`
	fi

	# version of icc (Intel C compiler)

	if test $COMPILER = "icc"; then
		ICC_VERSION=`icc -dumpversion`
		ICC_MAJOR_VERSION=`echo $ICC_VERSION | cut -d . -f 1`
		ICC_MINOR_VERSION=`echo $ICC_VERSION | cut -d . -f 2`
	fi

	# version of spro (Sun Pro compiler, Sun Studio)

	if test $COMPILER = "spro"; then
		SPRO_VERSION=`$CC -xhelp=readme | head -n 1 | cut -d ' ' -f 3`
		SPRO_MAJOR_VERSION=`echo $SPRO_VERSION | cut -d : -f 1`
	fi

	# generate the makefile snippet with conditional variables
	# (conditional, so that users can overwrite them for instance
	# for cross-compilation)
	cat >"${MAKEFILE_DIR}/makefiles/gmake/platform.mk.vars" <<EOF
ARCH?=$ARCH
PLATFORM?=$PLATFORM
OS_MAJOR_VERSION?=$OS_MAJOR_VERSION
OS_MINOR_VERSION?=$OS_MINOR_VERSION
COMPILER?=$COMPILER
GCC_MAJOR_VERSION?=$GCC_MAJOR_VERSION
GCC_MINOR_VERSION?=$GCC_MINOR_VERSION
ICC_MAJOR_VERSION?=$ICC_MAJOR_VERSION
ICC_MINOR_VERSION?=$ICC_MINOR_VERSION
SPRO_MAJOR_VERSION?=$SPRO_MAJOR_VERSION
LINUX_DIST?=$LINUX_DIST
LINUX_REV?=$LINUX_REV
EOF

	# write the cache file (to protect against constant recomputations!)
	cat >"${MAKEFILE_DIR}/makefiles/gmake/platform.vars" <<EOF
ARCH=$ARCH
PLATFORM=$PLATFORM
OS_MAJOR_VERSION=$OS_MAJOR_VERSION
OS_MINOR_VERSION=$OS_MINOR_VERSION
COMPILER=$COMPILER
GCC_MAJOR_VERSION=$GCC_MAJOR_VERSION
GCC_MINOR_VERSION=$GCC_MINOR_VERSION
ICC_MAJOR_VERSION=$ICC_MAJOR_VERSION
ICC_MINOR_VERSION=$ICC_MINOR_VERSION
SPRO_MAJOR_VERSION=$SPRO_MAJOR_VERSION
LINUX_DIST=$LINUX_DIST
LINUX_REV=$LINUX_REV
EOF

fi

case "$1" in
	--platform)		echo $PLATFORM
				;;

	--os-major-version)	echo $OS_MAJOR_VERSION
				;;

	--os-minor-version)	echo $OS_MINOR_VERSION
				;;

	--arch)			echo $ARCH
				;;

	--compiler)		echo $COMPILER
				;;

	--gcc-major-version)	echo $GCC_MAJOR_VERSION
				;;

	--gcc-minor-version)	echo $GCC_MINOR_VERSION
				;;

	--icc-major-version)	echo $ICC_MAJOR_VERSION
				;;

	--icc-minor-version)	echo $ICC_MINOR_VERSION
				;;

	--spro-major-version)	echo $SPRO_MAJOR_VERSION
				;;

	*)			echo "unkown flag '$1' requested!"
				exit 1
esac