aboutsummaryrefslogtreecommitdiff
path: root/scripts/incremental_build/cuj_catalog.py
blob: e2d4868e9bb142b0618302a81bb552d94f2db419 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import dataclasses
import enum
import functools
import io
import logging
import os
import shutil
import tempfile
import textwrap
import uuid
from enum import Enum
from pathlib import Path
from typing import Callable, Optional
from typing import TypeAlias

import util
import ui

"""
Provides some representative CUJs. If you wanted to manually run something but
would like the metrics to be collated in the metrics.csv file, use
`perf_metrics.py` as a stand-alone after your build.
"""


class BuildResult(Enum):
  SUCCESS = enum.auto()
  FAILED = enum.auto()
  TEST_FAILURE = enum.auto()


Action: TypeAlias = Callable[[], None]
Verifier: TypeAlias = Callable[[], None]


def skip_when_soong_only(func: Verifier) -> Verifier:
  """A decorator for Verifiers that are not applicable to soong-only builds"""

  def wrapper():
    if InWorkspace.ws_counterpart(util.get_top_dir()).exists():
      func()

  return wrapper


@skip_when_soong_only
def verify_symlink_forest_has_only_symlink_leaves():
  """Verifies that symlink forest has only symlinks or directories but no
  files except for merged BUILD.bazel files"""

  top_in_ws = InWorkspace.ws_counterpart(util.get_top_dir())
  def helper(d: Path):
    for child in os.scandir(d):
      child_path: Path = Path(child.path)
      if child_path.name == 'symlink_forest_version' and d == top_in_ws:
         continue
      if child_path.is_symlink():
        continue
      if child_path.is_file() and child.name != 'BUILD.bazel':
        # only "merged" BUILD.bazel files expected
        raise AssertionError(f'{child_path} is an unexpected file')
      if child_path.is_dir():
        helper(child_path)

  helper(top_in_ws)
  logging.info('VERIFIED Symlink Forest has no real files except BUILD.bazel')


@dataclasses.dataclass(frozen=True)
class CujStep:
  verb: str
  """a human-readable description"""
  apply_change: Action
  """user action(s) that are performed prior to a build attempt"""
  verify: Verifier = verify_symlink_forest_has_only_symlink_leaves
  """post-build assertions, i.e. tests.
  Should raise `Exception` for failures.
  """


@dataclasses.dataclass(frozen=True)
class CujGroup:
  """A sequence of steps to be performed, such that at the end of all steps the
  initial state of the source tree is attained.
  NO attempt is made to achieve atomicity programmatically. It is left as the
  responsibility of the user.
  """
  description: str
  steps: list[CujStep]

  def __str__(self) -> str:
    if len(self.steps) < 2:
      return f'{self.steps[0].verb} {self.description}'.strip()
    return ' '.join(
      [f'({chr(ord("a") + i)}) {step.verb} {self.description}'.strip() for
       i, step in enumerate(self.steps)])


class InWorkspace(Enum):
  """For a given file in the source tree, the counterpart in the symlink forest
   could be one of these kinds.
  """
  SYMLINK = enum.auto()
  NOT_UNDER_SYMLINK = enum.auto()
  UNDER_SYMLINK = enum.auto()
  OMISSION = enum.auto()

  @staticmethod
  def ws_counterpart(src_path: Path) -> Path:
    return util.get_out_dir().joinpath('soong/workspace').joinpath(
      de_src(src_path))

  def verifier(self, src_path: Path) -> Verifier:
    @skip_when_soong_only
    def f():
      ws_path = InWorkspace.ws_counterpart(src_path)
      actual: Optional[InWorkspace] = None
      if ws_path.is_symlink():
        actual = InWorkspace.SYMLINK
        if not ws_path.exists():
          logging.warning('Dangling symlink %s', ws_path)
      elif not ws_path.exists():
        actual = InWorkspace.OMISSION
      else:
        for p in ws_path.parents:
          if not p.is_relative_to(util.get_out_dir()):
            actual = InWorkspace.NOT_UNDER_SYMLINK
            break
          if p.is_symlink():
            actual = InWorkspace.UNDER_SYMLINK
            break

      if self != actual:
        raise AssertionError(
          f'{ws_path} expected {self.name} but got {actual.name}')
      logging.info(f'VERIFIED {de_src(ws_path)} {self.name}')

    return f


