xref: /freebsd/contrib/bmake/mk/install-mk (revision ab393e9548f8cc0ee28499c411963b798ebb38a5)
1:
2# NAME:
3#	install-mk - install mk files
4#
5# SYNOPSIS:
6#	install-mk [options] [var=val] [dest]
7#
8# DESCRIPTION:
9#	This tool installs mk files in a semi-intelligent manner into
10#	"dest".
11#
12#	Options:
13#
14#	-n	just say what we want to do, but don't touch anything.
15#
16#	-f	use -f when copying sys,mk.
17#
18#	-v	be verbose
19#
20#	-q	be quiet
21#
22#	-m "mode"
23#		Use "mode" for installed files (444).
24#
25#	-o "owner"
26#		Use "owner" for installed files.
27#
28#	-g "group"
29#		Use "group" for installed files.
30#
31#	-U "umask"
32#		Use "umask" so directories are created with suitable
33#		mode (default is 022).
34#
35#	var=val
36#		Set "var" to "val".  See below.
37#
38#	All our *.mk files are copied to "dest" with appropriate
39#	ownership and permissions.
40#
41#	By default if a sys.mk can be found in a standard location
42#	(that bmake will find) then no sys.mk will be put in "dest".
43#
44#	SKIP_SYS_MK:
45#		If set, we will avoid installing our 'sys.mk'
46#		This is probably a bad idea.
47#
48#	SKIP_BSD_MK:
49#		If set, we will skip making bsd.*.mk links to *.mk
50#
51#	sys.mk:
52#
53#	By default (and provided we are not installing to the system
54#	mk dir - '/usr/share/mk') we install our own 'sys.mk' which
55#	includes a sys specific file, or a generic one.
56#
57#
58# AUTHOR:
59#       Simon J. Gerraty <sjg@crufty.net>
60
61# RCSid:
62#	$Id: install-mk,v 1.242 2023/09/09 16:00:16 sjg Exp $
63#
64#	@(#) Copyright (c) 1994-2023 Simon J. Gerraty
65#
66#	This file is provided in the hope that it will
67#	be of use.  There is absolutely NO WARRANTY.
68#	Permission to copy, redistribute or otherwise
69#	use this file is hereby granted provided that
70#	the above copyright notice and this notice are
71#	left intact.
72#
73#	Please send copies of changes and bug-fixes to:
74#	sjg@crufty.net
75#
76
77MK_VERSION=20230909
78OWNER=
79GROUP=
80MODE=444
81BINMODE=555
82ECHO=:
83SKIP=
84cp_f=-f
85
86umask 022
87
88while :
89do
90	case "$1" in
91	*=*) eval "$1"; shift;;
92	+f) cp_f=; shift;;
93	-f) cp_f=-f; shift;;
94	-m) MODE=$2; shift 2;;
95	-o) OWNER=$2; shift 2;;
96	-g) GROUP=$2; shift 2;;
97	-v) ECHO=echo; shift;;
98	-q) ECHO=:; shift;;
99	-n) ECHO=echo SKIP=:; shift;;
100	-U) umask $2; shift;;
101	--) shift; break;;
102	*) break;;
103	esac
104done
105
106case $# in
1070)	echo "$0 [options] <destination> [<os>]"
108	echo "eg."
109	echo "$0 -o bin -g bin -m 444 /usr/local/share/mk"
110	exit 1
111	;;
112esac
113dest=$1
114os=${2:-`uname`}
115osrel=${3:-`uname -r`}
116
117Do() {
118	$ECHO "$@"
119	$SKIP "$@"
120}
121
122Error() {
123	echo "ERROR: $@" >&2
124	exit 1
125}
126
127Warning() {
128	echo "WARNING: $@" >&2
129}
130
131[ "$FORCE_SYS_MK" ] && Warning "ignoring: FORCE_{BSD,SYS}_MK (no longer supported)"
132
133SYS_MK_DIR=${SYS_MK_DIR:-/usr/share/mk}
134SYS_MK=${SYS_MK:-$SYS_MK_DIR/sys.mk}
135
136realpath() {
137	[ -d $1 ] && cd $1 && 'pwd' && return
138	echo $1
139}
140
141# some Linux systems have deprecated egrep in favor of grep -E
142case "`echo bmake | egrep 'a' 2>&1`" in
143*"grep -E"*) egrep='grep -E';;
144*) egrep=egrep;;
145esac
146
147if [ -s $SYS_MK -a -d $dest ]; then
148	# if this is a BSD system we don't want to touch $SYS_MK
149	dest=`realpath $dest`
150	sys_mk_dir=`realpath $SYS_MK_DIR`
151	if [ $dest = $sys_mk_dir ]; then
152		case "$os" in
153		*BSD*)	SKIP_SYS_MK=:
154			SKIP_BSD_MK=:
155			;;
156		*)	# could be fake?
157			if [ ! -d $dest/sys -a ! -s $dest/Generic.sys.mk ]; then
158				SKIP_SYS_MK=: # play safe
159				SKIP_BSD_MK=:
160			fi
161			;;
162		esac
163	fi
164fi
165
166[ -d $dest/sys ] || Do mkdir -p $dest/sys
167[ -d $dest/sys ] || Do mkdir $dest/sys || exit 1
168[ -z "$SKIP" ] && dest=`realpath $dest`
169
170cd `dirname $0`
171mksrc=`'pwd'`
172if [ $mksrc = $dest ]; then
173	SKIP_MKFILES=:
174else
175	# we do not install the examples
176	mk_files=`grep '^[a-z].*\.mk' FILES | $egrep -v '(examples/|^sys\.mk|sys/)'`
177	mk_scripts=`$egrep '^[a-z].*[.-](sh|py)' FILES | $egrep -v '/'`
178	sys_mk_files=`grep 'sys/.*\.mk' FILES`
179	SKIP_MKFILES=
180	[ -z "$SKIP_SYS_MK" ] && mk_files="sys.mk $mk_files"
181fi
182$SKIP_MKFILES Do cp $cp_f $mk_files $dest
183$SKIP_MKFILES Do cp $cp_f $sys_mk_files $dest/sys
184$SKIP_MKFILES Do cp $cp_f $mk_scripts $dest
185$SKIP cd $dest
186$SKIP_MKFILES Do chmod $MODE $mk_files $sys_mk_files
187$SKIP_MKFILES Do chmod $BINMODE $mk_scripts
188[ "$GROUP" ] && $SKIP_MKFILES Do chgrp $GROUP $mk_files $sys_mk_files
189[ "$OWNER" ] && $SKIP_MKFILES Do chown $OWNER $mk_files $sys_mk_files
190# if this is a BSD system the bsd.*.mk should exist and be used.
191if [ -z "$SKIP_BSD_MK" ]; then
192	for f in dep doc files inc init lib links man nls obj own prog subdir
193	do
194		b=bsd.$f.mk
195		[ -s $b ] || Do ln -s $f.mk $b
196	done
197fi
198exit 0
199