Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

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 

The output looks like this:

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

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

like image 384
JRG-Developer Avatar asked May 14 '15 18:05

JRG-Developer


People also ask

How do I install dependency on CocoaPods?

After you have initially installed CocoaPods into your project, you can add new dependencies (or remove unused ones) by editing the Podfile. Then simply run pod install again.


1 Answers

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 

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 

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 
like image 200
tonymontana Avatar answered Sep 23 '22 13:09

tonymontana