Home > Blockchain >  Make Xcode build a C file generated by processing a file with a custom extension
Make Xcode build a C file generated by processing a file with a custom extension

Time:06-11

I'm trying to set up Xcode to build generated .c files just like it does for files generated by yacc (*.y) and lex (*.l) files.

Say I have input files with a *.corn extension that are meant to be processed by a tool of mine to create C source files. I then want these C source files to be compiled and linked using the rest of Xcode's mechanisms.

This is exactly what happens to my .y and .l files for yacc and lex. The project target contains only the .y and .l files and the rest happens automatically. Generated .c files get put in the derived files directory and those get compiled and linked automatically.

Here's what I've tried so far:

-If I just add a .corn file to my target, Xcode copies it into the bundle at build time presuming it is a resource. Okay, whatever.

-I then create a custom build rule to handle

Source files with names matching: *.corn

Using Custom Script:

#!/bin/sh
cp "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.c"

I have to give it an output file so:

OutputFiles: ${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.c

Note in this case I'm just copying the file as I rename it with .c extension. (As I'm playing around, the .corn file just contains C source.)

So now before the bundle directory even gets created I get the following warning:

unexpected C compiler invocation with specified outputs: 'blahblahblah/Build/Products/Debug-iphonesimulator/MyApp.app/foo.c' (for input: 'blahblahblah/Build/Intermediates.noindex/MyApp.build/Debug-iphonesimulator/MyApp.build/DerivedSources/foo.c')

Then after the link phase (sheesh!), the script above runs and I do end up with my foo.c in DERIVED_FILES_DIR. It doesn't get compiled however and notably, foo.corn didn't get copied into the bundle anymore so there's hope.

I've tried a bunch of other stuff, but this is the cleanest I can describe that others can try. Please what is the magic incantation to make this work?!

Note that I've also tried using DERIVED_SOURCES_DIR instead of DERIVED_FILES_DIR.

CodePudding user response:

So I missed two things:

  1. I needed to add the .corn files to the "Compile Sources" build phase.
  2. By adding .corn files to the project/target before the adding the custom build rule, Xcode added the .corn files to the "Copy Bundle Resources" build phase which broke the build.
  • Related