|
| 1 | +"""Aspect to validate allowed C/C++ dependencies at build time. |
| 2 | +
|
| 3 | +This aspect enforces the dependency hierarchy based on tags: |
| 4 | +- internal (level 0): Can depend on anything |
| 5 | +- prod (level 1): Can depend on prod or safe only |
| 6 | +- safe (level 2): Can depend on safe only |
| 7 | +
|
| 8 | +Additionally: |
| 9 | +- portable targets can only depend on other portable targets |
| 10 | +- Targets without level tags (including external dependencies) are ignored |
| 11 | +""" |
| 12 | + |
| 13 | +# Provider to carry dependency level information through the dependency graph |
| 14 | +DependencyLevelInfo = provider( |
| 15 | + "Information about a target's dependency level", |
| 16 | + fields = { |
| 17 | + "level": "Dependency level (0=internal, 1=prod, 2=safe, None=no level)", |
| 18 | + "portable": "Whether the target is portable", |
| 19 | + "label": "The target's label" |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | +def _get_coding_level_from_tags(tags): |
| 24 | + """Extract coding standard level from tags. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + 0 for internal, 1 for prod, 2 for safe, None if no level found |
| 28 | + """ |
| 29 | + if "internal" in tags: |
| 30 | + return 0 |
| 31 | + if "prod" in tags: |
| 32 | + return 1 |
| 33 | + if "safe" in tags: |
| 34 | + return 2 |
| 35 | + return None |
| 36 | + |
| 37 | +def _level_to_str(level): |
| 38 | + """Convert numeric level to string.""" |
| 39 | + if level == 0: |
| 40 | + return "internal" |
| 41 | + if level == 1: |
| 42 | + return "prod" |
| 43 | + if level == 2: |
| 44 | + return "safe" |
| 45 | + return "unknown" |
| 46 | + |
| 47 | +def _is_portable(tags): |
| 48 | + """Check if target is marked as portable.""" |
| 49 | + return "portable" in tags |
| 50 | + |
| 51 | +def _check_allowed_cc_deps_impl(target, ctx): |
| 52 | + """Aspect implementation to check allowed C/C++ dependencies.""" |
| 53 | + |
| 54 | + # Skip non-C/C++ targets |
| 55 | + if not CcInfo in target: |
| 56 | + return [] |
| 57 | + |
| 58 | + # Skip if this is not a rule (e.g., a source file) |
| 59 | + if not hasattr(ctx.rule, "attr"): |
| 60 | + return [] |
| 61 | + |
| 62 | + # Get the current target's tags |
| 63 | + current_tags = getattr(ctx.rule.attr, "tags", []) |
| 64 | + current_level = _get_coding_level_from_tags(current_tags) |
| 65 | + current_portable = _is_portable(current_tags) |
| 66 | + label_str = str(target.label) |
| 67 | + |
| 68 | + # If target has no dependency level, skip validation |
| 69 | + # This includes external dependencies, test libraries, and other untagged targets |
| 70 | + if current_level == None: |
| 71 | + # Check if it's a test target - tests are implicitly internal (level 0) |
| 72 | + if "test" in current_tags or "test_library" in current_tags: |
| 73 | + current_level = 0 |
| 74 | + else: |
| 75 | + # Return provider indicating no level (external deps, untagged targets, etc.) |
| 76 | + return [DependencyLevelInfo( |
| 77 | + level = None, |
| 78 | + portable = current_portable, |
| 79 | + label = label_str |
| 80 | + )] |
| 81 | + |
| 82 | + # Check dependencies |
| 83 | + deps = getattr(ctx.rule.attr, "deps", []) |
| 84 | + |
| 85 | + for dep in deps: |
| 86 | + # Get dependency level info from dependency via provider |
| 87 | + if not DependencyLevelInfo in dep: |
| 88 | + # Dependency doesn't have dependency level info - skip it |
| 89 | + continue |
| 90 | + |
| 91 | + dep_info = dep[DependencyLevelInfo] |
| 92 | + dep_level = dep_info.level |
| 93 | + dep_portable = dep_info.portable |
| 94 | + |
| 95 | + # If dependency has no level, skip validation |
| 96 | + # This handles external dependencies and untagged targets |
| 97 | + if dep_level == None: |
| 98 | + continue |
| 99 | + |
| 100 | + # Check dependency level hierarchy |
| 101 | + if dep_level < current_level: |
| 102 | + error_msg = ("ERROR: Target {} (level={}) cannot depend on {} (level={}). " + |
| 103 | + "Higher level targets can only depend on same or higher level targets.").format( |
| 104 | + target.label, |
| 105 | + _level_to_str(current_level), |
| 106 | + dep_info.label, |
| 107 | + _level_to_str(dep_level) |
| 108 | + ) |
| 109 | + fail(error_msg) |
| 110 | + |
| 111 | + # Check portability requirements |
| 112 | + if current_portable and not dep_portable: |
| 113 | + error_msg = ("ERROR: Target {} is marked as portable but depends on {} which is not portable. " + |
| 114 | + "Portable targets can only depend on other portable targets.").format( |
| 115 | + target.label, |
| 116 | + dep_info.label |
| 117 | + ) |
| 118 | + fail(error_msg) |
| 119 | + |
| 120 | + # Return provider for this target |
| 121 | + return [DependencyLevelInfo( |
| 122 | + level = current_level, |
| 123 | + portable = current_portable, |
| 124 | + label = label_str |
| 125 | + )] |
| 126 | + |
| 127 | +check_allowed_cc_deps = aspect( |
| 128 | + implementation = _check_allowed_cc_deps_impl, |
| 129 | + attr_aspects = ["deps"], |
| 130 | + attrs = {} |
| 131 | +) |
| 132 | + |
| 133 | +def _validate_allowed_deps_rule_impl(ctx): |
| 134 | + """Rule that applies the validation aspect to specified targets.""" |
| 135 | + # This rule doesn't produce any output, it just triggers the aspect |
| 136 | + output = ctx.actions.declare_file(ctx.label.name + ".validation") |
| 137 | + ctx.actions.write( |
| 138 | + output = output, |
| 139 | + content = "Allowed C/C++ dependency validation passed\n" |
| 140 | + ) |
| 141 | + return [DefaultInfo(files = depset([output]))] |
| 142 | + |
| 143 | +validate_allowed_cc_deps = rule( |
| 144 | + implementation = _validate_allowed_deps_rule_impl, |
| 145 | + attrs = { |
| 146 | + "targets": attr.label_list( |
| 147 | + aspects = [check_allowed_cc_deps], |
| 148 | + doc = "Targets to validate" |
| 149 | + ) |
| 150 | + }, |
| 151 | + doc = "Validates allowed C/C++ dependencies for specified targets" |
| 152 | +) |
| 153 | + |
| 154 | +def validate_all_allowed_cc_deps(name, targets = ["//..."]): |
| 155 | + """Macro to create a validation target for allowed C/C++ dependencies. |
| 156 | +
|
| 157 | + Args: |
| 158 | + name: Name of the validation target |
| 159 | + targets: List of target patterns to validate (default: all targets) |
| 160 | + """ |
| 161 | + validate_allowed_cc_deps( |
| 162 | + name = name, |
| 163 | + targets = targets, |
| 164 | + tags = ["manual"] # Don't build by default, only when explicitly requested |
| 165 | + ) |
0 commit comments