-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
c.go
100 lines (90 loc) · 3.16 KB
/
c.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
var cProtoLibraryRuleTemplate = mustTemplate(`load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "filter_files", "proto_compile_attrs")
load("//:{{ .Lang.Name }}_{{ .Rule.Kind }}_compile.bzl", "{{ .Lang.Name }}_{{ .Rule.Kind }}_compile")
def {{ .Rule.Name }}(name, **kwargs): # buildifier: disable=function-docstring
# Compile protos
name_pb = name + "_pb"
{{ .Lang.Name }}_{{ .Rule.Kind }}_compile(
name = name_pb,
{{ .Common.CompileArgsForwardingSnippet }}
)
# Filter files to sources and headers
filter_files(
name = name_pb + "_srcs",
target = name_pb,
extensions = ["c"],
)
filter_files(
name = name_pb + "_hdrs",
target = name_pb,
extensions = ["h"],
)
# Create {{ .Lang.Name }} library
cc_library(
name = name,
srcs = [name_pb + "_srcs"],
deps = PROTO_DEPS + kwargs.get("deps", []),
hdrs = [name_pb + "_hdrs"],
includes = [name_pb],
**{
k: v
for (k, v) in kwargs.items()
if k in bazel_build_rule_common_attrs + [
"alwayslink",
"copts",
"defines",
"include_prefix",
"linkopts",
"linkstatic",
"local_defines",
"nocopts",
"strip_include_prefix",
]
}
)
PROTO_DEPS = [
Label("@upb//:upb"),
Label("@upb//:message_accessors"),
]`)
// For C, we need to manually generate the files for any.proto
var cProtoLibraryExampleTemplate = mustTemplate(`load("@rules_proto_grpc_{{ .Lang.Name }}//:defs.bzl", "{{ .Rule.Name }}")
{{ .Rule.Name }}(
name = "proto_{{ .Lang.Name }}_{{ .Rule.Kind }}",
protos = [
"@protobuf//:any_proto",
"@rules_proto_grpc_example_protos//:person_proto",
"@rules_proto_grpc_example_protos//:place_proto",
"@rules_proto_grpc_example_protos//:thing_proto",
],
)`)
var cModuleSuffixLines = `bazel_dep(name = "protobuf", version = "23.1")`
func makeC() *Language {
return &Language{
Name: "c",
DisplayName: "C",
Notes: mustTemplate("Rules for generating C protobuf ``.c`` & ``.h`` files and libraries using `upb <https://github.com/protocolbuffers/upb>`_. Libraries are created with the Bazel native ``cc_library``"),
ModuleSuffixLines: cModuleSuffixLines,
Rules: []*Rule{
&Rule{
Name: "c_proto_compile",
Kind: "proto",
Implementation: compileRuleTemplate,
Plugins: []string{"//:proto_plugin"},
BuildExample: protoCompileExampleTemplate,
Doc: "Generates C protobuf ``.h`` & ``.c`` files",
Attrs: compileRuleAttrs,
Experimental: true,
},
&Rule{
Name: "c_proto_library",
Kind: "proto",
Implementation: cProtoLibraryRuleTemplate,
BuildExample: cProtoLibraryExampleTemplate,
Doc: "Generates a C protobuf library using ``cc_library``, with dependencies linked",
Attrs: cppLibraryRuleAttrs,
Experimental: true,
},
},
}
}