| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2024 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """A script gets the information needed by lDE language services. |
| 6 | |
| 7 | Expected to run it at repository root, where top DEP, .gn etc exists. |
| 8 | Not intended to run by user. |
| 9 | See go/reqs-for-peep |
| 10 | """ |
| 11 | |
| 12 | import argparse |
| 13 | import os |
| 14 | import re |
| 15 | import subprocess |
| 16 | import sys |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 17 | import uuid |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 18 | |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 19 | sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
| 20 | 'third_party', 'depot_tools')) |
| 21 | import android_build_server_helper |
| 22 | import gn_helper |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 23 | |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 24 | def _retrieve_gn_args(out_dir): |
| 25 | """ |
| 26 | Retrieves relevant GN args and calculate the defaults. |
| 27 | """ |
| 28 | is_android = False |
| 29 | use_android_build_server = True |
| Fumitoshi Ukai | 128f48d | 2024-06-04 08:25:20 | [diff] [blame] | 30 | use_reclient = None |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 31 | if gn_helper.exists(out_dir): |
| 32 | for k, v in gn_helper.args(out_dir): |
| 33 | if k == "use_reclient" and v == "true": |
| 34 | use_reclient = True |
| 35 | continue |
| 36 | if k == "use_reclient" and v == "false": |
| 37 | use_reclient = False |
| 38 | continue |
| 39 | if k == "android_static_analysis" and v != '"build_server"': |
| 40 | use_android_build_server = False |
| 41 | continue |
| 42 | if k == "target_os" and v == '"android"': |
| 43 | is_android = True |
| 44 | continue |
| 45 | if use_android_build_server is None and is_android: |
| 46 | use_android_build_server = True |
| 47 | |
| 48 | return use_reclient, use_android_build_server |
| Fumitoshi Ukai | 128f48d | 2024-06-04 08:25:20 | [diff] [blame] | 49 | |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 50 | |
| 51 | def main(): |
| 52 | parser = argparse.ArgumentParser() |
| Philipp Wollermann | c583010 | 2024-04-19 04:35:23 | [diff] [blame] | 53 | parser.add_argument('source', nargs='+', |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 54 | help=('The source file being analyzed.' |
| Philipp Wollermann | c583010 | 2024-04-19 04:35:23 | [diff] [blame] | 55 | 'Multiple source arguments can be passed in order to batch ' |
| 56 | 'process if desired.')) |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 57 | parser.add_argument('--perform-build', action='store_true', |
| 58 | help=('If specified, actually build the target, including any generated ' |
| 59 | 'prerequisite files. ' |
| 60 | 'If --perform-build is not passed, the contents of ' |
| 61 | 'the GeneratedFile results will only be returned if a build has ' |
| 62 | 'been previously completed, and may be stale.')) |
| 63 | parser.add_argument('--out-dir', |
| Philipp Wollermann | c583010 | 2024-04-19 04:35:23 | [diff] [blame] | 64 | help=('Output directory, containing args.gn, which specifies the build ' |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 65 | 'configuration.')) |
| Richard Wang | 02fbd2dd | 2024-12-03 08:29:31 | [diff] [blame] | 66 | parser.add_argument('--log-dir', help=('Directory to save log files to.')) |
| Fumitoshi Ukai | 3788c9e | 2025-02-06 06:05:47 | [diff] [blame] | 67 | parser.add_argument('--format', choices=['proto', 'prototext', 'json'], |
| 68 | default='proto', help=('Output format.')) |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 69 | options = parser.parse_args() |
| 70 | |
| 71 | this_dir = os.path.dirname(__file__) |
| 72 | repo_root = os.path.join(this_dir, '..', '..') |
| 73 | |
| 74 | targets = [] |
| Fumitoshi Ukai | 82a8262 | 2025-02-06 02:49:16 | [diff] [blame] | 75 | use_prepare = True |
| Fumitoshi Ukai | 3fb1a04 | 2024-10-08 00:58:25 | [diff] [blame] | 76 | use_prepare_header_only = True |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 77 | for source in options.source: |
| Fumitoshi Ukai | 3fb1a04 | 2024-10-08 00:58:25 | [diff] [blame] | 78 | _, ext = os.path.splitext(source) |
| Fumitoshi Ukai | 82a8262 | 2025-02-06 02:49:16 | [diff] [blame] | 79 | if ext == '.java': |
| 80 | # need to include generated *.jar for java. |
| 81 | use_prepare = False |
| Fumitoshi Ukai | 3fb1a04 | 2024-10-08 00:58:25 | [diff] [blame] | 82 | if ext not in ('.c', '.cc', '.cxx', '.cpp', '.m', '.mm', '.S', |
| 83 | '.h', '.hxx', '.hpp', '.inc'): |
| 84 | use_prepare_header_only = False |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 85 | # source is repo root (cwd) relative, |
| 86 | # but siso uses out dir relative target. |
| 87 | target = os.path.relpath(source, start=options.out_dir) + "^" |
| 88 | targets.append(target) |
| 89 | |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 90 | use_reclient, use_android_build_server = _retrieve_gn_args(options.out_dir) |
| 91 | |
| 92 | if use_reclient: |
| Fumitoshi Ukai | d1f5e335 | 2024-05-09 04:31:04 | [diff] [blame] | 93 | # b/335795623 ide_query compiler_arguments contain non-compiler arguments |
| 94 | sys.stderr.write( |
| Fumitoshi Ukai | 128f48d | 2024-06-04 08:25:20 | [diff] [blame] | 95 | 'ide_query won\'t work well with "use_reclient=true"\n' |
| 96 | 'Set "use_reclient=false" in args.gn.\n') |
| Fumitoshi Ukai | d1f5e335 | 2024-05-09 04:31:04 | [diff] [blame] | 97 | sys.exit(1) |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 98 | if options.perform_build: |
| Fumitoshi Ukai | 9ac4c621 | 2025-03-19 03:31:32 | [diff] [blame] | 99 | # forget last targets of normal build as this build will update |
| 100 | # .siso_fs_state. |
| 101 | if os.path.exists(os.path.join(options.out_dir, '.siso_last_targets')): |
| 102 | os.remove(os.path.join(options.out_dir, '.siso_last_targets')) |
| Fumitoshi Ukai | d1f5e335 | 2024-05-09 04:31:04 | [diff] [blame] | 103 | args = ['siso', 'ninja'] |
| Fumitoshi Ukai | 4ec4322 | 2024-04-30 08:51:36 | [diff] [blame] | 104 | # use `-k=0` to build generated files as much as possible. |
| 105 | args.extend([ |
| 106 | '-k=0', |
| Fumitoshi Ukai | 4ec4322 | 2024-04-30 08:51:36 | [diff] [blame] | 107 | '-C', |
| 108 | options.out_dir, |
| 109 | ]) |
| Fumitoshi Ukai | 82a8262 | 2025-02-06 02:49:16 | [diff] [blame] | 110 | if use_prepare: |
| 111 | args.extend(['--prepare']) |
| Richard Wang | 02fbd2dd | 2024-12-03 08:29:31 | [diff] [blame] | 112 | if options.log_dir: |
| 113 | args.extend(['-log_dir', options.log_dir]) |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 114 | args.extend(targets) |
| Junji Watanabe | ed7f7a0 | 2025-04-25 00:05:43 | [diff] [blame] | 115 | # Set build ID for android build server. |
| 116 | build_id = str(uuid.uuid4()) |
| 117 | os.environ.setdefault("AUTONINJA_BUILD_ID", build_id) |
| Fumitoshi Ukai | 35d16de | 2024-05-02 09:18:32 | [diff] [blame] | 118 | env = os.environ.copy() |
| Fumitoshi Ukai | 3fb1a04 | 2024-10-08 00:58:25 | [diff] [blame] | 119 | if use_prepare_header_only: |
| Fumitoshi Ukai | 86ca44c | 2025-04-11 04:41:52 | [diff] [blame] | 120 | env['SISO_EXPERIMENTS'] = 'no-fast-deps,prepare-header-only,ignore-missing-targets' |
| Fumitoshi Ukai | 3fb1a04 | 2024-10-08 00:58:25 | [diff] [blame] | 121 | else: |
| Fumitoshi Ukai | 86ca44c | 2025-04-11 04:41:52 | [diff] [blame] | 122 | env['SISO_EXPERIMENTS'] = 'no-fast-deps,ignore-missing-targets' |
| Junji Watanabe | 000c40b | 2025-04-18 07:50:08 | [diff] [blame] | 123 | with android_build_server_helper.build_server_context( |
| 124 | build_id, |
| 125 | options.out_dir, |
| 126 | use_android_build_server=use_android_build_server, |
| 127 | ): |
| 128 | with subprocess.Popen( |
| 129 | args, |
| 130 | cwd=repo_root, |
| 131 | env=env, |
| 132 | stderr=subprocess.STDOUT, |
| 133 | stdout=subprocess.PIPE, |
| 134 | universal_newlines=True |
| 135 | ) as p: |
| 136 | for line in p.stdout: |
| 137 | print(line, end='', file=sys.stderr) |
| 138 | # loop ends when program finishes, but must wait else returncode is None. |
| 139 | p.wait() |
| 140 | if p.returncode != 0: |
| 141 | # TODO: report error in IdeAnalysis.Status? |
| 142 | sys.stderr.write('build failed with %d\n' % p.returncode) |
| 143 | # even if build fails, it should report ideanalysis back. |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 144 | |
| 145 | args = ['siso', 'query', 'ideanalysis', '-C', options.out_dir] |
| Fumitoshi Ukai | 3788c9e | 2025-02-06 06:05:47 | [diff] [blame] | 146 | if options.format: |
| 147 | args.extend(['--format', options.format]) |
| Fumitoshi Ukai | 8599cf5 | 2024-02-15 04:34:52 | [diff] [blame] | 148 | args.extend(targets) |
| 149 | subprocess.run(args, cwd=repo_root, check=True) |
| 150 | |
| 151 | if __name__ == '__main__': |
| 152 | sys.exit(main()) |