5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
26 #ident "%Z%%M% %I% %E% SMI"
30 # Given a header file, extract function prototypes and global variable
31 # declarations in a form that can be used in a mapfile. The list of extracted
32 # functions and variables will be combined with a user-specified template to
33 # create a complete mapfile.
38 # The template contains two sections - the prologue, and the epilogue. These
39 # sections are used, verbatim, as the beginning and the end of the mapfile.
40 # Sections begin and end with single-line comments whose sole contents are
41 # "/* BEGIN $section */" and "/* END $section */".
45 # /* BEGIN PROLOGUE */
46 # [ ... prologue goes here ... ]
48 # /* BEGIN EPILOGUE */
49 # [ ... epilogue goes here ... ]
52 # Selective Exportation
53 # ---------------------
55 # Some header files will have a public/private interface mix that is strongly
56 # biased towards private interfaces. That is, of the interfaces declared by
57 # a given header file, the majority of them are private. Only a small subset
58 # of interfaces are to be exported publicly. Using Selective Exportation, a
59 # special comment is included in the header file, declaring to this script that
60 # only a subset of interfaces - those with a marking declared in the comment -
61 # should be included in the mapfile. The marking is itself a special comment,
62 # whose format is declared using a directive like this:
64 # MAPFILE: export "Driver OK"
66 # Using the above directive, only those function prototypes and variable
67 # declarations with "/* Driver OK */" comments included in the mapfile. Note
68 # that the comment must be at the end of the first line. If the declaration
69 # spans multiple lines, the exportation comment must appear on the first line.
71 # Examples of functions selected for exportation:
73 # MAPFILE: export "Driver OK"
75 # extern int foo(int); /* Driver OK */
76 # extern void bar(int, int, /* Driver OK */
79 # Selective Exportation may not be used in the same file as Selective Exclusion.
84 # Selective Exclusion is to be used in cases where the public/private interface
85 # mix is reversed - where public interfaces greatly outnumber the private ones.
86 # In this case, we want to be able to mark the private ones, thus telling this
87 # script that the marked interfaces are to be excluded from the mapfile.
88 # Marking is accomplished via a process similar to that used for Selective
89 # Exportation. A directive is included in a comment, and is formatted like
92 # MAPFILE: exclude "Internal"
94 # Using the above directive, function prototypes and variable declarations with
95 # "/* Internal */" comments would be excluded. Note that the comment must be at
96 # the end of the first line. If the declaration spans multiple lines, the
97 # exclusion comment must appear on the first line.
99 # Examples of functions excluded from exportation:
101 # MAPFILE: exclude "Internal"
103 # extern int foo(int); /* Internal */
104 # extern void bar(int, int, /* Internal */
107 # Selective Exclusion may not be used in the same file as Selective Exportation.
110 function extract_prototypes
115 nawk
-v prefix
="$prefix" <$header '
116 /^.*MAPFILE: export \"[^\"]*\"$/ {
118 print "ERROR: export after exclude\n";
122 sub(/^[^\"]*\"/, "");
125 exportmark=sprintf("/* %s */", $0);
129 /^.*MAPFILE: exclude \"[^\"]*\"$/ {
131 print "ERROR: exclude after export";
135 sub(/^[^\"]*\"/, "");
138 excludemark=sprintf("/* %s */", $0);
143 # Selective Exportation has been selected (exportmark is
144 # set), so exclude this line if it does not have the
146 if (length($0) < length(exportmark) ||
147 substr($0, length($0) - length(exportmark) + 1) != \
153 # Selective Exclusion has been selected (excludemark is
154 # set), so exclude this line only if it has the magic
156 if (length($0) > length(excludemark) &&
158 length($0) - length(excludemark) + 1) == \
165 for (i = 1; i <= NF; i++) {
166 if (sub(/\(.*$/, "", $i)) {
169 printf("%s%s;\n", prefix, $i);
180 for (i = 1; i <= NF; i++) {
181 if (match($i, /;$/)) {
182 printf("%s%s; /* variable */\n", prefix,
183 substr($i, 1, length($i) - 1));
189 ' || die
"Extraction failed"
192 function extract_section
197 nawk
<$skel -v name
=$secname -v skel
=$skel '
198 /\/\* [^ ]* [^ ]* \*\// && $3 == name {
207 printing != 0 { print; }
213 echo "$PROGNAME: $@" >&2
219 echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
223 PROGNAME
=$
(basename "$0")
225 while getopts t
: c
; do
235 [[ -z "$mapfile_skel" ]] && usage
236 [[ ! -f $mapfile_skel ]] && die
"Couldn't open template $tmplfile"
238 shift $
(($OPTIND - 1))
240 [[ $# -lt 1 ]] && usage
243 [[ ! -f $file ]] && die
"Can't open input file $file"
246 extract_section
$mapfile_skel PROLOGUE
250 echo "\t\t * Exported functions and variables from:"
253 extract_prototypes
$file "\t\t"
257 extract_section
$mapfile_skel EPILOGUE