Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Running a script before every build that modifies source code directly

What I did:

I have a script that

  1. Read some configuration files to generate source code snippets
  2. Find relevant Objective-C source files and
  3. Replace some portions of the source code with the generated code in step 1.

and a Makefile that has a special timestamp file as a make target and the configuration files as target sources:

SRC = $(shell find ../config -iname "*.txt") STAMP = $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME).stamp $(STAMP): $(SRC)     python inject.py     touch $(STAMP) 

I added this Makefile as a "Run Script Build Phase" on top of the stack of build phases for the project target.

What happened:

The script build phase was run before compiling the source.

However, since the script modifies source code during its execution, I needed to build twice to get the most recent version of the build product. Here is what I imagine to be happening:

  1. 1st run: Xcode collects dependency information ---> no changes
  2. 1st run: Xcode runs "Run Script Build Phase" ---> source is changed behind Xcode's back
  3. 1st run: Xcode finishes build, thinking nothing needs to be updated
  4. 2nd run: Xcode collects dependency information ---> source has changed, needs rebuild!
  5. 2nd run: Xcode runs Run Script Build Phase" ---> everything is up-to-date
  6. 2nd run: Xcode proceeds to compilation

After reading Xcode documentation on Build Phases, I tried adding a source file which is known to be updated every time the script is run as the output of "Run Script Build Phases", but nothing changed. Since the number of configuration files may vary in my project, I don't want to specify every input and output file.

Question:

How do I make Xcode aware of source file changes made during "Run Script Build Phase"?

Edit:

  • Added that I placed the script build phase before the other build phases
like image 452
ento Avatar asked Jun 10 '09 15:06

ento


People also ask

What is Xcode Run script?

Xcode uses the set of input and output files to optimize build times, by running your script only when necessary. If you don't specify input or output files, Xcode runs your script every time you build the target. For more information, see Improving the Speed of Incremental Builds.

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.”


1 Answers

Every technique mentioned so far is an overkill. Reproducing steve kim's comment for visibility:

In the build phases tab, simply drag the "Run Script" step to a higher location (e.g. before "Compile Sources").

Tested on Xcode 6

like image 66
marinosb Avatar answered Sep 19 '22 18:09

marinosb