Cocoapods optimization level for schemes
Asked Answered
R

4

19

Looking at the project file for Cocoapods Pods.xcodeproj it looks like every single scheme for every library (except for Debug scheme) has an optimization level of Fastest, Smallest.

Is there a quick and easy way to change the Podfile or some other configuration of Cocoapods so that the optimization level is None for specific schemes when I use pod install?

enter image description here

Reeve answered 12/2, 2014 at 0:12 Comment(0)
K
30

I use post_install hook in Podfile. Like this:

post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        if config.name.include?("Debug")
            config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
            config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
        end
    end
end

EDIT GCC_OPTIMIZATION_LEVEL is ignored for Swift Pods. If you're using Swift Pods you also need to set SWIFT_OPTIMIZATION_LEVEL.

Kingwood answered 13/3, 2014 at 8:48 Comment(5)
Had to include if config.name.include?("Localhost") as well.Reeve
actually it should be config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone' for the swift settingDarill
There really is no need for this - IMHO, it is a much cleaner solution to use the project keyword in your Podfile and then define which Schemes are debug. See guides.cocoapods.org/syntax/podfile.html#project "Using custom build configurations".Trichinopoly
How can I do that only for specific pods, and not for all of them?Handkerchief
Great answer! You can also just do unless config.name == 'Release' if you want to disable optimization on everything but the release scheme.Jaclin
D
14

To anybody seeing this using cocoapods 0.38.0 or later:

Use "pods_project" instead of "project"

The previous answers use the word "project" (installer.project.build_configurations.each)

Project was deprecated and replaced with pods_project. https://github.com/CocoaPods/CocoaPods/issues/3747

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    if config.name.include?("Debug")
      config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
    end
  end
end
Dela answered 13/10, 2015 at 14:56 Comment(0)
P
4

If you also want the DEBUG macro added for debugging with assertions enabled, you can use the following script:

post_install do |installer|
  installer.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.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
Phlebotomy answered 20/10, 2014 at 12:59 Comment(0)
P
4

Instead of post_install you can add following line to Podfile:

project 'ProjectName', 'DebugConfigurationName' => :debug

Peccadillo answered 29/8, 2017 at 20:7 Comment(2)
That works for a single configuration, but what's the syntax when you want multiple configurations to behave as debug?Gilder
I finally have an answer and it's pretty simple. First, this is the official guide page for that: guides.cocoapods.org/syntax/podfile.html#project and second, you can list different schemes repeating :debug and :release if you want so, for example: project 'ProjectName', 'FirstDebugName' => :debug, 'SecondDebugName' => :debug, 'FirstReleaseName' => :release, 'SecondReleaseName' => :releaseGilder

© 2022 - 2024 — McMap. All rights reserved.