def de_src(p: Path) -> str:
  return str(p.relative_to(util.get_top_dir()))


def src(p: str) -> Path:
  return util.get_top_dir().joinpath(p)


def modify_revert(file: Path, text: str = '//BOGUS line\n') -> CujGroup:
  """
  :param file: the file to be modified and reverted
  :param text: the text to be appended to the file to modify it
  :return: A pair of CujSteps, where the first modifies the file and the
  second reverts the modification
  """
  if not file.exists():
    raise RuntimeError(f'{file} does not exist')

  def add_line():
    with open(file, mode="a") as f:
      f.write(text)

  def revert():
    with open(file, mode="rb+") as f:
      # assume UTF-8
      f.seek(-len(text), io.SEEK_END)
      f.truncate()

  return CujGroup(de_src(file), [
    CujStep('modify', add_line),
    CujStep('revert', revert)
  ])


def create_delete(file: Path, ws: InWorkspace,
    text: str = '//Test File: safe to delete\n') -> CujGroup:
  """
  :param file: the file to be created and deleted
  :param ws: the expectation for the counterpart file in symlink
  forest (aka the synthetic bazel workspace) when its created
  :param text: the content of the file
  :return: A pair of CujSteps, where the fist creates the file and the
  second deletes it
  """
  missing_dirs = [f for f in file.parents if not f.exists()]
  shallowest_missing_dir = missing_dirs[-1] if len(missing_dirs) else None

  def create():
    if file.exists():
      raise RuntimeError(
        f'File {file} already exists. Interrupted an earlier run?\n'
        'TIP: `repo status` and revert changes!!!')
    file.parent.mkdir(parents=True, exist_ok=True)
    file.touch(exist_ok=False)
    with open(file, mode="w") as f:
      f.write(text)

  def delete():
    if shallowest_missing_dir:
      shutil.rmtree(shallowest_missing_dir)
    else:
      file.unlink(missing_ok=False)

  return CujGroup(de_src(file), [
    CujStep('create', create, ws.verifier(file)),
    CujStep('delete', delete, InWorkspace.OMISSION.verifier(file)),
  ])


def create_delete_bp(bp_file: Path) -> CujGroup:
  """
  This is basically the same as "create_delete" but with canned content for
  an Android.bp file.
  """
  return create_delete(
    bp_file, InWorkspace.SYMLINK,
    'filegroup { name: "test-bogus-filegroup", srcs: ["**/*.md"] }')


def delete_restore(original: Path, ws: InWorkspace) -> CujGroup:
  """
  :param original: The file to be deleted then restored
  :param ws: When restored, expectation for the file's counterpart in the
  symlink forest (aka synthetic bazel workspace)
  :return: A pair of CujSteps, where the first deletes a file and the second
  restores it
  """
  tempdir = Path(tempfile.gettempdir())
  if tempdir.is_relative_to(util.get_top_dir()):
    raise SystemExit(f'Temp dir {tempdir} is under source tree')
  if tempdir.is_relative_to(util.get_out_dir()):
    raise SystemExit(f'Temp dir {tempdir} is under '
                     f'OUT dir {util.get_out_dir()}')
  copied = tempdir.joinpath(f'{original.name}-{uuid.uuid4()}.bak')

  def move_to_tempdir_to_mimic_deletion():
    logging.warning('MOVING %s TO %s', de_src(original), copied)
    original.rename(copied)

  return CujGroup(de_src(original), [
    CujStep('delete',
            move_to_tempdir_to_mimic_deletion,
            InWorkspace.OMISSION.verifier(original)),
    CujStep('restore',
            lambda: copied.rename(original),
            ws.verifier(original))
  ])


def replace_link_with_dir(p: Path):
  """Create a file, replace it with a non-empty directory, delete it"""
  cd = create_delete(p, InWorkspace.SYMLINK)
  create_file: CujStep
  delete_file: CujStep
  create_file, delete_file, *tail = cd.steps
  assert len(tail) == 0

  # an Android.bp is always a symlink in the workspace and thus its parent
  # will be a directory in the workspace
  create_dir: CujStep
  delete_dir: CujStep
  create_dir, delete_dir, *tail = create_delete_bp(
    p.joinpath('Android.bp')).steps
  assert len(tail) == 0

  def replace_it():
    delete_file.apply_change()
    create_dir.apply_change()

  return CujGroup(cd.description, [
    create_file,
    CujStep(f'{de_src(p)}/Android.bp instead of',
            replace_it,
            create_dir.verify),
    delete_dir
  ])


