1#	$NetBSD: mknative.common,v 1.16 2018/04/15 15:13:37 christos Exp $
2#
3# from: NetBSD: mknative,v 1.12 2003/03/05 06:17:17 mrg Exp
4#
5# shell-fragment common to all "mknative" scripts
6
7bomb()
8{
9	echo >&2 "ABORT: $*"
10	exit 1
11}
12
13# Make sure we can run OK.
14if [ -x "$MAKE" ]; then
15	:
16else
17	bomb "MAKE not set"
18fi
19
20# usage: getvars MAKEFILE VARNAME [VARNAME...]
21#
22getvars()
23{
24	_mfp="$1"; shift
25	case "$MAKE" in
26	*gmake)
27	_dir=$(dirname "$_TMPDIR/$_mfp")
28	_mf=$(basename "$_TMPDIR/$_mfp")
29	env MAKEFLAGS= $MAKE -C "$_dir" --quiet -f - -f "$_mf" _x_ <<EOF || bomb "getvars $_mf $* failed"
30define echo_var
31	@echo G_\${var}="\${\${var}}" | sed -e 's,\([^\.]\)\./\([a-zA-Z0-9_-]*\.o\),\1\2,g' -e 's,$_VPATH,\$\${GNUHOSTDIST},g' -e 's,$_GNU_DIST,\$\${GNUHOSTDIST},g' -e 's@"\\([A-Za-z0-9_./]*\\)"@\\\\"\\1\\\\"@g' -e "s@'"'\\([A-Za-z0-9./]*\\)'"'"'@\\\\"\\1\\\\"@g'
32
33
34endef
35_x_:
36	\$(foreach var,$*,\$(echo_var))
37EOF
38		;;
39	*)
40	$MAKE -B -f - _x_ <<EOF || bomb "getvars $_mfp $* failed"
41_x_:
42.for var in $*
43	@echo G_\${var}=\${\${var}:Q} | sed -e 's,\([^\.]\)\./\([a-zA-Z0-9_-]*\.o\),\1\2,g' -e 's,$_VPATH,\$\${GNUHOSTDIST},g' -e 's,$_GNU_DIST,\$\${GNUHOSTDIST},g'
44.endfor
45.include "$_TMPDIR/$_mfp"
46EOF
47	;;
48	esac
49}
50
51# usage: getlinks <config.status> <subdir>
52#
53getlinks()
54{
55	_cs="$1"; shift
56	_dir="$1"; shift
57	echo -n G_CONFIGLINKS=
58	grep "^config_links=" $_cs | sed -e 's@config_links="\([^"]*\)"@\1@g' -e "s@\([^:]*\):\([^ ]*\)@\${GNUHOSTDIST}/${_dir}/\2 \1 @g"
59}
60
61# usage: write_c FILENAME
62#
63write_c()
64{
65	echo '/* This file is automatically generated.  DO NOT EDIT! */' >$_TOP/$1.tmp || \
66		bomb "cannot create $1"
67	grep '$''NetBSD' $0 | sed 's,[	#$],,g;s,.*,/* Generated from: & */,' >>$_TOP/$1.tmp
68	echo '$NetBSD: mknative.common,v 1.16 2018/04/15 15:13:37 christos Exp $' | sed 's,[#$],,g;s,.*,/* Generated from: & */,' >>$_TOP/$1.tmp
69	echo '' >>$_TOP/$1.tmp
70	writefile $1
71}
72
73# usage: write_mk FILENAME
74#
75write_mk()
76{
77	echo '# This file is automatically generated.  DO NOT EDIT!' >$_TOP/$1.tmp || \
78		bomb "cannot create $1"
79	grep '$''NetBSD' $0 | sed 's,[	#$],,g;s,.*,# Generated from: &,' >>$_TOP/$1.tmp
80	echo '$NetBSD: mknative.common,v 1.16 2018/04/15 15:13:37 christos Exp $' | sed 's,[#$],,g;s,.*,# Generated from: &,' >>$_TOP/$1.tmp
81	echo '#' >>$_TOP/$1.tmp
82	writefile $1
83}
84
85writefile()
86{
87	sed -e 's,netbsd\(elf\)*1[0-9\.]*\(_\)*[A-Z]*,netbsd\1,' \
88	    -e 's,^/\* #undef HAVE_MMAP \*/$,#define HAVE_MMAP 1,' \
89	    >>$_TOP/$1.tmp
90
91		# Compare new file, sans "generated from" comments and RCS Id,
92		# to old file.  If they match, don't change anything.
93	rm -f $_TMPDIR/.1 $_TMPDIR/.2
94	grep -v 'Generated from:' $_TOP/$1 >$_TMPDIR/.1 2>/dev/null
95	grep -v 'Generated from:' $_TOP/$1.tmp >$_TMPDIR/.2
96
97		# will not overwrite a file that has the same content
98	if cmp $_TMPDIR/.1 $_TMPDIR/.2 >/dev/null 2>&1; then
99		rm -f $_TOP/$1.tmp
100	else
101		echo >&2 "$1 changed"
102		mv -f $_TOP/$1.tmp $_TOP/$1
103	fi
104}
105