gn: iOS packaging script

This is enough to run DM on my iPad.

I've tweaks DM so that it can run as built by both GN and GYP.
When we kill off GYP, all the dm_main() nonsense goes away.

CQ_INCLUDE_TRYBOTS=skia.primary:Test-iOS-Clang-iPadMini4-GPU-GX6450-Arm7-Debug,Build-Mac-Clang-arm64-Debug-GN_iOS

Change-Id: I59176bc203ee3180618b94ac5f9d291e0ad20b62
Reviewed-on: https://skia-review.googlesource.com/7757
Commit-Queue: Mike Klein <mtklein@chromium.org>
Reviewed-by: Hal Canary <halcanary@google.com>
diff --git a/gn/package_ios.py b/gn/package_ios.py
new file mode 100644
index 0000000..ee8ab46
--- /dev/null
+++ b/gn/package_ios.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python2.7
+#
+# Copyright 2017 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import re
+import shutil
+import subprocess
+import sys
+import tempfile
+
+# Arguments to the script:
+#  app              path to binary to package, e.g. out/Debug/dm
+#  identity         code signing identity, a long hex string
+#                   (run security find-identity -v -p codesigning | grep Google)
+#  mobileprovision  path to Google_Development.mobileprovision
+app, identity, mobileprovision = sys.argv[1:]
+
+out, app = os.path.split(app)
+
+pkg = os.path.join(out, app + '.app')
+if not os.path.exists(pkg):
+  os.mkdir(pkg)
+
+# The binary and .mobileprovision just get copied into the package.
+shutil.copy(os.path.join(out, app), pkg)
+shutil.copy(mobileprovision,
+            os.path.join(pkg, 'embedded.mobileprovision'))
+
+# Write a minimal Info.plist to name the package and point at the binary.
+with open(os.path.join(pkg, 'Info.plist'), 'w') as f:
+  f.write('''
+<plist version="1.0">
+  <dict>
+    <key>CFBundleExecutable</key> <string>{app}</string>
+    <key>CFBundleIdentifier</key> <string>com.google.{app}</string>
+  </dict>
+</plist>
+'''.format(app=app))
+
+# Extract the appliciation identitifer prefix from the .mobileprovision.
+m = re.search(r'''<key>ApplicationIdentifierPrefix</key>
+\t<array>
+\t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE)
+prefix = m.group(1)
+
+# Write a minimal entitlements file, then codesign.
+with tempfile.NamedTemporaryFile() as f:
+  f.write('''
+<plist version="1.0">
+  <dict>
+    <key>application-identifier</key> <string>{prefix}.com.google.{app}</string>
+    <key>get-task-allow</key>         <true/>
+  </dict>
+</plist>
+'''.format(prefix=prefix, app=app))
+  f.flush()
+
+  subprocess.check_call(['codesign',
+                         '--force',
+                         '--sign', identity,
+                         '--entitlements', f.name,
+                         '--timestamp=none',
+                         pkg])