blob: a477c473f5ac8111cd355930276a29b57dbfcb8a [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()
Manoj Gupta70f1be92022-02-03 17:24:42 -080045 to_process = [elfpath]
Manoj Guptad1a78462022-01-13 21:46:42 -080046 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)
Manoj Guptad1a78462022-01-13 21:46:42 -080071
Manoj Gupta70f1be92022-02-03 17:24:42 -080072 # Add clang resources, gcc config and glibc loader files.
Manoj Guptad1a78462022-01-13 21:46:42 -080073 cmd = [str(clang_path), '--print-resource-dir']
74 resource_dir = cros_build_lib.run(
75 cmd, capture_output=True, encoding='utf-8',
76 print_cmd=False).stdout.splitlines()[0]
77 paths.add(Path(resource_dir) / 'share')
Manoj Gupta70f1be92022-02-03 17:24:42 -080078 paths.update(
79 Path(x) for x in (
80 '/etc/env.d/gcc',
81 '/etc/ld.so.cache',
82 '/etc/ld.so.conf',
83 '/etc/ld.so.conf.d',
84 ))
Manoj Guptad1a78462022-01-13 21:46:42 -080085
86 # Write the files relative to clang binary location.
Manoj Guptaf23b3932022-01-21 08:44:59 -080087 osutils.WriteFile(
88 clang_dir / out_file,
89 [os.path.relpath(x, clang_dir) + '\n' for x in sorted(paths)],
90 sudo=True)
Manoj Guptad1a78462022-01-13 21:46:42 -080091
92
93def ParseArgs(argv: Optional[List[str]]) -> commandline.argparse.Namespace:
94 """Parses program arguments."""
95 parser = commandline.ArgumentParser(description=__doc__)
96
97 parser.add_argument(
98 '--output',
99 default='remote_toolchain_inputs',
100 help='Name of remote toolchain file relative to clang binary directory.')
101 parser.add_argument(
102 '--clang', type=Path, default='/usr/bin/clang', help='Clang binary path.')
103
104 opts = parser.parse_args(argv)
105 opts.Freeze()
106 return opts
107
108
109def main(argv: Optional[List[str]] = None) -> Optional[int]:
110 cros_build_lib.AssertInsideChroot()
111 opts = ParseArgs(argv)
112 _GenerateRemoteInputsFile(opts.output, opts.clang)