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