xref: /freebsd/contrib/bmake/os.sh (revision 783d3ff6)
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.63 2023/05/22 20:44:47 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`
45# and even `uname -m` outputting usless info
46# fortunately not both together
47case "$MACHINE" in
48*[!A-Za-z0-9_-]*) MACHINE="$MACHINE_ARCH";;
49esac
50case "$MACHINE_ARCH" in
51unknown|*[!A-Za-z0-9_-]*) MACHINE_ARCH="$MACHINE";;
52esac
53
54# we need this here, and it is not always available...
55Which() {
56	case "$1" in
57	-*) t=$1; shift;;
58	*) t=-x;;
59	esac
60	case "$1" in
61	/*)	test $t $1 && echo $1;;
62	*)
63		# some shells cannot correctly handle `IFS`
64		# in conjunction with the for loop.
65		_dirs=`IFS=:; echo ${2:-$PATH}`
66		for d in $_dirs
67		do
68			test $t $d/$1 && { echo $d/$1; break; }
69		done
70		;;
71	esac
72}
73
74# tr is insanely non-portable wrt char classes, so we need to
75# spell out the alphabet. sed y/// would work too.
76toUpper() {
77	${TR:-tr} abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
78}
79
80toLower() {
81	${TR:-tr} ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
82}
83
84K=
85case "$OS" in
86AIX)	# everyone loves to be different...
87	OSMAJOR=`uname -v`
88	OSMINOR=`uname -r`
89	OSREL="$OSMAJOR.$OSMINOR"
90	LOCAL_FS=jfs
91	PS_AXC=-e
92	SHARE_ARCH=$OS/$OSMAJOR.X
93	;;
94Darwin) # this is more explicit (arm64 vs arm)
95        HOST_ARCH=$MACHINE
96        ;;
97SunOS)
98	CHOWN=`Which chown /usr/etc:/usr/bin`
99	export CHOWN
100
101	# Great! Solaris keeps moving arch(1)
102	# should just bite the bullet and use uname -p
103	arch=`Which arch /usr/bin:/usr/ucb`
104
105	MAILER=/usr/ucb/Mail
106	LOCAL_FS=4.2
107
108	case "$OSREL" in
109	4.0*)
110		# uname -m just says sun which could be anything
111		# so use arch(1).
112		MACHINE_ARCH=`arch`
113		MACHINE=$MACHINE_ARCH
114		;;
115	4*)
116		MACHINE_ARCH=`arch`
117		;;
118	5*)
119		K=-k
120		LOCAL_FS=ufs
121		MAILER=mailx
122		PS_AXC=-e
123		# can you believe that ln on Solaris defaults to
124		# overwriting an existing file!!!!! We want one that works!
125		test -x /usr/xpg4/bin/ln && LN=${LN:-/usr/xpg4/bin/ln}
126		# wonderful, 5.8's tr again require's []'s
127		# but /usr/xpg4/bin/tr causes problems if LC_COLLATE is set!
128		# use toUpper/toLower instead.
129		;;
130	esac
131	case "$OS/$MACHINE_ARCH" in
132	*sun386)	SHARE_ARCH=$MACHINE_ARCH;;
133	esac
134	;;
135*BSD)
136	K=-k
137	MAILER=/usr/bin/Mail
138	LOCAL_FS=local
139	: $-,$ENV
140	case "$-,$ENV" in
141	*i*,*) ;;
142	*,|*ENVFILE*) ;;
143	*) ENV=;;
144	esac
145	# NetBSD at least has good backward compatibility
146	# so NetBSD/i386 is good enough
147        # recent NetBSD uses x86_64 for MACHINE_ARCH
148	case $OS in
149	NetBSD)
150	        LOCALBASE=/usr/pkg
151		SHARE_ARCH=$OS/$HOST_ARCH
152		;;
153	OpenBSD)
154		arch=`Which arch /usr/bin:/usr/ucb:$PATH`
155		MACHINE_ARCH=`$arch -s`
156		;;
157	esac
158	NAWK=awk
159	export NAWK
160	;;
161HP-UX)
162	TMP_DIRS="/tmp /usr/tmp"
163	LOCAL_FS=hfs
164	MAILER=mailx
165	# don't rely on /bin/sh, its broken
166	_shell=/bin/ksh; ENV=
167	# also, no one would be interested in OSMAJOR=A
168	case "$OSREL" in
169	?.09*)	OSMAJOR=9; PS_AXC=-e;;
170	?.10*)	OSMAJOR=10; PS_AXC=-e;;
171	esac
172	;;
173IRIX)
174	LOCAL_FS=efs
175	;;
176Interix)
177	MACHINE=i386
178	MACHINE_ARCH=i386
179	;;
180UnixWare|SCO_SV)
181	OSREL=`uname -v`
182	OSMAJOR=`IFS=.; set $OSREL; echo $1`
183	MACHINE_ARCH=`uname -m`
184	;;
185Linux)
186	# Not really any such thing as Linux, but
187	# this covers red-hat and hopefully others.
188	case $MACHINE in
189	i?86)	MACHINE_ARCH=i386;; # we don't care about i686 vs i586
190	esac
191	LOCAL_FS=ext2
192	PS_AXC=axc
193	[ -x /usr/bin/md5sum ] && { MD5=/usr/bin/md5sum; export MD5; }
194	;;
195QNX)
196	case $MACHINE in
197	x86pc)	MACHINE_ARCH=i386;;
198	esac
199	;;
200Haiku)
201	case $MACHINE in
202	BeBox)	MACHINE_ARCH=powerpc;;
203	BeMac)	MACHINE_ARCH=powerpc;;
204	BePC)	MACHINE_ARCH=i386;;
205	esac
206	;;
207esac
208LOCALBASE=${LOCALBASE:-/usr/local}
209
210HOSTNAME=${HOSTNAME:-`( hostname ) 2>/dev/null`}
211HOSTNAME=${HOSTNAME:-`( uname -n ) 2>/dev/null`}
212case "$HOSTNAME" in
213*.*)	HOST=`IFS=.; set -- $HOSTNAME; echo $1`;;
214*)	HOST=$HOSTNAME;;
215esac
216
217TMP_DIRS=${TMP_DIRS:-"/tmp /var/tmp"}
218MACHINE_ARCH=${MACHINE_ARCH:-$MACHINE}
219HOST_ARCH=${HOST_ARCH:-$MACHINE_ARCH}
220case "$HOST_ARCH" in
221x86*64|amd64) MACHINE32_ARCH=i386;;
222*64) MACHINE32_ARCH=`echo $MACHINE_ARCH | sed 's,64,32,'`;;
223*) MACHINE32_ARCH=$MACHINE_ARCH;;
224esac
225HOST_ARCH32=${HOST_ARCH32:-$MACHINE32_ARCH}
226# we mount server:/share/arch/$SHARE_ARCH as /usr/local
227SHARE_ARCH_DEFAULT=$OS/$OSMAJOR.X/$HOST_ARCH
228SHARE_ARCH=${SHARE_ARCH:-$SHARE_ARCH_DEFAULT}
229LN=${LN:-ln}
230TR=${TR:-tr}
231
232# Some people like have /share/$HOST_TARGET/bin etc.
233HOST_TARGET=`echo ${OS}${OSMAJOR}-$HOST_ARCH | tr -d / | toLower`
234HOST_TARGET32=`echo ${OS}${OSMAJOR}-$HOST_ARCH32 | tr -d / | toLower`
235export HOST_TARGET HOST_TARGET32
236
237case `echo -n .` in -n*) echo_n=; echo_c="\c";; *) echo_n=-n; echo_c=;; esac
238
239Echo() {
240	case "$1" in
241	-n) shift; echo $echo_n "$@$echo_c";;
242	*)  echo "$@";;
243	esac
244}
245
246# for systems that deprecate egrep
247case "`echo egrep | egrep 'e|g' 2>&1`" in
248egrep) ;;
249*) egrep() { grep -E "$@"; };;
250esac
251
252export HOSTNAME HOST
253export OS MACHINE MACHINE_ARCH OSREL OSMAJOR LOCAL_FS TMP_DIRS MAILER N C K PS_AXC
254export LN SHARE_ARCH TR
255export LOCALBASE
256
257case /$0 in
258*/os.sh)
259	for v in $*
260	do
261		eval vv=\$$v
262		echo "$v='$vv'"
263	done
264	;;
265*/host_target32) echo $HOST_TARGET32;;
266*/host_target) echo $HOST_TARGET;;
267esac
268
269