Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Build target class and related functionality.""" |
| 6 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 7 | import os |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 8 | import re |
nohe@chromium.org | c78b38f | 2021-08-23 09:48:13 -0700 | [diff] [blame] | 9 | from typing import Optional |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 13 | """Base module error class.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 14 | |
| 15 | |
Alex Klein | 074f94f | 2023-06-22 10:32:06 -0600 | [diff] [blame^] | 16 | class BuildTarget: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 17 | """Class to handle the build target information.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | def __init__( |
| 20 | self, |
| 21 | name: Optional[str], |
| 22 | profile: Optional[str] = None, |
| 23 | build_root: Optional[str] = None, |
| 24 | ): |
| 25 | """Build Target init. |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 26 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | Args: |
Alex Klein | d52f46c | 2022-10-17 17:06:57 -0600 | [diff] [blame] | 28 | name: The full name of the target. |
| 29 | profile: The profile name. |
| 30 | build_root: The path to the buildroot. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | """ |
| 32 | self._name = name or None |
| 33 | self.profile = profile |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | if build_root: |
| 36 | self.root = os.path.normpath(build_root) |
| 37 | else: |
| 38 | self.root = get_default_sysroot_path(self.name) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | def __eq__(self, other): |
| 41 | if self.__class__ is other.__class__: |
| 42 | return ( |
| 43 | self.name == other.name |
| 44 | and self.profile == other.profile |
| 45 | and self.root == other.root |
| 46 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | return NotImplemented |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | def __hash__(self): |
| 51 | return hash(self.name) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | def __str__(self): |
| 54 | return self.name |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | @property |
| 57 | def name(self): |
| 58 | return self._name |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | def full_path(self, *args): |
| 61 | """Turn a sysroot-relative path into an absolute path.""" |
| 62 | return os.path.join(self.root, *[part.lstrip(os.sep) for part in args]) |
Michael Mortensen | 125bb01 | 2020-05-21 14:02:10 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def get_command(self, base_command: str) -> str: |
| 65 | """Get the build target's variant of the given base command. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | We create wrappers for many scripts that handle the build target's |
| 68 | arguments. Build the target-specific variant for such a command. |
| 69 | e.g. emerge -> emerge-eve. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | TODO: Add optional validation the command exists. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | Args: |
Alex Klein | d52f46c | 2022-10-17 17:06:57 -0600 | [diff] [blame] | 74 | base_command: The wrapped command. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | Returns: |
Alex Klein | d52f46c | 2022-10-17 17:06:57 -0600 | [diff] [blame] | 77 | The build target's command wrapper. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | """ |
| 79 | if self.is_host(): |
| 80 | return base_command |
Sloan Johnson | 4813805 | 2021-12-28 21:08:32 +0000 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | return "%s-%s" % (base_command, self.name) |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | def is_host(self) -> bool: |
| 85 | """Check if the build target refers to the host.""" |
| 86 | return not self.name |
Sloan Johnson | 4813805 | 2021-12-28 21:08:32 +0000 | [diff] [blame] | 87 | |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 88 | |
Alex Klein | 62b7e1e | 2020-03-09 13:36:50 -0600 | [diff] [blame] | 89 | def get_default_sysroot_path(build_target_name=None): |
Alex Klein | 79a68fa | 2023-03-21 17:11:07 -0600 | [diff] [blame] | 90 | """Get the default sysroot location or / if |build_target_name| is None.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | if build_target_name is None: |
| 92 | return "/" |
| 93 | return os.path.join("/build", build_target_name) |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 94 | |
| 95 | |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 96 | def get_sdk_sysroot_path() -> str: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | """Get the SDK's sysroot path. |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | Convenience/clarification wrapper for get_default_sysroot_path for use when |
| 100 | explicitly fetching the SDK's sysroot path. |
| 101 | """ |
| 102 | return get_default_sysroot_path() |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 103 | |
| 104 | |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 105 | def is_valid_name(build_target_name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | """Validate |build_target_name| is a valid name.""" |
| 107 | return bool(re.match(r"^[a-zA-Z0-9-_]+$", build_target_name)) |