xref: /freebsd/contrib/bmake/os.sh (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1:
2# NAME:
3#	os.sh - operating system specifics
4#
5# DESCRIPTION:
6#	This file is included at the start of processing. Its role is
7#	to set the variables OS, OSREL, OSMAJOR, MACHINE and MACHINE_ARCH to
8#	reflect the current system.
9#
10#	It also sets variables such as MAILER, LOCAL_FS, PS_AXC to hide
11#	certain aspects of different UNIX flavours.
12#
13# SEE ALSO:
14#	site.sh,funcs.sh
15#
16# AUTHOR:
17#	Simon J. Gerraty <sjg@crufty.net>
18
19# RCSid:
20#	$Id: os.sh,v 1.56 2020/08/05 23:25:22 sjg Exp $
21#
22#	@(#) Copyright (c) 1994 Simon J. Gerraty
23#
24#	This file is provided in the hope that it will
25#	be of use.  There is absolutely NO WARRANTY.
26#	Permission to copy, redistribute or otherwise
27#	use this file is hereby granted provided that
28#	the above copyright notice and this notice are
29#	left intact.
30#
31#	Please send copies of changes and bug-fixes to:
32#	sjg@crufty.net
33#
34
35# this lets us skip sourcing it again
36_OS_SH=:
37
38OS=`uname`
39OSREL=`uname -r`
40OSMAJOR=`IFS=.; set $OSREL; echo $1`
41MACHINE=`uname -m`
42MACHINE_ARCH=`uname -p 2>/dev/null || echo $MACHINE`
43
44# there is at least one case of `uname -p` outputting
45# a bunch of usless drivel
46case "$MACHINE_ARCH" in
47unknown|*[!A-Za-z0-9_-]*) MACHINE_ARCH="$MACHINE";;
48esac
49
50# we need this here, and it is not always available...
51Which() {
52	case "$1" in
53	-*) t=$1; shift;;
54	*) t=-x;;
55	esac
56	case "$1" in
57	/*)	test $t $1 && echo $1;;
58	*)
59		# some shells cannot correctly handle `IFS`
60		# in conjunction with the for loop.
61		_dirs=`IFS=:; echo ${2:-$PATH}`
62		for d in $_dirs
63		do
64			test $t $d/$1 && { echo $d/$1; break; }
65		done
66		;;
67	esac
68}
69
70# tr is insanely non-portable wrt char classes, so we need to
71# spell out the alphabet. sed y/// would work too.
72toUpper() {
73	${TR:-tr} abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
74}
75
76toLower() {
77	${TR:-tr} ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
78}
79
80K=
81case $OS in
82AIX)	# everyone loves to be different...
83	OSMAJOR=`uname -v`
84	OSREL="$OSMAJOR.`uname -r`"
85	LOCAL_FS=jfs
86	PS_AXC=-e
87	SHARE_ARCH=$OS/$OSMAJOR.X
88	;;
89Darwin) # a bit like BSD
90        HOST_ARCH=$MACHINE
91        ;;
92SunOS)
93	CHOWN=`Which chown /usr/etc:/usr/bin`
94	export CHOWN
95
96	# Great! Solaris keeps moving arch(1)
97	# should just bite the bullet and use uname -p
98	arch=`Which arch /usr/bin:/usr/ucb`
99
100	MAILER=/usr/ucb/Mail
101	LOCAL_FS=4.2
102
103	case "$OSREL" in
104	4.0*)
105		# uname -m just says sun which could be anything
106		# so use arch(1).
107		MACHINE_ARCH=`arch`
108		MACHINE=$MACHINE_ARCH
109		;;
110	4*)
111		MACHINE_ARCH=`arch`
112		;;
113	5*)
114		K=-k
115		LOCAL_FS=ufs
116		MAILER=mailx
117		PS_AXC=-e
118		# can you believe that ln on Solaris defaults to
119		# overwriting an existing file!!!!! We want one that works!
120		test -x /usr/xpg4/bin/ln && LN=${LN:-/usr/xpg4/bin/ln}
121		# wonderful, 5.8's tr again require's []'s
122		# but /usr/xpg4/bin/tr causes problems if LC_COLLATE is set!
123		# use toUpper/toLower instead.
124		;;
125	esac
126	case "$OS/$MACHINE_ARCH" in
127	*sun386)	SHARE_ARCH=$MACHINE_ARCH;;
128	esac
129	;;
130*BSD)
131	K=-k
132	MAILER=/usr/bin/Mail
133	LOCAL_FS=local
134	: $-,$ENV
135	case "$-,$ENV" in
136	*i*,*) ;;
137	*,|*ENVFILE*) ;;
138	*) ENV=;;
139	esac
140	# NetBSD at least has good backward compatibility
141	# so NetBSD/i386 is good enough
142	case $OS in
143	NetBSD)
144	        LOCALBASE=/usr/pkg
145		HOST_ARCH=$MACHINE
146		SHARE_ARCH=$OS/$HOST_ARCH
147		;;
148	OpenBSD)
149		arch=`Which arch /usr/bin:/usr/ucb:$PATH`
150		MACHINE_ARCH=`$arch -s`
151		;;
152	esac
153	NAWK=awk
154	export NAWK
155	;;
156HP-UX)
157	TMP_DIRS="/tmp /usr/tmp"
158	LOCAL_FS=hfs
159	MAILER=mailx
160	# don't rely on /bin/sh, its broken
161	_shell=/bin/ksh; ENV=
162	# also, no one would be interested in OSMAJOR=A
163	case "$OSREL" in
164	?.09*)	OSMAJOR=9; PS_AXC=-e;;
165	?.10*)	OSMAJOR=10; PS_AXC=-e;;
166	esac
167	;;
168IRIX)
169	LOCAL_FS=efs
170	;;
171Interix)
172	MACHINE=i386
173	MACHINE_ARCH=i386
174	;;
175UnixWare)
176	OSREL=`uname -v`
177	OSMAJOR=`IFS=.; set $OSREL; echo $1`
178	MACHINE_ARCH=`uname -m`
179	;;
180Linux)
181	# Not really any such thing as Linux, but
182	# this covers red-hat and hopefully others.
183	case $MACHINE in
184	i?86)	MACHINE_ARCH=i386;; # we don't care about i686 vs i586
185	esac
186	LOCAL_FS=ext2
187	PS_AXC=axc
188	[ -x /usr/bin/md5sum ] && { MD5=/usr/bin/md5sum; export MD5; }
189	;;
190QNX)
191	case $MACHINE in
192	x86pc)	MACHINE_ARCH=i386;;
193	esac
194	;;
195Haiku)
196	case $MACHINE in
197	BeBox)	MACHINE_ARCH=powerpc;;
198	BeMac)	MACHINE_ARCH=powerpc;;
199	BePC)	MACHINE_ARCH=i386;;
200	esac
201	;;
202esac
203LOCALBASE=${LOCALBASE:-/usr/local}
204
205HOSTNAME=${HOSTNAME:-`( hostname ) 2>/dev/null`}
206HOSTNAME=${HOSTNAME:-`( uname -n ) 2>/dev/null`}
207case "$HOSTNAME" in
208*.*)	HOST=`IFS=.; set -- $HOSTNAME; echo $1`;;
209*)	HOST=$HOSTNAME;;
210esac
211
212TMP_DIRS=${TMP_DIRS:-"/tmp /var/tmp"}
213MACHINE_ARCH=${MACHINE_ARCH:-$MACHINE}
214HOST_ARCH=${HOST_ARCH:-$MACHINE_ARCH}
215case "$HOST_ARCH" in
216x86*64|amd64) MACHINE32_ARCH=i386;;
217*64) MACHINE32_ARCH=`echo $MACHINE_ARCH | sed 's,64,32,'`;;
218*) MACHINE32_ARCH=$MACHINE_ARCH;;
219esac
220HOST_ARCH32=${HOST_ARCH32:-$MACHINE32_ARCH}
221# we mount server:/share/arch/$SHARE_ARCH as /usr/local
222SHARE_ARCH_DEFAULT=$OS/$OSMAJOR.X/$HOST_ARCH
223SHARE_ARCH=${SHARE_ARCH:-$SHARE_ARCH_DEFAULT}
224LN=${LN:-ln}
225TR=${TR:-tr}
226
227# Some people like have /share/$HOST_TARGET/bin etc.
228HOST_TARGET=`echo ${OS}${OSMAJOR}-$HOST_ARCH | tr -d / | toLower`
229HOST_TARGET32=`echo ${OS}${OSMAJOR}-$HOST_ARCH32 | tr -d / | toLower`
230export HOST_TARGET HOST_TARGET32
231
232case `echo -n .` in -n*) N=; C="\c";; *) N=-n; C=;; esac
233
234Echo() {
235	case "$1" in
236	-n) _n=$N _c=$C; shift;;
237	*) _n= _c=;;
238	esac
239	echo $_n "$@" $_c
240}
241
242export HOSTNAME HOST
243export OS MACHINE MACHINE_ARCH OSREL OSMAJOR LOCAL_FS TMP_DIRS MAILER N C K PS_AXC
244export LN SHARE_ARCH TR
245export LOCALBASE
246
247case /$0 in
248*/os.sh)
249	for v in $*
250	do
251		eval vv=\$$v
252		echo "$v='$vv'"
253	done
254	;;
255*/host_target32) echo $HOST_TARGET32;;
256*/host_target) echo $HOST_TARGET;;
257esac
258
259