Akshay Rawat
Akshay Rawat

Reputation: 4784

Xcode - Automatically add all files in a folder to a target

In Xcode, is there a way to specify that all files in a folder are compiled by a target. Eg. the 'Test' target automatically compiles all files in the 'Tests' folder, whereas the 'App' target compiles everything in the 'Sources' folder.

Today, the way I'm doing it, is to add a file to a target every time I create it. This feels a bit error prone and redundant since the files are already organized in the correct folders.

Thanks.

Upvotes: 48

Views: 39910

Answers (9)

Motti Shneor
Motti Shneor

Reputation: 2194

As far as I know, there is no bug in Xcode regarding this, and Xcode never supported such a mechanism (automatically adding/removing sources from a target based on availability in file-system).

The Group variations (groups-representing-a-folder vs. "regular" groups) merely allow you flexibility in how your project tree looks visually - should it closely follow your repository on disk, or be more "human-readable" and descriptive - regardless of the placement of sources in the repository.

I'm speaking of all Xcode versions I worked with - i.e. v1.0b2 to v11.3

In any way, I admit it WOULD BE NICE to have such a mechanism as a new feature (we could pledge Apple for this).

A way to do it now, could be to build a tiny new "Compiler" whose input is a Folder path, and whose "Generated code" would be the set of source files in that folder. If we add such a 'compiler' as a build rule to our target --- we might achieve the desired effect.

If you want to dig in this, follow the way Protobuf can be added into Xcode projects, and how CoreML models are handled in an Xcode target.

Upvotes: 2

HughHughTeotl
HughHughTeotl

Reputation: 5789

This is completely broken in Xcode. Adding folders by folder reference (@Pavel's answer) should be correct - but it just doesn't work. See also https://stackoverflow.com/a/42600782/2518722. I'm using Xcode 8.3 (update: still broken in Xcode 11), but it's been broken for many generations prior, too.

There isn't a perfect workaround, but this is what I do:

  1. Drag the directory into the project
  2. When asked, set options as follows:
    • Select Create groups for any added folder (ie, the opposite to what you think would be right)
    • Deselect Copy files if needed (because you want a link, not a copy).

Now any changes to files in the shared directory will be reflected in Xcode, which is good.

But, there's no way of refreshing the group. (Someone please tell me if I'm missing something obvious here.) So if you've added new files to the directory, you have to remove the group in Xcode and re-add it before those files show up. This is obviously inconvenient, but at least it's not too prone to user error - and is better than adding files manually one-by-one.

Have to say I find it pretty unbelievable that such a simple and necessary feature of a development environment is so broken here.

Upvotes: 22

abc
abc

Reputation: 2027

I just sort by type, so all my source files are listed consecutively.

Then I hold Shift+Cmd and press the down arrow key to select all files I want.

Once selected I can just check all to add to a specific target :)

Upvotes: 1

Pavel
Pavel

Reputation: 930

When you add files to Xcode project, you will have two options to choose from in files select dialog:

  1. Create groups for any added folders.
  2. Create folder references for any added folders.

You need to select the second option. In this case, Xcode will always reflect the changes to files and subfolders of selected folder.

It seems the question was asked a long time ago, but it's still on first place in google search, so hopefully it will be helpful for someone.

Upvotes: 10

Hlung
Hlung

Reputation: 14298

You can add multiple files to targets with this. But it requires some work in filtering what files you want.

Project > Target > Compile Sources > cmd+A to select all files > filter for .m files > click "Add"

Similar to other resources,

Project > Target > Copy Bundle Resources > filter for .png .wav .strings .xib .storyboard, etc. > click "Add"

Upvotes: 5

justin
justin

Reputation: 104698

One approach you could take is to use CMake to generate the Xcode target for you.

Detail: CMake - Automatically add all files in a folder to a target?

Another potential avenue would be to configure a custom build phase.

Upvotes: 0

ThomasW
ThomasW

Reputation: 17317

Here are some other questions on stackoverflow attempting to deal with this issue:

Add files to an Xcode project from a script?

Automatically adding generated source files to an xcode project

Upvotes: 2

regulus6633
regulus6633

Reputation: 19030

Xcode is extensible using shell scripts. You can set the shell script to run at build time so you can basically make xcode do anything you want. You're only limited by your shell scripting abilities and I'm sure you can find help for that too.

Upvotes: -1

Paul R
Paul R

Reputation: 212959

Yes - just select all the files in the folder and use the inspector window (targets tab).

Upvotes: 0

Related Questions