aboutsummaryrefslogtreecommitdiff
path: root/build/apple/tweak_info_plist.py
blob: 66dfbdc63d40981ec52be364f892a928b72a3997 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python3

# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

#
# Xcode supports build variable substitutions and CPP; sadly, that doesn't work
# because:
#
# 1. Xcode wants to do the Info.plist work before it runs any build phases,
#    this means if we were to generate a .h file for INFOPLIST_PREFIX_HEADER
#    we'd have to put it in another target so it runs in time.
# 2. Xcode also doesn't check to see if the header being used as a prefix for
#    the Info.plist has changed.  So even if we updated it, it's only looking
#    at the modtime of the info.plist to see if that's changed.
#
# So, we work around all of this by making a script build phase that will run
# during the app build, and simply update the info.plist in place.  This way
# by the time the app target is done, the info.plist is correct.
#


import optparse
import os
import plistlib
import re
import subprocess
import sys
import tempfile

TOP = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

assert sys.version_info.major >= 3, "Requires python 3.0 or higher."


def _WritePlistIfChanged(plist, output_path, fmt):
  """Write a plist file.

  Write `plist` to `output_path` in `fmt`. If `output_path` already exist,
  the file is only overwritten if its content would be different. This allows
  ninja to consider all dependent step to be considered as unnecessary (see
  "restat" in ninja documentation).
  """
  if os.path.isfile(output_path):
    with open(output_path, 'rb') as f:
      try:
        exising_plist = plistlib.load(f)
        if exising_plist == plist:
          return
      except plistlib.InvalidFileException:
        # If the file cannot be parsed by plistlib, then overwrite it.
        pass

  with open(output_path, 'wb') as f:
    plist_format = {'binary1': plistlib.FMT_BINARY, 'xml1': plistlib.FMT_XML}
    plistlib.dump(plist, f, fmt=plist_format[fmt])


def _GetOutput(args):
  """Runs a subprocess and waits for termination. Returns (stdout, returncode)
  of the process. stderr is attached to the parent."""
  proc = subprocess.Popen(args, stdout=subprocess.PIPE)
  stdout, _ = proc.communicate()
  return stdout.decode('UTF-8'), proc.returncode


def _RemoveKeys(plist, *keys):
  """Removes a varargs of keys from the plist."""
  for key in keys:
    try:
      del plist[key]
    except KeyError:
      pass


def _ApplyVersionOverrides(version, keys, overrides, separator='.'):
  """Applies version overrides.

  Given a |version| string as "a.b.c.d" (assuming a default separator) with
  version components named by |keys| then overrides any value that is present
  in |overrides|.

  >>> _ApplyVersionOverrides('a.b', ['major', 'minor'], {'minor': 'd'})
  'a.d'
  """
  if not overrides:
    return version
  version_values = version.split(separator)
  for i, (key, value) in enumerate(zip(keys, version_values)):
    if key in overrides:
      version_values[i] = overrides[key]
  return separator.join(version_values)


def _GetVersion(version_format, values, overrides=None):
  """Generates a version number according to |version_format| using the values
  from |values| or |overrides| if given."""
  result = version_format
  for key in values:
    if overrides and key in overrides:
      value = overrides[key]
    else:
      value = values[key]
    result = result.replace('@%s@' % key, value)
  return result


def _AddVersionKeys(plist, version_format_for_key, version=None,
                    overrides=None):
  """Adds the product version number into the plist. Returns True on success and
  False on error. The error will be printed to stderr."""
  if not version:
    # Pull in the Chrome version number.
    VERSION_TOOL = os.path.join(TOP, 'build/util/version.py')
    VERSION_FILE = os.path.join(TOP, 'chrome/VERSION')
    (stdout, retval) = _GetOutput([
        VERSION_TOOL, '-f', VERSION_FILE, '-t',
        '@MAJOR@.@MINOR@.@BUILD@.@PATCH@'
    ])

    # If the command finished with a non-zero return code, then report the
    # error up.
    if retval != 0:
      return False

    version = stdout.strip()

  # Parse the given version number, that should be in MAJOR.MINOR.BUILD.PATCH
  # format (where each value is a number). Note that str.isdigit() returns
  # True if the string is composed only of digits (and thus match \d+ regexp).
  groups = version.split('.')
  if len(groups) != 4 or not all(element.isdigit() for element in groups):
    print('Invalid version string specified: "%s"' % version, file=sys.stderr)
    return False
  values = dict(zip(('MAJOR', 'MINOR', 'BUILD', 'PATCH'), groups))

  for key in version_format_for_key:
    plist[key] = _GetVersion(version_format_for_key[key], values, overrides)

  # Return with no error.
  return True


