1 |
#!/bin/sh |
2 |
# autoconf_builder.sh |
3 |
# Created by Toshi Nagata on 07/04/18. |
4 |
# |
5 |
# Copyright 2007 Toshi Nagata. |
6 |
# |
7 |
# Redistribution and use in source and binary forms, with or without modification, are permitted |
8 |
# provided that the following conditions are met: |
9 |
# |
10 |
# 1. Redistributions of source code must retain the above copyright notice, this list of |
11 |
# conditions and the following disclaimer. |
12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
13 |
# conditions and the following disclaimer in the documentation and/or other materials provided |
14 |
# with the distribution. |
15 |
# |
16 |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
17 |
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
18 |
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
19 |
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
20 |
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
21 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 |
# |
25 |
# Influential environmental variables: |
26 |
# ACTION(*): autoconf - run "make -f Makefile.cvs"; configure - run "configure"; |
27 |
# make - run "make"; install - run "make install"; |
28 |
# build (or empty string) - autoconf && configure && make (&& install) |
29 |
# "install" step is only done if WITH_INSTALL is non-empty |
30 |
# BUILD_STYLE(*): The build style. If it ends with "ppc" or "i386", it also specifies the architecture. |
31 |
# If it ends with "UB" or "Universal", it creates the Universal Binaries for the products |
32 |
# specified by $UB_PRODUCTS (see below). |
33 |
# If any of the products are missing, then this script is recursively called for |
34 |
# $BUILD_STYLE's that have "ppc" and "i386" in place of "UB" or "Universal". |
35 |
# WITH_INSTALL: When non-empty, "install" action is automatically done in "build" action. |
36 |
# CFLAGS, CXXFLAGS: Compiler flags |
37 |
# CONFIG_OPTIONS: Options for "configure" |
38 |
# BUILD_BASE_DIR: Temporary building and install directory. By default "$PWD/build". The source |
39 |
# tree is duplicated (by use of symbolic links) in $BUILD_BASE_DIR/$BUILD_STYLE/<basename>.build, |
40 |
# where <basename> is the basename of the toplevel directory of the source tree (which also |
41 |
# should be the current directory when this script is invoked). The product is "installed" |
42 |
# into $BUILD_BASE_DIR/$BUILD_STYLE/local. |
43 |
# SDKROOT: The root directory for SDK |
44 |
# UB_PRODUCTS: The products for which Universal Binaries are to be created. The paths should be |
45 |
# relative to $BUILD_BASE_DIR/$BUILD_STYLE/local. Eg. bin/some_executable, lib/some_library.a. |
46 |
# |
47 |
# The variables marked with (*) are automatically set by Xcode. |
48 |
|
49 |
BASE_DIR=$PWD |
50 |
BASE_NAME=`basename $PWD` |
51 |
|
52 |
function rel2abs () { |
53 |
(cd "$1"; pwd) |
54 |
} |
55 |
|
56 |
function abs2rel () { |
57 |
/usr/bin/perl -e 'use File::Spec; print File::Spec->abs2rel($ARGV[0], $ARGV[1]), "\n";' "$1" "$2" |
58 |
} |
59 |
|
60 |
function link_recursive () { |
61 |
local arg base i |
62 |
arg="$1" |
63 |
base=`basename "$arg"` |
64 |
if test -d "$arg"; then |
65 |
if expr "$base" = "CVS" "|" `rel2abs "$arg"` = "$BUILD_BASE_DIR" >/dev/null; then |
66 |
echo "Skipping directory $arg" |
67 |
else |
68 |
echo "Copying directory $arg" |
69 |
mkdir -p "$base" |
70 |
cd "$base" |
71 |
if ! expr "$arg" : "^/" >/dev/null; then |
72 |
arg="../$arg" |
73 |
fi |
74 |
for i in "$arg"/*; do |
75 |
link_recursive "$i" |
76 |
done |
77 |
cd .. |
78 |
fi |
79 |
else |
80 |
echo "Linking $arg" |
81 |
ln -sf $arg |
82 |
fi |
83 |
} |
84 |
|
85 |
# Sanity checks |
86 |
if test "x$BUILD_STYLE" = "x"; then |
87 |
BUILD_STYLE="Default" |
88 |
fi |
89 |
if test "x$ACTION" = "x"; then |
90 |
ACTION="build" |
91 |
fi |
92 |
if test "x$BUILD_BASE_DIR" = "x"; then |
93 |
BUILD_BASE_DIR=$PWD/build |
94 |
fi |
95 |
mkdir -p "$BUILD_BASE_DIR" || exit $? |
96 |
BUILD_BASE_DIR=`rel2abs "$BUILD_BASE_DIR"` |
97 |
|
98 |
if test -e "$SDKROOT"; then |
99 |
SYSROOT_CFLAGS="-isysroot $SDKROOT" |
100 |
else |
101 |
SYSROOT_CFLAGS= |
102 |
fi |
103 |
PATH="/usr/local/bin:$PATH" |
104 |
|
105 |
|
106 |
ARCH_CFLAGS="" |
107 |
ARCH_CONFIG_OPTIONS="" |
108 |
case "$BUILD_STYLE" in |
109 |
*ppc) |
110 |
ARCH="ppc" |
111 |
ARCH_CFLAGS="-arch ppc" |
112 |
;; |
113 |
*i386) |
114 |
ARCH="i386" |
115 |
ARCH_CFLAGS="-arch i386 -msse -msse2" |
116 |
ARCH_CONFIG_OPTIONS="--host=i386-apple-darwin8" |
117 |
;; |
118 |
*UB) |
119 |
ARCH="UB" |
120 |
BUILD_STYLE_BASE=${BUILD_STYLE/UB/} |
121 |
;; |
122 |
*Universal) |
123 |
ARCH="UB" |
124 |
BUILD_STYLE_BASE=${BUILD_STYLE/Universal/} |
125 |
;; |
126 |
*) |
127 |
echo "Warning: architecture cannot be recognized from the build style" |
128 |
ARCH="unknown" |
129 |
;; |
130 |
esac |
131 |
|
132 |
case "$BUILD_STYLE" in |
133 |
Development|Default) |
134 |
OPT_CFLAGS="-O2 -g" |
135 |
;; |
136 |
Deployment*) |
137 |
OPT_CFLAGS="-O3" |
138 |
;; |
139 |
esac |
140 |
|
141 |
export CFLAGS="$SYSROOT_CFLAGS $ARCH_CFLAGS $OPT_CFLAGS $CFLAGS" |
142 |
export CXXFLAGS="$SYSROOT_CFLAGS $ARCH_CFLAGS $OPT_CFLAGS $CXXFLAGS" |
143 |
|
144 |
if test "x$ARCH" = "xUB" -a "x$ACTION" != "xclean"; then |
145 |
# Test the existence of the products |
146 |
BUILD_STYLE_PPC=${BUILD_STYLE_BASE}ppc |
147 |
BUILD_STYLE_386=${BUILD_STYLE_BASE}i386 |
148 |
for style in $BUILD_STYLE_PPC $BUILD_STYLE_386; do |
149 |
missing=no |
150 |
for i in $UB_PRODUCTS; do |
151 |
if ! test -e "$BUILD_BASE_DIR/$style/local/$i"; then |
152 |
missing=yes |
153 |
fi |
154 |
done |
155 |
if test "$missing" = "yes"; then |
156 |
BUILD_STYLE_SAVE=$BUILD_STYLE |
157 |
export BUILD_STYLE=$style |
158 |
echo "Building with BUILD_STYLE=$style" |
159 |
/bin/sh $0 || exit $? |
160 |
BUILD_STYLE=$BUILD_STYLE_SAVE |
161 |
fi |
162 |
done |
163 |
mkdir -p "$BUILD_BASE_DIR/$BUILD_STYLE/local" || exit $? |
164 |
cd "$BUILD_BASE_DIR" |
165 |
for i in $UB_PRODUCTS; do |
166 |
mkdir -p "$BUILD_STYLE/local/"`dirname $i` || exit $? |
167 |
echo "Creating universal binary $BUILD_STYLE/local/$i" |
168 |
lipo -create "$BUILD_STYLE_PPC/local/$i" "$BUILD_STYLE_386/local/$i" -output "$BUILD_STYLE/local/$i" || exit $? |
169 |
done |
170 |
exit $? |
171 |
fi |
172 |
|
173 |
# Move to the working directory |
174 |
BUILD_DIR="$BUILD_BASE_DIR/$BUILD_STYLE/$BASE_NAME.build" |
175 |
mkdir -p "$BUILD_DIR" |
176 |
BUILD_DIR=`rel2abs "$BUILD_DIR"` |
177 |
CONFIG_OPTIONS="--prefix=$BUILD_BASE_DIR/$BUILD_STYLE/local $CONFIG_OPTIONS" |
178 |
|
179 |
# Display all environments |
180 |
set |
181 |
|
182 |
cd $BUILD_DIR |
183 |
|
184 |
# Clean if specified |
185 |
if test "x$ACTION" = "xclean"; then |
186 |
# if test "x$WITH_INSTALL" != "x" -a -e "Makefile"; then |
187 |
# echo "Doing make uninstall" |
188 |
# make uninstall |
189 |
# fi |
190 |
echo "Removing files in $BUILD_DIR" |
191 |
cd $BASE_DIR |
192 |
rm -rf "$BUILD_DIR" |
193 |
exit $? |
194 |
fi |
195 |
|
196 |
if test "x$ACTION" = "xbuild" -o "x$ACTION" = "xconfigure"; then |
197 |
# Copy the source files if necessary |
198 |
if test ! -e Makefile -a ! -e configure -a ! -e Makefile.cvs; then |
199 |
echo "Copying the source files to $BUILD_DIR" |
200 |
for i in `abs2rel "$BASE_DIR" "$BUILD_DIR"`/*; do |
201 |
link_recursive $i |
202 |
done |
203 |
fi |
204 |
# Make ./configure if necessary |
205 |
if test -e Makefile.cvs -a ! -e configure; then |
206 |
echo "Running make -f Makefile.cvs" |
207 |
make -f Makefile.cvs || exit $? |
208 |
fi |
209 |
# Run ./configure if necessary |
210 |
if test -e configure -a ! -e Makefile; then |
211 |
CONFIG_ARGS="$CONFIG_OPTIONS $ARCH_CONFIG_OPTIONS $CONFIG_ENVS" |
212 |
echo "Running configure $CONFIG_ARGS" |
213 |
./configure $CONFIG_ARGS || exit $? |
214 |
fi |
215 |
fi |
216 |
|
217 |
if test "x$ACTION" = "xbuild" -o "x$ACTION" = "xmake"; then |
218 |
# make |
219 |
echo "Running make" |
220 |
make || exit $? |
221 |
fi |
222 |
|
223 |
# make install if specified |
224 |
if test "x$ACTION" = "xinstall" -o "(" "x$ACTION" = "xbuild" -a "x$WITH_INSTALL" != "x" ")"; then |
225 |
echo "Running make install" |
226 |
make install || exit $? |
227 |
fi |