如何通过CocoaPods安装后挂钩修改OTHER_LDFLAGS?

JRG*_*per 26 ruby ios cocoapods

我的项目使用CocoaPods和自定义xcconfig文件.到目前为止,这并没有造成任何问题:我只是#include在自定义配置结束时不得不使用CocoaPods生成的配置.

但是,我遇到了需要根据情况有条件地指定OTHER_LDFLAGS的问题xcconfig,但我无法弄清楚如何做到这一点.

首先,我尝试简单地记录OTHER_LDFLAGS这样,但实际上没有记录标志:

post_install do |installer_representation|
  installer_representation.project.targets.each do |target|
    target.build_configurations.each do |config|      

      name = target.name
      puts "Target Found: #{name}"

      flags = config.build_settings['OTHER_LDFLAGS']
      puts "OTHER_LDFLAGS Found: #{flags}"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

Target Found: Pods-ProjectName-DependencyName1
OTHER_LDFLAGS Found: # nothing here...?
Target Found: Pods-ProjectName-DependencyName2    
OTHER_LDFLAGS Found: # again nothing...
# etc...
Target Found: Pods-ProjectName  # Cool, this is the main target pod
OTHER_LDFLAGS Found: # ...
Run Code Online (Sandbox Code Playgroud)

如何OTHER_LDFLAGS通过CocoaPods安装后挂钩实际修改?

mrv*_*nzo 33

我偶然发现了同样的问题.首先,我尝试OTHER_LDFLAGS用明显的修改:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                config.build_settings['OTHER_LDFLAGS'] ||= ['$(inherited)']
                config.build_settings['OTHER_LDFLAGS'] << '-l"AFNetworking"'
            end
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

但它不起作用.相关的xcconfig没有得到改变.最终我找到了一个运行良好的解决方法 - 首先读取post_intall钩子中的相关xcconfig文件内容,修改它并将其写回:

V1.0

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                xcconfig_path = config.base_configuration_reference.real_path
                xcconfig = File.read(xcconfig_path)
                new_xcconfig = xcconfig.sub('OTHER_LDFLAGS = $(inherited)', 'OTHER_LDFLAGS = $(inherited) -l"AFNetworking"')
                File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
            end
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

编辑:改进v1.0.不是String直接对xcconfig 内容进行操作,而是将xccconfig 读入build_configuration Hash,修改哈希值,然后将其刷新到xcconfig.

V1.5

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                xcconfig_path = config.base_configuration_reference.real_path

                # read from xcconfig to build_settings dictionary
                build_settings = Hash[*File.read(xcconfig_path).lines.map{|x| x.split(/\s*=\s*/, 2)}.flatten]

                # modify OTHER_LDFLAGS
                build_settings['OTHER_LDFLAGS'] << '-l"AFNetworking"'

                # write build_settings dictionary to xcconfig
                build_settings.each do |key,value|
                  File.open(xcconfig_path, "a") {|file| file.puts key = value}
                end
            end
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

  • 我想从 pod xcconfig 中删除 OTHER_LDFLAGS,因此我使用了 v1.5 并进行了修改:`File.open(xcconfig_path, "w") { |file| build_settings.each 做 |key,value| if key != 'OTHER_LDFLAGS' file.puts "#{key} = #{value}" end end }` 效果很好! (3认同)
  • @mrvincenzo,使用__v1.5__我得到错误`哈希`的奇数个参数. (2认同)

Sve*_*ker 13

根据上面的答案和cocoapodsxcodeproj的官方rubydocs ,我提出了这个解决方案,它纯粹基于上述宝石提供的API:

post_install do |installer|
    installer.aggregate_targets.each do |aggregate_target|
        aggregate_target.xcconfigs.each do |config_name, config_file|
            config_file.attributes['OTHER_LDFLAGS'] << '-l"AFNetworking"'

            xcconfig_path = aggregate_target.xcconfig_path(config_name)
            config_file.save_as(xcconfig_path)
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

这会成功将链接器标志添加-l"AFNetworking"到任何聚合目标('Pod -...')的任何xcconfig文件中.

在Xcode8.3.3和Xcode9 Beta 4上使用cocoapods 1.2.0和1.3.0进行测试.