Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally include a file based on build configuration in Xcode?

I have an Xcode project with a large number of targets where I would like to include a settings bundle for apps built under the Ad-hoc and Debug configurations, but not under the Release configuration.

Build Phases don't seem to allow for making themselves conditional on configuration (they can obviously be conditional on target, but doubling the number of targets in the project would make it completely unusable).

That leaves writing a custom Build Rule. My plan is to exclude the Settings.bundle from all targets, and create a build rule that conditionally copies it into the product package, but applicable examples are really hard to find.

The build rule I've started has the Process setting set to "Source files with names matching:" and Settings.bundle as the name. The Using setting is "Custom script:".

My custom script is as follows (with the caveat that my bash scripting is on a cargo cult level):

if [${CONFIGURATION} = 'Debug'] then     cp -r ${INPUT_FILE_PATH} ${DERIVED_FILES_DIR}/. fi 

Finally, I have ${DERIVED_FILES_DIR}/Settings.bundle listed as an output file.

Since I'm here, it should be obvious that it's not working. My first question is whether there is somewhere I can view the output of the build rules as the execute to make sure that 1) it's actually being executed and that 2) I don't have a stupid syntax error somewhere.

Also, what's the proper location (in the form of an environment variable) to copy the output to?

like image 524
Frank Schmitt Avatar asked Dec 13 '11 21:12

Frank Schmitt


People also ask

What is configuration file in Xcode?

Overview. A build configuration file is a plain-text file you use to specify the build settings for a specific target or your entire project. Build configuration files make it easier to manage build settings yourself, and to change build settings automatically for different architectures and platforms.

How do I add files to Xcconfig?

To create an xcconfig file, choose File -> New -> File... in your project. In the new file dialog, scroll down until you see the Configuration Settings File in the Other section. You can add configurations for pretty much anything you want.


2 Answers

I finally figured it out.

For each target for which you want to conditionally include the settings bundle, choose its Project from the source list, choose the target, and switch to the "Build Phases" tab.

Click the "Add Build Phase" button and choose "Add Run Script".

Then enter the following for the script:

if [ "${CONFIGURATION}" == "Debug" ]; then     cp -r "${PROJECT_DIR}/Settings.bundle" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app" fi 
like image 195
Frank Schmitt Avatar answered Sep 19 '22 09:09

Frank Schmitt


I know this question has been answered already, and the answer was very helpful to me, but I wanted to throw my own modified solution out there as well.

My requirement was to have different settings bundles for different build configurations, rather than just not including it at release. Assuming a simplistic approach of only Debug and Release configurations, here's how to do it:

Start by adding 2 settings bundles to the project, named Settings-debug.bundle and Settings-release.bundle and then remove these files from the Copy Bundle Resources build phase. Next add a user defined build setting called SETTINGS_BUNDLE, which has different values for each configuration:

Debug        ${PROJECT_DIR}/relative/path/to/Settings-debug.bundle Release      ${PROJECT_DIR}/relative/path/to/Settings-release.bundle 

Next add a run-script build phase (after Copy Bundle Resources) named Copy Settings Bundle with a modified version of the script in Frank's solution.

cp -r "${SETTINGS_BUNDLE}/" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/Settings.bundle" 

The difference here is that the copied bundle is always named Settings.bundle regardless of the source name.

You then need to add another build phase script to prevent code signing errors when the only changes are in the settings bundles. It forces the code signing step to occur on every build. This should run before the Compile Source Files build phase. I called mine Force Codesign.

touch "${PROJECT_DIR}/relative/path/to/main.m" 
like image 42
Ell Neal Avatar answered Sep 21 '22 09:09

Ell Neal