Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorial or Guide for Scripting Xcode Build Phases

I would like to add some files to the Compile Sources build phase using a script in Xcode, which pulls from some folder references. I haven't been able to find much documentation so far.

  1. Where is the general documentation (or a good tutorial) for scripting Xcode build phases?
  2. How can I add files to the Compile Sources phase?
  3. How can I discover information about the project and the folder references within it?
  4. Are there any special considerations if I want to script in Ruby or Python vs. bash scripting?
like image 240
Dan Rosenstark Avatar asked Sep 26 '11 15:09

Dan Rosenstark


People also ask

How do I run a build phase script in Xcode?

Go to the Build Phases section of your project. (Click on the project, then on the main target, then on the “Build Phases” tab along the top.) Click the + at the top left to create a new build phase; choose “New Run Script Phase.” Xcode creates the script at the end of the list, naming it “Run Script.”

What are the build phases?

A Build Lifecycle is Made Up of Phases validate - validate the project is correct and all necessary information is available. compile - compile the source code of the project. test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.


1 Answers

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!

like image 138
prairiedogg Avatar answered Oct 06 '22 11:10

prairiedogg