|
| 1 | +// Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See /LICENSE for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +use std::cmp::Ordering; |
| 6 | +use std::fmt::{self, Display}; |
| 7 | +use std::hash::{Hash, Hasher}; |
| 8 | +use std::rc::Rc; |
| 9 | + |
| 10 | +/// A Bazel label, e.g. `//foo:bar`. |
| 11 | +#[derive(Debug, Eq, Clone)] |
| 12 | +pub struct BazelLabel(pub Rc<str>); |
| 13 | + |
| 14 | +impl BazelLabel { |
| 15 | + pub fn as_str(&self) -> &str { |
| 16 | + &self.0 |
| 17 | + } |
| 18 | + |
| 19 | + /// Returns the target name. E.g. `bar` for `//foo:bar`. |
| 20 | + pub fn target_name(&self) -> &str { |
| 21 | + if let Some((_package, target_name)) = self.0.split_once(':') { |
| 22 | + return target_name; |
| 23 | + } |
| 24 | + if let Some((_, last_package_component)) = self.0.rsplit_once('/') { |
| 25 | + return last_package_component; |
| 26 | + } |
| 27 | + &self.0 |
| 28 | + } |
| 29 | + |
| 30 | + pub fn package_name(&self) -> &str { |
| 31 | + self.0.rsplit_once(':').unwrap_or((&self.0, "")).0 |
| 32 | + } |
| 33 | + |
| 34 | + fn last_package_component(&self) -> &str { |
| 35 | + self.package_name().rsplit_once('/').unwrap_or(("", "")).1 |
| 36 | + } |
| 37 | + |
| 38 | + // TODO(b/216587072): Remove this hacky escaping and use the import! macro once |
| 39 | + // available. |
| 40 | + // For now, use the simple escaping scheme of mapping all invalid characters |
| 41 | + // to underscore, instead of the one similar to `convert_to_cc_identifier`, so |
| 42 | + // that the escaped target name doesn't become longer (rustc currently produces |
| 43 | + // .o artifacts that repeat the target name twice, which can easily cause |
| 44 | + // the path length of artifacts to exceed the limit of the file system.) |
| 45 | + pub fn target_name_escaped(&self) -> String { |
| 46 | + let mut target_name = self.target_name().to_owned(); |
| 47 | + if target_name == "core" { |
| 48 | + target_name = "core_".to_owned() + self.last_package_component(); |
| 49 | + } else if target_name.starts_with(char::is_numeric) { |
| 50 | + target_name.insert(0, 'n'); |
| 51 | + } |
| 52 | + target_name.replace(|c: char| !c.is_ascii_alphanumeric(), "_") |
| 53 | + } |
| 54 | + |
| 55 | + // Returns the bazel label as a valid C++ identifier, with a leading underscore. |
| 56 | + // Non-alphanumeric characters are escaped as `_xx`, where `xx` is the the byte |
| 57 | + // as hexadecimal. |
| 58 | + // |
| 59 | + // For instance, `//foo` becomes `__2f_2ffoo`. |
| 60 | + pub fn convert_to_cc_identifier(&self) -> String { |
| 61 | + use std::fmt::Write; |
| 62 | + let mut result = "_".to_string(); |
| 63 | + result.reserve_exact(self.0.len().checked_mul(2).unwrap_or(self.0.len())); |
| 64 | + |
| 65 | + // This is yet another escaping scheme... :-/ Compare this with |
| 66 | + // https://github.com/bazelbuild/rules_rust/blob/1f2e6231de29d8fad8d21486f0d16403632700bf/rust/private/utils.bzl#L459-L586 |
| 67 | + for b in self.0.bytes() { |
| 68 | + if (b as char).is_ascii_alphanumeric() { |
| 69 | + result.push(b as char); |
| 70 | + } else { |
| 71 | + write!(result, "_{b:02x}").unwrap(); |
| 72 | + } |
| 73 | + } |
| 74 | + result.shrink_to_fit(); |
| 75 | + |
| 76 | + #[cfg(debug_assertions)] |
| 77 | + for c in result.chars() { |
| 78 | + debug_assert!( |
| 79 | + c.is_ascii_alphanumeric() || c == '_', |
| 80 | + "invalid result identifier: {result:?}" |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + result |
| 85 | + } |
| 86 | + |
| 87 | + fn components(&self) -> (&str, &str) { |
| 88 | + (self.target_name(), self.package_name()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl PartialEq for BazelLabel { |
| 93 | + fn eq(&self, other: &Self) -> bool { |
| 94 | + self.components() == other.components() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl PartialOrd for BazelLabel { |
| 99 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 100 | + Some(self.cmp(other)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Ord for BazelLabel { |
| 105 | + fn cmp(&self, other: &Self) -> Ordering { |
| 106 | + self.components().cmp(&other.components()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Hash for BazelLabel { |
| 111 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 112 | + self.components().hash(state); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<T: Into<String>> From<T> for BazelLabel { |
| 117 | + fn from(label: T) -> Self { |
| 118 | + Self(label.into().into()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl Display for BazelLabel { |
| 123 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 124 | + // If this isn't actually a known bazel target, stringify for humans as the filename. |
| 125 | + if let Some(s) = self.0.strip_prefix("//_unknown_target:") { |
| 126 | + write!(f, "{}", s) |
| 127 | + } else { |
| 128 | + write!(f, "{}", &*self.0) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments