1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
#!/bin/bash
# Script to compile and install FPC 2.6.0 for Mac OS X/ARM (iOS)
# (iPhone/iPod Touch/iPad)
# Copyright (c) 2009-2011 Jonas Maebe
# Licensed under the GPLv2 or later
STARTDIR=`pwd`
FULLSCRIPTNAME="$0"
#############################################################################
# Helper functions
#############################################################################
# error messages function
doerror() {
cd "$FPCSOURCEDIR"
chown -R root:admin . >/dev/null 2>&1
chmod -R g+w . >/dev/null 2>&1
rm -f "$FPCROSSARMBINARY"
echo "An error occurred during" "$1"
echo "When asking for help with this error, please provide the entire contents of"
echo "this window (you can copy/paste it)"
}
# ask a user for a particular reply
# (from http://ubuntuforums.org/archive/index.php/t-436799.html)
function AskUser {
local choices=
local input=
local match="FALSE"
# Construct a nice list of possible choices
for i in $@
do
choices="$choices / $i"
done
# Display list, removing heading garbage due to loop.
echo -n "(${choices# / }) "
read input
i=0
while [ x"$1" != "x" -a "$match" = "FALSE" ]
do
i=$(($i+1))
if [ "$1" = "$input" ]
then
match="TRUE"
fi
# echo "[DEBUG] $i test: $1 = $input? $match"
shift
done
# No match, fallback to default.
if [ $match = "FALSE" ]
then
i=0
fi
# echo "[DEBUG] returning $i"
return $i
}
failure() {
if [ x"$FPCARMV7UNITSINSTALLDIR" -ne "x" ]; then
rm -rf "$FPCARMV7UNITSINSTALLDIR"
fi
echo
echo "You can restart this script by double-clicking it in the Finder at"
echo "$FULLSCRIPTNAME"
echo
read
exit 1
}
#############################################################################
# begin main script
#############################################################################
# get root
if [ $UID != 0 ] ; then
echo
echo
echo "This script, which will finish the FPC for iOS 2.6.0 installation, has to run"
echo "with root permission. Please enter your administrator password below if it is"
echo "asked (it will not be asked if this script or the sudo program was run within"
echo "the last 5 minutes)."
echo
echo "The script that will be executed is \"$0\""
echo
sudo "$0" $@
exit $?
fi
# introduction
echo
echo
echo
echo "This script finishes the installation of FPC for the iPhone SDK 4.x/5.x (should"
echo "work with future versions). It has to be executed by an administrator user."
echo
echo "You can cancel/stop the execution of this script at any time by holding down"
echo "ctrl-c (if you want to copy/paste the output) or by closing this window."
echo
echo
echo "The script will now perform a few sanity checks and then start"
echo "compiling and installing all FPC components."
echo
echo "Press enter to continue"
read
# Obtain the installation directory of the SDK.
IPHONEINSTALLDIR=/Developer
IPHONESDKDIR=`ls -d "$IPHONEINSTALLDIR"/Platforms/iPhoneOS.platform/Developer/SDKs/*|sort|tail -1`
if [ ! -f "$IPHONESDKDIR/usr/include/arm/_structs.h" ]; then
echo
echo "To start, please drag the folder in which Xcode with iPhone SDK is installed"
echo "on top of this window, or copy it in the Finder and paste it in this window."
echo "This will fill in the full path to this folder. Then, press the return key to"
echo "continue."
echo
echo -n "Enter iPhone SDK installation directory (e.g., /iPhoneSDK): "
read IPHONEINSTALLDIR
IPHONESDKDIR=`ls -d "$IPHONEINSTALLDIR"/Platforms/iPhoneOS.platform/Developer/SDKs/*|sort|tail -1`
if [ ! -f "$IPHONESDKDIR/usr/include/arm/_structs.h" ]; then
IPHONESDKDIR=not_found
fi
fi
# some required/used file and directory names
IPHONEPLATFORMBASEDIR="$IPHONEINSTALLDIR"/"Platforms/iPhoneOS.platform/Developer"
SIMULATORPLATFORMBASEDIR="$IPHONEINSTALLDIR"/"Platforms/iPhoneSimulator.platform/Developer"
SIMULATORSDKDIR=`echo $IPHONESDKDIR | sed -e 's/iPhoneOS/iPhoneSimulator/g'`
INCLUDEDIR="$IPHONESDKDIR/usr/include"
STRUCTSHEADER="$INCLUDEDIR"/arm/_structs.h
FPCSOURCEDIR="/Developer/FreePascalCompiler/2.6.0/Source/"
FPCBOOTSTRAPBINARY="/usr/local/bin/ppc386"
PARALLELMAKE=`sysctl -n hw.availcpu`
# check whether we can find the file that we have to convert
if [ ! -f "$STRUCTSHEADER" ]; then
echo "Unable to find the file \"$STRUCTSHEADER\""
echo
echo "Is \"$IPHONEINSTALLDIR\" the installation directory of the iPhone SDK 2.2.1 or later?"
failure
fi
# check whether h2pas is installed (part of the regular FPC installation)
if [ ! -f /usr/local/bin/h2pas -o ! -x /usr/local/bin/h2pas ]; then
echo "Unable to find the h2pas utility. It is part of the regular FPC for"
echo "Mac OS X/Intel installation. Please install the FPC 2.6.0 before"
echo "installing this add-on package."
failure
fi
# ensure that the SDK isn't installed in a directory with a space in
# its name (unfortunately, "make" cannot deal with this)
if echo "$IPHONEINSTALLDIR" | grep ' ' >/dev/null 2>&1; then
echo "The full path to your iPhone SDK contains a space character, and unfortunately"
echo "some of the required tools cannot deal with this. Please rename the directory"
echo "(and the hard drive partition containing it, if it's not your startup drive)"
echo "so it no longer contains a space in its name."
echo
echo "You can rename everything back after this script has finished running."
echo
echo "Path: \"$IPHONEINSTALLDIR\""
failure
fi
# check whether we can find the installed FPC sources (check a random file)
if [ ! -f "$FPCSOURCEDIR"/compiler/arm/cgcpu.pas ]; then
echo "Unable to find the installed FPC sources. They were expected in \"$FPCSOURCEDIR\""
failure
fi
# check whether we can find the installed FPC bootstrap compiler
if [ ! -f "$FPCBOOTSTRAPBINARY" -o ! -x "$FPCBOOTSTRAPBINARY" ]; then
echo "Unable to find the bootstrap compiler. It was expected at \"$FPCBOOTSTRAPBINARY\""
failure
fi
# check whether the bootstrap compiler's version is 2.6.0
currentfpcversion=`$FPCBOOTSTRAPBINARY -iV|tr -d '.'`
if [ "$currentfpcversion" -lt 260 -o "$currentfpcversion" -gt 260 ]; then
# look for fpc 2.6.0
FPCBOOTSTRAPBINARY=/usr/local/lib/fpc/2.6.0/ppc386
FPCBOOTSTRAPBINARY=`echo $FPCBOOTSTRAPBINARY|sed -e 's/ .*//'`
if [ x"$FPCBOOTSTRAPBINARY" = x ]; then
echo "Unable to find a valid FPC bootstrap compiler (need version 2.6.0)"
failure
fi
if [ ! -x "$FPCBOOTSTRAPBINARY" ]; then
echo "The bootstrap compiler at \"$FPCBOOTSTRAPBINARY\" is not executable. Please"
echo "reinstall FPC 2.6.0 for Intel."
failure
fi
fi
echo "Using \"$FPCBOOTSTRAPBINARY\" as FPC bootstrap binary"
# create the Pascal version of the _structs.h header, the whole reason why we
# we have to go through all this trouble...
/usr/bin/gcc -I"$INCLUDEDIR" -E -D__need_mcontext_t -D__DARWIN_UNIX03 "$STRUCTSHEADER" | sed -e 's/__uint32_t/cuint32/g' -e 's/_STRUCT_ARM_EXCEPTION_STATE/__darwin_arm_exception_state/' -e 's/_STRUCT_ARM_THREAD_STATE/__darwin_arm_thread_state/' -e 's/_STRUCT_ARM_VFP_STATE/__darwin_arm_vfp_state/' -e 's/__darwin_mcontext/mcontext_t/' > "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.h
if [ $? != 0 ]; then
echo "Unable to preprocess the _structs.h header file at \"$STRUCTSHEADER\" using \"/usr/bin/gcc\""
failure
fi
/usr/local/bin/h2pas -i -o "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.inc "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.h
if [ $? != 0 ]; then
echo "Unable to translate the preprocessed header file at \"$FPCSOURCEDIR/rtl/darwin/arm/sig_cpu.h\""
echo "Make sure not to distribute this file in source form, because you would violate"
echo "your iPhone SDK agreement!"
failure
fi
# Safe temporary places for the generated compilers
FPCROSSARMBINARY=`mktemp -t ppcrossarm.XXXXXXXXXXXXXXXX`
if [ $? != 0 ]; then
echo "Unable reserve a temporary location for the ARM cross compiler. You can try"
echo "to restart your system to clean up the /tmp/ directory to solve this problem."
failure
fi
chmod u+x "$FPCROSSARMBINARY"
# Temp location for ARMv7 units
FPCARMV7UNITSINSTALLDIR=`mktemp -d -t fpcarmv7.XXXXXXXXXXXX`
if [ $? != 0 ]; then
echo "Unable reserve a temporary location for the ARMv7 units. You can try"
echo "to restart your system to clean up the /tmp/ directory to solve this problem."
failure
fi
####################################################
echo
echo
echo "Compiling the compiler and all units for ARMv6..."
echo
cd "$FPCSOURCEDIR"
make FPC="$FPCBOOTSTRAPBINARY" distclean -j 4
if [ $? != 0 ]; then
doerror "cleaning the source directory for ARM"
failure
fi
# these do not appear to be erased by distclean for some reason
rm -rf "packages/fcl-db/units" "compiler/ppcarm" "compiler/ppcrossarm"
make FPC="$FPCBOOTSTRAPBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv6 -Cfvfpv2" all -j "$PARALLELMAKE"
if [ $? != 0 ]; then
doerror "building everything for ARM"
failure
fi
# copy cross-comiler to a safe place (useful for later)
cat compiler/ppcrossarm >> "$FPCROSSARMBINARY"
if [ $? != 0 ]; then
doerror "copying the ARM cross-compiler"
failure
fi
echo
echo
echo "Installing the ARM cross-compiler and all units..."
echo
make FPC="$FPCROSSARMBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv6 -Cfvfpv2" install CROSSINSTALL=1
if [ $? != 0 ]; then
doerror "installing the ARM cross-compiler and all units"
failure
fi
VERSION=`$FPCROSSARMBINARY -iV`
# rename compiler binary
cd /usr/local/lib/fpc/$VERSION
mv ppcrossarm ppcarm
if [ $? != 0 ]; then
doerror "renaming the ARM cross-compiler"
failure
fi
# add compiler symlink
cd /usr/local/bin
ln -sf ../lib/fpc/$VERSION/ppcarm ppcarm
if [ $? != 0 ]; then
doerror "creating the ARM cross-compiler symlink"
failure
fi
####################################################
echo
echo
echo "Compiling all units for ARMv7..."
echo
cd "$FPCSOURCEDIR"
make FPC="$FPCROSSARMBINARY" CPU_TARGET=arm distclean -j 4
if [ $? != 0 ]; then
doerror "cleaning the source directory for ARMv7"
failure
fi
# these do not appear to be erased by distclean for some reason
rm -rf "packages/fcl-db/units" "compiler/ppcarm" "compiler/ppcrossarm"
cd rtl/darwin
make FPC="$FPCROSSARMBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv7 -Cfvfpv3" RELEASE=1 all -j "$PARALLELMAKE"
if [ $? != 0 ]; then
doerror "building RTL for ARMv7"
failure
fi
cd ../..
cd packages
make FPC="$FPCROSSARMBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv7 -Cfvfpv3" RELEASE=1 all -j "$PARALLELMAKE"
if [ $? != 0 ]; then
doerror "building packages for ARMv7"
failure
fi
cd ..
echo
echo
echo "Installing the ARMv7 units into temporary location..."
echo
# first install into temporary install directory in /tmp, then lipo everything
# into the previously generated ARMv6 object files
cd rtl/darwin
make FPC="$FPCROSSARMBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv7 -Cfvfpv3" INSTALL_PREFIX="$FPCARMV7UNITSINSTALLDIR" RELEASE=1 install CROSSINSTALL=1
if [ $? != 0 ]; then
doerror "installing the ARMv7 RTL"
failure
fi
cd ../..
cd packages
make FPC="$FPCROSSARMBINARY" OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONESDKDIR}/ -ap -Cparmv7 -Cfvfpv3" INSTALL_PREFIX="$FPCARMV7UNITSINSTALLDIR" RELEASE=1 install CROSSINSTALL=1
if [ $? != 0 ]; then
doerror "installing the ARMv7 packages"
failure
fi
cd ..
# now lipo everything together
cd "$FPCARMV7UNITSINSTALLDIR"
cd lib/fpc/$VERSION/units/arm-darwin/
for objfile in */*.o
do
lipo -create $objfile /usr/local/lib/fpc/$VERSION/units/arm-darwin/$objfile -output /usr/local/lib/fpc/$VERSION/units/arm-darwin/$objfile
if [ $? != 0 ]; then
doerror "lipo'ing $FPCARMV7UNITSINSTALLDIR/lib/fpc/$VERSION/units/arm-darwin/$objfile and /usr/local/lib/fpc/$VERSION/units/arm-darwin/$objfile"
failure
fi
done
# remove temporary files
rm -rf "$FPCARMV7UNITSINSTALLDIR"
####################################################
echo
echo
echo "Compiling all units for the Simulator..."
echo
cd "$FPCSOURCEDIR"
make FPC="$FPCBOOTSTRAPBINARY" OS_TARGET=iphonesim distclean -j 4
if [ $? != 0 ]; then
doerror "cleaning the source directory for the Simulator"
failure
fi
# these do not appear to be erased by distclean for some reason
rm -rf "packages/fcl-db/units" "compiler/ppcarm" "compiler/ppcrossarm"
cd rtl/darwin
make FPC="$FPCBOOTSTRAPBINARY" OPT="-ap" RELEASE=1 OS_TARGET=iphonesim CROSSOPT="-FD${SIMULTORPLATFORMBASEDIR}/usr/bin -XR${SIMULTORSDKDIR}/ -ap" all -j "$PARALLELMAKE"
if [ $? != 0 ]; then
doerror "building the RTL for the Simulator"
failure
fi
cd ../..
cd packages
make FPC="$FPCBOOTSTRAPBINARY" OPT="-ap" RELEASE=1 OS_TARGET=iphonesim CROSSOPT="-FD${SIMULTORPLATFORMBASEDIR}/usr/bin -XR${SIMULTORSDKDIR}/ -ap" all -j "$PARALLELMAKE"
if [ $? != 0 ]; then
doerror "building the packages for the Simulator"
failure
fi
cd ..
echo
echo
echo "Installing the Simulator units..."
echo
cd rtl/darwin
make FPC="$FPCBOOTSTRAPBINARY" OPT="-ap" OS_TARGET=iphonesim CROSSOPT="-FD${SIMULTORPLATFORMBASEDIR}/usr/bin -XR${SIMULTORSDKDIR}/ -ap" install CROSSINSTALL=1
if [ $? != 0 ]; then
doerror "installing the Simulator RTL units"
failure
fi
cd ../..
cd packages
make FPC="$FPCBOOTSTRAPBINARY" OPT="-ap" OS_TARGET=iphonesim CROSSOPT="-FD${SIMULTORPLATFORMBASEDIR}/usr/bin -XR${SIMULTORSDKDIR}/ -ap" install CROSSINSTALL=1
if [ $? != 0 ]; then
doerror "installing the Simulator RTL units"
failure
fi
cd ..
####################################################
# clean up
echo
echo
echo "Cleaning up..."
echo
cd "$FPCSOURCEDIR"
make FPC="$FPCBOOTSTRAPBINARY" distclean -j 4
make FPC="$FPCROSSARMBINARY" distclean -j 4
# these do not appear to be erased by distclean for some reason
rm -rf "packages/fcl-db/units" "compiler/ppcarm" "compiler/ppcrossarm"
chown -R root:admin .
chmod -R g+w .
rm "$FPCROSSARMBINARY"
cd "$STARTDIR"
# we're done!
echo
echo
echo "The installation of iOS SDK support for FPC was successful! You can now"
echo "create FPC iOS projects using the Xcode application included in the iOS"
echo "SDK and the templates installed by FPC for iOS if you are using Xcode 3.x."
echo "They are available in Xcode under \"User Templates\" in the \"iPhone (FPC)\""
echo " group."
echo
echo "If you are using Xcode 4.x, see http://web.me.com/macpgmr/ObjP/Xcode4/ for"
echo "example templates."
echo
echo "(You can now close this window and/or quit Terminal)"
echo
echo
echo
# wait for keypress in case Terminal is configured to automatically close windows after the
# process has finished
read
|