Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 1 | # Copyright 2019 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 | """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 | |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 11 | from chromite.api.gen.chromiumos import common_pb2 |
| 12 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 13 | |
| 14 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 15 | """Base module error class.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 16 | |
| 17 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 18 | class BuildTarget(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 19 | """Class to handle the build target information.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 21 | def __init__( |
| 22 | self, |
| 23 | name: Optional[str], |
| 24 | profile: Optional[str] = None, |
| 25 | build_root: Optional[str] = None, |
| 26 | ): |
| 27 | """Build Target init. |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 29 | Args: |
| 30 | name: The full name of the target. |
| 31 | profile: The profile name. |
| 32 | build_root: The path to the buildroot. |
| 33 | """ |
| 34 | self._name = name or None |
| 35 | self.profile = profile |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 37 | if build_root: |
| 38 | self.root = os.path.normpath(build_root) |
| 39 | else: |
| 40 | self.root = get_default_sysroot_path(self.name) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 42 | def __eq__(self, other): |
| 43 | if self.__class__ is other.__class__: |
| 44 | return ( |
| 45 | self.name == other.name |
| 46 | and self.profile == other.profile |
| 47 | and self.root == other.root |
| 48 | ) |
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 | return NotImplemented |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 52 | def __hash__(self): |
| 53 | return hash(self.name) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 55 | def __str__(self): |
| 56 | return self.name |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 58 | @property |
| 59 | def name(self): |
| 60 | return self._name |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 62 | @property |
| 63 | def as_protobuf(self): |
| 64 | return common_pb2.BuildTarget(name=self.name or "") |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 66 | @classmethod |
| 67 | def from_protobuf(cls, message): |
| 68 | return cls(name=message.name) |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 70 | @property |
| 71 | def profile_protobuf(self): |
| 72 | return common_pb2.Profile(name=self.profile) |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 74 | def full_path(self, *args): |
| 75 | """Turn a sysroot-relative path into an absolute path.""" |
| 76 | 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] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 78 | def get_command(self, base_command: str) -> str: |
| 79 | """Get the build target's variant of the given base command. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 81 | We create wrappers for many scripts that handle the build target's |
| 82 | arguments. Build the target-specific variant for such a command. |
| 83 | e.g. emerge -> emerge-eve. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 85 | TODO: Add optional validation the command exists. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 87 | Args: |
| 88 | base_command: The wrapped command. |
Alex Klein | 309c757 | 2020-01-27 10:55:01 -0700 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 90 | Returns: |
| 91 | The build target's command wrapper. |
| 92 | """ |
| 93 | if self.is_host(): |
| 94 | return base_command |
Sloan Johnson | 4813805 | 2021-12-28 21:08:32 +0000 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 96 | return "%s-%s" % (base_command, self.name) |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 98 | def is_host(self) -> bool: |
| 99 | """Check if the build target refers to the host.""" |
| 100 | return not self.name |
Sloan Johnson | 4813805 | 2021-12-28 21:08:32 +0000 | [diff] [blame] | 101 | |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 102 | |
Alex Klein | 62b7e1e | 2020-03-09 13:36:50 -0600 | [diff] [blame] | 103 | def get_default_sysroot_path(build_target_name=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 104 | """Get the default sysroot location or '/' if |build_target_name| is None.""" |
| 105 | if build_target_name is None: |
| 106 | return "/" |
| 107 | return os.path.join("/build", build_target_name) |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 108 | |
| 109 | |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 110 | def get_sdk_sysroot_path() -> str: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 111 | """Get the SDK's sysroot path. |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 113 | Convenience/clarification wrapper for get_default_sysroot_path for use when |
| 114 | explicitly fetching the SDK's sysroot path. |
| 115 | """ |
| 116 | return get_default_sysroot_path() |
Alex Klein | 5417f3d | 2021-07-09 15:23:02 -0600 | [diff] [blame] | 117 | |
| 118 | |
Alex Klein | 1a55dde | 2020-03-06 15:53:29 -0700 | [diff] [blame] | 119 | def is_valid_name(build_target_name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 120 | """Validate |build_target_name| is a valid name.""" |
| 121 | return bool(re.match(r"^[a-zA-Z0-9-_]+$", build_target_name)) |