xref: /titanic_41/usr/src/tools/ndrgen/ndrgen.sh (revision d69c2551e89e9440043ac6ff5739b58746286f33)
1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
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.
8#
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.
13#
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]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27#
28
29# This is a wrapper script around the ndrgen compiler (ndrgen1).
30# CC must be defined in the environment or on the command line.
31
32NDRPROG="${0%/*}/ndrgen1"
33INCDIR=${ROOT}/usr/include/smbsrv
34
35PROGNAME=`basename $0`
36
37ndrgen_usage()
38{
39	if [[ $1 != "" ]] ; then
40		print "$PROGNAME: ERROR: $1"
41	fi
42
43	echo "usage: $PROGNAME [-Y cpp-path] file [file]..."
44	exit 1
45}
46
47# Copy header text from the input ndl file to the generated ndr C file.
48ndrgen_copy_header()
49{
50	ndl_file=$1
51	ndr_file=$2
52
53	nawk 'BEGIN { copy=0; }
54	/^\/\* NDRGEN_HEADER_BEGIN \*\// { copy=1; next; }
55	/^\/\* NDRGEN_HEADER_END \*\// { copy=0; next; }
56	/./ { if (copy==1) print; }' < $ndl_file > $ndr_file
57}
58
59if [[ $# -lt 1 ]] ; then
60	ndrgen_usage
61fi
62
63while getopts "Y" FLAG $*; do
64	case $FLAG in
65	Y)
66		CC_FLAG="y"
67		;;
68	*)
69		ndrgen_usage
70		;;
71	esac
72done
73
74if [[ $CC_FLAG = "y" ]] ; then
75	shift $(($OPTIND - 1))
76
77	if [[ $# -lt 1 ]] ; then
78		ndrgen_usage "C pre-processor path is missing"
79	else
80		CC=$1
81		shift $(($OPTIND - 1))
82
83		# Check for cw being invoked with -_cc or -_gcc
84		if [[ $1 = "-_cc" || $1 = "-_gcc" ]] ; then
85			CC_ARG=$1
86			shift $(($OPTIND - 1))
87		fi
88	fi
89fi
90
91if [[ $CC = "" ]] ; then
92	ndrgen_usage "C pre-processor is not defined"
93fi
94
95if [ ! -f $CC ] || [ ! -x $CC ] ; then
96	ndrgen_usage "cannot run $CC"
97fi
98
99for i
100do
101	if [[ ! -r $i ]] ; then
102		print "$PROGNAME: ERROR: cannot read $i"
103		exit 1
104	fi
105
106	BASENAME=`basename $i .ndl`
107	TMP_NAME=$BASENAME.ndl.c
108
109	cp $i $TMP_NAME
110
111	if $CC $CC_ARG -E  -D__a64 -D__EXTENSIONS__ -D_FILE_OFFSET_BITS=64 \
112		-I. -I${INCDIR} -I${INCDIR}/ndl -DNDRGEN $TMP_NAME | \
113		$NDRPROG > $BASENAME.raw
114	then
115		touch ${BASENAME}_ndr.c
116		ndrgen_copy_header $i ${BASENAME}_ndr.c
117
118		cat - << EOF >> ${BASENAME}_ndr.c
119/*
120 * Please do not edit this file.
121 * It was generated using ndrgen.
122 */
123
124#pragma ident	"@(#)${BASENAME}_ndr.c"
125
126#include <strings.h>
127#include <smbsrv/ndr.h>
128#include <smbsrv/ndl/$BASENAME.ndl>
129EOF
130
131		cat $BASENAME.raw >> ${BASENAME}_ndr.c
132
133		rm -f $BASENAME.raw
134		rm -f $TMP_NAME
135	else
136		rm -f $BASENAME.raw
137		rm -f $TMP_NAME
138		exit 1
139	fi
140done
141