1#! /bin/bash
2#
3#
4# Copyright (c) 2010 Apple Inc. All rights reserved.
5#
6# @APPLE_LICENSE_HEADER_START@
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11#
12# 1.  Redistributions of source code must retain the above copyright
13#     notice, this list of conditions and the following disclaimer.
14# 2.  Redistributions in binary form must reproduce the above copyright
15#     notice, this list of conditions and the following disclaimer in the
16#     documentation and/or other materials provided with the distribution.
17# 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
18#     contributors may be used to endorse or promote products derived from
19#     this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
22# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
25# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#
32# @APPLE_LICENSE_HEADER_END@
33#
34#
35
36set -e
37
38error()
39{
40    echo "$PROG: error: " "$@" 1>&2
41    exit 1
42}
43
44message()
45{
46    echo "$PROG: " "$@"
47}
48
49# We need to know something about the archs supported by the SDK
50# so that we can set CFLAGS to make sure that the compiler can
51# create executables. If we don't do this, configure will fail
52# unless once of the SDK archs happens to be our native arch.
53detect_sdk_arch()
54{
55    local crt1=${SDKROOT}/usr/lib/crt1.o
56    local native=$(xcrun arch)
57
58    # Get the list of archs that the SDK compiler will be able to
59    # link
60    local archs=$(xcrun lipo -info ${crt1} | awk -F: '{print $3}')
61
62    # If the SDK supports our native arch, prefer to use that.
63    for arch in $archs ; do
64	if [ "$arch" = "$native" ]; then
65	    echo $arch
66	    return
67	fi
68    done
69
70    # OK, just take anything
71    for arch in $archs ; do
72	echo $arch
73	return
74    done
75}
76
77# We need to run autoreconf if it hasn't been run or configure is
78# out of date WRT configure.in.
79autoreconf_is_required()
80{
81    if [ ! -e "./configure" ] ; then
82	return 0
83    fi
84
85    if [ "./configure.in" -nt "./configure" ] ; then
86	return 0
87    fi
88
89    return 1
90}
91
92# We need to run configure if it hasn't been run, it's out of
93# date, or the SDK changed.
94configure_is_required()
95{
96    # We need a reconfigure if there's no configure yet
97    if [ ! -e "./config.status" ]; then
98	return 0
99    fi
100
101    # We need a reconfigure if configure.in was updated and
102    # we name a new configure script
103    if [ "./configure" -nt "./config.status" ] ; then
104	return 0
105    fi
106
107    # We need a reconfigure if the SDKROOT does not match
108    if ! grep -q "#define SDKROOT \"$SDKROOT\"" \
109	    ${SRCROOT}/dcerpc/include/config.h ; then
110	message SDK changed, reconfiguring
111	return 0
112    fi
113
114    return 1
115}
116
117# If we don't have autoreconf but we already have a "config.h"
118# file, we're done. This assumes that the existance of config.h
119# means the other ".in" files have already been configured.
120skip_autoconf()
121{
122   xcrun -find autoreconf >& /dev/null || {
123      if  [[ ! -f ${SRCROOT}/dcerpc/include/config.h ]]; then
124	  error "You will need to install the automake, autoconf, and glibtool packages"
125      else
126	  return 0
127      fi
128   }
129
130    return 1
131}
132
133PROG=$(basename $0)
134SRCROOT=${SRCROOT:=$(cd $(dirname $0)/.. && pwd)}
135SDKROOT=${SDKROOT:-/}
136SDKARCH=$(detect_sdk_arch)
137
138cd "${SRCROOT}/dcerpc" || error "${SRCROOT}/dcerpc" has gone AWOL
139
140message SRCROOT is $SRCROOT
141message SDKROOT is $SDKROOT
142message SDK architecture is $SDKARCH
143
144if skip_autoconf ; then
145    message 'Skipping autoconf/configure. Using supplied "config.h" file.'
146    exit 0
147fi
148
149if autoreconf_is_required ; then
150    message "running buildconf"
151    ./buildconf || error buildconf failed
152else
153    message "configure exists, skipping buildconf phase"
154fi
155
156case $(sw_vers -productVersion) in
157    10.6.*)
158	PLATFORM_CONFIGURE_OPTIONS=--disable-gss_negotiate
159	;;
160    *)
161	PLATFORM_CONFIGURE_OPTIONS=
162	;;
163esac
164
165if configure_is_required ; then
166    message "running configure"
167    ./configure \
168	--host=$SDKARCH-apple-darwin$(uname -r) \
169	--build=$(xcrun arch)-apple-darwin$(uname -r) \
170	CC=$(xcrun -find cc) \
171	CXX=$(xcrun -find c++) \
172	CFLAGS="-arch $SDKARCH -isysroot $SDKROOT" \
173	CXXFLAGS="-arch $SDKARCH -isysroot $SDKROOT" \
174	${PLATFORM_CONFIGURE_OPTIONS} \
175	--disable-dependency-tracking \
176	--enable-idldumpers \
177	--disable-ncadg \
178	rpcd_cv_rpc_c_np_dir=/var/rpc/ncacn_np \
179	rpcd_cv_rpc_c_uxd_dir=/var/rpc/ncalrpc \
180	|| error configure failed
181else
182    message "config.status exists, skipping configure phase"
183fi
184