title: Xcode: Running a script before every build that modifies source code directly
body: <h2>What I did:</h2>
  
  <p>I have a script that</p>
  
  <ol>
  <li>Read some configuration files to generate source code snippets</li>
  <li>Find relevant Objective-C source files and</li>
  <li>Replace some portions of the source code with the generated code in step 1.</li>
  </ol>
  
  <p>and a Makefile that has a special timestamp file as a make target and the configuration files as target sources:</p>
  
  <pre><code>SRC = $(shell find ../config -iname "*.txt")
  STAMP = $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME).stamp
  $(STAMP): $(SRC)
      python inject.py
      touch $(STAMP)
  </code></pre>
  
  <p>I added this Makefile as a "Run Script Build Phase" on top of the stack of build phases for the project target.</p>
  
  <h2>What happened:</h2>
  
  <p>The script build phase was run before compiling the source.</p>
  
  <p>However, since the script modifies source code during its execution, I needed to build <strong>twice</strong> to get the most recent version of the build product. Here is what I imagine to be happening:</p>
  
  <ol>
  <li>1st run: Xcode collects dependency information ---> no changes</li>
  <li>1st run: Xcode runs "Run Script Build Phase" ---> source is changed behind Xcode's back</li>
  <li>1st run: Xcode finishes build, thinking nothing needs to be updated</li>
  <li>2nd run: Xcode collects dependency information ---> source has changed, needs rebuild!</li>
  <li>2nd run: Xcode runs Run Script Build Phase" ---> everything is up-to-date</li>
  <li>2nd run: Xcode proceeds to compilation</li>
  </ol>
  
  <p>After reading <a href="http://gemma.apple.com/DOCUMENTATION/DeveloperTools/Conceptual/XcodeBuildSystem/200-Build_Phases/bs_build_phases.html" rel="noreferrer">Xcode documentation on Build Phases</a>, 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.</p>
  
  <h2>Question:</h2>
  
  <p>How do I make Xcode aware of source file changes made during "Run Script Build Phase"?</p>
  
  <h3>Edit:</h3>
  
  <ul>
  <li>Added that I placed the script build phase before the other build phases</li>
  </ul>