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

     1  package rules_cc
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/config"
     7  	"github.com/bazelbuild/bazel-gazelle/label"
     8  	"github.com/bazelbuild/bazel-gazelle/resolve"
     9  	"github.com/bazelbuild/bazel-gazelle/rule"
    10  
    11  	"github.com/stackb/rules_proto/pkg/protoc"
    12  )
    13  
    14  var ccLibraryKindInfo = rule.KindInfo{
    15  	MergeableAttrs: map[string]bool{
    16  		"srcs": true,
    17  		"hdrs": true,
    18  	},
    19  	NonEmptyAttrs: map[string]bool{
    20  		"srcs": true,
    21  	},
    22  	ResolveAttrs: map[string]bool{
    23  		"deps": true,
    24  	},
    25  }
    26  
    27  // CcLibrary implements RuleProvider for 'cc_library'-derived rules.
    28  type CcLibrary struct {
    29  	KindName       string
    30  	RuleNameSuffix string
    31  	Outputs        []string
    32  	Config         *protoc.ProtocConfiguration
    33  	RuleConfig     *protoc.LanguageRuleConfig
    34  	Resolver       protoc.DepsResolver
    35  }
    36  
    37  // Kind implements part of the ruleProvider interface.
    38  func (s *CcLibrary) Kind() string {
    39  	return s.KindName
    40  }
    41  
    42  // Name implements part of the ruleProvider interface.
    43  func (s *CcLibrary) Name() string {
    44  	return s.Config.Library.BaseName() + s.RuleNameSuffix
    45  }
    46  
    47  // Srcs computes the srcs list for the rule.
    48  func (s *CcLibrary) Srcs() []string {
    49  	srcs := make([]string, 0)
    50  	for _, output := range s.Outputs {
    51  		if strings.HasSuffix(output, ".cc") {
    52  			srcs = append(srcs, protoc.StripRel(s.Config.Rel, output))
    53  		}
    54  	}
    55  	return srcs
    56  }
    57  
    58  // Hdrs computes the hdrs list for the rule.
    59  func (s *CcLibrary) Hdrs() []string {
    60  	hdrs := make([]string, 0)
    61  	for _, output := range s.Outputs {
    62  		if strings.HasSuffix(output, ".h") {
    63  			hdrs = append(hdrs, protoc.StripRel(s.Config.Rel, output))
    64  		}
    65  	}
    66  	return hdrs
    67  }
    68  
    69  // Deps computes the deps list for the rule.
    70  func (s *CcLibrary) Deps() []string {
    71  	deps := s.RuleConfig.GetDeps()
    72  	resolvedDeps := protoc.ResolveLibraryRewrites(s.RuleConfig.GetRewrites(), s.Config.Library)
    73  	deps = append(deps, resolvedDeps...)
    74  	return deps
    75  }
    76  
    77  // Visibility provides visibility labels.
    78  func (s *CcLibrary) Visibility() []string {
    79  	return s.RuleConfig.GetVisibility()
    80  }
    81  
    82  // Rule implements part of the ruleProvider interface.
    83  func (s *CcLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule {
    84  	newRule := rule.NewRule(s.Kind(), s.Name())
    85  
    86  	newRule.SetAttr("srcs", s.Srcs())
    87  	newRule.SetAttr("hdrs", s.Hdrs())
    88  
    89  	stripImportPrefix := s.Config.Library.StripImportPrefix()
    90  	if stripImportPrefix != "" {
    91  		newRule.SetAttr("strip_include_prefix", stripImportPrefix)
    92  	}
    93  
    94  	deps := s.Deps()
    95  	if len(deps) > 0 {
    96  		newRule.SetAttr("deps", deps)
    97  	}
    98  
    99  	visibility := s.Visibility()
   100  	if len(visibility) > 0 {
   101  		newRule.SetAttr("visibility", visibility)
   102  	}
   103  
   104  	return newRule
   105  }
   106  
   107  // Imports implements part of the RuleProvider interface.
   108  func (s *CcLibrary) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
   109  	if lib, ok := r.PrivateAttr(protoc.ProtoLibraryKey).(protoc.ProtoLibrary); ok {
   110  		return protoc.ProtoLibraryImportSpecsForKind(r.Kind(), lib)
   111  	}
   112  	return nil
   113  }
   114  
   115  // Resolve implements part of the RuleProvider interface.
   116  func (s *CcLibrary) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
   117  	if s.Resolver == nil {
   118  		return
   119  	}
   120  	s.Resolver(c, ix, r, imports, from)
   121  }