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