1#!/bin/bash
2
3#
4# build-inc.sh - include file for other build-* scripts
5#
6# Written by
7#  Christian Vogelgsang <chris@vogelgsang.org>
8#
9# This file is part of VICE, the Versatile Commodore Emulator.
10# See README for copyright notice.
11#
12#  This program is free software; you can redistribute it and/or modify
13#  it under the terms of the GNU General Public License as published by
14#  the Free Software Foundation; either version 2 of the License, or
15#  (at your option) any later version.
16#
17#  This program is distributed in the hope that it will be useful,
18#  but WITHOUT ANY WARRANTY; without even the implied warranty of
19#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20#  GNU General Public License for more details.
21#
22#  You should have received a copy of the GNU General Public License
23#  along with this program; if not, write to the Free Software
24#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25#  02111-1307  USA.
26#
27
28parse_args () {
29  # get arguments
30  BASE_DIR="$1"
31  ARCH="$2"
32  SDK_VERSION="$3"
33  COMPILER="$4"
34  export FORCE_BUILD="$5"
35  if [ "x$ARCH" = "x" ]; then
36    echo "Usage: $0 <build-dir> <arch:ppc|i386|x86_64> [sdk:10.4|10.5|10.6] [gcc40|gcc42|clang] [force build!=0]"
37    exit 1
38  fi
39  if [ ! -d "$BASE_DIR" ]; then
40    echo "Missing base dir!"
41    exit 1
42  fi
43  # normalize base dir
44  BASE_DIR="`cd \"$BASE_DIR\" && pwd`"
45
46  # check arch
47  if [ "$ARCH" = "ppc" ]; then
48    INSTALL_DIR="$BASE_DIR/ppc"
49  elif [ "$ARCH" = "i386" ]; then
50    INSTALL_DIR="$BASE_DIR/i386"
51  elif [ "$ARCH" = "x86_64" ]; then
52    INSTALL_DIR="$BASE_DIR/x86_64"
53  else
54    echo "Unknown ARCH: $ARCH (ppc|i386|x86_64)"
55    exit 1
56  fi
57
58  # autoselect SDK
59  if [ "x$SDK_VERSION" = "x" ]; then
60    if [ "$ARCH" = "x86_64" ]; then
61      SDK_VERSION="10.6"
62    else
63      SDK_VERSION="10.4"
64    fi
65  fi
66
67  # check SDK
68  if [ "$SDK_VERSION" = "10.4" ]; then
69    SDK=/Developer/SDKs/MacOSX10.4u.sdk
70  elif [ "$SDK_VERSION" = "10.5" ]; then
71    SDK=/Developer/SDKs/MacOSX10.5.sdk
72  elif [ "$SDK_VERSION" = "10.6" ]; then
73    SDK=/Developer/SDKs/MacOSX10.6.sdk
74  else
75    echo "Unknown SDK_VERSION: $SDK_VERSION"
76    exit 1
77  fi
78
79  # autoselect compiler
80  if [ "x$COMPILER" = "x" ]; then
81    if [ "$SDK_VERSION" = "10.6" ]; then
82      COMPILER="gcc42"
83    else
84      COMPILER="gcc40"
85    fi
86  fi
87
88  # check compiler
89  if [ "$COMPILER" = "gcc40" ]; then
90    GCC="/usr/bin/gcc-4.0"
91    GXX="/usr/bin/g++-4.0"
92  elif [ "$COMPILER" = "gcc42" ]; then
93    GCC="/usr/bin/gcc-4.2"
94    GXX="/usr/bin/g++-4.2"
95  elif [ "$COMPILER" = "clang" ]; then
96    GCC="/Developer/usr/bin/clang"
97    GXX="/Developer/usr/bin/llvm-g++-4.2"
98  else
99    echo "Unknown COMPILER: $COMPILER"
100    exit 1
101  fi
102
103  # extend install dir
104  INSTALL_DIR="$INSTALL_DIR-$SDK_VERSION-$COMPILER"
105
106  # setup base dir
107  if [ ! -d "$INSTALL_DIR" ]; then
108    echo "  creating install dir $INSTALL_DIR"
109    mkdir -p "$INSTALL_DIR"
110    mkdir -p "$INSTALL_DIR/bin"
111    mkdir -p "$INSTALL_DIR/include"
112    mkdir -p "$INSTALL_DIR/lib"
113    mkdir -p "$INSTALL_DIR/man"
114  fi
115
116  # number of cpus
117  if [ "x$MULTI_CPU" != "x" ]; then
118    NUM_CPUS=`hostinfo | grep 'processors are logically available' | awk '{print $1}'`
119  else
120    NUM_CPUS=1
121  fi
122  export NUM_CPUS
123
124  # build tag
125  export BUILD_TAG="[$ARCH-$SDK_VERSION-$COMPILER,force=$FORCE_BUILD,cpus=$NUM_CPUS]"
126}
127
128# configure/compile/install a autoconf'ed distribution
129#
130# SRC         source archive
131# DIR         source directory
132# CHECK_FILE  if this file exists then assume build is already done
133# INSTALL     rule to install
134# URL         where to download archive if its missing
135# CONFIG_OPT  extra switches for configure
136#
137configure_make_install () {
138  SRC="$1"
139  DIR="$2"
140  CHECK_FILE="$3"
141  INSTALL="$4"
142  URL="$5"
143  CONFIG_OPT="$6"
144
145  echo "----- $DIR $BUILD_TAG -----"
146
147  # check if lib is available
148  if [ -e "$INSTALL_DIR/$CHECK_FILE" -a "x$FORCE_BUILD" = "x" ]; then
149    echo "  Already installed. ($CHECK_FILE available)"
150  else
151
152    # check for source archive
153    if [ "x$SRC" = "x" ]; then
154      if [ ! -d "$DIR" ]; then
155        echo "FATAL: source dir '$DIR' is missing!"
156        echo "       please setup first (from e.g. $URL)"
157        exit 1
158      fi
159    else
160      if [ ! -e "$SRC" ]; then
161        echo "FATAL: source archive '$SRC' missing in curent directory!"
162        echo "       please download first (from e.g. $URL)"
163        exit 1
164      fi
165    fi
166
167    # check if source is already unpacked
168    if [ ! -d "$DIR" ]; then
169      echo "  Unpacking source for $DIR"
170      echo "$SRC" | grep .bz2 > /dev/null
171      if [ $? != 0 ]; then
172        tar xfz "$SRC"
173      else
174        tar xfj "$SRC"
175      fi
176    fi
177    if [ ! -d "$DIR" ]; then
178      echo "FATAL: source not unpacked to $DIR"
179      exit 1
180    fi
181
182    # prepare BUILD
183    if [ "$COMPILE_IN_SOURCE" != "" ]; then
184      BUILD_DIR="$DIR"
185    else
186      BUILD_DIR="BUILD"
187      if [ -d "$BUILD_DIR" ]; then
188        if [ "x$FORCE_BUILD" != "x" ]; then
189          rm -rf "$BUILD_DIR"
190        else
191          echo "FATAL: build directory '$BUILD_DIR' already here!"
192          exit 1
193        fi
194      fi
195      mkdir BUILD
196    fi
197
198    # patch source?
199    if [ "$PATCH" != "" ]; then
200      (cd "$DIR" && eval "$PATCH")
201    fi
202
203    # build
204    echo "  configure options: $CONFIG_OPT"
205    (cd "$BUILD_DIR" && eval "../$DIR/configure --prefix=\"$INSTALL_DIR\" $CONFIG_OPT $EXTRA_OPT")
206    (cd "$BUILD_DIR" && make -j$NUM_CPUS)
207    if [ "$?" != "0" ]; then
208      echo "FATAL: make failed!"
209      exit 1
210    fi
211    echo "make $INSTALL"
212    (cd "$BUILD_DIR" && make $INSTALL)
213
214    # check for lib
215    if [ ! -e "$INSTALL_DIR/$CHECK_FILE" ]; then
216      echo "FATAL: $CHECK_FILE not found!"
217      exit 1
218    fi
219
220    # clean up
221    if [ "$COMPILE_IN_SOURCE" = "" ]; then
222      rm -rf BUILD
223    fi
224    if [ "x$SRC" != "x" ]; then
225      rm -rf "$DIR"
226    fi
227
228    echo "----- ready with $2 $BUILD_TAG -----"
229  fi
230}
231
232# create directories in install dir
233make_dirs () {
234  for DIR in "$@" ; do
235    if [ ! -d "$INSTALL_DIR/$DIR" ]; then
236      echo "  creating directory $DIR"
237      mkdir -p "$INSTALL_DIR/$DIR"
238    fi
239  done
240}
241
242set_compiler_env () {
243  # set common flags
244  export CPPFLAGS="-I$INSTALL_DIR/include"
245  export LDFLAGS="-L$INSTALL_DIR/lib"
246  export PATH="$INSTALL_DIR/bin:$PATH"
247
248  # use X11 from SDK
249  if [ "$USE_X11" = "1" ]; then
250    EXTRA_OPT="--x-includes=$SDK/usr/X11R6/include --x-libraries=$SDK/usr/X11R6/lib"
251  fi
252
253  export COMPILE_TAG="-arch $ARCH -isysroot $SDK -mmacosx-version-min=$SDK_VERSION"
254  export CC="$GCC $COMPILE_TAG"
255  export CXX="$GXX $COMPILE_TAG"
256  export LD="$GCC $COMPILE_TAG"
257}
258