Compiler flags for each file in the Xcode project template

I am creating a new Xcode project template and want to include a non-ARC library. But my whole project is ARC, so the only way to create a project is to specify the compiler flag ( -fno-objc-arc) in the files from this library.

How to do this in an Xcode project template?
I tried to install it on certain files in the dictionary Definitions, both in COMPILER_FLAG and in CompilerFlag. None of them work.

I did not find absolutely any documentation on this, but I am sure that this can be done.

UPDATE:
Apple responded to my request for support, saying there is no way to do this right now. Unfortunately, we were not lucky until they finally did nothing about the templates and their documentation.

UPDATE 2:
I have an idea how to hack this a bit using script build phases that will validate the Xcode project and add the necessary flags. I will send an answer soon.

+1
source share
4 answers

So, although I received a negative response from Apple (they do not support this in their template parser), I found a way to do this.

I would basically add a new build phase to the template - run script. This woudl script flag indicates the necessary flag files -fno-objc-arc, and then removes itself from the project.

Here is how you can add 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 the same way, you scan the project file and delete the build phase (using the script), so it does not start every time.

.

+1

, , :

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

REQUIRE_ARC REQUIRE_NO_ARC (, ) .m . STR_TEST_CASE

script (Editor > Add Build Phase > Add Run script Build Phase) xcode ( ).

, script.

- Dan

+2

, script , :

 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

grep will make sure I have this exact replacement string. Otherwise, I assume that the fobjc-arc flag has already been added and I no longer need this script. This allowed me to run the assembly without deleting the actual script.

+1
source

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

enter image description here

0
source

All Articles