Andrew Bennett's Blog

Just another WordPress.com site

Setting XCode 4.0 Environment Variables from a Script

leave a comment »

I have struggled for quite a while with XCode 4, it is excellent for Cocoa development, but if you’re trying to do anything lower level I often find myself pining after XCode 3.x or wanting to use a Makefile or something instead. However there are many advantages to an IDE, like code completion and integrated debugging tools, so it’s always nice to use things how they are designed. However there are some things XCode 4 does not do nicely, this article describes one such thing and the solution I’ve come up with. I don’t think XCode 3.x did this particular thing well either, so perhaps my solution might actually be useful to someone else.

The Problem

Often I want a bit more control over how I compile and link things than XCode allows. Tools like llvm-config allow you to get the compiler settings for your local build of llvm api, which is very useful. If I run something like:

llvm-config --ldflags

Then the command will output something like:

-L/usr/local/lib -lpthread -lm

To use this with XCode what you might consider doing is setting the OTHER_LD_FLAGS environment variable (“Other Linker Flags” in the Build Settings) to something like this:

`llvm-config --ldflags`

However if you look in the build transcript (Expanding the link section in the Navigator Log, command+7), you will find that it puts double quotation marks around the space separated parts of the command and ld is forced to interpret it as a file. I have tried for hours to fix this, on many different occasions, but I could not find a satisfactory portable solution. That is until now.

The Solution

The solution in short is to create an Xcode Configuration file from a script build phase and then set that as the base configuration for all of your targets dependent on the build settings. You can do this in “5 Simple Steps”!

Step 1

This script will create the xcode configuration file in your project directory, place it in a script build phase in your project. Make sure that you do this before the “Compile Sources” phase so it is made before the settings are needed.

Automatically Generating the XCode Configuration File

Automatically Generating the XCode Configuration File

Of course this script only applies to llvm, but I will put it here in case anyone wants it:

LLVM_XCCONFIG="llvm.xcconfig"
LLVM_CFLAGS=`/usr/local/bin/llvm-config --cflags`
LLVM_LDFLAGS=`/usr/local/bin/llvm-config --ldflags`" "`/usr/local/bin/llvm-config --libs cbackend jit x86 linker`
echo "// Configuration file for LLVM settings, generated as a build phase." > $LLVM_XCCONFIG
echo "LLVM_CFLAGS = $LLVM_CFLAGS" >> $LLVM_XCCONFIG
echo "LLVM_LDFLAGS = $LLVM_LDFLAGS" >> $LLVM_XCCONFIG

Step 2

Build the project and the XCode Configuration file should be created, add this file to the project. If you’re not sure how to do this you can find it in Finder and then drag it into the project’s file navigator (Or by pressing command+option+a, and selecting the file).

Add the XCode Configuration file to the project

Add the XCode Configuration file to the project

Step 3

Now you have to set this configuration file as the base configuration of each target that uses it, to do this select the project in the file  navigator. Selecting the project if the target is selected and then go to the Info tab. Within the info tab set the configuration file to the file you just added for each relevant target.

Setting the Target's base Configuration File

Setting the Target's base Configuration File

Step 4

Now all you need to do is use the new environment variables in your project settings, of course you could have called them OTHER_LDFLAGS or similar to avoid this step, but I thought it was cleaner to do it this way.

Using the new Environment Variables in the the Build Settings

Using the new Environment Variables in the the Build Settings

Step 5

Build your project and hopefully everything is working as you wanted it to.

Advertisement

Written by therealbnut

January 1, 2012 at 5:08 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.