Skip to content

Instantly share code, notes, and snippets.

@mrackwitz
Last active June 14, 2018 13:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrackwitz/d332145b02369f329514 to your computer and use it in GitHub Desktop.
Save mrackwitz/d332145b02369f329514 to your computer and use it in GitHub Desktop.
Force Duplicate Pod Targets by Pre Install Hook

Since CocoaPods 0.38, pod targets are de-duplicated across multiple user targets. But this led to the issue that if the pod's source files rely on precompiler definitions to determine conditional API availability, then this can't be re-exposed through the de-duplication in individual variants for each integrating target.

This could occur e.g. when a pod offers a limited extension API (APPLICATION_EXTENSION_API_ONLY), but is used for the app and an extension. The app should then be able to use full API. This was given before CocoaPods 0.38, because the pod target was generated multiple times and so also built multiple times. The feature effectively restricts those pods to have only the limited API available for all targets depending on that pod.

Please note that this only applies as long as the implementation files contain conditional source code. Differences in the header files are retained after the build and exposed to integrating targets.

A subspec for extensions definining the precompiler definitions for the pod_target_xcconfig and user_target_xcconfig is one way to avoid that issue, because the CocoaPods analyzer will recognize that the pod is used with different sets of subspecs and will duplicate the pod targets by itself.

If you need this hook in your Podfile:

  1. Please inform the owner of the podspec about your issues.
  2. Comment on this Gist, so that the CocoaPods team can figure out whether force duplicating individual pods is something needed in general and in long-term and should get enhanced support in the Podfile DSL.
# This will enforce that the pods with the names in the list below will be duplicated.
targets_to_duplicate = %w(AFNetworking)
pre_install do |installer|
pod_targets = installer.pod_targets.flat_map do |pod_target|
targets_to_duplicate.include?(pod_target.name) ? pod_target.scoped : pod_target
end
installer.aggregate_targets.each do |aggregate_target|
aggregate_target.pod_targets = pod_targets.select do |pod_target|
pod_target.target_definitions.include?(aggregate_target.target_definition)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment