6

As part of our Unity build process we drag and drop a directory of C source files which are generated by the Unity IDE into our iOS Xcode project. We want Xcode to "Create groups" for the files because the header files don't seem to be recognized by the compiler when using "Create folder references".

I'm wondering if there's a way to script this so we don't have to manually drag the directories into Xcode each time. Adding a "Copy Files" build phase only seems to work if I choose "Create folder references". When I choose "Create groups", the "Copy Files" section remains empty. Is this a bug in Xcode or am I doing something wrong? I'm using Xcode 9.1.

I know Apple recommends creating a framework rather than copying in a directory of source files. The reason we can't do that is because we're relying on the Unity build process which gives us a bunch of C source files.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike Onorato
  • 1,283
  • 1
  • 10
  • 16
  • 1
    I think this is valid question. Anyway, down voting without giving a reason is not very constructive! – Reinhard Männer Dec 07 '17 at 20:22
  • 2
    Perhaps a valid question, but a bit of an unclear question. Is you're goal to add files to compile? Or are these resource files or both. It would seem like it is files to compile. And are you wanting to use Xcode normally other than that (ie. add arbitrary files out of band of this drag and drop thing you're talking about?). Why does such a need exist (ie. why do the files change so much that you would need this?). Only asking because it makes me a bit suspicious of the build process in general... – Mobile Ben Dec 09 '17 at 23:01
  • These are C source files that need to be present at compile time. We need to still be able to make other changes to the XCode project independently of this. These C source files change every time we create a new Unity build. – Mike Onorato Dec 12 '17 at 04:38
  • 1
    What does this have to do with "copying"?? You don't need to "copy" anything to make it part of a project. You just add it to the project for purposes of the build. – matt Dec 12 '17 at 04:40
  • You're right. I edited the question to not use the word "copying". Thanks. – Mike Onorato Dec 31 '17 at 21:22

3 Answers3

1

In Xcode, you can manage your build process under the "Build Phases" tab when you select your app target. These tasks are executed on each build of your app, unless specified otherwise by some phases (for example: you can choose to run a phase only when the app installs for the first time).

Click the + button and you can add a "Copy files phase" that will copy your selected files into the app. When adding the files you'll be able to select if added folders will create groups or folder references.

You can also add a custom "Run script phase" and write some bash code to do whatever you like, or even run an external script that will do more complex work.

Artal
  • 8,933
  • 2
  • 27
  • 30
  • 2
    Adding a "Copy Files" build phase only seems to work if I choose "Create folder references", but folder references aren't useful for us because apparently the compiler won't recognize the source files if they're in a folder reference rather than a group. When I choose "Create groups", the "Copy Files" section remains empty. Is this a bug in XCode? I'm using XCode 9.1. – Mike Onorato Dec 12 '17 at 04:42
  • 1
    Regarding the "Run script phase" approach, I was able to find an example script in the answer to this question but it seems to only work for adding resources like images or data. Can you provide an example script that would work for including source files in the xcode project for compile time? https://stackoverflow.com/questions/1371351/add-files-to-an-xcode-project-from-a-script – Mike Onorato Dec 12 '17 at 04:49
  • 1
    don't have a specific example but if it works for resources it should be relatively easy to make it handle of kinds of files. You can create an empty reference folder in your Xcode project and then copy the files in the build step with your script. an important note though - if you want the copied files to be compiled you will need to position this phase above the "compile sources" (you can reorder by dragging the phase). This applied to both copy and run script phase types – Artal Dec 12 '17 at 08:42
  • 1
    I don't think it's possible to use a reference folder for this since the files have to be accessible for compile. If I copy the files into a "Folder reference" they don't recognized by the compiler. I have to choose "Create groups" but I don't think there's any way to script that. The only potential way I found was to using rb-appscript as in the below link but I tried it and it seems it's not supported by any versions of XCode. https://stackoverflow.com/questions/6685637/how-do-i-add-files-to-targets-in-xcode-using-rb-appscript – Mike Onorato Dec 12 '17 at 23:22
  • 1
    And when you used create groups in the copy phase you made sure to position it first? (Above the compile sources phase). Otherwise I don’t have a better advice :) – Artal Dec 13 '17 at 07:13
  • Yes I have the copy phase first in the list of phases. – Mike Onorato Dec 19 '17 at 02:00
1

Xcode makes it possible to script adding resource files such as images to the project file using the "Build Phases" tab however this does not work for source code files that need to be recognized at compile time. Luckily I managed to come across a nice community-developed Python script which does allow us to do this.

https://github.com/kronenthaler/mod-pbxproj

With this script we can make all sorts of modifications to the project file including adding or deleting files.

See this link for a full list of available operations:

https://github.com/kronenthaler/mod-pbxproj/wiki

Mike Onorato
  • 1,283
  • 1
  • 10
  • 16
-2

xcode->Build Phases-> click (+) button -> New Copy Files Phase / New Run Script Phase

enter image description here

enter image description here

AppHero2
  • 681
  • 7
  • 25
  • Can you please elaborate on this answer? I'm aware of how to invoke a script but unsure how to write that script such that it copies source files into the Xcode project for compilation. It seems like rb-appscript is no longer supported by Xcode. Maybe you can elaborate on what script you were envisioning? – Mike Onorato Dec 19 '17 at 01:59
  • I have experienced in adding following scripts before. ,<< bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh" >> – AppHero2 Dec 19 '17 at 06:05
  • That script is for stripping frameworks for the Realm framework and doesn't seem pertinent to my question about copying source files for compile. – Mike Onorato Dec 19 '17 at 22:29