本文介绍了如何通过CocoaPods安装后挂钩修改OTHER_LDFLAGS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用CocoaPods以及自定义 xcconfig 文件。到目前为止,这并没有造成任何问题:我只需要 #include 在我的自定义配置结束时生成的CocoaPods配置。

My project uses CocoaPods and also custom xcconfig files. Until now, this hasn't caused any problems: I've just had to #include the CocoaPods-generated configuration at the end of my custom configuration.

但是,我遇到了一个问题,需要根据 xcconfig OTHER_LDFLAGS / code>,但我无法弄清楚如何做到这一点。

However, I've run into a problem where a need to conditionally specify OTHER_LDFLAGS based on the xcconfig, but I can't figure out how to do this.

首先,我尝试过只记录 OTHER_LDFLAGS 像这样,但实际上没有记录标志:

As a start, I've tried simply logging the OTHER_LDFLAGS like this, but the flags aren't actually logged:

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

输出如下所示:

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: # ...

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

How can I actually modify OTHER_LDFLAGS via the CocoaPods post-install hook?

推荐答案

我偶然发现了问题。首先,我试图用明显的方法修改 OTHER_LDFLAGS

I stumbled across the same problem. First I tried to modify OTHER_LDFLAGS with the obvious:

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

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

but it didn't work. The relevant xcconfig didn't get the change. Eventually I found a workaround that works well - first read the relevant xcconfig file content in the post_intall hook, modify it and write it back:

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

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

EDIT: Improvement over the v1.0. Instead of operating on xcconfig String content directly, read xccconfig into a build_configuration Hash, modify the hash and then flush it to 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

这篇关于如何通过CocoaPods安装后挂钩修改OTHER_LDFLAGS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 00:42