def _DoSCMKeys(plist, add_keys):
  """Adds the SCM information, visible in about:version, to property list. If
  |add_keys| is True, it will insert the keys, otherwise it will remove them."""
  scm_revision = None
  if add_keys:
    # Pull in the Chrome revision number.
    VERSION_TOOL = os.path.join(TOP, 'build/util/version.py')
    LASTCHANGE_FILE = os.path.join(TOP, 'build/util/LASTCHANGE')
    (stdout, retval) = _GetOutput(
        [VERSION_TOOL, '-f', LASTCHANGE_FILE, '-t', '@LASTCHANGE@'])
    if retval:
      return False
    scm_revision = stdout.rstrip()

  # See if the operation failed.
  _RemoveKeys(plist, 'SCMRevision')
  if scm_revision != None:
    plist['SCMRevision'] = scm_revision
  elif add_keys:
    print('Could not determine SCM revision.  This may be OK.', file=sys.stderr)

  return True


def _AddBreakpadKeys(plist, branding, platform, staging):
  """Adds the Breakpad keys. This must be called AFTER _AddVersionKeys() and
  also requires the |branding| argument."""
  plist['BreakpadReportInterval'] = '3600'  # Deliberately a string.
  plist['BreakpadProduct'] = '%s_%s' % (branding, platform)
  plist['BreakpadProductDisplay'] = branding
  if staging:
    plist['BreakpadURL'] = 'https://clients2.google.com/cr/staging_report'
  else:
    plist['BreakpadURL'] = 'https://clients2.google.com/cr/report'

  # These are both deliberately strings and not boolean.
  plist['BreakpadSendAndExit'] = 'YES'
  plist['BreakpadSkipConfirm'] = 'YES'


def _RemoveBreakpadKeys(plist):
  """Removes any set Breakpad keys."""
  _RemoveKeys(plist, 'BreakpadURL', 'BreakpadReportInterval', 'BreakpadProduct',
              'BreakpadProductDisplay', 'BreakpadVersion',
              'BreakpadSendAndExit', 'BreakpadSkipConfirm')


def _TagSuffixes():
  # Keep this list sorted in the order that tag suffix components are to
  # appear in a tag value. That is to say, it should be sorted per ASCII.
  components = ('full', )
  assert tuple(sorted(components)) == components

  components_len = len(components)
  combinations = 1 << components_len
  tag_suffixes = []
  for combination in range(0, combinations):
    tag_suffix = ''
    for component_index in range(0, components_len):
      if combination & (1 << component_index):
        tag_suffix += '-' + components[component_index]
    tag_suffixes.append(tag_suffix)
  return tag_suffixes


def _AddKeystoneKeys(plist, bundle_identifier, base_tag):
  """Adds the Keystone keys. This must be called AFTER _AddVersionKeys() and
  also requires the |bundle_identifier| argument (com.example.product)."""
  plist['KSVersion'] = plist['CFBundleShortVersionString']
  plist['KSProductID'] = bundle_identifier
  plist['KSUpdateURL'] = 'https://tools.google.com/service/update2'

  _RemoveKeys(plist, 'KSChannelID')
  if base_tag != '':
    plist['KSChannelID'] = base_tag
  for tag_suffix in _TagSuffixes():
    if tag_suffix:
      plist['KSChannelID' + tag_suffix] = base_tag + tag_suffix


