1#!/bin/sh
2#
3# Copyright (c) 1999-2008 Apple Inc. All rights reserved.
4#
5# @APPLE_LICENSE_HEADER_START@
6#
7# "Portions Copyright (c) 1999, 2008 Apple Inc.  All Rights
8# Reserved.  This file contains Original Code and/or Modifications of
9# Original Code as defined in and that are subject to the Apple Public
10# Source License Version 1.0 (the 'License').  You may not use this file
11# except in compliance with the License.  Please obtain a copy of the
12# License at http://www.apple.com/publicsource and read it before using
13# this file.
14#
15# The Original Code and all software distributed under the License are
16# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
20# License for the specific language governing rights and limitations
21# under the License."
22#
23# @APPLE_LICENSE_HEADER_END@
24#
25# Mach Operating System
26# Copyright (c) 1991,1990 Carnegie Mellon University
27# All Rights Reserved.
28#
29# Permission to use, copy, modify and distribute this software and its
30# documentation is hereby granted, provided that both the copyright
31# notice and this permission notice appear in all copies of the
32# software, derivative works or modified versions, and any portions
33# thereof, and that both notices appear in supporting documentation.
34#
35# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
36# CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
37# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
38#
39# Carnegie Mellon requests users of this software to return to
40#
41#  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
42#  School of Computer Science
43#  Carnegie Mellon University
44#  Pittsburgh PA 15213-3890
45#
46# any improvements or extensions that they make and grant Carnegie Mellon
47# the rights to redistribute these changes.
48#
49
50realpath()
51{
52	local FILE="$1"
53	local PARENT=$(dirname "$FILE")
54	local BASE=$(basename "$FILE")
55	pushd "$PARENT" >/dev/null 2>&1 || return 0
56	local DIR=$(pwd -P)
57	popd >/dev/null
58	if [ "$DIR" == "/" ]; then
59		echo "/$BASE"
60	else
61		echo "$DIR/$BASE"
62	fi
63	return 1
64}
65
66scriptPath=$(realpath "$0")
67scriptRoot=$(dirname "$scriptPath")
68migcomPath=$(realpath "${scriptRoot}/../libexec/migcom")
69
70if [ -n "${SDKROOT}" ]; then
71	sdkRoot="${SDKROOT}";
72fi
73
74if [ -z "${MIGCC}" ]; then
75  xcrunPath="/usr/bin/xcrun"
76  if [ -x "${xcrunPath}" ]; then
77    MIGCC=`"${xcrunPath}" -sdk "$sdkRoot" -find cc`
78  else
79    MIGCC=$(realpath "${scriptRoot}/cc")
80  fi
81fi
82
83C=${MIGCC}
84M=${MIGCOM-${migcomPath}}
85
86cppflags="-D__MACH30__"
87
88files=
89arch=`/usr/bin/arch`
90
91WORKTMP=`/usr/bin/mktemp -d "${TMPDIR:-/tmp}/mig.XXXXXX"`
92if [ $? -ne 0 ]; then
93      echo "Failure creating temporary work directory: ${WORKTMP}"
94      echo "Exiting..."
95      exit 1
96fi
97
98# parse out the arguments until we hit plain file name(s)
99
100until [ $# -eq 0 ]
101do
102    case "$1" in
103	-[dtqkKQvVtTrRsSlLxXnN] ) migflags=( "${migflags[@]}" "$1" ); shift;;
104	-i	) sawI=1; migflags=( "${migflags[@]}" "$1" "$2" ); shift; shift;;
105	-user   )  user="$2"; if [ ! "${sawI-}" ]; then migflags=( "${migflags[@]}" "$1" "$2" ); fi; shift; shift;;
106	-server )  server="$2";  migflags=( "${migflags[@]}" "$1" "$2" ); shift; shift;;
107	-header )  header="$2";  migflags=( "${migflags[@]}" "$1" "$2"); shift; shift;;
108	-sheader ) sheader="$2"; migflags=( "${migflags[@]}" "$1" "$2"); shift; shift;;
109	-iheader ) iheader="$2"; migflags=( "${migflags[@]}" "$1" "$2"); shift; shift;;
110	-dheader ) dheader="$2"; migflags=( "${migflags[@]}" "$1" "$2"); shift; shift;;
111	-arch ) arch="$2"; shift; shift;;
112	-maxonstack ) migflags=( "${migflags[@]}" "$1" "$2"); shift; shift;;
113	-split ) migflags=( "${migflags[@]}" "$1" ); shift;;
114	-novouchers ) migflags=( "${migflags[@]}" "$1" ); shift;;
115	-MD ) sawMD=1; cppflags=( "${cppflags[@]}" "$1"); shift;;
116	-cpp) shift; shift;;
117	-cc) C="$2"; shift; shift;;
118	-migcom) M="$2"; shift; shift;;
119	-isysroot) sdkRoot=$(realpath "$2"); shift; shift;;
120	-* ) cppflags=( "${cppflags[@]}" "$1"); shift;;
121	* ) break;;
122    esac
123done
124
125# process the rest as files
126until [ $# -eq 0 ]
127do
128    case "$1" in
129	-[dtqkKQvVtTrRsSlLxXnN] ) echo "warning: option \"$1\" after filename(s) ignored"; shift; continue;;
130	-i	) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
131	-user   ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
132	-server ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
133	-header ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
134	-sheader ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
135	-iheader ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
136	-dheader ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
137	-arch ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift ; shift; continue;;
138	-maxonstack ) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
139	-split ) echo "warning: option \"$1\" after filename(s) ignored"; shift; continue;;
140	-novouchers ) echo "warning: option \"$1\" after filename(s) ignored"; shift; continue;;
141	-MD ) echo "warning: option \"$1\" after filename(s) ignored"; shift; continue;;
142	-cpp) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
143	-cc) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
144	-migcom) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
145	-isysroot) echo "warning: option \"$1 $2\" after filename(s) ignored"; shift; shift; continue;;
146	-* ) echo "warning: option \"$1\" after filename(s) ignored"; shift; continue;;
147        * ) file="$1"; shift;;
148    esac
149    base="$(basename "${file}" .defs)"
150    temp="${WORKTMP}/${base}.$$"
151    sourcedir="$(dirname "${file}")"
152    if [ -n "${sdkRoot}" ]
153    then
154      iSysRootParm=( "-isysroot" "${sdkRoot}" )
155    fi
156    if [ ! -r "${file}" ]
157    then
158      echo "error: cannot read file ${file}"
159      rm -rf ${WORKTMP}
160      exit 1
161    fi
162    rm -f "${temp}.c" "${temp}.d"
163    (echo '#line 1 '\"${file}\" ; cat "${file}" ) > "${temp}.c"
164    "$C" -E -arch ${arch} "${cppflags[@]}" -I "${sourcedir}" "${iSysRootParm[@]}" "${temp}.c" | "$M" "${migflags[@]}"
165    if [ $? -ne 0 ]
166    then
167      rm -rf "${temp}.c" "${temp}.d" "${WORKTMP}"
168      exit 1
169    fi
170    if [ "${sawMD}" -a -f "${temp}.d" ]
171    then
172	deps=
173	s=
174	rheader="${header-${base}.h}"
175	if [ "${rheader}" != /dev/null ]; then
176		deps="${deps}${s}${rheader}"; s=" "
177	fi
178	ruser="${user-${base}User.c}"
179	if [ "${ruser}" != /dev/null ]; then
180		deps="${deps}${s}${ruser}"; s=" "
181	fi
182	rserver="${server-${base}Server.c}"
183	if [ "${rserver}" != /dev/null ]; then
184		deps="${deps}${s}${rserver}"; s=" "
185	fi
186	rsheader="${sheader-/dev/null}"
187	if [ "${rsheader}" != /dev/null ]; then
188		deps="${deps}${s}${rsheader}"; s=" "
189	fi
190	riheader="${iheader-/dev/null}"
191	if [ "${riheader}" != /dev/null ]; then
192		deps="${deps}${s}${riheader}"; s=" "
193	fi
194	rdheader="${dheader-/dev/null}"
195	if [ "${rdheader}" != /dev/null ]; then
196		deps="${deps}${s}${rdheader}"; s=" "
197	fi
198	for target in "${deps}"
199	do
200		sed -e 's;^'"${temp}"'.o[ 	]*:;'"${target}"':;' \
201		    -e 's;: '"${temp}"'.c;: '"$file"';' \
202		< "${temp}.d" > "${target}.d"
203	done
204	rm -f "${temp}.d"
205    fi
206    rm -f "${temp}.c"
207done
208
209/bin/rmdir "${WORKTMP}"
210exit 0
211
212