He Loves You Too

Due to the overwhelmingly positive response towards the Amarok 2 packages for Leopard (10.5-intel) (i.e. no one was complaining about how much they sucked) I decided to take a stab at doing universal builds for Tiger (10.4). What would be good about these packages is that they would work for individuals with both powerpc and intel macs. They would also work for users with 10.4 or 10.5 installed, so being able to create them would be a win, win, win, win situation. Kill 4 birds with one stone if you will.

The primary obstacle to this is gcc 4.2. As far as I know Amarok, because of it's dependency on KDE's Plasma, still needs to be built with at least this version of the compiler (otherwise patching be necessary). My last foray into apple-gcc42 land didn't go so well, but deliverance came in the form of the iPhone SDK. No iPhone SDK for Tiger however. Apple has only blessed the Leopard with this wondrous gift. So it was that I set out to get apple-gcc42 compiled on Tiger.

I didn't just want to get it installed though. I wanted macports to do it. I tried to do the same thing I did on 10.5, which at least got apple-gcc42 installed via macports, but I kept on hitting a compilation error. You see apple-gcc42 has its own little build script which, if you want to run just that, works fine. It also allows configuration using the traditional ./configure, make, make install facilities however. The latter method is far more amenable to packaging software. Verily, they've been designed with this basic routine in mind.

Alas, after the fifth or fifteenth try, I gave up on configure and just used Apple's provided build_gcc script. I extracted the tarball using sudo port extract apple-gcc42 and then ran the script. This left me with two problems however. The most serious of which was that, for some reason the stupid thing was trying to delete my home directory after installing. After quickly control-c'ing the script to stop whatever it was deleting, I checked to see what was left to do. It was all related to removing debugging information so I elected to leave things alone and did not re-run the script again. By God's grace I was able to catch the deletion in time and, it seems the only things I lost were the ~/Documents directory and symbolic links to the ~/Applications and ~/Downloads directories on the Leopard volume (don't ask). Thankfully there wasn't much in the ~/Documents directory and deleting the symlnks left the data in the originals intact. *WHEW*

The problem seems to have come about because of this bit of code in the build_gcc script

# Create SYM_DIR with information required for debugging.

cd $SYM_DIR || exit 1

# Clean out SYM_DIR in case -noclean was passed to buildit.
rm -rf * || exit 1

When I ran the script, I used these parameters:
sudo ./build_gcc "ppc i386" "ppc i386" `pwd` /usr `pwd`/opt/local/
I didn't set the SYM_DIR so apparently this defaulted to my home directory. I shudder to think what would have happened if I'd been asleep when all this was going on. The script already deleted my macports directory once because it rm's the DEST_DIR before copying any files over. Yeah, this's a dangerous one folks. Be sure to read the script before running it. Do as I say, not as I do. You can open the script, which is in the root directory of the extracted apple-gcc42 tarball, in any text editor and read the comments and code to learn what exactly it is doing. Or just run it and find out. Don't be surprised when it eats your babies however. I warned you.

Again, BEWARE the build_gcc script. Its evil knows no bounds. I would recommend, if you use it again, to remove the # Create SYM_DIR with information required for debugging. section before running the script.

Having dodged that bullet I got back to the second issue at hand, forcing macports to install my new compiler. I had initially intended to just use the ditto tool to place everything into /opt/local directly. Because of how the build proceeded however, I decided to use macports as that would make it easy to uninstall, as well install. To do this I had to lie a little to macports, otherwise it would not co-operate. The first thing I did was move the base$DEST_DIR directory into the ${worksrcdir} and rename it to destroot.
cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/lang/apple-gcc42/work
sudo mv gcc_42-5531/opt/local destroot

Then I modified the location of the GCC 4.2 Documentation to match that of Xcode 2.5.
sudo mkdir -p destroot/Developer/ADC\ Reference\ Library/documentation/DeveloperTools/
sudo mv destroot/Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.2.1 destroot/Developer/ADC\ Reference\ Library/documentation/DeveloperTools/
sudo rm -ri destroot/Developer/Documentation

This was painstakingly followed by going through the XCode installed gcc 4.0 and ensuring that my gcc 4.2 matched it as closely as possible.
sudo ln -s ../../../../bin/libtool destroot/usr/libexec/gcc/powerpc-apple-darwin8/4.2.1/libtool
sudo ln -s ../../../../bin/libtool destroot/usr/libexec/gcc/i686-apple-darwin8/4.2.1/libtool

After all that I needed to let macports know it had already done everything necessary to install apple-gcc42 (this is where the lying comes in).
sudo echo "target: org.macports.patch" >> .macports.apple-gcc42.state
sudo echo "target: org.macports.configure" >> .macports.apple-gcc42.state
sudo echo "target: org.macports.build" >> .macports.apple-gcc42.state
sudo echo "target: org.macports.destroot" >> .macports.apple-gcc42.state

I also created an installer for this and put it somewhere safe. You never know when something horrible is going to happen, and we don't want all that hard work we just did going to waste.
sudo port pkg apple-gcc42
scp apple-gcc42-5531.pkg somewhere:safe

And then, finally, I installed it.
sudo port install apple-gcc42

But will it work...

Better magic

For the -compatibility-list error.

post-configure {
	reinplace "s|verstring\=|\ s:\${wl}:|g" ${worksrcpath}/libtool
}

No rest ...

Here's a very incomplete list of all the portfiles I added it to: clucene expat lzmautils tiff lcms giflib jasper libgpg-error libvorbis id3lib subversion.

The flac Portfile needed a similar modification for the ltmain.sh file. The post-patch section was used here however not post-configure.

Issues so far...

ld is misbehavin' w/ gcc 4.2, complaining about the -compatibility_version flag so I need stream edit some port after configure executes.
Here's the error:

/usr/libexec/gcc/i686-apple-darwin8/4.2.1/ld: unknown flag: -compatibility_version

Here's the magic:

post-configure {
       system "sed -e '/verstring=/s/\${wl}//g' \
       < ${worksrcpath}/libtool > ${worksrcpath}/libtool2"        
       system "mv ${worksrcpath}/libtool2 ${worksrcpath}/libtool"
       system "chmod a+x  ${worksrcpath}/libtool"
}

Thank the cloud: http://lists.apple.com/archives/darwin-dev/2008/Jul/msg00043.html