Xcode 4 custom build step before compile
Asked Answered
E

3

7

I'm trying to integrate a tool into Xcode 4 that generates a C header from a descriptor file. In Xcode 3 it worked to add a custom build step for files with a specific extension. Those files then get compiled before the .m/.mm/.cpp files that included them. When I try to do this with Xcode 4 it seems like it runs my custom step after compiling the other source. This doesn't work of course. Is there any way to tell Xcode to run the step before?

Here's a simple repro setup:

enter image description here

My main.m contains a:

#include <mytest.h>

and I've added ${DERIVED_FILE_DIR} to the header search path. When I compile this project I get the following:

enter image description here

If I remove the include and build it again I get this:

enter image description here

So the rule is indeed working, but it's executed way too late. Is there any way to change this behavior?

Enoch answered 10/12, 2011 at 16:31 Comment(1)
This may be a little late, but would it work if you created a separate build target that just created the headers and then added that target as a dependency of your main target?Cockeyed
S
7

I already have my Revival badge, but here goes :)

I just ran into this problem, and the answer is simple ( now I've spent a half a day staring at it).

The build rule is responding to the source file (.abc), and the source file is being processed in the order it is listed in the 'Compile Sources' build step. Simply go into the build rule and drag the .abc to the top of the list.

One small step towards my archeologist badge, I hope.

Shrewsbury answered 18/6, 2013 at 8:56 Comment(0)
A
3

Rather than adding a Build Rule: In the Build Phases tab, using the Add Build Phase button, choose Add Run Script. A Run Script phase will be added in which you can implement your script requirements. The new run script phase can be dragged to before the Compile Sources phase which is when you want your functionality executed.

Airmail answered 10/12, 2011 at 16:51 Comment(3)
That means I'll need to add all the files manually (there will be more than one) as input/output to that build phase. I guess that should work, but it would be way nicer to have it working by just adding the input file to the project and match on extension.Enoch
Craft your script to use shell expansions and wildcards. This'll make it dynamic and low maintainence. Going too deep into shell scripting is off topic with regard to this question though.Airmail
I'd rather not do that since it'll probably confuse the dependency checking.Enoch
G
0

You need to add an explicit dependency between the generated file and your target.

Open the DerivedData directory in Finder, select the mytest.h file and drag it in XCode inside your project. I usually create a new group called "Derived Files" and place such files under it.

To easily find the DerivedData directory, I change the project settings so it makes the directory project relative (see File -> Project Settings, the Build tab).

After the file is referenced in the project, you need to add it to the main target Compile Sources rule. Select the project's main target, expand the Compile Sources if it's not already expanded, and drag the newly added derived file in the list of files to compile.

Lastly, since you really want to compile this file, you may want to rename it to .m or .c, instead of .h.

Update: Actually looks like the above is not working all the time. The most reliable way is to use a build phase instead of a build rule.

Ginger answered 29/5, 2012 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.