4

I have a situation where I need to enable ARC for many files in few Non-ARC xcode projects. I don't want complete conversion to ARC only few files needs to be enabled with arc.

There are many solutions that includes -fobjc-arc flag to specific files in the Compile Sources using the XCode console. But I have too many files in multiple xcode-projects to change it individually through XCode.

Is there a method by which I can modify the build phase settings programmatically. I found few posts like

Both of which are good solutions but I need to setup a mechanism where I can write scripts to manage them. As some of these run on servers and the resultant xcode is distributed to other people to test.

Manipulations of project.pbxproj code seems to be the most likely choice. But I am not sure how to go about it.

Community
  • 1
  • 1
Edwin Abraham
  • 645
  • 7
  • 24

3 Answers3

2

If you look inside the .pbxproj file inside your .xcodeproj file you can see the files included in your project. I'm not sure how in depth you want to go with your manual modification of flags, but I have a few files that have compiler flags set to them. In the .pbxproj file they look like this:

D5298D04170F232900D3B684 /* ASIHTTPRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = FACE22CF15B72C4C00A0A4AD /* ASIHTTPRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };

Whereas files with no compiler flags look like this:

D5298D02170F232900D3B684 /* GradientShapeView.m in Sources */ = {isa = PBXBuildFile; fileRef = FACE22C715B6F90000A0A4AD /* GradientShapeView.m */; };

I have no idea why, but these compiler flags and files are listed in that .pbxproj file 4 times each. I have 12 files in the project with the compiler flags set, but the flag is found in that file 48 times.

You can setup a text parser to run through this file and add the flag to the specific files that you would like to have them. I'm not sure what else you will screw up in doing this, but that would be a perl scriptable way of adding compiler flags to specific files.

Putz1103
  • 6,211
  • 1
  • 18
  • 25
  • Thanx. This seems to be the only way to edit the compiler flags. As for the flags turning up four times it should because references for KRelease, KDebug , Preview also they should turn up. That's only three times. Don't know how you got four. There must be apply to all files reference also. – Edwin Abraham Jan 10 '14 at 10:01
  • I came across mod_pbxproj this week. See my answer for more info. – andyzinsser Jun 23 '14 at 23:57
2

If you want to keep your default target as non-ARC, then you have two options:

  1. enable -fobjc-arc for each ARC file in the compile sources phase
  2. create a new static library target for your ARC files (and enable ARC for that target)
Nick Dowell
  • 2,030
  • 17
  • 17
  • I need to enable -fobjc-arc programmatically as there are multiple files in multiple xcode projects. Without opening XCode I want to achieve this. If possible using perl (as it is using a perl script to generate the xcode from a remote server). – Edwin Abraham Jan 09 '14 at 16:02
1

Select all of the files that you want to enable ARC on simultaneously, and then press return/enter. It will bring up a text box where you can then type in -fobjc-arc and it will add it to all of them.

Gavin
  • 8,204
  • 3
  • 32
  • 42
  • I need to enable -fobjc-arc programmatically as there are multiple files in multiple xcode projects. Without opening XCode I want to achieve this. If possible using perl (as it is using a perl script to generate the xcode from a remote server). – Edwin Abraham Jan 09 '14 at 16:03
  • Why does it need to be programmatically? If this just needs to be done once, why not just do it? It seems like you'd have to specifically choose which files need it enabled anyway, so it seems like it would be just as much or more work to try to do it your way as it would doing it with Xcode. Besides, using a script you risk breaking the files you're modifying if you don't fully understand the file format. – Gavin Jan 09 '14 at 16:31
  • That's true doing through Xcode is way safer. But the project.pbxproj programming will modify the build phases without opening xcode. Since the file references are generated along with xcode project creation, the issue of breaking the files will be reduced considerably. It is however true that there is no official apple guide to edit project.pbxproj. – Edwin Abraham Jan 10 '14 at 10:08