Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/react-native/scripts/cocoapods/__tests__/spm-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,16 @@ def test_signature_cleanup_is_added_only_to_pods_not_built_into_the_shared_produ
assert_loadable_project(project.path)
end

def test_module_map_references_use_the_module_name_of_pods_built_into_the_shared_products_dir
def test_swift_compatibility_headers_and_module_maps_stay_in_each_pods_directory
project = build_project(0)
project.new_target(:static_library, "rnmapbox-maps", :ios)
pods = { POD_NAME => POD_NAME, "rnmapbox-maps" => "rnmapbox_maps" }
pods.each_key do |name|
target = project.targets.find { |t| t.name == name }
phase = target.new_shell_script_build_phase("Copy generated compatibility header")
phase.output_paths = ['${BUILT_PRODUCTS_DIR}/${PRODUCT_MODULE_NAME}.modulemap']
phase.shell_script = 'echo "${BUILT_PRODUCTS_DIR}/${PRODUCT_MODULE_NAME}.modulemap"'
end
project.save
manager = SPMManager.new
[POD_NAME, "rnmapbox-maps"].each do |pod_name|
Expand All @@ -230,7 +237,18 @@ def test_module_map_references_use_the_module_name_of_pods_built_into_the_shared

manager.apply_on_post_install(InstallerStub.new(project, [aggregate_target]))

expected = flags % ["#{POD_NAME}.modulemap", "rnmapbox_maps.modulemap"]
pods.each do |name, mod|
target = project.targets.find { |t| t.name == name }
phase = target.shell_script_build_phases.find { |p| p.name == "Copy generated compatibility header" }
env = {
'BUILT_PRODUCTS_DIR' => TMP_DIR, 'PODS_CONFIGURATION_BUILD_DIR' => TMP_DIR,
'PRODUCT_MODULE_NAME' => mod, 'TARGET_NAME' => name,
}
expected_path = "#{TMP_DIR}/#{name}/#{mod}.modulemap"
assert_equal(expected_path, IO.popen(env, ['sh', '-c', phase.shell_script], &:read).strip)
assert_equal(expected_path, phase.output_paths.first.gsub(/\$\{(\w+)\}/) { env.fetch($1) })
end
expected = flags % ["#{POD_NAME}/#{POD_NAME}.modulemap", "rnmapbox-maps/rnmapbox_maps.modulemap"]
assert_equal(expected, config.attributes["OTHER_CFLAGS"])
assert_equal(expected, config.attributes["OTHER_SWIFT_FLAGS"])
end
Expand Down
38 changes: 9 additions & 29 deletions packages/react-native/scripts/cocoapods/spm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ def apply_on_post_install(installer)
end
end
end
keep_compatibility_headers_in_pod_dir(target) if flattened_pod_names.include?(pod_name)
end

rewrite_aggregate_modulemap_references(installer, flattened_pod_names) unless flattened_pod_names.empty?

unless @dependencies_by_pod.empty?
log 'Embedding dynamic frameworks of Swift packages'
add_embed_frameworks(installer)
Expand Down Expand Up @@ -141,34 +140,15 @@ def add_signature_cleanup(project, pod_names)
end
end

# Flattening a pod's build dir moves its generated modulemap from
# "<Pod>/<Module>.modulemap" to "<Module>.modulemap"; the aggregate xcconfigs
# reference the old path via -fmodule-map-file. The module name differs from
# the pod name when the pod name isn't a C identifier (e.g. "rnmapbox-maps").
# Must mutate the in-memory Config attributes — later post_install steps
# (NewArchitectureHelper) re-save the same Config objects and would clobber a
# plain file edit.
def rewrite_aggregate_modulemap_references(installer, pod_names)
installer.aggregate_targets.each do |aggregate_target|
pod_targets = aggregate_target.pod_targets.select { |pod_target| pod_names.include?(pod_target.name) }
aggregate_target.xcconfigs.each do |config_name, config_file|
%w[OTHER_CFLAGS OTHER_SWIFT_FLAGS].each do |key|
value = config_file.attributes[key]
next unless value

updated_value = pod_targets.reduce(value) do |acc, pod_target|
module_name = pod_target.product_module_name
acc.gsub(
"${PODS_CONFIGURATION_BUILD_DIR}/#{pod_target.name}/#{module_name}.modulemap",
"${PODS_CONFIGURATION_BUILD_DIR}/#{module_name}.modulemap"
)
end
config_file.attributes[key] = updated_value if updated_value != value
end
# Swift package products share a build directory, but CocoaPods consumers expect
# each pod's module map and its umbrella header together in a per-pod directory.
def keep_compatibility_headers_in_pod_dir(target)
phase = target.shell_script_build_phases.find { |p| p.name == 'Copy generated compatibility header' }
return unless phase

config_file.save_as(aggregate_target.xcconfig_path(config_name))
end
end
pod_dir = '${PODS_CONFIGURATION_BUILD_DIR}/${TARGET_NAME}'
phase.shell_script = phase.shell_script.gsub('${BUILT_PRODUCTS_DIR}', pod_dir)
phase.output_paths = phase.output_paths.map { |path| path.gsub('${BUILT_PRODUCTS_DIR}', pod_dir) }
end

# Creates a new object in the project with a UUID guaranteed not to collide
Expand Down