Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoapods and custom xcconfig

I am trying to use Cocoapods with some custom configurations in an iOS project.
I have 3 (Dev, Stage, Prod) and each of them has some custom GCC_PREPROCESSOR_DEFINITIONS. I have seen around people suggesting to us #include <path-to-pods.xcconfig>, but this seems to the old way to do this.
I have seen Cocoapods 0.39 is automatically generating its config files based on my configurations and adding them to my targets automatically (and this is good).
This is also confirmed by this article who is talking about a "new way" to create Podfiles. The problem is these files don't contain my configurations.
I was trying to use xcodeproj and link_with, but without success. Does anyone know what is the correct way to deal with Cocoapods + custom xcconfig files?

like image 993
pasine Avatar asked Mar 31 '16 15:03

pasine


1 Answers

The problem is that CocoaPods is based on xcconfig files and sets the actual variables. But these values cannot be used in any way when the full configuration is in xcconfig files like:

#include "../Pods/Target Support Files/Pods-Demo/Pods-Demo.debug.xcconfig"
GCC_PREPROCESSOR_DEFINITIONS = ...

In this case GCC_PREPROCESSOR_DEFINITIONS overwrites the previous value.

Here is the way to solve it:

  1. Update the Podfile to re-define the GCC_PREPROCESSOR_DEFINITIONS value with PODS_ prefix on post_install:

    post_install do |installer|
        work_dir = Dir.pwd
        Dir.glob("Pods/Target Support Files/Pods-Demo/*.xcconfig") do |xc_config_filename|
            full_path_name = "#{work_dir}/#{xc_config_filename}"
            xc_config = File.read(full_path_name)
            new_xc_config = new_xc_config.sub(/GCC_PREPROCESSOR_DEFINITIONS/, 'PODS_GCC_PREPROCESSOR_DEFINITIONS')
            File.open(full_path_name, 'w') { |file| file << new_xc_config }
        end
    
    end
    
  2. Define xcconfig file in the next way:

    #include "../Pods/Target Support Files/Pods-Demo/Pods-Demo.debug.xcconfig"
    GCC_PREPROCESSOR_DEFINITIONS = $(PODS_GCC_PREPROCESSOR_DEFINITIONS) ...
    

In this case GCC_PREPROCESSOR_DEFINITIONS should contain PODS_GCC_PREPROCESSOR_DEFINITIONS & you custom values.

like image 171
sgl0v Avatar answered Oct 19 '22 22:10

sgl0v