summaryrefslogtreecommitdiffstats
path: root/build/config/ios/ios_sim.py
blob: 371719d5a31f64dd0cc8106884b6988e514f1f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import errno
import os
import subprocess
import sys
import re

SIMCTL_PATH = [
  '/usr/bin/env',
  'xcrun',
  'simctl',
]

PLIST_BUDDY_PATH = [
  '/usr/bin/env',
  'xcrun',
  'PlistBuddy',
]


def ApplicationIdentifier(path):
  identifier = subprocess.check_output(PLIST_BUDDY_PATH + [
    '-c',
    'Print CFBundleIdentifier',
    '%s/Info.plist' % path,
  ])
  return identifier.strip()


def Install(args):
  return subprocess.check_call(SIMCTL_PATH + [
    'install',
    'booted',
    args.path,
  ])


def InstallLaunchAndWait(args, wait):
  res = Install(args)

  if res != 0:
    return res

  identifier = ApplicationIdentifier(args.path)

  launch_args = [ 'launch' ]

  if wait:
    launch_args += [ '-w' ]

  launch_args += [
    'booted',
    identifier,
  ]

  return subprocess.check_output(SIMCTL_PATH + launch_arg).strip()


def Launch(args):
  InstallLaunchAndWait(args, False)


def Debug(args):
  launch_res = InstallLaunchAndWait(args, True)
  launch_pid = re.search('.*: (\d+)', launch_res).group(1)
  return os.system(' '.join([
    '/usr/bin/env',
    'xcrun',
    'lldb',
    '-s',
    os.path.join(os.path.dirname(__file__), 'lldb_start_commands.txt'),
    '-p',
    launch_pid,
  ]))


def main():
  parser = argparse.ArgumentParser(description='A script that launches an'
                                   ' application in the simulator and attaches'
                                   ' the debugger to the same')

  parser.add_argument('-p', dest='path', required=True,
                      help='Path the the simulator application')

  subparsers = parser.add_subparsers()

  launch_parser = subparsers.add_parser('launch', help='Launch')
  launch_parser.set_defaults(func=Launch)

  install_parser = subparsers.add_parser('install', help='Install')
  install_parser.set_defaults(func=Install)

  debug_parser = subparsers.add_parser('debug', help='Debug')
  debug_parser.set_defaults(func=Debug)

  args = parser.parse_args()
  return args.func(args)


if __name__ == '__main__':
  sys.exit(main())