1#!/usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
16#
17
18: "${MAKE:=/usr/bin/make}"
19: "${FILEDIR:=/opt/util-tests/tests/files}"
20
21[[ -d "$FILEDIR" ]] || fail "no files directory $FILEDIR"
22
23typeset -i fail=0
24
25function fail {
26	echo "FAIL $@"
27	((fail++))
28}
29
30function pass {
31	echo "PASS $@"
32}
33
34function check_results {
35	typeset expected="$1"
36	typeset actual="$2"
37	typeset name="$3"
38
39	if ! cmp -s $expected $actual; then
40		fail "$name"
41		diff -u "$expected" "$actual" | sed 's/^/    /'
42	else
43		pass "$name"
44	fi
45}
46
47test_make_C() {
48	TD=$(mktemp -d -t)
49
50	if [[ ! -d "$TD" ]]; then
51		fail "couldn't create test directory $TD"
52		return
53	fi
54
55	# Create output files corresponding to running make in each directory
56	# and to running make with a -C argument pointing to the directory.
57	# The results should be identical.
58	for s in "" a b c; do
59		# Baseline - running 'make' in the directory
60		( cd "$FILEDIR/make_a/$s"; $MAKE ) > $TD/M_a_$s.out 2>&1
61
62		# Running 'make -C' from a directory with no make* files
63		( cd "$TD"; $MAKE -C "$FILEDIR/make_a/$s" ) \
64		    > $TD/Ce_a_$s.out 2>&1
65		check_results $TD/{M,Ce}_a_$s.out \
66		    "make -C a/$s from empty directory"
67
68		# Running 'make -C' from a directory WITH make* files
69		( cd "$FILEDIR/make_a"; $MAKE -C "$FILEDIR/make_a/$s" ) \
70		    > $TD/C_a_$s.out 2>&1
71		check_results $TD/{M,C}_a_$s.out \
72		    "make -C a/$s from non-empty directory"
73
74		# Using MAKEFLAGS from a directory with no make* files
75		( cd "$TD"; MAKEFLAGS="-C $FILEDIR/make_a/$s" $MAKE) \
76		    > $TD/Fe_a_$s.out 2>&1
77		check_results $TD/{M,Fe}_a_$s.out \
78		    "makeflags -C a/$s from empty directory"
79
80		# Using MAKEFLAGS from a directory WITH make* files
81		( cd "$FILEDIR/make_a"; \
82		    MAKEFLAGS="-C $FILEDIR/make_a/$s" $MAKE) \
83		    > $TD/F_a_$s.out 2>&1
84		check_results $TD/{M,F}_a_$s.out \
85		    "makeflags -C a/$s from non-empty directory"
86	done
87
88	rm -rf "$TD"
89}
90
91test_make_C_multiple() {
92	TD=$(mktemp -d -t)
93
94	if [[ ! -d "$TD" ]]; then
95		fail "couldn't create test directory $TD"
96		return
97	fi
98
99	# Expected output
100	( cd "$FILEDIR/make_a/b"; $MAKE ) > $TD/expect 2>&1
101
102	# Running 'make -C' from a directory with no make* files
103	( cd "$TD"; $MAKE -C $FILEDIR/make_a/a -C $FILEDIR/make_a/b ) \
104	    > $TD/empty 2>&1
105	check_results $TD/{expect,empty} "make -C -C from empty directory"
106
107	# Running 'make -C' from a directory WITH make* files
108	( cd "$FILEDIR/make_a"; \
109	    $MAKE -C $FILEDIR/make_a/a -C $FILEDIR/make_a/b ) \
110	    > $TD/with 2>&1
111	check_results $TD/{expect,with} "make -C -C from non-empty directory"
112
113	# Using MAKEFLAGS from a directory with no make* files
114	( cd "$TD";
115	    MAKEFLAGS="-C $FILEDIR/make_a/a -C $FILEDIR/make_a/b" $MAKE ) \
116	    > $TD/emptyflags 2>&1
117	check_results $TD/{expect,emptyflags} \
118	    "makeflags -C -C from empty directory"
119
120	# Using MAKEFLAGS from a directory WITH make* files
121	( cd "$FILEDIR/make_a";
122	    MAKEFLAGS="-C $FILEDIR/make_a/a -C $FILEDIR/make_a/b" $MAKE ) \
123	    > $TD/withflags 2>&1
124	check_results $TD/{expect,withflags} \
125	    "makeflags -C -C from non-empty directory"
126
127	( cd "$FILEDIR/make_l"; $MAKE -C ../make_a/a -C ../b ) \
128	    > $TD/relative 2>&1
129	check_results $TD/{expect,relative} \
130	    "make -C -C relative from empty directory"
131
132	rm -rf "$TD"
133}
134
135test_make_C_invalid() {
136	outf=$(mktemp)
137
138	tst="make -C error"
139	$MAKE -C > $outf 2>&1 && fail "$tst" || pass "$tst"
140	egrep -s 'Missing argument' $outf && pass "$tst (output)" \
141	    || fail "$tst (output)"
142
143	tst="MAKEFLAGS=-C error"
144	MAKEFLAGS="-C" $MAKE > $outf 2>&1 && fail "$tst" || pass "$tst"
145	egrep -s 'Missing argument' $outf && pass "$tst (output)" \
146	    || fail "$tst (output)"
147
148	tst="make -C <noexist>"
149	$MAKE -C /no/such/directory > $outf 2>&1 && fail "$tst" || pass "$tst"
150	egrep -s 'No such file or directory' $outf && pass "$tst (output)" \
151	    || fail "$tst (output)"
152
153	tst="MAKEFLAGS=-C <noexist>"
154	MAKEFLAGS="-C /no/such/directory" $MAKE > $outf 2>&1 && fail "$tst" \
155	    || pass "$tst"
156	egrep -s 'No such file or directory' $outf  && pass "$tst (output)" \
157	    || fail "$tst (output)"
158
159	rm -f $outf
160}
161
162test_make_C
163test_make_C_multiple
164test_make_C_invalid
165
166[[ $fail -gt 0 ]] && exit -1
167
168exit 0
169
170