def _RemoveKeystoneKeys(plist):
  """Removes any set Keystone keys."""
  _RemoveKeys(plist, 'KSVersion', 'KSProductID', 'KSUpdateURL')

  tag_keys = ['KSChannelID']
  for tag_suffix in _TagSuffixes():
    tag_keys.append('KSChannelID' + tag_suffix)
  _RemoveKeys(plist, *tag_keys)


def _AddGTMKeys(plist, platform):
  """Adds the GTM metadata keys. This must be called AFTER _AddVersionKeys()."""
  plist['GTMUserAgentID'] = plist['CFBundleName']
  if platform == 'ios':
    plist['GTMUserAgentVersion'] = plist['CFBundleVersion']
  else:
    plist['GTMUserAgentVersion'] = plist['CFBundleShortVersionString']


def _RemoveGTMKeys(plist):
  """Removes any set GTM metadata keys."""
  _RemoveKeys(plist, 'GTMUserAgentID', 'GTMUserAgentVersion')


def _AddPrivilegedHelperId(plist, privileged_helper_id):
  plist['SMPrivilegedExecutables'] = {
      privileged_helper_id: 'identifier ' + privileged_helper_id
  }


def _RemovePrivilegedHelperId(plist):
  _RemoveKeys(plist, 'SMPrivilegedExecutables')


