Is there a macro that Xcode automatically sets in debug builds? - xcode

So I can write code like this:
#ifdef [whatever]
// do stuff that will never show up in the production version
#endif

Nothing useful per default, but you can set a DEBUG macro for debug builds in the "Preprocessor Macros" of the targets build settings and then do:
#ifdef DEBUG
// do stuff
#endif
If you want to automate that, edit the project templates in "/Developer/Library/Xcode/Project Templates":
Find the XCBuildConfiguration section(s) for which name = Debug;.
In the buildSettings add DEBUG to the list for GCC_PREPROCESSOR_DEFINITIONS if it exists
Otherwise add GCC_PREPROCESSOR_DEFINITIONS = (DEBUG); to the buildSettings
For per-user customizations and to avoid them being overwritten, see this question.

If you can assume that debug builds always use gcc -O0 (this is normally the case, but there may be odd exceptions where someone has changed the optimisation level for debug builds) then you can do this:
#if __OPTIMIZE__
// ... non-debug stuff ...
#else
// ... debug stuff ...
#endif

Related

How do I include my own xcconfigs when using Cocoapods?

Is there a way to specify in the Podfile that an xcconfig file should be #included in the the one generated by Cocoapods?
Is there an exposed method/variable for appending this #include or do I need to read the generated xcconfig and regurgitate it with the additional text?
For instance, in the generated Pods-SomeTarget.[configuration].xcconfig, I'd like to see:
#include "my_other_config.xcconfig" //<-- I want this to be inserted
FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/Crashlytics" "${PODS_ROOT}/Fabric" "${PODS_ROOT}/Google-Mobile-Ads-SDK/Frameworks" "${PODS_ROOT}/GoogleAds-IMA-iOS-SDK-For-AdMob/GoogleInteractiveMediaAds/GoogleInteractiveMediaAds-GoogleIMA3ForAdMob" "${PODS_ROOT}/NewRelicAgent/NewRelicAgent" "${PODS_ROOT}/TwitterCore/iOS" "${PODS_ROOT}/TwitterKit/iOS"
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1
Include these lines in your debug and release xcconfig files, respectively:
#include "Pods/Target Support Files/Pods-Used/Pods-Used.debug.xcconfig"
#include "Pods/Target Support Files/Pods-Used/Pods-Used.release.xcconfig"
Then click on your project and in the detail navigation bar on the left switch from your current target to your project and select Info at the top. Set the project to use your base/shared config file and Debug/Release to point to your xcconfig files.

Why isn't Swift compiler flag being set?

I want some code to only be executed in a release build, but the code does not get executed when the scheme is configured to use the release configuration.
What am I missing?
I have the following in my app delegate's didFinishLaunchingWithOptions method:
#if RELEASE
Countly.sharedInstance().startOnCloudWithAppKey(appKey)
Crittercism.enableWithAppID(appID)
NSLog("crash logging enabled")
#endif
The target build settings look like this:
And the scheme is configured to use the Release configuration when I run the app:
You will need to set the preprocessor flags explicitly in the "Swift Compiler - Custom Flags" section, "Other Swift Flags" line.
Please check-
In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
It looks like the Swift compiler ignores -D flags that assign a specific value. If you use -DDEBUG and -DRELEASE, it seems to work.
now you need to set
Active Compilation Conditions
under Swift Compiler - Custom Flags
e.g.
Active Compilation Conditions DEV
you can check
#if DEV
print("DEV mode")
#else
print("PROD")
#endif

Using preprocessor defined during compilation time

I want to define a preprocessor in compile time so that I can use it like this in the code
#ifdef MYFLAG
do some thing
#endif
I added that flag using AC_DEFINE([MYFLAG], [1], [My Flag]) in configure.ac and ran autoreconf. I can see it defined in configure file but still I can not use it.

How to setup which libraries link in RelWithDebugInfo build configuration?

I know how to link different libraries based on whether build configuration is Debug or Release. I use:
foreach(dep ${DEPENDENCIES})
target_link_libraries (${PROJECT_NAME}
debug ${dep}_d
optimized ${dep}
)
endforeach(dep)
CMake by default create 4 build configurations in VS2010 (Debug, Release, RelWithDebugInfo, MinSizeRelease). But how to define taget link libraries for RelWithDebugInfo configuration?
Bakcground:
I use only Debug, Release and RelWithDebugInfo. My debug libraries have suffix _d and others have no suffix. So output files from Release and RelWithDebugInfo are the same. Sometimes when I build RelWithDebugInfo and then Release some output files are not overwritten and thus bad ones are loaded and program crashes. I want to solve this problem by adding some other suffix to RelWithDebugInfo configuration.
I have found the solution. It is impossible to do via target_link_librearies but it can be done by setting linker flags:
set(DEBUG_DEP)
set(RWD_DEP)
set(RELEASE_DEP)
foreach(dep ${DEPENDENCIES})
set(RWD_DEP ${RWD_DEP} ${dep}_rwd)
set(DEBUG_DEP ${DEBUG_DEP} ${dep}_d)
set(RELEASE_DEP ${RELEASE_DEP} ${dep})
endforeach(dep)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG} " /LIBPATH:" ${DEBUG_DEP})
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE ${CMAKE_SHARED_LINKER_FLAGS_RELEASE} " /LIBPATH:" ${RELEASE_DEP})
set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}" /LIBPATH:" ${RWD_DEP})

Automated Testing and Xcode Configuration File to specify API Target

So I am writing Automated tests that run from the command line to test the UI of my iOS application. I have the bash script working that cleans and builds the project and runs my UI Automated tests.
The problem is, I want the command-line script to be able to change a C Flag in my Xcode project that determines which server I am pointed at for my application. For example:
./run-test -target "Debug-Server"
will change the value of the C-Flag SERVER_ADDRESS to DEBUG_SERVER while:
./run-test -target "QA-Server"
changes the value of the C-Flag SERVER_ADDRESS to QA_SERVER
In order to do this, I plan to create a .xcconfig file that is fed into xcodebuild that will set the C Flags in my code to point to the correct server. Something like this:
xcodebuild -target <TARGET_NAME> -configuration Debug-QA.xcconfig -sdk "$DEVICE_SLUG""$CURRENT_SDK" DSTROOT=. clean build
I really am not familiar at all with .xcconfig files so I have a few questions about them.
Do I have to describe every build setting in my .xcconfig file? Or is there some kind of "default" value that Xcode uses?
Is there a better way of doing this?
I figured it out, used a combination of .xcconfig and #ifdef statements:
In the configuration file where I declare the server:
#ifdef USE_DEV
#define SERVER_ADDRESS DEV_SERVER_ADDRESS //USED IN AUTOMATED TESTING DEBUG SERVERS
#elif defined USE_QA
#define SERVER_ADDRESS QA_ADDRESSS //USED IN AUTOMATED TESTING STAGING SERVERS
#elif defined USE_LIVE
#define SERVER_ADDRESS LIVE_SERVER_ADDRESS //USED IN AUTOMATED TESTING LIVE SERVERS
#else
#define SERVER_ADDRESS DEV_SERVER_ADDRESS //DEFAULT VALUES
#endif
I then used three .xcconfig files which state the following:
dev.xcconfig:
GCC_PREPROCESSOR_DEFINITIONS = USE_DEV
QA.xcconfig:
GCC_PREPROCESSOR_DEFINITIONS = USE_QA
LIVE.xcconfig:
GCC_PREPROCESSOR_DEFINITIONS = USE_LIVE

Resources