1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#pragma ident	"@(#)tst.include.ksh	1.1	06/08/28 SMI"
27
28if [ -f /usr/lib/dtrace/darwin.d ]; then
29CC=/usr/bin/gcc
30else
31CC=/usr/sfw/bin/gcc
32fi
33CFLAGS=
34
35doit()
36{
37	file=$1
38	ofile=$2
39	errfile=$3
40	cfile=${TMPDIR:-/tmp}/inc.$$.$file.c
41	cofile=${TMPDIR:-/tmp}/inc.$$.$file
42	cat > $cfile <<EOF
43#include <sys/$file>
44void
45main()
46{}
47EOF
48	if $CC $CFLAGS -o $cofile $cfile >/dev/null 2>&1; then
49		dtrace -xerrtags -C -s /dev/stdin \
50		    >/dev/null 2>$errfile <<EOF
51#include <sys/$file>
52BEGIN
53{
54	exit(0);
55}
56EOF
57		if [ $? -ne 0 ]; then
58			echo $inc failed: `cat $errfile | head -1` > $ofile
59		else
60			echo $inc succeeded > $ofile
61		fi
62		rm -f $errfile
63	fi
64
65	rm -f $cofile $cfile 2>/dev/null
66}
67
68if [ ! -x $CC ]; then
69	echo "$0: bad compiler: $CC" >& 2
70	exit 1
71fi
72
73if [ -f /usr/lib/dtrace/darwin.d ]; then
74let concurrency=1
75else
76concurrency=`psrinfo | wc -l`
77let concurrency=concurrency*4
78fi
79
80let i=0
81
82files=/usr/include/sys/*.h
83
84#
85# There are a few files in /usr/include/sys that are known to be bad -- usually
86# because they include static globals (!) or function bodies (!!) in the header
87# file.  Hopefully these remain sufficiently few that the O(#files * #badfiles)
88# algorithm, below, doesn't become a problem.  (And yes, writing scripts in
89# something other than ksh1888 would probably be a good idea.)  If this script
90# becomes a problem, kindly fix it by reducing the number of bad files!  (That
91# is, fix it by fixing the broken file, not the broken script.)
92#
93
94if [ -f /usr/lib/dtrace/darwin.d ]; then
95#
96# libkern/_OSByteOrder.h gives us fits ...
97#
98badfiles="attr.h buf.h dtrace.h fasttrap.h fasttrap_isa.h file.h kern_event.h kernel_types.h lock.h mount.h param.h select.h sys_domain.h sysctl.h time.h tty.h types.h ucred.h user.h vnode_if.h wait.h xattr.h"
99else
100badfiles="ctype.h eri_msg.h ser_sync.h sbpro.h"
101fi
102
103for inc in $files; do
104	file=`basename $inc`
105	for bad in $badfiles; do
106		if [ "$file" = "$bad" ]; then
107			continue 2 
108		fi
109	done
110
111	ofile=${TMPDIR:-/tmp}/inc.$file.$$.out
112	errfile=${TMPDIR:-/tmp}/inc.$file.$$.err
113	doit $file $ofile $errfile &
114	let i=i+1
115
116	if [ $i -eq $concurrency ]; then
117		#
118		# This isn't optimal -- it creates a highly fluctuating load
119		# as we wait for all work to complete -- but it's an easy
120		# way of parallelizing work.
121		#
122		wait
123		let i=0
124	fi
125done
126
127wait
128
129bigofile=${TMPDIR:-/tmp}/inc.$$.out
130
131for inc in $files; do
132	file=`basename $inc`
133	ofile=${TMPDIR:-/tmp}/inc.$file.$$.out
134
135	if [ -f $ofile ]; then
136		cat $ofile >> $bigofile
137		rm $ofile
138	fi
139done
140
141status=$(grep "failed:" $bigofile | wc -l)
142cat $bigofile
143rm -f $bigofile
144exit $status
145