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

但是,我遇到了一个问题,即需要根据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

输出看起来像这样:
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: # ...

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

最佳答案

我偶然发现了同样的问题。首先,我尝试使用显而易见的方式修改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

但这没用。相关的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

编辑:对 v1.0 的改进。与其直接操作xcconfig String内容,不如将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

10-08 13:03