github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/rule/rules_cc/grpc_cc_library.go (about)

     1  package rules_cc
     2  
     3  import (
     4  	"github.com/bazelbuild/bazel-gazelle/config"
     5  	"github.com/bazelbuild/bazel-gazelle/label"
     6  	"github.com/bazelbuild/bazel-gazelle/resolve"
     7  	"github.com/bazelbuild/bazel-gazelle/rule"
     8  
     9  	"github.com/stackb/rules_proto/pkg/protoc"
    10  )
    11  
    12  const (
    13  	grpcCcLibraryRuleName   = "grpc_cc_library"
    14  	grpcCcLibraryRuleSuffix = "_grpc_cc_library"
    15  )
    16  
    17  func init() {
    18  	protoc.Rules().MustRegisterRule("stackb:rules_proto:grpc_cc_library", &grpcCcLibrary{})
    19  }
    20  
    21  // grpcCcLibrary implements LanguageRule for the 'grpc_cc_library' rule from
    22  // @rules_proto.
    23  type grpcCcLibrary struct{}
    24  
    25  // Name implements part of the LanguageRule interface.
    26  func (s *grpcCcLibrary) Name() string {
    27  	return grpcCcLibraryRuleName
    28  }
    29  
    30  // KindInfo implements part of the LanguageRule interface.
    31  func (s *grpcCcLibrary) KindInfo() rule.KindInfo {
    32  	return ccLibraryKindInfo
    33  }
    34  
    35  // LoadInfo implements part of the LanguageRule interface.
    36  func (s *grpcCcLibrary) LoadInfo() rule.LoadInfo {
    37  	return rule.LoadInfo{
    38  		Name:    "@build_stack_rules_proto//rules/cc:grpc_cc_library.bzl",
    39  		Symbols: []string{grpcCcLibraryRuleName},
    40  	}
    41  }
    42  
    43  // ProvideRule implements part of the LanguageRule interface.
    44  func (s *grpcCcLibrary) ProvideRule(cfg *protoc.LanguageRuleConfig, pc *protoc.ProtocConfiguration) protoc.RuleProvider {
    45  	outputs := pc.GetPluginOutputs("grpc:grpc:cpp")
    46  	if len(outputs) == 0 {
    47  		return nil
    48  	}
    49  
    50  	return &CcLibrary{
    51  		KindName:       grpcCcLibraryRuleName,
    52  		RuleNameSuffix: grpcCcLibraryRuleSuffix,
    53  		Outputs:        outputs,
    54  		RuleConfig:     cfg,
    55  		Config:         pc,
    56  		Resolver: func(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
    57  			deps := append(r.AttrStrings("deps"), ":"+pc.Library.BaseName()+ProtoCcLibraryRuleSuffix)
    58  
    59  			if len(deps) > 0 {
    60  				r.SetAttr("deps", deps)
    61  			}
    62  		},
    63  	}
    64  }