Tutorial or Guide for Scripting Xcode Build Phases

Tutorial or Guide for Scripting Xcode Build Phases

To add files to the Compile Sources build phase using a script, you will need to manipulate your project's project.pbxproj file programmatically.

Generally speaking, you would accomplish this by parsing the project.pbxproj file into an in-memory data structure, manipulating that data structure through a programmatic interface, and then writing the data structure out to a new project.pbxproj file.

There are several projects out there that could potentially help you do this, I haven't tried any of them:

  • https://github.com/owlforestry/pbxproject
  • http://github.com/gonzoua/pbxproj/
  • https://github.com/facebook/three20/blob/master/src/scripts/Pbxproj.py
  • http://code.google.com/p/xcodeutils
  • https://github.com/appcelerator/titanium_mobile/blob/master/support/iphone/pbxproj.py

And here is a series of blog posts with great general info on the contents and format of XCode project.pbxproj files.

  • http://danwright.info/blog/2010/10/xcode-pbxproject-files/
  • http://danwright.info/blog/2010/10/xcode-pbxproject-files-2/
  • http://danwright.info/blog/2010/10/xcode-pbxproject-files-3/

Finally, it may be worth noting that for very simple manipulations, particularly if you aren't concerned about the cosmetics of your project.pbxproj file getting messed up, you can follow the suggestion over at this Stack Overflow answer to parse the project.pbxproj file on the command line like so:

plutil -convert xml1 -o - myproj.xcodeproj/project.pbxproj

Happy parsing!

Xcode Build Script (Build Phases- Run Script) Increment Build Version based on Username(User)

You can see the script log in the Log Navigator, with your script i've got the following issue:

Sample Image

I believe the default comparison is case sensitive, to make it not sensitive you can change the username to uppercase/lowercase before comparison:

if [ `echo $USER | tr [:lower:] [:upper:]` = "USERNAME" ]; then
echo "Got user check"
fi

As you can see I moved $USER to the condition to avoid additional var usage and the script failure.

And the semicolon at if-then block is a normal thing, check the man page. then word might be moved to the new line if that is more convenient for you to read.

What types of languages are supported in Xcode build phases run script?

The correct answer would likely be: "a script that works in any shell that comes with MacOS"

On my Mountain Lion machine, I see:

[/]:; more /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

You can specify the shell you want in the "Shell" field of the Run Script panel when you're creating your build script. It looks like this:

Enter The Shell Here

How can I programmatically add build files to Xcode4?

I ended up sending this question to the devs on the xcode developer list and the response I got was effectively "you can't".

How to include file as resource in Xcode programmatically during build?

I ended up using build references.

How to enable ARC programmatically for specific files in a non-ARC xcode project?

If you look inside the .pbxproj file inside your .xcodeproj file you can see the files included in your project. I'm not sure how in depth you want to go with your manual modification of flags, but I have a few files that have compiler flags set to them. In the .pbxproj file they look like this:

D5298D04170F232900D3B684 /* ASIHTTPRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = FACE22CF15B72C4C00A0A4AD /* ASIHTTPRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };

Whereas files with no compiler flags look like this:

D5298D02170F232900D3B684 /* GradientShapeView.m in Sources */ = {isa = PBXBuildFile; fileRef = FACE22C715B6F90000A0A4AD /* GradientShapeView.m */; };

I have no idea why, but these compiler flags and files are listed in that .pbxproj file 4 times each. I have 12 files in the project with the compiler flags set, but the flag is found in that file 48 times.

You can setup a text parser to run through this file and add the flag to the specific files that you would like to have them. I'm not sure what else you will screw up in doing this, but that would be a perl scriptable way of adding compiler flags to specific files.

XCode universal library build phase - Lipo can't find files

This is a bash problem/mistake.

You're passing all the file names as a single argument to lipo, so it's gonna look for a single file named /Users/username/Projects/project/plugins/build/Release-iphoneos/libproject-plugins.a /Users/username/Projects/project/plugins/build/Release-macos/libproject-plugins.a.

You should use an array for the file names instead.

  • Initialise it with () instead of "".
  • Add elements with +=(...) instead of ="$var ...".
  • Pass every element separately to lipo with "${var[@]}" instead of "$var".

Applied to your script:

targets=$(xcodebuild -list | sed -n '/Targets/,/^$/p' | grep -v -e 'Targets:\|all\|^$');
target_results=();

for target in $targets; do
xcodebuild ${ACTION} -target $target -configuration ${CONFIGURATION};
target_results+=("${PROJECT_DIR}/build/${CONFIGURATION}-$target/libproject-plugins.a");
done;

xcrun lipo -create "${target_results[@]}" -o "${PROJECT_DIR}/plugins-universal.a";


Related Topics



Leave a reply



Submit