2

I have a tool that generates additional source code on a build phase in a run script section. I would like to include resulting files of this section into compilation and linking. How it is possible to do? I know that it is possible to write clang calls in additional run script section but I am looking alternative options, since it will be too complex to keep run script section with clang and project compiler settings in sync.

Files that I am generating is a set of categories to classes currently included into the project. I do not need to worry about importing categories into the project, since all the code that generated automatically imported into generated-categories.h which is imported by default into precompiled header.

Nikita Leonov
  • 5,684
  • 31
  • 37

1 Answers1

5

You can solve this problem by adding a file to your project that contains something like the following:

#include "generatedFile1.c"
#include "genreatedFile2.c"

And so on. Then, you just need to make this file (or build phase) depend on (or run after) the source-code generating step.

I'm not very familiar with Xcode, so I don't know specifically how you would accomplish that; hopefully someone with more specific experience can point you in the right direction on that front.

Edit: I made it work with a simple project here. Example:

main.m:

#import <Foundation/Foundation.h>
#import "generatedFile.m"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // insert code here...
        NSLog(@"%@", string);

    }
    return 0;
}

script.sh:

#!/bin/sh

echo "NSString *string = @\"Hello, World\";" > ${SYMROOT}/generatedFile.m

And then I added ${SYMROOT} in the "Header search paths" in the project settings and added a "run shell script" phase before the "compile sources" phase.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    That is actually a challenge in Xcode to add files into the compilation phase from the script. :) So the second part is the most important one. Anyway thanks for trying to help :) – Nikita Leonov Jul 27 '13 at 04:52
  • I'm fiddling with it now. Adding the script phase seemed pretty easy, I just need to figure out how to add the build results directory to the include search path, I think. Edit: I have it working by generating the file in `/tmp`, so I'm not far off. – Carl Norum Jul 27 '13 at 04:53
  • You know what, it is actually could be an option. Thanks for an idea. I will take a look into it. I will let you know later if it will work for me. Thanks! – Nikita Leonov Jul 27 '13 at 04:57
  • It works perfectly on my side as well. Thank you very much :) – Nikita Leonov Jul 27 '13 at 05:25