How to run an xcode project from bash? - xcode

I have a build script calling xcodebuild. that works, but I want to also run the project from bash as well. Effectively I want to negate the need to click "Build and Run" button from the GUI. I'm looking at xcrun but it's not too obvious to me what to do

It sounds like you want to run the product of the build, not the project itself. If you want to do that, you just need to use the bash invocation for the product. If you're building a command-line program, then there will be an executable with the product's name in the project's built products directory after a succesful build. The project's built products directory depends on your preferences for Xcode and the project, but can be determined from the $BUILT_PRODUCTS_DIR environment variable within an Xcode build phase.

I do this for one of my projects in a shell script. It let's me remotely build over SSH.
xcodebuild clean
xcodebuild
cp -rp ~/Projects/VSM/Mac/iCar/build/Release/iCar.app ~/Desktop/
open ~/Desktop/iCar.app
I chose to copy the app to the Desktop on purpose but you wouldn't have to.

try open xcode

Related

How to get a Run Script phase to run first during compile in xcode?

In xcode 10.2, there is a run-script build phase. I have a script to copy a framework to a specific pod in DerivedData/somename-someguid/products/etc...
However, it seems that the script is not running first based on the print statements and it fails to find the file. If I manually copy the file there first, I can see the output of my script in the build log and it succeeds.
I tried dragging it up in the build-phase panel. I also tried adding it to the copy-file section, but copy does not understand how to embed with the correct pod.
Is there a way to specify the order of running these scripts? Is there a pre-everything section I don't know about?
Of course, it turns out I had the run script phase on the main app project instead of the pod project targets not realizing it built the pods first. So adding it there fixes it. Saving other noobs from this mistake by leaving the question up....

How do I add compile sources via a run script build phase in XCode?

I have an XCode project which I would like to add source files to via scripting.
Is it possible to add compile sources via a run script build phase?
If so, how would I go about this?
I haven't found any documentation or examples of this, so I'm not sure it's possible.
You can create target that runs script. In this script any sources could be passed to xcodebuild arguments.
If this is not what is needed, XCode project by itself is an xml, so any files could be added to any targets, but project should be reloaded after, so I don't know how to add files to target withing runnign this target build.

Adding external scripts to Xcode4

I need to do some tasks before any Archive launched in Xcode.
Is there any way in Xcode to add a .sh or .py script run before Archive ?
PS : I can complete these tasks by hand but it requires time, some tasks might be forgotten or if the process is done by someone else all the tasks can be forgotten. The problem is that these tasks are required to have a successfull Apple validation.
Thanks
You can add script build phases at any point between the existing phases. Here is a post which shows you how. In Xcode 4 you select your project, then go to the build phases tab were you usually have the "target dependencies", "compile sources", "Link binary with libraries" and "copy bundle resources" phases, just click on "Add build phase" and select "Add run script". You can select your shell, but not script in python from there.
You can also look at building xcode projects from the command line. You could call that command line from a python script and do anything else you want from "outside" the Xcode project.

How to clean project before each build?

Is there a way (possibly using schemes) in Xcode to specify that a clean is automatically done before doing a new build.?
I have a project that sometimes fails to build unless I do a clean first, currently I am doing it by hand.
Press ⌥⌘R, expand the selected scheme, select Pre-actions, click +, select New Run Script Action, set Provide Build Settings from to your target. In the box below type rm -rf ${BUILT_PRODUCTS_DIR}. Note: it is BUILT not BUILD as seen in the Xcode dialog. You can type echo ${BUILT_PRODUCTS_DIR} > ~/Desktop/log.txt to see what's going to be deleted.
The selected answer did not work for me, it caused my build to fail (Xcode 4.6.3) when trying to run on the simulator.
Based on Jano's answer and on this link in the Pre-action script instead of writing
rm -rf ${BUILT_PRODUCTS_DIR}
I wrote
touch ${BUILT_PRODUCTS_DIR}
This should have the same effect and it doesn't cause my build to fail

How do you change the PATH used in an external makefile build using XCode?

I currently have a project that I'm building with a makefile. This project includes some additional software (jflex) which is not on one of the default system search paths (it's installed to (/opt/local/bin/). My .profile file adds this directory to my PATH, and so building the project from a terminal window succeeds.
However, if I try to run the makefile from within XCode (XCode project with an externally managed makefile), it fails since it's not looking in /opt/local/bin for jflex, and as such can't find it.
How can I change the settings of my XCode project to correctly build from within XCode? I assume there's some kind of path setting in XCode, or some kind of additional argument I can have XCode give to make so that it doesn't fail. Alternately, I could hardcode the path into my makefile if I could detect that it was being run from XCode (although this is the less preferable option for me, since my makefile will sometimes need to be called from the command line and I'd rather keep it simple).
I'm relatively new to using XCode, so apologies if I've missed something obvious.
Instead of calling make directly, you could call a shell script as the "Build Tool" in the "External Build Tool Configuration" pane. Then modify the path in the shell script and call make from there, i.e.
#!/bin/sh
PATH=/opt/local/bin:$PATH
make
Don't forget to set permissions such that Xcode can run the script, and provide the full path to the script as the "Build Tool".
May not be the best way, But can you launch XCode from command line. It will inherit the Path from it.
Or, in the XCode launcher change:
XCode
to
PATH=$PATH:/opt/local/bin/ XCode
may work (depending on launcher)
Or, did you relaunch the launcher/window manager (logout and back in again after setting .profile)?

Resources