|
| 1 | +# Copyright 2019-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 2 | + |
| 3 | +load("//aspects:extensions.bzl", "EXTENSIONS", "TOOLCHAINS") |
| 4 | +load("//aspects:utils/utils.bzl", "ALL_DEPS", "COMPILE_DEPS", "PRIVATE_COMPILE_DEPS", "RUNTIME_DEPS", "abs", "collect_targets_from_attrs", "create_struct", "file_location", "get_aspect_ids", "update_sync_output_groups") |
| 5 | +load("//aspects:rules/java/java_info.bzl", "java_info_in_target", "java_info_reference") |
| 6 | + |
| 7 | +def create_all_extension_info(target, ctx, output_groups, dep_targets): |
| 8 | + all_info = [create_extension_info(target = target, ctx = ctx, output_groups = output_groups, dep_targets = dep_targets) for create_extension_info in EXTENSIONS] |
| 9 | + return [(info, exported_properties) for info, exported_properties in all_info if info != None] |
| 10 | + |
| 11 | +COMPILE = 0 |
| 12 | +RUNTIME = 1 |
| 13 | + |
| 14 | +def make_dep(dep, dependency_type): |
| 15 | + return struct( |
| 16 | + id = str(dep.bsp_info.id), |
| 17 | + dependency_type = dependency_type, |
| 18 | + ) |
| 19 | + |
| 20 | +def make_deps(deps, dependency_type): |
| 21 | + return [make_dep(dep, dependency_type) for dep in deps] |
| 22 | + |
| 23 | +def _is_proto_library_wrapper(target, ctx): |
| 24 | + if not ctx.rule.kind.endswith("proto_library") or ctx.rule.kind == "proto_library": |
| 25 | + return False |
| 26 | + |
| 27 | + deps = collect_targets_from_attrs(ctx.rule.attr, ["deps"]) |
| 28 | + return len(deps) == 1 and deps[0].bsp_info and deps[0].bsp_info.kind == "proto_library" |
| 29 | + |
| 30 | +def _get_forwarded_deps(target, ctx): |
| 31 | + if _is_proto_library_wrapper(target, ctx): |
| 32 | + return collect_targets_from_attrs(ctx.rule.attr, ["deps"]) |
| 33 | + return [] |
| 34 | + |
| 35 | +def _is_analysis_test(target): |
| 36 | + """Returns if the target is an analysis test. |
| 37 | + |
| 38 | + Rules created with analysis_test=True cannot create write actions, so the |
| 39 | + aspect should skip them. |
| 40 | + """ |
| 41 | + return AnalysisTestResultInfo in target |
| 42 | + |
| 43 | +def files_to_list(source): |
| 44 | + if not hasattr(source, "files"): |
| 45 | + return [] |
| 46 | + return source.files.to_list() |
| 47 | + |
| 48 | +def _bsp_target_info_aspect_impl(target, ctx): |
| 49 | + if target.label.name.endswith(".semanticdb") or _is_analysis_test(target): |
| 50 | + return [] |
| 51 | + |
| 52 | + rule_attrs = ctx.rule.attr |
| 53 | + rule_kind = ctx.rule.kind |
| 54 | + |
| 55 | + direct_dep_targets = collect_targets_from_attrs(rule_attrs, COMPILE_DEPS) |
| 56 | + private_direct_dep_targets = collect_targets_from_attrs(rule_attrs, PRIVATE_COMPILE_DEPS) |
| 57 | + direct_deps = make_deps(direct_dep_targets, COMPILE) |
| 58 | + |
| 59 | + exported_deps_from_deps = [] |
| 60 | + if ${isPropagateExportsFromDepsEnabled}: |
| 61 | + for dep in direct_dep_targets: |
| 62 | + exported_deps_from_deps = exported_deps_from_deps + dep.bsp_info.export_deps |
| 63 | + |
| 64 | + compile_deps = direct_deps + exported_deps_from_deps |
| 65 | + |
| 66 | + runtime_dep_targets = collect_targets_from_attrs(rule_attrs, RUNTIME_DEPS) |
| 67 | + runtime_deps = make_deps(runtime_dep_targets, RUNTIME) |
| 68 | + |
| 69 | + all_deps = depset(compile_deps + runtime_deps).to_list() |
| 70 | + |
| 71 | + # Propagate my own exports |
| 72 | + export_deps = [] |
| 73 | + direct_exports = [] |
| 74 | + if java_info_in_target(target): |
| 75 | + direct_exports = collect_targets_from_attrs(rule_attrs, ["exports"]) |
| 76 | + export_deps.extend(make_deps(direct_exports, COMPILE)) |
| 77 | + for export in direct_exports: |
| 78 | + export_deps.extend(export.bsp_info.export_deps) |
| 79 | + export_deps = depset(export_deps).to_list() |
| 80 | + |
| 81 | + forwarded_deps = _get_forwarded_deps(target, ctx) + direct_exports |
| 82 | + |
| 83 | + dep_targets = direct_dep_targets + private_direct_dep_targets + runtime_dep_targets + direct_exports |
| 84 | + output_groups = dict() |
| 85 | + for dep in dep_targets: |
| 86 | + for k, v in dep.bsp_info.output_groups.items(): |
| 87 | + output_groups[k] = output_groups[k] + [v] if k in output_groups else [v] |
| 88 | + |
| 89 | + for k, v in output_groups.items(): |
| 90 | + output_groups[k] = depset(transitive = v) |
| 91 | + |
| 92 | + srcs_attr = getattr(ctx.rule.attr, "srcs", []) |
| 93 | + sources = [] |
| 94 | + generated_sources = [] |
| 95 | + |
| 96 | + if type(srcs_attr) == "list": |
| 97 | + all_sources = [ |
| 98 | + file_location(f) |
| 99 | + for t in srcs_attr |
| 100 | + for f in files_to_list(t) |
| 101 | + if not f.is_directory |
| 102 | + ] |
| 103 | + |
| 104 | + sources = [ |
| 105 | + s |
| 106 | + for s in all_sources |
| 107 | + if s.is_source |
| 108 | + ] |
| 109 | + |
| 110 | + generated_sources = [ |
| 111 | + s |
| 112 | + for s in all_sources |
| 113 | + if not s.is_source |
| 114 | + ] |
| 115 | + |
| 116 | + resources_attr = getattr(ctx.rule.attr, "resources", []) |
| 117 | + resources = [] |
| 118 | + |
| 119 | + if type(resources_attr) == "list": |
| 120 | + resources = [ |
| 121 | + file_location(f) |
| 122 | + for t in resources_attr |
| 123 | + for f in files_to_list(t) |
| 124 | + ] |
| 125 | + |
| 126 | + aspect_ids = get_aspect_ids(ctx, target) |
| 127 | + |
| 128 | + default_info = target[DefaultInfo] |
| 129 | + executable = default_info and default_info.files_to_run.executable != None |
| 130 | + |
| 131 | + env = getattr(rule_attrs, "env", {}) |
| 132 | + if type(env) == "Target": |
| 133 | + env = str(env.label) |
| 134 | + |
| 135 | + info = dict( |
| 136 | + id = str(target.label), |
| 137 | + kind = rule_kind, |
| 138 | + tags = rule_attrs.tags, |
| 139 | + dependencies = list(all_deps), |
| 140 | + sources = sources, |
| 141 | + generated_sources = generated_sources, |
| 142 | + resources = resources, |
| 143 | + env = env, |
| 144 | + env_inherit = getattr(rule_attrs, "env_inherit", []), |
| 145 | + executable = executable, |
| 146 | + ) |
| 147 | + |
| 148 | + exported_properties = dict( |
| 149 | + id = target.label, |
| 150 | + kind = rule_kind, |
| 151 | + export_deps = export_deps, |
| 152 | + output_groups = output_groups, |
| 153 | + ) |
| 154 | + |
| 155 | + all_extension_info = create_all_extension_info(target, ctx, output_groups, dep_targets) |
| 156 | + for (extension_info, extension_exported_properties) in all_extension_info: |
| 157 | + info.update(extension_info) |
| 158 | + if extension_exported_properties != None: |
| 159 | + exported_properties.update(extension_exported_properties) |
| 160 | + |
| 161 | + file_name = target.label.name |
| 162 | + file_name = file_name + "-" + str(abs(hash(file_name))) |
| 163 | + if aspect_ids: |
| 164 | + file_name = file_name + "-" + str(abs(hash(".".join(aspect_ids)))) |
| 165 | + file_name = "bsp.%s.bsp-info.textproto" % file_name |
| 166 | + info_file = ctx.actions.declare_file(file_name) |
| 167 | + ctx.actions.write(info_file, proto.encode_text(create_struct(**info))) |
| 168 | + update_sync_output_groups(output_groups, "bsp-target-info", depset([info_file])) |
| 169 | + |
| 170 | + return struct( |
| 171 | + bsp_info = struct(**exported_properties), |
| 172 | + output_groups = output_groups, |
| 173 | + ) |
| 174 | + |
| 175 | +bsp_target_info_aspect = aspect( |
| 176 | + implementation = _bsp_target_info_aspect_impl, |
| 177 | + required_aspect_providers = [java_info_reference()], |
| 178 | + attr_aspects = ALL_DEPS, |
| 179 | + toolchains = TOOLCHAINS, |
| 180 | + fragments = ["cpp"] |
| 181 | +) |
0 commit comments