Passing compiler flags through xcodebuild - ios

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!

Related

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

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

xcodebuild linker failing when custom output directory provided

I have a problem using xcodebuild to build the iOS app to custom directory.
Everything works well if I am building using command:
xcodebuild -workspace MyWorkspace.xcworkspace -scheme MyAppScheme -sdk iphoneos -configuration Release ONLY_ACTIVE_ARCH=NO
...but I would like the .app and .dSYM files to be saved in a specific directory (./build for example). So, I tried adding:
CONFIGURATION_BUILD_DIR=./build
or
OBJROOT=./build SYMROOT=./build
as a xcodebuild parameters. Then I've got this error (output comes from xctool as it's more readable, but the same error occurs when using xcodebuild):
✗ Link MyApp
(...)
ld: library not found for -lPods-MyApp-SomePodName
clang: error: linker command failed with exit code 1 (use -v to see invocation)
As I am using Cocoapods, my workspace contains app's project and "Pods" project. I tried using xcodebuild on several other projects that uses Cocoapods and I am always getting this error (SomePodName differs depending on project's dependencies) when trying to provide custom output directory. Please note, that it works without an issue when CONFIGURATION_BUILD_DIR, OBJROOT and SYMROOT parameters are skipped.
I wonder if anything changed recently, because I am pretty sure that I was able to build to custom directory using xcodebuild in the past. Currently, Travis-CI is still able to do it without an issue, but I have no luck when trying on my local machines.
Perhaps providing CONFIGURATION_BUILD_DIR, OBJROOT or SYMROOT is not the perfect way for specifying custom output directory. I am not sure if the problem is connected directly to Cocoapods, but the errors are always connected with some pod library.
Any hints will be appreciated.
My environment:
OS X 10.10.3
Xcode 6.3
xctool 0.2.3
cocoapods 0.36.3
Update: Problem solved
It looks like xcodebuild requires absolute path for CONFIGURATION_BUILD_DIR, OBJROOT and SYMROOT parameters.
Providing absolute path solved the problem:
CONFIGURATION_BUILD_DIR=/Users/me/Dev/MyApp/build

xcodebuild PRODUCT_NAME

I'm able to change my Product name with CLI xcodebuild but when I open my project I still have the old name. I would like to affect my product name's project permanently with CLI.
Is it possible ? Or perhaps I'm doing something wrong :
/usr/bin/xcodebuild -configuration Debug PRODUCT_NAME="NewName"
Passing PRODUCT_NAME to xcodebuild invocation will not change the product name in project but only in this particular build. You need to set it via Xcode in GUI mode if it needs to be done permanently. Look here
Try the following:
Hint 1
how-to-change-the-product-name-with-command-line-builds
/usr/bin/xcodebuild -configuration Debug BUILD PRODUCT_NAME="NewName".
You might have to add BUILD keyword there.
Make sure you have CLEAN your configuration as well

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".

Implicit dependencies do not work with xcodebuild

I have a little problem with one of my projects. In my workspace I have my main project and a lot of projects for static libraries. When I build from Xcode, everything works fine, but with xcodebuild, one of my project is not built, it seems that implicit dependencies do not work.k. Maybe I did something wrong with my configuration. So there it is.
My scheme configuration:
My QAPreferences.a information:
My target settings:
From Xcode, if I build with buildAppTarget in debug for iphoneos, it works.
From xcodebuild, with command below, QAPreferences project is not built.
xcodebuild -workspace myProject.xcworkspace -scheme buildApp -configuration Debug -sdk iphoneos
It worked before, but I recently added QAPreferences, and it does work since this moment. Maybe the problem comes from QAPreferences project...
Regards,
Quentin
So I finally found why the QAPreferences was not build through xcodebuild but I still do not understand the reason. In fact QAPreferences 'Build Archive Architecture Only' was set to YES, I just set it to NO and now it works!
I had a similar issue but forgot to specify the -workspace argument to xcodebuild, which led to it seeming like the implicit dependencies weren't working correctly. I realize this wasn't the issue with the original question, but for people having issues with implicit dependencies, this could be helpful.

Resources