File: ide_query

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (152 lines) | stat: -rwxr-xr-x 5,459 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3
# Copyright 2024 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.
"""A script gets the information needed by lDE language services.

Expected to run it at repository root,  where top DEP, .gn etc exists.
Not intended to run by user.
See go/reqs-for-peep
"""

import argparse
import os
import re
import subprocess
import sys
import uuid

sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
                             'third_party', 'depot_tools'))
import android_build_server_helper
import gn_helper

def _retrieve_gn_args(out_dir):
  """
  Retrieves relevant GN args and calculate the defaults.
  """
  is_android = False
  use_android_build_server = True
  use_reclient = None
  if gn_helper.exists(out_dir):
    for k, v in gn_helper.args(out_dir):
      if k == "use_reclient" and v == "true":
        use_reclient = True
        continue
      if k == "use_reclient" and v == "false":
        use_reclient = False
        continue
      if k == "android_static_analysis" and v != '"build_server"':
        use_android_build_server = False
        continue
      if k == "target_os" and v == '"android"':
        is_android = True
        continue
  if use_android_build_server is None and is_android:
    use_android_build_server = True

  return use_reclient, use_android_build_server


def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('source', nargs='+',
    help=('The source file being analyzed.'
          'Multiple source arguments can be passed in order to batch '
          'process if desired.'))
  parser.add_argument('--perform-build', action='store_true',
    help=('If specified, actually build the target, including any generated '
          'prerequisite files. '
          'If --perform-build is not passed, the contents of '
          'the GeneratedFile results will only be returned if a build has '
          'been previously completed, and may be stale.'))
  parser.add_argument('--out-dir',
    help=('Output directory, containing args.gn, which specifies the build '
          'configuration.'))
  parser.add_argument('--log-dir', help=('Directory to save log files to.'))
  parser.add_argument('--format', choices=['proto', 'prototext', 'json'],
                      default='proto', help=('Output format.'))
  options = parser.parse_args()

  this_dir = os.path.dirname(__file__)
  repo_root = os.path.join(this_dir, '..', '..')

  targets = []
  use_prepare = True
  use_prepare_header_only = True
  for source in options.source:
    _, ext = os.path.splitext(source)
    if ext == '.java':
        # need to include generated *.jar for java.
        use_prepare = False
    if ext not in ('.c', '.cc', '.cxx', '.cpp', '.m', '.mm', '.S',
                   '.h', '.hxx', '.hpp', '.inc'):
        use_prepare_header_only = False
    # source is repo root (cwd) relative,
    # but siso uses out dir relative target.
    target = os.path.relpath(source, start=options.out_dir) + "^"
    targets.append(target)

  use_reclient, use_android_build_server = _retrieve_gn_args(options.out_dir)

  if use_reclient:
    # b/335795623 ide_query compiler_arguments contain non-compiler arguments
    sys.stderr.write(
        'ide_query won\'t work well with "use_reclient=true"\n'
        'Set "use_reclient=false" in args.gn.\n')
    sys.exit(1)
  if options.perform_build:
    # forget last targets of normal build as this build will update
    # .siso_fs_state.
    if os.path.exists(os.path.join(options.out_dir, '.siso_last_targets')):
        os.remove(os.path.join(options.out_dir, '.siso_last_targets'))
    args = ['siso', 'ninja']
    # use `-k=0` to build generated files as much as possible.
    args.extend([
        '-k=0',
        '-C',
        options.out_dir,
    ])
    if use_prepare:
        args.extend(['--prepare'])
    if options.log_dir:
        args.extend(['-log_dir', options.log_dir])
    args.extend(targets)
    # Set build ID for android build server.
    build_id = str(uuid.uuid4())
    os.environ.setdefault("AUTONINJA_BUILD_ID", build_id)
    env = os.environ.copy()
    if use_prepare_header_only:
        env['SISO_EXPERIMENTS'] = 'no-fast-deps,prepare-header-only,ignore-missing-targets'
    else:
        env['SISO_EXPERIMENTS'] = 'no-fast-deps,ignore-missing-targets'
    with android_build_server_helper.build_server_context(
        build_id,
        options.out_dir,
        use_android_build_server=use_android_build_server,
    ):
        with subprocess.Popen(
            args,
            cwd=repo_root,
            env=env,
            stderr=subprocess.STDOUT,
            stdout=subprocess.PIPE,
            universal_newlines=True
        ) as p:
          for line in p.stdout:
              print(line, end='', file=sys.stderr)
          # loop ends when program finishes, but must wait else returncode is None.
          p.wait()
          if p.returncode != 0:
            # TODO: report error in IdeAnalysis.Status?
            sys.stderr.write('build failed with %d\n' % p.returncode)
            # even if build fails, it should report ideanalysis back.

  args = ['siso', 'query', 'ideanalysis', '-C', options.out_dir]
  if options.format:
      args.extend(['--format', options.format])
  args.extend(targets)
  subprocess.run(args, cwd=repo_root, check=True)

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