blob: c47ab9fb4b995cc4011c30cb3c2685ed90717aaf [file] [log] [blame]
Greg Estren2323dfe2020-07-15 12:02:51 -07001# Lint as: python3
2# Copyright 2020 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""The core data types ctexplain manipulates."""
16
17from typing import Mapping
18from typing import Optional
19from typing import Tuple
20# Do not edit this line. Copybara replaces it with PY2 migration helper.
21from dataclasses import dataclass
22from dataclasses import field
23from frozendict import frozendict
24
25
26@dataclass(frozen=True)
27class Configuration():
28 """Stores a build configuration as a collection of fragments and options."""
Greg Estrenc1d70872020-08-25 07:17:08 -070029 # Mapping of each BuildConfiguration.Fragment in this configuration to the
30 # FragmentOptions it requires.
31 #
32 # All names are qualified up to the base file name, without package prefixes.
33 # For example, foo.bar.BazConfiguration appears as "BazConfiguration".
34 # foo.bar.BazConfiguration$Options appears as "BazeConfiguration$Options".
35 fragments: Mapping[str, Tuple[str, ...]]
Greg Estren2323dfe2020-07-15 12:02:51 -070036 # Mapping of FragmentOptions to option key/value pairs. For example:
37 # {"CoreOptions": {"action_env": "[]", "cpu": "x86", ...}, ...}.
38 #
39 # Option values are stored as strings of whatever "bazel config" outputs.
40 #
41 # Note that Fragment and FragmentOptions aren't the same thing.
Greg Estrenc1d70872020-08-25 07:17:08 -070042 options: Mapping[str, Mapping[str, str]]
Greg Estren2323dfe2020-07-15 12:02:51 -070043
44
45@dataclass(frozen=True)
46class ConfiguredTarget():
47 """Encapsulates a target + configuration + required fragments."""
48 # Label of the target this represents.
49 label: str
50 # Configuration this target is applied to. May be None.
51 config: Optional[Configuration]
52 # The hash of this configuration as reported by Bazel.
53 config_hash: str
54 # Fragments required by this configured target and its transitive
55 # dependencies. Stored as base names without packages. For example:
Greg Estrenc1d70872020-08-25 07:17:08 -070056 # "PlatformOptions" or "FooConfiguration$Options".
Greg Estren2323dfe2020-07-15 12:02:51 -070057 transitive_fragments: Tuple[str, ...]
58
59
60@dataclass(frozen=True)
61class HostConfiguration(Configuration):
62 """Special marker for the host configuration.
63
64 There's exactly one host configuration per build, so we shouldn't suggest
65 merging it with other configurations.
66
67 TODO(gregce): suggest host configuration trimming once we figure out the right
68 criteria. Even if Bazel's not technically equipped to do the trimming, it's
69 still theoretically valuable information. Note that moving from host to exec
70 configurations make this all a little less relevant, since exec configurations
71 aren't "special" compared to normal configurations.
72 """
73 # We don't currently read the host config's fragments or option values.
74 fragments: Tuple[str, ...] = ()
75 options: Mapping[str,
76 Mapping[str,
77 str]] = field(default_factory=lambda: frozendict({}))
78
79
80@dataclass(frozen=True)
81class NullConfiguration(Configuration):
82 """Special marker for the null configuration.
83
84 By definition this has no fragments or options.
85 """
86 fragments: Tuple[str, ...] = ()
87 options: Mapping[str,
88 Mapping[str,
89 str]] = field(default_factory=lambda: frozendict({}))