def Main(argv):
  parser = optparse.OptionParser('%prog [options]')
  parser.add_option('--plist',
                    dest='plist_path',
                    action='store',
                    type='string',
                    default=None,
                    help='The path of the plist to tweak.')
  parser.add_option('--output', dest='plist_output', action='store',
      type='string', default=None, help='If specified, the path to output ' + \
      'the tweaked plist, rather than overwriting the input.')
  parser.add_option('--breakpad',
                    dest='use_breakpad',
                    action='store',
                    type='int',
                    default=False,
                    help='Enable Breakpad [1 or 0]')
  parser.add_option(
      '--breakpad_staging',
      dest='use_breakpad_staging',
      action='store_true',
      default=False,
      help='Use staging breakpad to upload reports. Ignored if --breakpad=0.')
  parser.add_option('--keystone',
                    dest='use_keystone',
                    action='store',
                    type='int',
                    default=False,
                    help='Enable Keystone [1 or 0]')
  parser.add_option('--keystone-base-tag',
                    default='',
                    help='Base Keystone tag to set')
  parser.add_option('--scm',
                    dest='add_scm_info',
                    action='store',
                    type='int',
                    default=True,
                    help='Add SCM metadata [1 or 0]')
  parser.add_option('--branding',
                    dest='branding',
                    action='store',
                    type='string',
                    default=None,
                    help='The branding of the binary')
  parser.add_option('--bundle_id',
                    dest='bundle_identifier',
                    action='store',
                    type='string',
                    default=None,
                    help='The bundle id of the binary')
  parser.add_option('--platform',
                    choices=('ios', 'mac'),
                    default='mac',
                    help='The target platform of the bundle')
  parser.add_option('--add-gtm-metadata',
                    dest='add_gtm_info',
                    action='store',
                    type='int',
                    default=False,
                    help='Add GTM metadata [1 or 0]')
  parser.add_option(
      '--version-overrides',
      action='append',
      help='Key-value pair to override specific component of version '
      'like key=value (can be passed multiple time to configure '
      'more than one override)')
  parser.add_option('--format',
                    choices=('binary1', 'xml1'),
                    default='xml1',
                    help='Format to use when writing property list '
                    '(default: %(default)s)')
  parser.add_option('--version',
                    dest='version',
                    action='store',
                    type='string',
                    default=None,
                    help='The version string [major.minor.build.patch]')
  parser.add_option('--privileged_helper_id',
                    dest='privileged_helper_id',
                    action='store',
                    type='string',
                    default=None,
                    help='The id of the privileged helper executable.')
  (options, args) = parser.parse_args(argv)

  if len(args) > 0:
    print(parser.get_usage(), file=sys.stderr)
    return 1

  if not options.plist_path:
    print('No --plist specified.', file=sys.stderr)
    return 1

  # Read the plist into its parsed format.
  with open(options.plist_path, 'rb') as f:
    plist = plistlib.load(f)

  # Convert overrides.
  overrides = {}
  if options.version_overrides:
    for pair in options.version_overrides:
      if not '=' in pair:
        print('Invalid value for --version-overrides:', pair, file=sys.stderr)
        return 1
      key, value = pair.split('=', 1)
      overrides[key] = value
      if key not in ('MAJOR', 'MINOR', 'BUILD', 'PATCH'):
        print('Unsupported key for --version-overrides:', key, file=sys.stderr)
        return 1

  if options.platform == 'mac':
    version_format_for_key = {
        # Add public version info so "Get Info" works.
        'CFBundleShortVersionString': '@MAJOR@.@MINOR@.@BUILD@.@PATCH@',

        # Honor the 429496.72.95 limit.  The maximum comes from splitting
        # 2^32 - 1 into  6, 2, 2 digits.  The limitation was present in Tiger,
        # but it could have been fixed in later OS release, but hasn't been
        # tested (it's easy enough to find out with "lsregister -dump).
        # http://lists.apple.com/archives/carbon-dev/2006/Jun/msg00139.html
        # BUILD will always be an increasing value, so BUILD_PATH gives us
        # something unique that meetings what LS wants.
        'CFBundleVersion': '@BUILD@.@PATCH@',
    }
  else:
    version_format_for_key = {
        'CFBundleShortVersionString': '@MAJOR@.@BUILD@.@PATCH@',
        'CFBundleVersion': '@MAJOR@.@MINOR@.@BUILD@.@PATCH@'
    }

  if options.use_breakpad:
    version_format_for_key['BreakpadVersion'] = \
        '@MAJOR@.@MINOR@.@BUILD@.@PATCH@'

  # Insert the product version.
  if not _AddVersionKeys(plist,
                         version_format_for_key,
                         version=options.version,
                         overrides=overrides):
    return 2

  # Add Breakpad if configured to do so.
  if options.use_breakpad:
    if options.branding is None:
      print('Use of Breakpad requires branding.', file=sys.stderr)
      return 1
    # Map "target_os" passed from gn via the --platform parameter
    # to the platform as known by breakpad.
    platform = {'mac': 'Mac', 'ios': 'iOS'}[options.platform]
    _AddBreakpadKeys(plist, options.branding, platform,
                     options.use_breakpad_staging)
  else:
    _RemoveBreakpadKeys(plist)

  # Add Keystone if configured to do so.
  if options.use_keystone:
    if options.bundle_identifier is None:
      print('Use of Keystone requires the bundle id.', file=sys.stderr)
      return 1
    _AddKeystoneKeys(plist, options.bundle_identifier,
                     options.keystone_base_tag)
  else:
    _RemoveKeystoneKeys(plist)

  # Adds or removes any SCM keys.
  if not _DoSCMKeys(plist, options.add_scm_info):
    return 3

  # Add GTM metadata keys.
  if options.add_gtm_info:
    _AddGTMKeys(plist, options.platform)
  else:
    _RemoveGTMKeys(plist)

  # Add SMPrivilegedExecutables keys.
  if options.privileged_helper_id:
    _AddPrivilegedHelperId(plist, options.privileged_helper_id)
  else:
    _RemovePrivilegedHelperId(plist)

  output_path = options.plist_path
  if options.plist_output is not None:
    output_path = options.plist_output

  # Now that all keys have been mutated, rewrite the file.
  # Convert Info.plist to the format requested by the --format flag. Any
  # format would work on Mac but iOS requires specific format.
  _WritePlistIfChanged(plist, output_path, options.format)


if __name__ == '__main__':
  # TODO(crbug.com/40618161): Temporary workaround until all scripts use
  # python3 by default.
  if sys.version_info[0] < 3:
    os.execvp('python3', ['python3'] + sys.argv)
  sys.exit(Main(sys.argv[1:]))