Skip to content

Commit

Permalink
Merge pull request #63 from trinhngocthuyen/fix/pkg_with_headers_path
Browse files Browse the repository at this point in the history
FIX: Build failures when a package has headers path
  • Loading branch information
trinhngocthuyen authored Apr 12, 2024
2 parents 5088a21 + 772529d commit 570ef46
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
36 changes: 28 additions & 8 deletions lib/cocoapods-spm/hooks/post_integrate/5.update_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ module SPM
class Hook
class UpdateSettings < Hook
def run
update_other_swift_flags
update_macro_plugin_flags
update_modulemap_flags
update_swift_include_paths
update_linker_flags
end

private

def other_swift_flags_by_config
@other_swift_flags_by_config ||= begin
def macro_plugin_flag_by_config
@macro_plugin_flag_by_config ||= begin
hash = user_build_configurations.keys.to_h do |config|
flags = macro_pods.keys.map do |name|
metadata = Metadata.for_pod(name)
Expand All @@ -31,13 +32,23 @@ def other_swift_flags_by_config
end
end

def update_other_swift_flags
def update_macro_plugin_flags
return if spm_config.all_macros.empty?

# For prebuilt macros
perform_settings_update(
update_targets: lambda do |_, _, config|
{ "OTHER_SWIFT_FLAGS" => other_swift_flags_by_config[config] }
{ "OTHER_SWIFT_FLAGS" => macro_plugin_flag_by_config[config] }
end
)
end

def update_modulemap_flags
perform_settings_update(
update_targets: lambda do |target, _, _|
{
"OTHER_SWIFT_FLAGS" => modulemap_args_for_target(target, prefix: "-Xcc"),
"OTHER_CFLAGS" => modulemap_args_for_target(target)
}
end
)
end
Expand Down Expand Up @@ -66,15 +77,24 @@ def linker_flags_for(target)
end

def update_swift_include_paths
return if @spm_resolver.result.spm_pkgs.empty?
return if @spm_resolver.result.spm_pkgs.empty? && spm_config.all_macros.empty?

# For macro packages
perform_settings_update(
update_targets: lambda do |_, _, _|
{ "SWIFT_INCLUDE_PATHS" => "$(PODS_CONFIGURATION_BUILD_DIR)" }
end
)
end

def modulemap_args_for_target(target, prefix: nil)
@spm_resolver
.result
.spm_products_for(target)
.reject { |p| p.headers_path.nil? }
.map { |p| "-fmodule-map-file=\"${GENERATED_MODULEMAP_DIR}/#{p.name}.modulemap\"" }
.map { |v| prefix.nil? ? v : "#{prefix} #{v}" }
.join(" ")
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/cocoapods-spm/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def products
raw["products"]
end

def product_names_of_target(name)
names = products.select { |h| h["targets"].include?(name) }.map { |h| h["name"] }
names.empty? ? [name] : names
end

def targets_of_type(type)
targets.select { |t| t["type"] == type }
end
Expand Down
3 changes: 2 additions & 1 deletion lib/cocoapods-spm/resolver/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module Pod
module SPM
class Resolver
class Product
attr_reader :pkg, :name, :linkage
attr_reader :pkg, :name, :linkage, :headers_path

def initialize(options = {})
@pkg = options[:pkg]
@name = options[:name]
@linkage = options.fetch(:linkage, :static)
@headers_path = options[:headers_path]
end

def inspect
Expand Down
51 changes: 31 additions & 20 deletions lib/cocoapods-spm/resolver/product_dep_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def initialize(podfile, result)

def resolve
generate_metadata
resolve_dynamic_products
resolve_headers_path_by_target
resolve_product_deps
end

Expand All @@ -36,6 +38,29 @@ def generate_metadata
end
end

def resolve_dynamic_products
@dynamic_products ||= Set.new
@result.metadata_cache.each_value do |metadata|
metadata.products.each do |h|
library_types = h.fetch("type", {}).fetch("library", [])
@dynamic_products << h["name"] if library_types.include?("dynamic")
end
end
end

def resolve_headers_path_by_target
@headers_path_by_product ||= {}
@result.metadata_cache.each_value do |metadata|
metadata.targets.each do |h|
next unless h.key?("publicHeadersPath")

metadata.product_names_of_target(h["name"]).each do |name|
@headers_path_by_product[name] = h["publicHeadersPath"]
end
end
end
end

def resolve_product_deps
@result.spm_dependencies_by_target.values.flatten.uniq(&:name).each do |dep|
verify_product_exists_in_pkg(dep.pkg.name, dep.product)
Expand Down Expand Up @@ -87,26 +112,12 @@ def product_from_hash(hash, metadata)
end

def create_product(pkg, name)
Product.new(pkg: pkg, name: name, linkage: linkage_of(pkg, name))
end

def linkage_of(pkg, name)
@cache_linkage ||= {}
return @cache_linkage[name] if @cache_linkage.key?(name)

@cache_linkage[name] =
if @result
.metadata_of(pkg)
.products
.find { |h| h["name"] == name }
.to_h
.fetch("type", {})
.fetch("library", [])
.include?("dynamic")
:dynamic
else
:static
end
Product.new(
pkg: pkg,
name: name,
linkage: @dynamic_products.include?(name) ? :dynamic : :static,
headers_path: @headers_path_by_product[name]
)
end
end
end
Expand Down

0 comments on commit 570ef46

Please sign in to comment.