def _sequence(*vs: Verifier) -> Verifier:
  def f():
    for v in vs:
      v()

  return f


def content_verfiers(
    ws_build_file: Path, content: str) -> (Verifier, Verifier):
  def search() -> bool:
    with open(ws_build_file, "r") as f:
      for line in f:
        if line == content:
          return True
    return False

  @skip_when_soong_only
  def contains():
    if not search():
      raise AssertionError(
        f'{de_src(ws_build_file)} expected to contain {content}')
    logging.info(f'VERIFIED {de_src(ws_build_file)} contains {content}')

  @skip_when_soong_only
  def does_not_contain():
    if search():
      raise AssertionError(
        f'{de_src(ws_build_file)} not expected to contain {content}')
    logging.info(f'VERIFIED {de_src(ws_build_file)} does not contain {content}')

  return contains, does_not_contain


def modify_revert_kept_build_file(build_file: Path) -> CujGroup:
  content = f'//BOGUS {uuid.uuid4()}\n'
  step1, step2, *tail = modify_revert(build_file, content).steps
  assert len(tail) == 0
  ws_build_file = InWorkspace.ws_counterpart(build_file).with_name(
    'BUILD.bazel')
  merge_prover, merge_disprover = content_verfiers(ws_build_file, content)
  return CujGroup(de_src(build_file), [
    CujStep(step1.verb,
            step1.apply_change,
            _sequence(step1.verify, merge_prover)),
    CujStep(step2.verb,
            step2.apply_change,
            _sequence(step2.verify, merge_disprover))
  ])


def create_delete_kept_build_file(build_file: Path) -> CujGroup:
  content = f'//BOGUS {uuid.uuid4()}\n'
  ws_build_file = InWorkspace.ws_counterpart(build_file).with_name(
    'BUILD.bazel')
  if build_file.name == 'BUILD.bazel':
    ws = InWorkspace.NOT_UNDER_SYMLINK
  elif build_file.name == 'BUILD':
    ws = InWorkspace.SYMLINK
  else:
    raise RuntimeError(f'Illegal name for a build file {build_file}')

  merge_prover, merge_disprover = content_verfiers(ws_build_file, content)

  step1: CujStep
  step2: CujStep
  step1, step2, *tail = create_delete(build_file, ws, content).steps
  assert len(tail) == 0
  return CujGroup(de_src(build_file), [
    CujStep(step1.verb,
            step1.apply_change,
            _sequence(step1.verify, merge_prover)),
    CujStep(step2.verb,
            step2.apply_change,
            _sequence(step2.verify, merge_disprover))
  ])


def create_delete_unkept_build_file(build_file) -> CujGroup:
  content = f'//BOGUS {uuid.uuid4()}\n'
  ws_build_file = InWorkspace.ws_counterpart(build_file).with_name(
    'BUILD.bazel')
  step1: CujStep
  step2: CujStep
  step1, step2, *tail = create_delete(
    build_file, InWorkspace.SYMLINK, content).steps
  assert len(tail) == 0
  _, merge_disprover = content_verfiers(ws_build_file, content)
  return CujGroup(de_src(build_file), [
    CujStep(step1.verb,
            step1.apply_change,
            _sequence(step1.verify, merge_disprover)),
    CujStep(step2.verb,
            step2.apply_change,
            _sequence(step2.verify, merge_disprover))
  ])


NON_LEAF = '*/*'
"""If `a/*/*` is a valid path `a` is not a leaf directory"""
LEAF = '!*/*'
"""If `a/*/*` is not a valid path `a` is a leaf directory, i.e. has no other
non-empty sub-directories"""
PKG = ['Android.bp', '!BUILD', '!BUILD.bazel']
"""limiting the candidate to Android.bp file with no sibling bazel files"""
PKG_FREE = ['!**/Android.bp', '!**/BUILD', '!**/BUILD.bazel']
"""no Android.bp or BUILD or BUILD.bazel file anywhere"""


