Finding Derived Data

posted on

Prior to Xcode 4 there existed the Build folder. I hated this folder, especially as I often wanted to zip up a project and send it to someone, and I'd end up having a huge file because I'd forgotten to delete the Build folder. So imagine my delight when Apple created the Derived Data directory in Xcode 4. This directory contains all the build products, intermediate files, indexes, logs etc. It is usually located in ~/Library/Developer/Xcode/DerivedData. The problem is, it is rather hard to find the particular Derived Data directory for a particular project…

The Problem

If you're inside Xcode's build environment then you're ok, as you can use the BUILD_PRODUCT_DIR setting. If you're outside though there is no obvious way to find it. The problem is that the Derived Data directory, if placed in the usual location, will be of the format -. Now there's no public documentation of what that hash is, so it seems like there's no way to find out which folder you need if you have multiple projects or workspaces of the same name

The other, less thought about issue is that a user can choose to put their derived data folder elsewhere. It can be placed in a project relative location, or just in a random path elsewhere on the system. So how on earth do we find out where the directory is located for a given project?

The Solution

The solution it turns out is quite easy. Lets take the two problems in reverse, starting with the case of the user located directory. When you change these setting, an extra file is added to your workspace or project. This is called WorkspaceSettings.xcsettings and is located in the xcuserdata/<yourusername>.xcuserdatad folder of your workspace bundle (note that if you have a project, it will contain a project.xcworkspace bundle).

This file is just a plist so easily readable. Our first step is to check the IDEWorkspaceUserSettings_DerivedDataLocationStyle value. If this is 1 we have a full path, if it is 2 we have a project relative path. Next we look at the IDEWorkspaceUserSettings_DerivedDataLocationStyle value to find the path. Simple enough.

If we don't have a WorkspaceSettings file, or the LocationStyle value is 0, we want to look in our standard Derived Data location. But we still have those pesky hashes to deal with. Well thankfully Xcode provides us with a way around that. We can whittle down the folders by looking for those prefixed with our project or workspace name. Now what we need to do is look in each one, where we will find an info.plist file. This file contains a single value with the key WorkspacePath. As you can probably tell this contains the path to our workspace or project, which we already know and so can match up.

And voila, we can now access our project's Derived Data directory outside of Xcode. And for my encore I just need a babbelfish and a deity…