Is there a way, in Xcode, to run a bash script in a build phase, only when I "Build & Run" and not in all other occasions?
Actually, what I would like to do, is to write a script to update the build number every time I build and run a new version on the device.
Xcode does not tell your script why it's building — that is, if it's going to run the app afterwards or not —; only that it is building. Also note that selecting 'Run' as opposed to 'Build & Run' does not run scripts in Run Script phases.
If you build in other occasions, you can pass build settings to xcodebuild (if building from the command line) or by selecting a different build configuration (if building from the Xcode application), which you can refer to in your build scripts as environment variables. For example:
if [ "$CONFIGURATION" == "Debug" ]; then
# increment build number
fi
(This script uses the CONFIGURATION built-in build setting, but you can create custom build settings if needed.)
Related
I have a build phase Run Script in my Xcode project that is required for ensuring the module bundle is up to date. The script runs when actions are executed through the Xcode GUI, and the Swift Package is able to build and run tests successfully. But when I try to run xcodebuild test from the command line for CI/CD, the tests fail because the Run Script did not get executed during the build process.
Is there a way to tell xcodebuild to use the Run Scripts from my Xcode project? Or is there a flag I can use to specify a script for it to run?
Judging by the radio silence, I'm guessing this isn't possible using an xcodebuild flag or changing any Xcode settings.
What I ended up doing as a workaround was splitting build and test commands and then running my script in between.
# build-for-test is used bcus normal build wont build the test bundle, which was important in my use case
xcodebuild build-for-test ...
bash path/to/run-script.sh
xcodebuild test ...
The xcodebuild test command will use the files compiled by the earlier build command, so it won't clean any changes made to the xcode build dir by the run script (which is what I was doing).
I need to run a custom shell script on the clean step in Xcode (Xcode 9 to be exact). I have a set of makefiles that build my dependencies, initially I was calling this script in the run script phase, but then the outputs of these makefiles never get cleaned. So I though that if I handled the Xcode environemnt variable ${ACTION} in this script then it could also clean them. The issue is - the script is not called on clean.
There were several posts regarding this so here is what I've tried:
Making an additional aggregate target. I added the target and put a script that handles ${ACTION} from the build environment, but that script is never executed upon clean. When cleaning there is only a Clean.remove clean line in build log.
Making an external build system target (some older posts stated an external script target, but I could not find that in Xcode 9). This then allows me to execute a custom script (if I replace the default /usr/bin/make command to my script), but in this case the ${ACTION} environment variable is not set (I verified this in the build output, it always has export ACTION= in it), thus I cannot distinguish between clean and build phases.
Regarding the aggregate target, it has the output files property, if I specify them - could that make Xcode clean them?
A bit of a zombie, but closest to the issue that I am seeing. This appears to be a bug in the new build system. Using the old build system, the action is set. Two options here:
Use the Legacy build system.
Use separate external targets that call your script and manually pass the action parameter required and swap schemes manually as needed.
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.
There is a "run script only when installing" option in Xcode Run script Build Phase, I am not able to find documentation on this. What does it do?
With Run script only when installing checked, the script only runs when do Product Archive.
In the Xcode Build System Guide (Xcode 3.2.x), it says:
Run script only when installing. Runs the script only during install builds, that is, when using the install option of xcodebuild or when the build settings Deployment Location (DEPLOYMENT_LOCATION) and Deployment Postprocessing (DEPLOYMENT_POSTPROCESSING) are on.
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