Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DEBUG preprocessor macro not defined for CocoaPods targets

I'm having issues with a pod called DCIntrospect-ARC which should only work in DEBUG mode. It checks if the DEBUG macro is defined before running. However, it is not defined in the CocoaPods target and even though I am running in debug mode in Xcode it fails to run because the DEBUG macro is not defined.

I can define the DEBUG macro in the podspec using

s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => '$(inherited) DEBUG=1' }

but this defined DEBUG for all build configurations and not only the DEBUG configuration.

  1. Is this a CocoaPods issue? Shouldn't the DEBUG macro generally be defined for Pods?
  2. Can I work around this in the Podspec file and declare the DEBUG macro in the Debug build configuration only?
like image 375
Mikkel Selsøe Avatar asked Jan 10 '14 14:01

Mikkel Selsøe


3 Answers

you can use the post_install hook in Podfile.

This hook allows you to make any last changes to the generated Xcode project before it is written to disk, or any other tasks you might want to perform. http://guides.cocoapods.org/syntax/podfile.html#post_install

post_install do |installer_representation|
    installer_representation.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if config.name != 'Release'
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
            end
        end
    end
end
like image 73
johndpope Avatar answered Nov 05 '22 15:11

johndpope


Thanks to John I completed my custom Podfile script, which also changes the optimization level to zero and enables assertions.

I've got multiple debug configurations (for ACC and PROD), so I needed to update several properties for debugging purposes.

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    if config.name.include?("Debug")
      # Set optimization level for project
      config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'

      # Add DEBUG to custom configurations containing 'Debug'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
      if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
      end
    end
  end

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name.include?("Debug")
        # Set optimization level for target
        config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
        # Add DEBUG to custom configurations containing 'Debug'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
        end
        # Enable assertions for target
        config.build_settings['ENABLE_NS_ASSERTIONS'] = 'YES'

        config.build_settings['OTHER_CFLAGS'] ||= ['$(inherited)']
        if config.build_settings['OTHER_CFLAGS'].include? '-DNS_BLOCK_ASSERTIONS=1'
          config.build_settings['OTHER_CFLAGS'].delete('-DNS_BLOCK_ASSERTIONS=1')
        end
      end
    end
  end
end
like image 13
Antoine Avatar answered Nov 05 '22 14:11

Antoine


The accepted answer as of now doesn't work for Swift Pods. Here is a one-line change to that answer that appears to work for both.

    post_install do |installer_representation|
        installer_representation.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                if config.name != 'Release'
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
                    config.build_settings['OTHER_SWIFT_FLAGS'] = ['$(inherited)', '-DDEBUG']
                end
            end
        end
    end
like image 2
xaphod Avatar answered Nov 05 '22 14:11

xaphod