github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/kinds.go (about)

     1  /* Copyright 2018 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package golang
    17  
    18  import (
    19  	"fmt"
    20  	"github.com/bazelbuild/bazel-gazelle/rule"
    21  )
    22  
    23  var goKinds = map[string]rule.KindInfo{
    24  	"alias": {
    25  		NonEmptyAttrs:  map[string]bool{"actual": true},
    26  		MergeableAttrs: map[string]bool{"actual": true},
    27  	},
    28  	"filegroup": {
    29  		NonEmptyAttrs:  map[string]bool{"srcs": true},
    30  		MergeableAttrs: map[string]bool{"srcs": true},
    31  	},
    32  	"go_binary": {
    33  		MatchAny: true,
    34  		NonEmptyAttrs: map[string]bool{
    35  			"deps":  true,
    36  			"embed": true,
    37  			"srcs":  true,
    38  		},
    39  		SubstituteAttrs: map[string]bool{"embed": true},
    40  		MergeableAttrs: map[string]bool{
    41  			"cgo":       true,
    42  			"clinkopts": true,
    43  			"cppopts":   true,
    44  			"copts":     true,
    45  			"cxxopts":   true,
    46  			"embed":     true,
    47  			"embedsrcs": true,
    48  			"srcs":      true,
    49  		},
    50  		ResolveAttrs: map[string]bool{"deps": true},
    51  	},
    52  	"go_library": {
    53  		MatchAttrs: []string{"importpath"},
    54  		NonEmptyAttrs: map[string]bool{
    55  			"deps":  true,
    56  			"embed": true,
    57  			"srcs":  true,
    58  		},
    59  		SubstituteAttrs: map[string]bool{
    60  			"embed": true,
    61  		},
    62  		MergeableAttrs: map[string]bool{
    63  			"cgo":        true,
    64  			"clinkopts":  true,
    65  			"cppopts":    true,
    66  			"copts":      true,
    67  			"cxxopts":    true,
    68  			"embed":      true,
    69  			"embedsrcs":  true,
    70  			"importmap":  true,
    71  			"importpath": true,
    72  			"srcs":       true,
    73  		},
    74  		ResolveAttrs: map[string]bool{"deps": true},
    75  	},
    76  	"go_proto_library": {
    77  		MatchAttrs: []string{"importpath"},
    78  		NonEmptyAttrs: map[string]bool{
    79  			"deps":  true,
    80  			"embed": true,
    81  			"proto": true,
    82  			"srcs":  true,
    83  		},
    84  		SubstituteAttrs: map[string]bool{"proto": true},
    85  		MergeableAttrs: map[string]bool{
    86  			"srcs":       true,
    87  			"importpath": true,
    88  			"importmap":  true,
    89  			"cgo":        true,
    90  			"clinkopts":  true,
    91  			"cppopts":    true,
    92  			"copts":      true,
    93  			"cxxopts":    true,
    94  			"embed":      true,
    95  			"proto":      true,
    96  			"compilers":  true,
    97  		},
    98  		ResolveAttrs: map[string]bool{"deps": true},
    99  	},
   100  	"go_repository": {
   101  		MatchAttrs: []string{"importpath"},
   102  		NonEmptyAttrs: map[string]bool{
   103  			"importpath": true,
   104  		},
   105  		MergeableAttrs: map[string]bool{
   106  			"commit":       true,
   107  			"build_tags":   true,
   108  			"importpath":   true,
   109  			"remote":       true,
   110  			"replace":      true,
   111  			"sha256":       true,
   112  			"strip_prefix": true,
   113  			"sum":          true,
   114  			"tag":          true,
   115  			"type":         true,
   116  			"urls":         true,
   117  			"vcs":          true,
   118  			"version":      true,
   119  		},
   120  	},
   121  	"go_test": {
   122  		NonEmptyAttrs: map[string]bool{
   123  			"deps":  true,
   124  			"embed": true,
   125  			"srcs":  true,
   126  		},
   127  		MergeableAttrs: map[string]bool{
   128  			"cgo":       true,
   129  			"clinkopts": true,
   130  			"cppopts":   true,
   131  			"copts":     true,
   132  			"cxxopts":   true,
   133  			"embed":     true,
   134  			"embedsrcs": true,
   135  			"srcs":      true,
   136  		},
   137  		ResolveAttrs: map[string]bool{"deps": true},
   138  	},
   139  	// HACK(#834): remove when bazelbuild/rules_go#2374 is resolved.
   140  	"go_tool_library": {
   141  		MatchAttrs: []string{"importpath"},
   142  		NonEmptyAttrs: map[string]bool{
   143  			"deps":  true,
   144  			"embed": true,
   145  			"srcs":  true,
   146  		},
   147  		SubstituteAttrs: map[string]bool{
   148  			"embed": true,
   149  		},
   150  		MergeableAttrs: map[string]bool{
   151  			"cgo":        true,
   152  			"clinkopts":  true,
   153  			"cppopts":    true,
   154  			"copts":      true,
   155  			"cxxopts":    true,
   156  			"embed":      true,
   157  			"importmap":  true,
   158  			"importpath": true,
   159  			"srcs":       true,
   160  		},
   161  		ResolveAttrs: map[string]bool{"deps": true},
   162  	},
   163  }
   164  
   165  func (*goLang) Kinds() map[string]rule.KindInfo { return goKinds }
   166  
   167  func (*goLang) Loads() []rule.LoadInfo {
   168  	panic("ApparentLoads should be called instead")
   169  }
   170  
   171  func (*goLang) ApparentLoads(moduleToApparentName func(string) string) []rule.LoadInfo {
   172  	return apparentLoads(moduleToApparentName)
   173  }
   174  
   175  func apparentLoads(moduleToApparentName func(string) string) []rule.LoadInfo {
   176  	rulesGo := moduleToApparentName("rules_go")
   177  	if rulesGo == "" {
   178  		rulesGo = "io_bazel_rules_go"
   179  	}
   180  	gazelle := moduleToApparentName("gazelle")
   181  	if gazelle == "" {
   182  		gazelle = "bazel_gazelle"
   183  	}
   184  
   185  	return []rule.LoadInfo{
   186  		{
   187  			Name: fmt.Sprintf("@%s//go:def.bzl", rulesGo),
   188  			Symbols: []string{
   189  				"cgo_library",
   190  				"go_binary",
   191  				"go_library",
   192  				"go_prefix",
   193  				"go_repository",
   194  				"go_test",
   195  				"go_tool_library",
   196  			},
   197  		}, {
   198  			Name: fmt.Sprintf("@%s//proto:def.bzl", rulesGo),
   199  			Symbols: []string{
   200  				"go_grpc_library",
   201  				"go_proto_library",
   202  			},
   203  		}, {
   204  			Name: fmt.Sprintf("@%s//:deps.bzl", gazelle),
   205  			Symbols: []string{
   206  				"go_repository",
   207  			},
   208  			After: []string{
   209  				"go_rules_dependencies",
   210  				"go_register_toolchains",
   211  				"gazelle_dependencies",
   212  			},
   213  		},
   214  	}
   215  }
   216  
   217  var goLoadsForTesting = apparentLoads(func(string) string { return "" })