module_map.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env ruby
  2. # Copyright 2019 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Generate a module map file from a podspec.
  16. # CocoaPods generated module maps are not appropriate because CocoaPods uses
  17. # xcconfig files to specify framework and library dependencies.
  18. # This script is currently intended for Zipfile building only for iOS.
  19. # If multi-platform support is required in the future, the consumer parameter
  20. # could be added to the command line.
  21. require 'cocoapods'
  22. # Enable ruby options after 'require' because cocoapods is noisy
  23. $VERBOSE = true # ruby -w
  24. #$DEBUG = true # ruby --debug
  25. def usage()
  26. script = File.basename($0)
  27. STDERR.puts <<~EOF
  28. USAGE: #{script} podspec output_file
  29. podspec is the podspec to generate the module map for.
  30. output_file is the file to write the modulemap.
  31. EOF
  32. end
  33. def main(args)
  34. if args.size < 2 then
  35. usage()
  36. exit(1)
  37. end
  38. podspec_file = args[0]
  39. trace('loading', podspec_file)
  40. spec = Pod::Spec.from_file(podspec_file)
  41. consumer = spec.consumer("ios")
  42. trace('generating module map for ', spec.module_name,)
  43. contents = generate(spec.module_name, consumer.frameworks, consumer.libraries)
  44. File.open(args[1], 'w') { |file| file.write(contents) }
  45. end
  46. # Generates the contents of the module.modulemap file.
  47. #
  48. # @return [String]
  49. #
  50. def generate(name, frameworks, libraries)
  51. <<~MODULE_MAP
  52. framework module #{name} {
  53. umbrella header "#{name}.h"
  54. export *
  55. module * { export * }
  56. #{frameworks.map {|framework| "link framework \"#{framework}\""}.join("\n ")}
  57. #{libraries.map {|library| "link \"#{library}\""}.join("\n ")}
  58. }
  59. MODULE_MAP
  60. end
  61. def trace(*args)
  62. return if not $DEBUG
  63. STDERR.puts(args.join(' '))
  64. end
  65. main(ARGV)