2

I am building a new Xcode project template and I want to include a library that is non-ARC. But my whole project is ARC, so the only way how to build the project is to specify a compiler flag (-fno-objc-arc) on the files from that library.

How do I do that in an Xcode project template?
I tried setting it on specific files in the Definitions dictionary, both as COMPILER_FLAG and CompilerFlag. Neither of them works.

I have found absolutely no documentation on this, but I am pretty sure it can be done.

UPDATE:
Apple replied to my support request stating that there is no way of doing that right now. So unfortunately, we are out of luck, until they finally do something about the templates and their documentation.

UPDATE 2:
I've got an idea how to hack this a little bit by using a build phases script, that will check the Xcode project and add the required flags. I will post an answer soon.

Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58

4 Answers4

3

I know this page is old but I wanted to offer my take on the provided scripts:

echo "Checking compiler flags."
PROJ_FILE="${PROJECT_FILE_PATH}/project.pbxproj"

REQUIRE_ARC=("File1ThatNeedsARC.m" "File2ThatNeedsARC.m")
REQUIRE_NO_ARC=("File1ThatNeedsARC.m" "File2ThatNeedsARC.m")

STR_TEST_CASE="File1ThatNeedsARC.m \*/; };"

if grep "${STR_TEST_CASE}" "$PROJ_FILE"
then
echo "Adding compiler flags."
for file in ${REQUIRE_ARC[@]}
do
STR_SRCH_ARC=$file" \*/; };"
STR_RPLC_ARC=$file" \*/; settings = {COMPILER_FLAGS = \"-fobjc-arc\"; }; };"
if grep "${STR_SRCH_ARC}" "$PROJ_FILE"
then
sed -i "" "s|${STR_SRCH_ARC}|${STR_RPLC_ARC}|g" "$PROJ_FILE"
fi
done
for file in ${REQUIRE_NO_ARC[@]}
do
STR_SRCH_NOARC=$file" \*/; };"
STR_RPLC_NOARC=$file" \*/; settings = {COMPILER_FLAGS = \"-fno-objc-arc\"; }; };"
if grep "${STR_SRCH_NOARC}" "$PROJ_FILE"
then
sed -i "" "s|${STR_SRCH_NOARC}|${STR_RPLC_NOARC}|g" "$PROJ_FILE"
fi
done
else
echo "Compiler flags already added."
exit 0
fi

just update the arrays REQUIRE_ARC and REQUIRE_NO_ARC (content separated by spaces) with the .m files appropriately. Also update the test case STR_TEST_CASE

This script should be added to the build phases (Editor>Add Build Phase > Add Run Script Build Phase) of your xcode project (above Compile Sources).

Thank you to every one for there help that led to this script.

- Dan

CoderDan
  • 442
  • 1
  • 4
  • 10
2

Ok, so even though I got a negative response from Apple (they don't support this in their template parser) I've found a way how to do it.

I would basically add new build phase to the template - run script. This script woudl flag the required files with the -fno-objc-arc flag and then delete itself from the project.

This is how you can add the flags:

PROJ_FILE="${PROJECT_FILE_PATH}/project.pbxproj"
STR_SRCH="\* Class.m \*\/"
STR_RPLC="\* Class.m *\/; settings = {COMPILER_FLAGS = \"-fno-objc-arc\"; };"
sed -i "" "s|${STR_SRCH};|${STR_RPLC}|g" "$PROJ_FILE"

Then in a similar manner you scan the project file and remove the build phase (with the script), so it doesn't get run each time.

I will update this answer with complete code soon.

Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • how do you get this to run prior to the compiler? My build fails because the compile checks my "Compile Sources" before running the actual run script. I also tried dragging the script to the row before "Compile Sources" – kevinl Jan 14 '14 at 22:03
  • I just dragged it before, that's all. It should work. – Dominik Hadl Jan 15 '14 at 00:45
  • How did you end up removing the build phase with the script? – kevinl Jan 22 '14 at 18:20
  • I got the compiler flags to automatically get added, but when I try to run my build, it just cancels. It only runs after i delete my run script. I'd like to some how keep it in the list of build phases – kevinl Jan 22 '14 at 18:53
2

After trying to make sure my script doesn't run after it's initial build, I've done the following:

 echo "Adding compiler flags"
PROJ_FILE="${PROJECT_FILE_PATH}/project.pbxproj"

STR_SRCH="SomeClass.m \*/; };"
STR_RPLC="SomeClass.m \*/; settings = {COMPILER_FLAGS = \"-fobjc-arc\"; }; };"

if grep "${STR_SRCH}" "$PROJ_FILE"
then
    sed -i "" "s|${STR_SRCH}|${STR_RPLC}|g" "$PROJ_FILE"
else
    exit 0
fi

the grep will make sure that I have that exact string to replace. Otherwise, I'm assuming the fobjc-arc flag has already been added and I no longer need this script. This allowed me to run the build without removing the actual script.

kevinl
  • 4,194
  • 6
  • 37
  • 55
  • 1
    Nice idea. +1 for that. If you don't mind having a one extra build phase then this would be the way to go. You could also move the echo after the if condition, so it prints out only when actually adding the compiler flags. – Dominik Hadl Jan 26 '14 at 15:08
0

You can add this flag int the project Build Phases>Compile sources as shown in the screenshot below. Put this flag in all .m files of this library:

enter image description here

Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • Sorry, I already know that. I am asking on how to do that when creating Xcode project templates. – Dominik Hadl Nov 03 '13 at 09:05
  • Then the answer to your question is no, except some workarounds http://stackoverflow.com/questions/8691921/can-a-custom-xcode-template-partially-and-selectively-enable-arc-automatic-refe – Nikos M. Nov 03 '13 at 09:11
  • I've now got it confirmed by Apple. It really is not possible. – Dominik Hadl Nov 06 '13 at 08:31