def _kept_build_cujs() -> list[CujGroup]:
  # Bp2BuildKeepExistingBuildFile(build/bazel) is True(recursive)
  kept = src('build/bazel')
  pkg = util.any_dir_under(kept, *PKG)
  examples = [pkg.joinpath('BUILD'),
              pkg.joinpath('BUILD.bazel')]

  return [
    *[create_delete_kept_build_file(build_file) for build_file in examples],
    create_delete(pkg.joinpath('BUILD/kept-dir'), InWorkspace.SYMLINK),
    modify_revert_kept_build_file(util.any_file_under(kept, 'BUILD'))]


def _unkept_build_cujs() -> list[CujGroup]:
  # Bp2BuildKeepExistingBuildFile(bionic) is False(recursive)
  unkept = src('bionic')
  pkg = util.any_dir_under(unkept, *PKG)
  return [
    *[create_delete_unkept_build_file(build_file) for build_file in [
      pkg.joinpath('BUILD'),
      pkg.joinpath('BUILD.bazel'),
    ]],
    *[create_delete(build_file, InWorkspace.OMISSION) for build_file in [
      unkept.joinpath('bogus-unkept/BUILD'),
      unkept.joinpath('bogus-unkept/BUILD.bazel'),
    ]],
    create_delete(pkg.joinpath('BUILD/unkept-dir'), InWorkspace.SYMLINK)
  ]


@functools.cache
def get_cujgroups() -> list[CujGroup]:
  # we are choosing "package" directories that have Android.bp but
  # not BUILD nor BUILD.bazel because
  # we can't tell if ShouldKeepExistingBuildFile would be True or not
  pkg, p_why = util.any_match(NON_LEAF, *PKG)
  pkg_free, f_why = util.any_match(NON_LEAF, *PKG_FREE)
  leaf_pkg_free, _ = util.any_match(LEAF, *PKG_FREE)
  ancestor, a_why = util.any_match('!Android.bp', '!BUILD', '!BUILD.bazel',
                                   '**/Android.bp')
  logging.info(textwrap.dedent(f'''Choosing:
            package: {de_src(pkg)} has {p_why}
   package ancestor: {de_src(ancestor)} has {a_why} but no direct Android.bp
       package free: {de_src(pkg_free)} has {f_why} but no Android.bp anywhere
  leaf package free: {de_src(leaf_pkg_free)} has neither Android.bp nor sub-dirs
  '''))

  android_bp_cujs = [
    modify_revert(src('Android.bp')),
    *[create_delete_bp(d.joinpath('Android.bp')) for d in
      [ancestor, pkg_free, leaf_pkg_free]]
  ]
  mixed_build_launch_cujs = [
    modify_revert(src('bionic/libc/tzcode/asctime.c')),
    modify_revert(src('bionic/libc/stdio/stdio.cpp')),
    modify_revert(src('packages/modules/adb/daemon/main.cpp')),
    modify_revert(src('frameworks/base/core/java/android/view/View.java')),
  ]
  unreferenced_file_cujs = [
    *[create_delete(d.joinpath('unreferenced.txt'), InWorkspace.SYMLINK) for
      d in [ancestor, pkg]],
    *[create_delete(d.joinpath('unreferenced.txt'), InWorkspace.UNDER_SYMLINK)
      for d
      in [pkg_free, leaf_pkg_free]]
  ]

  def clean():
    if ui.get_user_input().log_dir.is_relative_to(util.get_top_dir()):
      raise AssertionError(
        f'specify a different LOG_DIR: {ui.get_user_input().log_dir}')
    if util.get_out_dir().exists():
      shutil.rmtree(util.get_out_dir())

  return [
    CujGroup('', [CujStep('clean', clean)]),
    CujGroup('', [CujStep('no change', lambda: None)]),

    create_delete(src('bionic/libc/tzcode/globbed.c'),
                  InWorkspace.UNDER_SYMLINK),

    # TODO (usta): find targets that should be affected
    *[delete_restore(f, InWorkspace.SYMLINK) for f in [
      util.any_file('version_script.txt'),
      util.any_file('AndroidManifest.xml')]],

    *unreferenced_file_cujs,
    *mixed_build_launch_cujs,
    *android_bp_cujs,
    *_unkept_build_cujs(),
    *_kept_build_cujs(),
    replace_link_with_dir(pkg.joinpath('bogus.txt')),
    # TODO(usta): add a dangling symlink
  ]


def warmup_index() -> int:
  return 1