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