1module Pod
2  class PodTarget
3    private
4
5    _original_module_map_path = instance_method(:module_map_path)
6
7    public
8
9    # CocoaPods's default modulemap did not generate submodules correctly
10    # `ios/Pods/Headers/Public/React/React-Core.modulemap`
11    # ```
12    # module React {
13    #   umbrella header "React-Core-umbrella.h"
14    #
15    #   export *
16    #   module * { export * }
17    # }
18    # ```
19    # clang will generate submodules for headers relative to the umbrella header directory.
20    # https://github.com/llvm/llvm-project/blob/2782cb8da0b3c180fa7c8627cb255a026f3d25a2/clang/lib/Lex/ModuleMap.cpp#L1133
21    # In this case, it is `ios/Pods/Headers/Public/React`.
22    # But the React public headers are placed in `ios/Pods/Headers/Public/React-Core/React`, so clang cannot find the headers and generate submodules.
23    #
24    # This case happens when a pod's name different to its module name, e.g. the pod name is `React-Core` but the module name is `React` since it defines header_dir as `React`.
25    # To fix the issue, we rewrite the `module_map_path` and `umbrella_header_path` to be with the public headers,
26    # i.e. `ios/Pods/Headers/Public/React-Core/React/React-Core.modulemap` and `ios/Pods/Headers/Public/React-Core/React/React-Core-umbrella.h`
27    #
28    def rewrite_module_dir
29      # strip expo go versioning prefix
30      normalized_name = name.gsub(/^ABI\d+_\d+_\d+/, '')
31
32      if ['React-Core', 'React-RCTFabric'].include?(normalized_name) && product_module_name != name
33        return sandbox.public_headers.root + name + product_module_name
34      end
35      return nil
36    end
37
38    def umbrella_header_path
39      if dir = self.rewrite_module_dir
40        return dir + "#{label}-umbrella.h"
41      end
42      super
43    end
44
45    define_method(:module_map_path) do
46      if dir = self.rewrite_module_dir
47        return dir + "#{label}.modulemap"
48      end
49      _original_module_map_path.bind(self).()
50    end
51
52  end # class PodTarget
53end # module Pod
54