Compile different files for device/simulator in XCode - ios

I'm including a 3rd party library (sources) with my static library. It it intended for device only (since it includes some ARM assembly routines) and I do not wish to build it for the simulator (I just want my app to compile there so I can test the GUI).
Creating another target for simulator only is not an option since my projects reference my library as a dependency and it would be a nightmare to maintain.
Adding #if (TARGET_OS_IPHONE) for those files is not an option as well since these are not my original sources and I would like to update them easily for updates (there are more than 200 files there and I do not with to modify all of them)
I'm looking for a way (similar to #if (TARGET_OS_IPHONE) in source files) which will work from the IDE itself (so I can exclude a file from the actual build process based on my target architecture). The only thing I found is an option to exclude/include a file from a target - but not for a specific architecture.
Is there a way to set architecture conditions per specific files?

Your best bet would be to separate the third party library into a new target that builds it as a static framework. Set it as a dependency for your project, and then set the static framework to be conditionally linked as described by Apple here: http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/ios_development_workflow/115-Configuring_Applications/configuring_applications.html#//apple_ref/doc/uid/TP40007959-CH19-SW7
You can add a new target by selecting the Project in the project navigator and then hitting the Add Target button at the bottom of the Editor pane. You can setup a cocoa touch static library and then assign the relevant .m or .c files to belong only to that target. Then select your app's target and add the static framework in the "Target Dependencies" section of the Build Phases tab.

You can conditionally include/exclude files in Xcode using EXCLUDED_SOURCE_FILE_NAMES based on sdk. For example:
EXCLUDED_SOURCE_FILE_NAMES[sdk=*simulator*] = something.cpp something_else.cpp
See Conditional Compilation article for more details

Related

Add Files and Select Multiple Targets in Xcode

I have over 100 projects in my XCode and when I add a new file or class I have to select each target one by one to add it to it. Is there a way to select multiple targets instead?
The usual way is to have targets that don't duplicate each others files. E.g. some targets are libraries that are used by some others, and eventually there's an app or a unit test target that uses them. In this case there's no need to have the same file in multiple targets.
So consider refactoring the common files to a common library target that you can reference from the other targets. In this case when you add a file, you can add it to just that library, and it will be automatically resolved (linked) in the other targets.
Note also that in this case the file you add is only compiled once, and the compilation result is reused in various dependant targets, as opposed to compiling a separate version of that file for each target.
If that's not an option for you, an alternative is to generate the Xcode project file using some tool like CMake, GN, or a custom script that can write xcodeproj files (there are libraries to do that in various languages).

How to create an Xcode project that is not an app but is visible in other projects?

I want to create a project with a handful of categories that add useful functionality to UIKit.
I want to keep this as a separate project so that it can be source-controlled separately (and eventually hosted on github, and downloadable as a dependency via cocoa pods...but let's skip this for now.)
But all the Xcode 6.1 project templates imply an app inside. When I select an empty project it complains about missing base SDK. But this cannot be set without a target present. It is quite confusing. I don't need a target. Do I?
The project should be just a shell for a number of categories + a accompanying summary header file.
I want to be able to drag this project as a subproject to any other proper app project, import the summary header file and expose those categories to the main project.
Any advice?
You need a target. Every source file that should be compiled must be a part of a target.
You could reference each source file from the target(s) in your app's Xcode projects, but that'd be tedious as you'd have to add to every project manually when you add source to your shared repository.
Either create an embedded framework project or a static library. Unless you are really going to share code between an app and an app's extension, go with the static library. It is a lot easier to manage.
Create a project that has a static library target, add the files to that static library.
Then create a workspace that has your static library project and your app(s) project(s) in it. Modify the app targets to link against the static library.

Dynamically link binaries with library in xcode

To link binaries to you framework you can from build phase add your needed libraries to your project .. but what if I need to make some thing specific for example I need to customise my framework by adding or deleting binaries.. foe example I want lite version and full version so I need always to delete and add these binaries from "Link binary.." tab
Is there any dynamic way like to make different configuration files or some thing?
You'd need to create 2 Xcode targets, one for lite and one for pro, which compile the same source files and link to mostly the same frameworks etc.
You can also add compiler constants to the Build Settings of these targets, which will allow the common source files to behave differently between these target "products".

Script to add list of framework into xcode project? [duplicate]

I am looking for a way to add libraries to an Xcode project, using the command line.
I have been successful in adding files to groups with the XCS tool, but it does not support libraries.
I would, for example, like to be able to add CoreVideo.framework to a specific project with a command on the Terminal.
This project can handle frameworks:
https://github.com/kronenthaler/mod-pbxproj
Just add it as a normal file, it will figure out the correct type and how to set everything up (i.e., add it to the link library phase – before using it, you still need to import the header(s), of course).
// libFilePath: Path to the framework
frameworkGroup = project.get_or_create_group('Frameworks')
project.add_file(libFilePath,
parent=frameworkGroup,
tree='SDKROOT',
weak=True)
You can decide whether you want to weak-link frameworks (see code example) or not (just leave out the last parameter, it defaults to False). You can find the code to open/save Xcode projects in the repository.
Another way to do it is adding linker flags directly, e.g., -framework CoreVideo.framework. If the framework paths are set up correctly, you don't have to provide absolute paths. The disadvantage of this approach is that the linked frameworks aren't obvious if you open the Xcode project, as they are not part of the link library section, nor does the framework show up in any Xcode group in the Project Navigator.
You can try generating your .xcodeproj using tools like XcodeGen. It allows you to include different types of dependencies.

Xcode project templates: How can I specify a static library dependency?

I am trying to create an Xcode project template that contains two compilable targets, with one target (an app) dependent on the other (a static lib).
Everything works so far, except that I cannot figure out how to specify the actual dependency in the template so that it shows up in the Target Dependencies section of the Build Phases tab of the app target of an Xcode project created from the template.
I've configured the TemplateInfo.plist of the project as follows:
The Components section identifies the static library project template and successfully brings the contents of that template, and its target, into the app project. And once everything is compiled, the static library is successfully linked in the end, by Item 3 in the OTHER_LDFLAGS entry.
But try as I might, the Dependencies section of the template does not cause the static lib to appear in the Target Dependencies section of the Build Phases tab of the app target. I've tried specifying the dependency using the numeric values 0 and 1, and I've tried it as a string using the name of the static library target (cocos2d), but none of these work.
In the end, in each Xcode project created from this template, I'm left with users having to perform the step of navigating to the Target Dependencies section of the Build Phases tab of the app target, and manually selecting and adding the static library target. Although this is a relatively simple task, it shouldn't be necessary at all.
Can anyone advise me on how to configure the template dependencies so this manual step is not necessary?
As you've indicated, the Dependancies tag under Targets references target array indices. In the example you've posted, your dependency is under Components.
Components appear to be add as targets after the targets from your template file are added to the project. If you reference the Cocoa Touch Application template provided with Xcode, Cocoa Touch Application Unit Testing Bundle is included as a component.
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Temlpates/Application
Looking at the plist for the Cocoa Touch Application Unit Testing Bundle, there is a dependency on target 0. Given components appear to be added after "local" targets, target 0 is
present in the project when the unit test target is added and a dependency is created against the primary target.
Attempting this in reverse by adding a dependency on target 1 in the Cocoa Touch Application does not work.
The only work around that I've been able to find is to move Components to Ancestors and duplicate the Targets items from each component in the Targets section of your template. From there you can reference the indices of any components included in your targets array.
You should note that even with this technique, targets appear to be added to the project in order and you only seem to be able to set dependancies on indices that have already been added (i.e. index 0 cannot reference index 2).

Resources