Generate module.modulemap file when building Mac Framework

Without this file, the Framework can't be used by Swift projects.

Bug: webrtc:9142
Change-Id: I8803ec8b194dc116e133257e205f4620bb34a692
Reviewed-on: https://webrtc-review.googlesource.com/c/103340
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25047}
diff --git a/AUTHORS b/AUTHORS
index 6b0afd8..e24dd51 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -97,4 +97,5 @@
 Miguel Paris <mparisdiaz@gmail.com>
 Vewd Software AS <*@vewd.com>
 Highfive, Inc. <*@highfive.com>
-CoSMo Software Consulting, Pte Ltd <*@cosmosoftware.io>
\ No newline at end of file
+CoSMo Software Consulting, Pte Ltd <*@cosmosoftware.io>
+Tuple, LLC <*@tuple.app>
diff --git a/tools_webrtc/ios/generate_modulemap.py b/tools_webrtc/ios/generate_modulemap.py
new file mode 100644
index 0000000..45bd3d8
--- /dev/null
+++ b/tools_webrtc/ios/generate_modulemap.py
@@ -0,0 +1,32 @@
+# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
+#
+# Use of this source code is governed by a BSD-style license
+# that can be found in the LICENSE file in the root of the source
+# tree. An additional intellectual property rights grant can be found
+# in the file PATENTS.  All contributing project authors may
+# be found in the AUTHORS file in the root of the source tree.
+
+import argparse
+import sys
+
+def GenerateModulemap():
+  parser = argparse.ArgumentParser(description='Generate modulemap')
+  parser.add_argument("-o", "--out", type=str, help="Output file.")
+  parser.add_argument("-n", "--name", type=str, help="Name of binary.")
+
+  args = parser.parse_args()
+
+  with open(args.out, "w") as outfile:
+    module_template = 'framework module %s {\n' \
+                      '  umbrella header "%s.h"\n' \
+                      '\n' \
+                      '  export *\n' \
+                      '  module * { export * }\n' \
+                      '}\n' % (args.name, args.name)
+    outfile.write(module_template)
+  return 0
+
+
+if __name__ == '__main__':
+  sys.exit(GenerateModulemap())
+
diff --git a/webrtc.gni b/webrtc.gni
index 6ecf859..71c830e 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -649,6 +649,8 @@
     }
   }
 
+  # TODO: Generate module.modulemap file to enable use in Swift
+  # projects. See "mac_framework_bundle_with_umbrella_header".
   template("ios_framework_bundle_with_umbrella_header") {
     forward_variables_from(invoker, [ "output_name" ])
     umbrella_header_path =
@@ -699,12 +701,17 @@
     forward_variables_from(invoker, [ "output_name" ])
     this_target_name = target_name
     umbrella_header_path = "$target_gen_dir/umbrella_header/$output_name.h"
+    modulemap_path = "$target_gen_dir/Modules/module.modulemap"
 
     mac_framework_bundle(target_name) {
       forward_variables_from(invoker, "*", [])
 
       framework_version = "A"
-      framework_contents = [ "Headers" ]
+      framework_contents = [
+        "Headers",
+        "Modules",
+        "Resources",
+      ]
 
       ldflags = [
         "-all_load",
@@ -714,7 +721,9 @@
 
       deps += [
         ":copy_framework_headers_$this_target_name",
+        ":copy_modulemap_$this_target_name",
         ":copy_umbrella_header_$this_target_name",
+        ":modulemap_$this_target_name",
         ":umbrella_header_$this_target_name",
       ]
     }
@@ -727,6 +736,31 @@
       ]
     }
 
+    action("modulemap_$this_target_name") {
+      script = "//tools_webrtc/ios/generate_modulemap.py"
+      args = [
+        "--out",
+        rebase_path(modulemap_path, root_build_dir),
+        "--name",
+        output_name,
+      ]
+      outputs = [
+        modulemap_path,
+      ]
+    }
+
+    bundle_data("copy_modulemap_$this_target_name") {
+      sources = [
+        modulemap_path,
+      ]
+      outputs = [
+        "{{bundle_contents_dir}}/Modules/module.modulemap",
+      ]
+      deps = [
+        ":modulemap_$this_target_name",
+      ]
+    }
+
     action("umbrella_header_$this_target_name") {
       forward_variables_from(invoker, [ "sources" ])