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