方案的Cocoapods优化级别

时间:2014-02-12 00:12:20

标签: ios xcode cocoapods

查看Cocoapods Pods.xcodeproj的项目文件,看起来每个库的每个方案(Debug方案除外)都具有Fastest, Smallest的优化级别。

当我使用Podfile时,是否有一种快速简便的方法可以更改None或其他Cocoapods配置,以便优化级别为pod install

enter image description here

4 个答案:

答案 0 :(得分:18)

我在Podfile中使用post_install钩子。像这样:

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

修改 Swift Pods会忽略GCC_OPTIMIZATION_LEVEL。如果您正在使用Swift Pod,则还需要设置SWIFT_OPTIMIZATION_LEVEL

答案 1 :(得分:13)

对于使用cocoapods 0.38.0或更高版本看到此内容的任何人:

使用" pods_project"而不是"项目"

之前的答案使用" project" (installer.project.build_configurations.each)

项目已弃用,已替换为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

答案 2 :(得分:3)

如果您还希望在启用断言的情况下添加DEBUG宏以进行调试,则可以使用以下脚本:

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

答案 3 :(得分:3)

您可以在Podfile中添加以下行代替post_install

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