How do I pass preprocessor macro name in xcode build command? - ios

I am doing CI build on azure devops. I am wondering how to pass pre-processor macro value so that I can make different flavours of build.
AFAIK if we can set preprocessor macro in xcode build then we can do it on azure devops as well.
Any helpful pointer is welcome

Approach 1: By the means of GCC_PREPROCESSOR_DEFINITIONS
You have to make use of GCC_PREPROCESSOR_DEFINITIONS on xcodebuild command line.
Here is a sample macro in your code
#ifdef Flavour1
NSLog(#"This is flavour 1");
#endif
and Here is how you pass the macro through command line
xcodebuild -verbose -scheme "YourAppScheme" GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS Flavour1=1'
Approach 2: By the means of separate xcode Scheme
Have a scheme and it's respective target for each flavour. so that it can have different App name,version number,signers etc if required and you can have macro injected in preprocessor definition of build settings in target.
All you have to do is just pass the right scheme in xcodebuild command and that's you sorted
P.S:-
I personally prefer Approach 2 because it's easy to customise without worrying much about xcodebuild command line parameters.

You can create a ruby script which can setup the project. Xcodeproj is a powerful tool to modify Xcode projects.
https://github.com/CocoaPods/Xcodeproj

Related

Specify individual target build settings with xcodebuild

I have an iOS app that uses Cocoapods, and has a local cocoa touch framework target, and I'm running into an issue with specifying build settings in the command line using xcodebuild.
I understand that you can specify build settings like PROVISIONING_PROFILE, CODE_SIGN_IDENTITY, and DEVELOPMENT_TEAM by appending their key=value pair to the end of the command.
However those setting appear to be applied to the entire project, but I only need them to apply to the app target since touch frameworks cannot be signed.
How do I specify build settings for an individual target using xcodebuild? If it cannot be done using xcodebuild, could this be accomplished using fastlane?
Use schemes. You can set the scheme to build/archive with a particular build configuration and executable.

xcodebuild: error: The workspace named "myWorkspace" does not contain a scheme named

I got shell script for build and deploy xCode projects.
https://github.com/ciryon/xcodebuild-script
I am trying to run the script but getting the following error.
xcodebuild: error: The workspace named "jamesAppV2" does not contain a scheme named "". The "-list" option can be used to find the names of the schemes in the workspace.
Note:
Its pointing me to scheme named "" (empty), What could be the problem any hint.
here is my scheme -list
xcodebuild -workspace jamesAppV2.xcworkspace -list
Information about workspace "jamesAppV2":
Schemes:
jamesAppV2
jamesAppV2Tests
OAuth2
OAuth2iOS
AFNetworking
Alamofire
Firebase
Flurry-iOS-SDK
ForecastIOClient
ForecastIOClient-ForecastIOClient
Kingfisher
Pods-jamesAppV2
Starscream
SwiftyJSON
You can use fastlane.gym which is a wrapper around xcodebuild that simplifies its usage and fixes many problems.
I had a quick look at the scripts. It appears that they are attempting to use the shared scheme files to locate the scheme. So two things spring to mind, first - why not grep the xcodebuild -list command instead. Secondly, a blank result from the script may be caused by running the script on a project that doesn't have any shared schemes.
Finally I note that the scripts are quite old. This probably means that they are rather out of date and using old techniques (iso-sim) for example. I suspect that you might be better off starting from scratch and building a new script piece by piece using the latest commands offered by xcodebuild. It may take some time, but will be worth it in the long run.

Target Properties changes by xcodebuild command

How can i manipulate my iOS Target Properties by xcodebuild command tool ?
In example, i have one project with one target and with Facebook SDK Key in Target Properties in Info tab
FacebookAppID: 01234567891234
And with console command xcodebuild i'm compiling two Apps with Developer and Production provisions with two console commands:
For Developer:
xcodebuild -project Projectname.xcodeproj clean install OBJROOT=ObjRoot ... CODE_SIGN_IDENTITY={iPhoneDeveloper} PROVISIONING_PROFILE="Dev.mobileprovision"
For Distribution:
xcodebuild -project Projectname.xcodeproj clean install OBJROOT=ObjRoot ... CODE_SIGN_IDENTITY={iPhoneProduction} PROVISIONING_PROFILE="Prod.mobileprovision"
And i need use in Developer one FacebookAppID, and in Production a other FacebookAppID, and how i can change my command lines for realize this?
Thanks
Arguments to xcodebuild are best used for values that change every time they're invoked, like a destination directory that includes a build number.
For data as foundational as a Facebook app ID, I'd recommend using your build configurations, e.g. "Debug" and "Release". Create a user-defined build setting (in Xcode 5.0.2, Editor -> Add Build Setting -> Add User-Defined Setting) named something like "FACEBOOK_ID", and in your Info.plist, set your desired key's value to ${FACEBOOK_ID}. In your target's build settings, define FACEBOOK_ID differently for your Debug and Release configurations. Your code then now has to pull the current value out of Info.plist at runtime.
If you really want to override build settings as arguments to xcodebuild, you can do so, just by adding "FACEBOOK_ID=12341234" to the end of the command, but this only works if you've done the work I just described to make FACEBOOK_ID into a configuration-specific build setting. I can't think of any sound reasons to keep this kind of app data outside of a build configuration.
If build configurations are new to you, I'd suggest starting with WWDC 2012 session 408, "Working with Schemes and Projects in Xcode".

Passing compiler flags through xcodebuild

I'm currently using xcodebuild to automate testing for iOS.
Right now, I'm stuck on trying to pass compiler flags through Xcode directly to the compiler. These flags are: -fprofile-arcs -ftest-coverage.
I don't have the liberty of modifying the xcodeproj, that's why I want to inject these flags via the xcodebuild command.
It would be something like:
xcodebuild -project path/to/my.xcodeproj -scheme MyApp -fprofile-arcs -ftest-coverage
Is that feasible? How?
Apparently most compiler flags can be expressed as constants, and these can be passed to the compiler via xcodebuild easily.
To get them, simply select the option in the xcode build settings view, and hit command-C (copy). In my case, they were GCC_GENERATE_TEST_COVERAGE_FILES and GCC_INSTRUMENT_PROGRAM_FLOW_ARCS.
My command roughly looks like this:
xcodebuild GCC_GENERATE_TEST_COVERAGE_FILES=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES ...
To set compiler flags with xcodebuild, you need to put them in the OTHERCFLAGS option.
For example:
xcodebuild -project path/to/my.xcodeproj -scheme MyApp \
OTHERCFLAGS="-fprofile-arcs -ftest-coverage"
Yes, all of the compiler setting actually boils down to string key-value pairs. I answered a very similar question about setting preprocessor macros from the command line that is just as applicable to these settings you would like to set:
Setting a #define from the command line in xcode 4.6
I would also like to call attention to the use of ${inherited} -- using this values allows you to use the Xcode project-specified values AND append your own. Documentation for each of the build settings including those that you located via copy-paste can be found here:
http://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html
Glad you were able to get it to work -- now you can really make xcodebuild do even more for you without requiring changes of the target Xcode project!

How do you determine the build options that XCode uses so you can use them with xcodebuild?

I want to build my iOS projects with xcodebuild, but to do so I need to figure out all the flags and options that XCode is using to build my project when I build and run in XCode.
How can I find out what compilation flags and such XCode is using during the build process, so that I can figure out the equivalent xcodebuild command? I'm especially interested in getting this to work when I'm including external libraries like RestKit in my project.
All you need to do is tell xcodebuild the target and the configuration; everything else is contained in the project relating to the configuration.
See the manpage.

Resources