Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2016 Google, Inc |
| 4 | # |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 5 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 6 | import glob |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 7 | import os |
Paul Barker | ce7efa0 | 2021-09-08 12:38:02 +0100 | [diff] [blame] | 8 | import shlex |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 9 | import shutil |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 10 | import sys |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 11 | import tempfile |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 12 | import urllib.request |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 13 | |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 14 | from u_boot_pylib import command |
| 15 | from u_boot_pylib import tout |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 16 | |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 17 | # Output directly (generally this is temporary) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 18 | outdir = None |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 19 | |
| 20 | # True to keep the output directory around after exiting |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 21 | preserve_outdir = False |
| 22 | |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 23 | # Path to the Chrome OS chroot, if we know it |
| 24 | chroot_path = None |
| 25 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 26 | # Search paths to use for filename(), used to find files |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 27 | search_paths = [] |
| 28 | |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 29 | tool_search_paths = [] |
| 30 | |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 31 | # Tools and the packages that contain them, on debian |
| 32 | packages = { |
| 33 | 'lz4': 'liblz4-tool', |
| 34 | } |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 35 | |
Simon Glass | 867b906 | 2018-10-01 21:12:44 -0600 | [diff] [blame] | 36 | # List of paths to use when looking for an input file |
| 37 | indir = [] |
| 38 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 39 | def prepare_output_dir(dirname, preserve=False): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 40 | """Select an output directory, ensuring it exists. |
| 41 | |
| 42 | This either creates a temporary directory or checks that the one supplied |
| 43 | by the user is valid. For a temporary directory, it makes a note to |
| 44 | remove it later if required. |
| 45 | |
| 46 | Args: |
| 47 | dirname: a string, name of the output directory to use to store |
| 48 | intermediate and output files. If is None - create a temporary |
| 49 | directory. |
| 50 | preserve: a Boolean. If outdir above is None and preserve is False, the |
| 51 | created temporary directory will be destroyed on exit. |
| 52 | |
| 53 | Raises: |
| 54 | OSError: If it cannot create the output directory. |
| 55 | """ |
| 56 | global outdir, preserve_outdir |
| 57 | |
| 58 | preserve_outdir = dirname or preserve |
| 59 | if dirname: |
| 60 | outdir = dirname |
| 61 | if not os.path.isdir(outdir): |
| 62 | try: |
| 63 | os.makedirs(outdir) |
| 64 | except OSError as err: |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 65 | raise ValueError( |
| 66 | f"Cannot make output directory 'outdir': 'err.strerror'") |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 67 | tout.debug("Using output directory '%s'" % outdir) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 68 | else: |
| 69 | outdir = tempfile.mkdtemp(prefix='binman.') |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 70 | tout.debug("Using temporary directory '%s'" % outdir) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 71 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 72 | def _remove_output_dir(): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 73 | global outdir |
| 74 | |
| 75 | shutil.rmtree(outdir) |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 76 | tout.debug("Deleted temporary directory '%s'" % outdir) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 77 | outdir = None |
| 78 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 79 | def finalise_output_dir(): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 80 | global outdir, preserve_outdir |
| 81 | |
| 82 | """Tidy up: delete output directory if temporary and not preserved.""" |
| 83 | if outdir and not preserve_outdir: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 84 | _remove_output_dir() |
Simon Glass | 7f47e08 | 2019-07-20 12:24:07 -0600 | [diff] [blame] | 85 | outdir = None |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 86 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 87 | def get_output_filename(fname): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 88 | """Return a filename within the output directory. |
| 89 | |
| 90 | Args: |
| 91 | fname: Filename to use for new file |
| 92 | |
| 93 | Returns: |
| 94 | The full path of the filename, within the output directory |
| 95 | """ |
| 96 | return os.path.join(outdir, fname) |
| 97 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 98 | def get_output_dir(): |
Simon Glass | 4e8e846 | 2020-12-28 20:34:52 -0700 | [diff] [blame] | 99 | """Return the current output directory |
| 100 | |
| 101 | Returns: |
| 102 | str: The output directory |
| 103 | """ |
| 104 | return outdir |
| 105 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 106 | def _finalise_for_test(): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 107 | """Remove the output directory (for use by tests)""" |
| 108 | global outdir |
| 109 | |
| 110 | if outdir: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 111 | _remove_output_dir() |
Simon Glass | 7f47e08 | 2019-07-20 12:24:07 -0600 | [diff] [blame] | 112 | outdir = None |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 113 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 114 | def set_input_dirs(dirname): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 115 | """Add a list of input directories, where input files are kept. |
| 116 | |
| 117 | Args: |
| 118 | dirname: a list of paths to input directories to use for obtaining |
| 119 | files needed by binman to place in the image. |
| 120 | """ |
| 121 | global indir |
| 122 | |
| 123 | indir = dirname |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 124 | tout.debug("Using input directories %s" % indir) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 125 | |
Paul HENRYS | 7520381 | 2024-11-25 19:16:53 +0100 | [diff] [blame] | 126 | def append_input_dirs(dirname): |
| 127 | """Append a list of input directories to the current list of input |
| 128 | directories |
| 129 | |
| 130 | Args: |
| 131 | dirname: a list of paths to input directories to use for obtaining |
| 132 | files needed by binman to place in the image. |
| 133 | """ |
| 134 | global indir |
| 135 | |
| 136 | for dir in dirname: |
| 137 | if dirname not in indir: |
| 138 | indir.append(dirname) |
| 139 | |
| 140 | tout.debug("Updated input directories %s" % indir) |
| 141 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 142 | def get_input_filename(fname, allow_missing=False): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 143 | """Return a filename for use as input. |
| 144 | |
| 145 | Args: |
| 146 | fname: Filename to use for new file |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 147 | allow_missing: True if the filename can be missing |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 148 | |
| 149 | Returns: |
Simon Glass | eb751fd | 2021-03-18 20:25:03 +1300 | [diff] [blame] | 150 | fname, if indir is None; |
| 151 | full path of the filename, within the input directory; |
| 152 | None, if file is missing and allow_missing is True |
| 153 | |
| 154 | Raises: |
| 155 | ValueError if file is missing and allow_missing is False |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 156 | """ |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 157 | if not indir or fname[:1] == '/': |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 158 | return fname |
| 159 | for dirname in indir: |
| 160 | pathname = os.path.join(dirname, fname) |
| 161 | if os.path.exists(pathname): |
| 162 | return pathname |
| 163 | |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 164 | if allow_missing: |
| 165 | return None |
Simon Glass | f2eb054 | 2018-07-17 13:25:45 -0600 | [diff] [blame] | 166 | raise ValueError("Filename '%s' not found in input path (%s) (cwd='%s')" % |
| 167 | (fname, ','.join(indir), os.getcwd())) |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 168 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 169 | def get_input_filename_glob(pattern): |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 170 | """Return a list of filenames for use as input. |
| 171 | |
| 172 | Args: |
| 173 | pattern: Filename pattern to search for |
| 174 | |
| 175 | Returns: |
| 176 | A list of matching files in all input directories |
| 177 | """ |
| 178 | if not indir: |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 179 | return glob.glob(pattern) |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 180 | files = [] |
| 181 | for dirname in indir: |
| 182 | pathname = os.path.join(dirname, pattern) |
| 183 | files += glob.glob(pathname) |
| 184 | return sorted(files) |
| 185 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 186 | def align(pos, align): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 187 | if align: |
| 188 | mask = align - 1 |
| 189 | pos = (pos + mask) & ~mask |
| 190 | return pos |
| 191 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 192 | def not_power_of_two(num): |
Simon Glass | be39f05 | 2016-07-25 18:59:08 -0600 | [diff] [blame] | 193 | return num and (num & (num - 1)) |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 194 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 195 | def set_tool_paths(toolpaths): |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 196 | """Set the path to search for tools |
| 197 | |
| 198 | Args: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 199 | toolpaths: List of paths to search for tools executed by run() |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 200 | """ |
| 201 | global tool_search_paths |
| 202 | |
| 203 | tool_search_paths = toolpaths |
| 204 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 205 | def path_has_file(path_spec, fname): |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 206 | """Check if a given filename is in the PATH |
| 207 | |
| 208 | Args: |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 209 | path_spec: Value of PATH variable to check |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 210 | fname: Filename to check |
| 211 | |
| 212 | Returns: |
| 213 | True if found, False if not |
| 214 | """ |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 215 | for dir in path_spec.split(':'): |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 216 | if os.path.exists(os.path.join(dir, fname)): |
| 217 | return True |
| 218 | return False |
| 219 | |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 220 | def get_host_compile_tool(env, name): |
Alper Nebi Yasak | cd740d3 | 2020-09-06 14:46:06 +0300 | [diff] [blame] | 221 | """Get the host-specific version for a compile tool |
| 222 | |
| 223 | This checks the environment variables that specify which version of |
| 224 | the tool should be used (e.g. ${HOSTCC}). |
| 225 | |
| 226 | The following table lists the host-specific versions of the tools |
| 227 | this function resolves to: |
| 228 | |
| 229 | Compile Tool | Host version |
| 230 | --------------+---------------- |
| 231 | as | ${HOSTAS} |
| 232 | ld | ${HOSTLD} |
| 233 | cc | ${HOSTCC} |
| 234 | cpp | ${HOSTCPP} |
| 235 | c++ | ${HOSTCXX} |
| 236 | ar | ${HOSTAR} |
| 237 | nm | ${HOSTNM} |
| 238 | ldr | ${HOSTLDR} |
| 239 | strip | ${HOSTSTRIP} |
| 240 | objcopy | ${HOSTOBJCOPY} |
| 241 | objdump | ${HOSTOBJDUMP} |
| 242 | dtc | ${HOSTDTC} |
| 243 | |
| 244 | Args: |
| 245 | name: Command name to run |
| 246 | |
| 247 | Returns: |
| 248 | host_name: Exact command name to run instead |
| 249 | extra_args: List of extra arguments to pass |
| 250 | """ |
| 251 | host_name = None |
| 252 | extra_args = [] |
| 253 | if name in ('as', 'ld', 'cc', 'cpp', 'ar', 'nm', 'ldr', 'strip', |
| 254 | 'objcopy', 'objdump', 'dtc'): |
| 255 | host_name, *host_args = env.get('HOST' + name.upper(), '').split(' ') |
| 256 | elif name == 'c++': |
| 257 | host_name, *host_args = env.get('HOSTCXX', '').split(' ') |
| 258 | |
| 259 | if host_name: |
| 260 | return host_name, extra_args |
| 261 | return name, [] |
| 262 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 263 | def get_target_compile_tool(name, cross_compile=None): |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 264 | """Get the target-specific version for a compile tool |
| 265 | |
| 266 | This first checks the environment variables that specify which |
| 267 | version of the tool should be used (e.g. ${CC}). If those aren't |
| 268 | specified, it checks the CROSS_COMPILE variable as a prefix for the |
| 269 | tool with some substitutions (e.g. "${CROSS_COMPILE}gcc" for cc). |
| 270 | |
| 271 | The following table lists the target-specific versions of the tools |
| 272 | this function resolves to: |
| 273 | |
| 274 | Compile Tool | First choice | Second choice |
| 275 | --------------+----------------+---------------------------- |
| 276 | as | ${AS} | ${CROSS_COMPILE}as |
| 277 | ld | ${LD} | ${CROSS_COMPILE}ld.bfd |
| 278 | | | or ${CROSS_COMPILE}ld |
| 279 | cc | ${CC} | ${CROSS_COMPILE}gcc |
| 280 | cpp | ${CPP} | ${CROSS_COMPILE}gcc -E |
| 281 | c++ | ${CXX} | ${CROSS_COMPILE}g++ |
| 282 | ar | ${AR} | ${CROSS_COMPILE}ar |
| 283 | nm | ${NM} | ${CROSS_COMPILE}nm |
| 284 | ldr | ${LDR} | ${CROSS_COMPILE}ldr |
| 285 | strip | ${STRIP} | ${CROSS_COMPILE}strip |
| 286 | objcopy | ${OBJCOPY} | ${CROSS_COMPILE}objcopy |
| 287 | objdump | ${OBJDUMP} | ${CROSS_COMPILE}objdump |
| 288 | dtc | ${DTC} | (no CROSS_COMPILE version) |
| 289 | |
| 290 | Args: |
| 291 | name: Command name to run |
| 292 | |
| 293 | Returns: |
| 294 | target_name: Exact command name to run instead |
| 295 | extra_args: List of extra arguments to pass |
| 296 | """ |
| 297 | env = dict(os.environ) |
| 298 | |
| 299 | target_name = None |
| 300 | extra_args = [] |
| 301 | if name in ('as', 'ld', 'cc', 'cpp', 'ar', 'nm', 'ldr', 'strip', |
| 302 | 'objcopy', 'objdump', 'dtc'): |
| 303 | target_name, *extra_args = env.get(name.upper(), '').split(' ') |
| 304 | elif name == 'c++': |
| 305 | target_name, *extra_args = env.get('CXX', '').split(' ') |
| 306 | |
| 307 | if target_name: |
| 308 | return target_name, extra_args |
| 309 | |
| 310 | if cross_compile is None: |
| 311 | cross_compile = env.get('CROSS_COMPILE', '') |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 312 | |
| 313 | if name in ('as', 'ar', 'nm', 'ldr', 'strip', 'objcopy', 'objdump'): |
| 314 | target_name = cross_compile + name |
| 315 | elif name == 'ld': |
| 316 | try: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 317 | if run(cross_compile + 'ld.bfd', '-v'): |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 318 | target_name = cross_compile + 'ld.bfd' |
| 319 | except: |
| 320 | target_name = cross_compile + 'ld' |
| 321 | elif name == 'cc': |
| 322 | target_name = cross_compile + 'gcc' |
| 323 | elif name == 'cpp': |
| 324 | target_name = cross_compile + 'gcc' |
| 325 | extra_args = ['-E'] |
| 326 | elif name == 'c++': |
| 327 | target_name = cross_compile + 'g++' |
| 328 | else: |
| 329 | target_name = name |
| 330 | return target_name, extra_args |
| 331 | |
Simon Glass | 6d493a5 | 2022-01-09 20:13:40 -0700 | [diff] [blame] | 332 | def get_env_with_path(): |
| 333 | """Get an updated environment with the PATH variable set correctly |
| 334 | |
| 335 | If there are any search paths set, these need to come first in the PATH so |
| 336 | that these override any other version of the tools. |
| 337 | |
| 338 | Returns: |
| 339 | dict: New environment with PATH updated, or None if there are not search |
| 340 | paths |
| 341 | """ |
| 342 | if tool_search_paths: |
| 343 | env = dict(os.environ) |
| 344 | env['PATH'] = ':'.join(tool_search_paths) + ':' + env['PATH'] |
| 345 | return env |
| 346 | |
| 347 | def run_result(name, *args, **kwargs): |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 348 | """Run a tool with some arguments |
| 349 | |
| 350 | This runs a 'tool', which is a program used by binman to process files and |
| 351 | perhaps produce some output. Tools can be located on the PATH or in a |
| 352 | search path. |
| 353 | |
| 354 | Args: |
| 355 | name: Command name to run |
| 356 | args: Arguments to the tool |
Alper Nebi Yasak | cd740d3 | 2020-09-06 14:46:06 +0300 | [diff] [blame] | 357 | for_host: True to resolve the command to the version for the host |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 358 | for_target: False to run the command as-is, without resolving it |
| 359 | to the version for the compile target |
Simon Glass | 6d493a5 | 2022-01-09 20:13:40 -0700 | [diff] [blame] | 360 | raise_on_error: Raise an error if the command fails (True by default) |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 361 | |
| 362 | Returns: |
| 363 | CommandResult object |
| 364 | """ |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 365 | try: |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 366 | binary = kwargs.get('binary') |
Alper Nebi Yasak | cd740d3 | 2020-09-06 14:46:06 +0300 | [diff] [blame] | 367 | for_host = kwargs.get('for_host', False) |
| 368 | for_target = kwargs.get('for_target', not for_host) |
Simon Glass | 6d493a5 | 2022-01-09 20:13:40 -0700 | [diff] [blame] | 369 | raise_on_error = kwargs.get('raise_on_error', True) |
| 370 | env = get_env_with_path() |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 371 | if for_target: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 372 | name, extra_args = get_target_compile_tool(name) |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 373 | args = tuple(extra_args) + args |
Alper Nebi Yasak | cd740d3 | 2020-09-06 14:46:06 +0300 | [diff] [blame] | 374 | elif for_host: |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 375 | name, extra_args = get_host_compile_tool(env, name) |
Alper Nebi Yasak | cd740d3 | 2020-09-06 14:46:06 +0300 | [diff] [blame] | 376 | args = tuple(extra_args) + args |
Simon Glass | 6e89224 | 2020-11-09 07:45:02 -0700 | [diff] [blame] | 377 | name = os.path.expanduser(name) # Expand paths containing ~ |
Simon Glass | 9f5051a | 2019-08-24 07:22:42 -0600 | [diff] [blame] | 378 | all_args = (name,) + args |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 379 | result = command.run_pipe([all_args], capture=True, capture_stderr=True, |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 380 | env=env, raise_on_error=False, binary=binary) |
Simon Glass | 9f5051a | 2019-08-24 07:22:42 -0600 | [diff] [blame] | 381 | if result.return_code: |
Simon Glass | 6d493a5 | 2022-01-09 20:13:40 -0700 | [diff] [blame] | 382 | if raise_on_error: |
| 383 | raise ValueError("Error %d running '%s': %s" % |
| 384 | (result.return_code,' '.join(all_args), |
| 385 | result.stderr or result.stdout)) |
| 386 | return result |
| 387 | except ValueError: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 388 | if env and not path_has_file(env['PATH'], name): |
Simon Glass | 90c3c4c | 2019-07-08 13:18:27 -0600 | [diff] [blame] | 389 | msg = "Please install tool '%s'" % name |
Simon Glass | 778ab84 | 2018-09-14 04:57:25 -0600 | [diff] [blame] | 390 | package = packages.get(name) |
| 391 | if package: |
| 392 | msg += " (e.g. from package '%s')" % package |
| 393 | raise ValueError(msg) |
| 394 | raise |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 395 | |
Simon Glass | 835b0e9 | 2022-01-09 20:13:43 -0700 | [diff] [blame] | 396 | def tool_find(name): |
| 397 | """Search the current path for a tool |
| 398 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 399 | This uses both PATH and any value from set_tool_paths() to search for a tool |
Simon Glass | 835b0e9 | 2022-01-09 20:13:43 -0700 | [diff] [blame] | 400 | |
| 401 | Args: |
| 402 | name (str): Name of tool to locate |
| 403 | |
| 404 | Returns: |
| 405 | str: Full path to tool if found, else None |
| 406 | """ |
| 407 | name = os.path.expanduser(name) # Expand paths containing ~ |
| 408 | paths = [] |
| 409 | pathvar = os.environ.get('PATH') |
| 410 | if pathvar: |
| 411 | paths = pathvar.split(':') |
| 412 | if tool_search_paths: |
| 413 | paths += tool_search_paths |
| 414 | for path in paths: |
| 415 | fname = os.path.join(path, name) |
| 416 | if os.path.isfile(fname) and os.access(fname, os.X_OK): |
| 417 | return fname |
| 418 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 419 | def run(name, *args, **kwargs): |
Simon Glass | 6d493a5 | 2022-01-09 20:13:40 -0700 | [diff] [blame] | 420 | """Run a tool with some arguments |
| 421 | |
| 422 | This runs a 'tool', which is a program used by binman to process files and |
| 423 | perhaps produce some output. Tools can be located on the PATH or in a |
| 424 | search path. |
| 425 | |
| 426 | Args: |
| 427 | name: Command name to run |
| 428 | args: Arguments to the tool |
| 429 | for_host: True to resolve the command to the version for the host |
| 430 | for_target: False to run the command as-is, without resolving it |
| 431 | to the version for the compile target |
| 432 | |
| 433 | Returns: |
| 434 | CommandResult object |
| 435 | """ |
| 436 | result = run_result(name, *args, **kwargs) |
| 437 | if result is not None: |
| 438 | return result.stdout |
| 439 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 440 | def filename(fname): |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 441 | """Resolve a file path to an absolute path. |
| 442 | |
| 443 | If fname starts with ##/ and chroot is available, ##/ gets replaced with |
| 444 | the chroot path. If chroot is not available, this file name can not be |
| 445 | resolved, `None' is returned. |
| 446 | |
| 447 | If fname is not prepended with the above prefix, and is not an existing |
| 448 | file, the actual file name is retrieved from the passed in string and the |
| 449 | search_paths directories (if any) are searched to for the file. If found - |
| 450 | the path to the found file is returned, `None' is returned otherwise. |
| 451 | |
| 452 | Args: |
| 453 | fname: a string, the path to resolve. |
| 454 | |
| 455 | Returns: |
| 456 | Absolute path to the file or None if not found. |
| 457 | """ |
| 458 | if fname.startswith('##/'): |
| 459 | if chroot_path: |
| 460 | fname = os.path.join(chroot_path, fname[3:]) |
| 461 | else: |
| 462 | return None |
| 463 | |
| 464 | # Search for a pathname that exists, and return it if found |
| 465 | if fname and not os.path.exists(fname): |
| 466 | for path in search_paths: |
| 467 | pathname = os.path.join(path, os.path.basename(fname)) |
| 468 | if os.path.exists(pathname): |
| 469 | return pathname |
| 470 | |
| 471 | # If not found, just return the standard, unchanged path |
| 472 | return fname |
| 473 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 474 | def read_file(fname, binary=True): |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 475 | """Read and return the contents of a file. |
| 476 | |
| 477 | Args: |
| 478 | fname: path to filename to read, where ## signifiies the chroot. |
| 479 | |
| 480 | Returns: |
| 481 | data read from file, as a string. |
| 482 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 483 | with open(filename(fname), binary and 'rb' or 'r') as fd: |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 484 | data = fd.read() |
| 485 | #self._out.Info("Read file '%s' size %d (%#0x)" % |
| 486 | #(fname, len(data), len(data))) |
| 487 | return data |
| 488 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 489 | def write_file(fname, data, binary=True): |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 490 | """Write data into a file. |
| 491 | |
| 492 | Args: |
| 493 | fname: path to filename to write |
| 494 | data: data to write to file, as a string |
| 495 | """ |
| 496 | #self._out.Info("Write file '%s' size %d (%#0x)" % |
| 497 | #(fname, len(data), len(data))) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 498 | with open(filename(fname), binary and 'wb' or 'w') as fd: |
Simon Glass | 0362cd2 | 2018-07-17 13:25:43 -0600 | [diff] [blame] | 499 | fd.write(data) |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 500 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 501 | def get_bytes(byte, size): |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 502 | """Get a string of bytes of a given size |
| 503 | |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 504 | Args: |
| 505 | byte: Numeric byte value to use |
| 506 | size: Size of bytes/string to return |
| 507 | |
| 508 | Returns: |
| 509 | A bytes type with 'byte' repeated 'size' times |
| 510 | """ |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 511 | return bytes([byte]) * size |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 512 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 513 | def to_bytes(string): |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 514 | """Convert a str type into a bytes type |
| 515 | |
| 516 | Args: |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 517 | string: string to convert |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 518 | |
| 519 | Returns: |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 520 | A bytes type |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 521 | """ |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 522 | return string.encode('utf-8') |
Simon Glass | dfd1901 | 2019-07-08 13:18:41 -0600 | [diff] [blame] | 523 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 524 | def to_string(bval): |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 525 | """Convert a bytes type into a str type |
| 526 | |
| 527 | Args: |
| 528 | bval: bytes value to convert |
| 529 | |
| 530 | Returns: |
| 531 | Python 3: A bytes type |
| 532 | Python 2: A string type |
| 533 | """ |
| 534 | return bval.decode('utf-8') |
| 535 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 536 | def to_hex(val): |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 537 | """Convert an integer value (or None) to a string |
| 538 | |
| 539 | Returns: |
| 540 | hex value, or 'None' if the value is None |
| 541 | """ |
| 542 | return 'None' if val is None else '%#x' % val |
| 543 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 544 | def to_hex_size(val): |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 545 | """Return the size of an object in hex |
| 546 | |
| 547 | Returns: |
| 548 | hex value of size, or 'None' if the value is None |
| 549 | """ |
| 550 | return 'None' if val is None else '%#x' % len(val) |
Paul Barker | 25ecd97 | 2021-09-08 12:38:01 +0100 | [diff] [blame] | 551 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 552 | def print_full_help(fname): |
Paul Barker | 25ecd97 | 2021-09-08 12:38:01 +0100 | [diff] [blame] | 553 | """Print the full help message for a tool using an appropriate pager. |
| 554 | |
| 555 | Args: |
| 556 | fname: Path to a file containing the full help message |
| 557 | """ |
Paul Barker | ce7efa0 | 2021-09-08 12:38:02 +0100 | [diff] [blame] | 558 | pager = shlex.split(os.getenv('PAGER', '')) |
Paul Barker | 25ecd97 | 2021-09-08 12:38:01 +0100 | [diff] [blame] | 559 | if not pager: |
Paul Barker | ce7efa0 | 2021-09-08 12:38:02 +0100 | [diff] [blame] | 560 | lesspath = shutil.which('less') |
| 561 | pager = [lesspath] if lesspath else None |
Paul Barker | 25ecd97 | 2021-09-08 12:38:01 +0100 | [diff] [blame] | 562 | if not pager: |
Paul Barker | ce7efa0 | 2021-09-08 12:38:02 +0100 | [diff] [blame] | 563 | pager = ['more'] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 564 | command.run(*pager, fname) |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 565 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 566 | def download(url, tmpdir_pattern='.patman'): |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 567 | """Download a file to a temporary directory |
| 568 | |
| 569 | Args: |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 570 | url (str): URL to download |
| 571 | tmpdir_pattern (str): pattern to use for the temporary directory |
| 572 | |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 573 | Returns: |
| 574 | Tuple: |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 575 | Full path to the downloaded archive file in that directory, |
| 576 | or None if there was an error while downloading |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 577 | Temporary directory name |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 578 | """ |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 579 | print('- downloading: %s' % url) |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 580 | leaf = url.split('/')[-1] |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 581 | tmpdir = tempfile.mkdtemp(tmpdir_pattern) |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 582 | response = urllib.request.urlopen(url) |
| 583 | fname = os.path.join(tmpdir, leaf) |
| 584 | fd = open(fname, 'wb') |
| 585 | meta = response.info() |
| 586 | size = int(meta.get('Content-Length')) |
| 587 | done = 0 |
| 588 | block_size = 1 << 16 |
| 589 | status = '' |
| 590 | |
| 591 | # Read the file in chunks and show progress as we go |
| 592 | while True: |
| 593 | buffer = response.read(block_size) |
| 594 | if not buffer: |
| 595 | print(chr(8) * (len(status) + 1), '\r', end=' ') |
| 596 | break |
| 597 | |
| 598 | done += len(buffer) |
| 599 | fd.write(buffer) |
| 600 | status = r'%10d MiB [%3d%%]' % (done // 1024 // 1024, |
| 601 | done * 100 // size) |
| 602 | status = status + chr(8) * (len(status) + 1) |
| 603 | print(status, end=' ') |
| 604 | sys.stdout.flush() |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 605 | print('\r', end='') |
| 606 | sys.stdout.flush() |
Simon Glass | 25885d1 | 2022-01-09 20:13:41 -0700 | [diff] [blame] | 607 | fd.close() |
| 608 | if done != size: |
| 609 | print('Error, failed to download') |
| 610 | os.remove(fname) |
| 611 | fname = None |
Simon Glass | 9158060 | 2022-01-09 20:13:42 -0700 | [diff] [blame] | 612 | return fname, tmpdir |