blob: 4e285cb8897f647716ec5237cfd7e2d4330c4ed4 [file] [log] [blame]
Manoj Guptad1a78462022-01-13 21:46:42 -08001# Copyright 2022 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Creates a remote_toolchain_inputs file for Reclient.
6
7Reclient(go/rbe/dev/x/reclient) is used for remote execution of build
8actions in build systems e.g. Chrome. It needs a toolchain inputs file
9next to clang compiler binary which has all the input dependencies
10needed to run the clang binary remotely.
11
12Running the script:
13$ generate_reclient_inputs [--output file_name] [--clang /path/to/clang]
14will create the file /path/to/file_name.
15
16By default, the script will write to /usr/bin/remote_toolchain_inputs.
17
18Contact: Chrome OS toolchain team.
19"""
20
21import os
22from pathlib import Path
23from typing import List, Optional, Set
24
25from chromite.lib import commandline
26from chromite.lib import cros_build_lib
Manoj Guptaf23b3932022-01-21 08:44:59 -080027from chromite.lib import osutils
Manoj Guptad1a78462022-01-13 21:46:42 -080028from chromite.third_party import lddtree
29
30
31def _GetSymLinkPath(base_dir: Path, link_path: str) -> Path:
32 """Return the actual symlink path relative to base directory."""
33 if not link_path:
34 return None
35 # Handle absolute symlink paths.
36 if link_path[0] == '/':
37 return link_path
38 # handle relative symlinks.
39 return base_dir / link_path
40
41
42def _CollectElfDeps(elfpath: Path) -> Set[Path]:
43 """Returns the set of dependent files for the elf file."""
44 libs = set()
45 to_process = []
46 elf = lddtree.ParseELF(elfpath, ldpaths=lddtree.LoadLdpaths())
47 for _, lib_data in elf['libs'].items():
48 if lib_data['path']:
49 to_process.append(Path(lib_data['path']))
50
51 while to_process:
52 path = to_process.pop()
53 if not path or path in libs:
54 continue
55 libs.add(path)
56 if path.is_symlink():
57 # TODO: Replace os.readlink() by path.readlink().
58 to_process.append(_GetSymLinkPath(path.parent, os.readlink(path)))
59
60 return libs
61
62
63def _GenerateRemoteInputsFile(out_file: str, clang_path: Path) -> None:
64 """Generate Remote Inputs for Clang for executing on reclient/RBE."""
65 clang_dir = clang_path.parent
66 # Start with collecting shared library dependencies.
67 paths = _CollectElfDeps(clang_path)
68
69 # Clang is typically a symlink, collect actual files.
70 paths.add(clang_path)
71 clang_file = clang_path
72 while clang_file.is_symlink():
73 clang_file = _GetSymLinkPath(clang_file.parent, os.readlink(clang_file))
74 paths.add(clang_file)
75
76 # Add clang resource directory and gcc config directory.
77 cmd = [str(clang_path), '--print-resource-dir']
78 resource_dir = cros_build_lib.run(
79 cmd, capture_output=True, encoding='utf-8',
80 print_cmd=False).stdout.splitlines()[0]
81 paths.add(Path(resource_dir) / 'share')
82 paths.add(Path('/etc/env.d/gcc'))
83
84 # Write the files relative to clang binary location.
Manoj Guptaf23b3932022-01-21 08:44:59 -080085 osutils.WriteFile(
86 clang_dir / out_file,
87 [os.path.relpath(x, clang_dir) + '\n' for x in sorted(paths)],
88 sudo=True)
Manoj Guptad1a78462022-01-13 21:46:42 -080089
90
91def ParseArgs(argv: Optional[List[str]]) -> commandline.argparse.Namespace:
92 """Parses program arguments."""
93 parser = commandline.ArgumentParser(description=__doc__)
94
95 parser.add_argument(
96 '--output',
97 default='remote_toolchain_inputs',
98 help='Name of remote toolchain file relative to clang binary directory.')
99 parser.add_argument(
100 '--clang', type=Path, default='/usr/bin/clang', help='Clang binary path.')
101
102 opts = parser.parse_args(argv)
103 opts.Freeze()
104 return opts
105
106
107def main(argv: Optional[List[str]] = None) -> Optional[int]:
108 cros_build_lib.AssertInsideChroot()
109 opts = ParseArgs(argv)
110 _GenerateRemoteInputsFile(opts.output, opts.clang)