build_private_module_map.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2023 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. require 'cocoapods'
  15. def usage()
  16. script = File.basename($0)
  17. STDERR.puts <<~EOF
  18. USAGE: #{script} <podspec> [options]
  19. Generates a private module map for the given podspec. The generated module
  20. map contains the private headers listed in the podspec's
  21. 'private_header_files' attribute.
  22. This generated module map will resemble the following template:
  23. framework module $(SPEC_MODULE_NAME)_Private {
  24. header "PrivateHeader_1.h"
  25. header "PrivateHeader_2.h"
  26. header "PrivateHeader_3.h"
  27. // And so on for each of the spec's private headers...
  28. }
  29. OPTIONS:
  30. --dry-run: Prints the generated private module map to STDOUT without
  31. writing it to the filesystem.
  32. EOF
  33. end
  34. def main(args)
  35. if args.length < 1 || args.length > 2
  36. usage()
  37. exit 1
  38. end
  39. STDOUT.sync = true
  40. begin
  41. spec = Pod::Spec.from_file(args[0])
  42. rescue => e
  43. STDERR.puts "#{e}"
  44. exit(1)
  45. end
  46. private_module_map_contents = "framework module #{spec.module_name}_Private {\n"
  47. private_hdrs = spec.attributes_hash['private_header_files']
  48. # Expand all path globs to get the complete list of private headers.
  49. .flat_map { |hdr_path_glob| Dir[hdr_path_glob] }
  50. # Add each private header to the private module map's contents.
  51. .each do |hdr_path|
  52. # Note: Only the header file's name is needed in the module map.
  53. # This is because the module map evaluates its headers relative to
  54. # the private headers directory (Foo.framework/PrivateHeaders)
  55. # within the CocoaPods-generated framework.
  56. hdr_name = File.basename(hdr_path)
  57. private_module_map_contents << " header \"#{hdr_name}\"\n"
  58. end
  59. private_module_map_contents << "}\n"
  60. if args.length == 2 && args[1] == '--dry-run'
  61. STDOUT.puts private_module_map_contents
  62. else
  63. # Overwrite the private module map with the generated contents.
  64. File.write(
  65. "#{spec.module_name}/Sources/#{spec.module_name}.private.modulemap",
  66. private_module_map_contents
  67. )
  68. end
  69. end
  70. main(ARGV)