4
4
5
5
"""Provides rules for building Carbon files using the toolchain."""
6
6
7
+ load ("@rules_cc//cc/common:cc_info.bzl" , "CcInfo" )
8
+
7
9
def _carbon_binary_impl (ctx ):
8
10
toolchain_driver = ctx .executable .internal_exec_toolchain_driver
9
11
toolchain_data = ctx .files .internal_exec_toolchain_data
@@ -14,12 +16,40 @@ def _carbon_binary_impl(ctx):
14
16
toolchain_driver = ctx .executable .internal_target_toolchain_driver
15
17
toolchain_data = ctx .files .internal_target_toolchain_data
16
18
19
+ # Pass any C++ flags from our dependencies onto Carbon.
20
+ dep_flags = []
21
+ dep_hdrs = []
22
+ dep_link_flags = []
23
+ dep_link_inputs = []
24
+ for dep in ctx .attr .deps :
25
+ if CcInfo in dep :
26
+ cc_info = dep [CcInfo ]
27
+
28
+ # TODO: We should reuse the feature-based flag generation in
29
+ # bazel/cc_toolchains here.
30
+ dep_flags += ["--clang-arg=-D{0}" .format (define ) for define in cc_info .compilation_context .defines .to_list ()]
31
+ dep_flags += ["--clang-arg=-I{0}" .format (path ) for path in cc_info .compilation_context .includes .to_list ()]
32
+ dep_flags += ["--clang-arg=-iquote{0}" .format (path ) for path in cc_info .compilation_context .quote_includes .to_list ()]
33
+ dep_flags += ["--clang-arg=-isystem{0}" .format (path ) for path in cc_info .compilation_context .system_includes .to_list ()]
34
+ dep_hdrs .append (cc_info .compilation_context .headers )
35
+ for link_input in cc_info .linking_context .linker_inputs .to_list ():
36
+ # TODO: `carbon link` doesn't support linker flags yet.
37
+ # dep_link_flags += link_input.user_link_flags
38
+ dep_link_inputs += link_input .additional_inputs
39
+ for lib in link_input .libraries :
40
+ dep_link_inputs += [dep for dep in [lib .dynamic_library , lib .static_library ] if dep ]
41
+ dep_link_inputs += lib .objects
42
+ if DefaultInfo in dep :
43
+ dep_link_inputs += dep [DefaultInfo ].files .to_list ()
44
+ dep_link_flags += [dep .path for dep in dep_link_inputs ]
45
+
17
46
# Build object files for the prelude and for the binary itself.
18
47
# TODO: Eventually the prelude should be build as a separate `carbon_library`.
19
48
srcs_and_flags = [
20
49
(ctx .files .prelude_srcs , ["--no-prelude-import" ]),
21
- (ctx .files .srcs , [] ),
50
+ (ctx .files .srcs , dep_flags ),
22
51
]
52
+
23
53
objs = []
24
54
for (srcs , extra_flags ) in srcs_and_flags :
25
55
for src in srcs :
@@ -42,21 +72,21 @@ def _carbon_binary_impl(ctx):
42
72
srcs_reordered = [s for s in srcs if s != src ] + [src ]
43
73
ctx .actions .run (
44
74
outputs = [out ],
45
- inputs = srcs_reordered ,
75
+ inputs = depset ( direct = srcs_reordered , transitive = dep_hdrs ) ,
46
76
executable = toolchain_driver ,
47
77
tools = depset (toolchain_data ),
48
- arguments = ["compile" , "--output=" + out .path ] + [s .path for s in srcs_reordered ] + extra_flags ,
78
+ arguments = ["compile" , "--output=" + out .path , "--clang-arg=-stdlib=libc++" ] + [s .path for s in srcs_reordered ] + extra_flags ,
49
79
mnemonic = "CarbonCompile" ,
50
80
progress_message = "Compiling " + src .short_path ,
51
81
)
52
82
53
83
bin = ctx .actions .declare_file (ctx .label .name )
54
84
ctx .actions .run (
55
85
outputs = [bin ],
56
- inputs = objs ,
86
+ inputs = objs + dep_link_inputs ,
57
87
executable = toolchain_driver ,
58
88
tools = depset (toolchain_data ),
59
- arguments = ["link" , "--output=" + bin .path ] + [o .path for o in objs ],
89
+ arguments = ["link" , "--output=" + bin .path ] + dep_link_flags + [o .path for o in objs ],
60
90
mnemonic = "CarbonLink" ,
61
91
progress_message = "Linking " + bin .short_path ,
62
92
)
@@ -65,6 +95,7 @@ def _carbon_binary_impl(ctx):
65
95
_carbon_binary_internal = rule (
66
96
implementation = _carbon_binary_impl ,
67
97
attrs = {
98
+ "deps" : attr .label_list (allow_files = True , providers = [[CcInfo ]]),
68
99
# The exec config toolchain driver and data. These will be `None` when
69
100
# using the target config and populated when using the exec config. We
70
101
# have to use duplicate attributes here and below to have different
@@ -94,21 +125,26 @@ _carbon_binary_internal = rule(
94
125
),
95
126
"prelude_srcs" : attr .label_list (allow_files = [".carbon" ]),
96
127
"srcs" : attr .label_list (allow_files = [".carbon" ]),
128
+ "_cc_toolchain" : attr .label (default = "@bazel_tools//tools/cpp:current_cc_toolchain" ),
97
129
},
98
130
executable = True ,
99
131
)
100
132
101
- def carbon_binary (name , srcs ):
133
+ def carbon_binary (name , srcs , deps = [], tags = [] ):
102
134
"""Compiles a Carbon binary.
103
135
104
136
Args:
105
137
name: The name of the build target.
106
138
srcs: List of Carbon source files to compile.
139
+ deps: List of dependencies.
140
+ tags: Tags to apply to the rule.
107
141
"""
108
142
_carbon_binary_internal (
109
143
name = name ,
110
144
srcs = srcs ,
111
145
prelude_srcs = ["//core:prelude_files" ],
146
+ deps = deps ,
147
+ tags = tags ,
112
148
113
149
# We synthesize two sets of attributes from mirrored `select`s here
114
150
# because we want to select on an internal property of these attributes
0 commit comments