21
21
load ("@bazel_skylib//lib:paths.bzl" , "paths" )
22
22
load ("//toolchain/fortran:action_names.bzl" , "ACTION_NAMES" )
23
23
24
+ FortranInfo = provider (
25
+ "Information from a Fortran rule." ,
26
+ fields = {"compiled_objects" : "The list of compiled Fortran objects." },
27
+ )
28
+
24
29
_attrs = {
25
30
"defines" : attr .string_list (
26
31
default = [],
@@ -45,8 +50,9 @@ _attrs = {
45
50
),
46
51
"srcs" : attr .label_list (
47
52
allow_files = True ,
53
+ default = [],
48
54
doc = "The list of Fortran source files." ,
49
- mandatory = True ,
55
+ mandatory = False ,
50
56
),
51
57
"linkopts" : attr .string_list (
52
58
default = [],
@@ -73,6 +79,13 @@ _binary_attrs = {
73
79
74
80
def _fortran_binary_impl (ctx ):
75
81
(fortran_toolchain , feature_configuration ) = _get_configuration (ctx )
82
+ fortran_sources = []
83
+ precompiled_fortran_objects = []
84
+ for src in ctx .attr .srcs :
85
+ if FortranInfo in src :
86
+ precompiled_fortran_objects .extend (src [FortranInfo ].compiled_objects )
87
+ else :
88
+ fortran_sources .extend (src [DefaultInfo ].files .to_list ())
76
89
objects = _compile (
77
90
actions = ctx .actions ,
78
91
defines = ctx .attr .defines ,
@@ -81,7 +94,7 @@ def _fortran_binary_impl(ctx):
81
94
fopts = ctx .attr .fopts ,
82
95
fortran_toolchain = fortran_toolchain ,
83
96
includes = ctx .files .includes ,
84
- srcs = ctx . files . srcs ,
97
+ srcs = fortran_sources ,
85
98
)
86
99
output = _link (
87
100
actions = ctx .actions ,
@@ -91,13 +104,18 @@ def _fortran_binary_impl(ctx):
91
104
linkopts = ctx .attr .linkopts ,
92
105
linkshared = ctx .attr .linkshared ,
93
106
linkstatic = ctx .attr .linkstatic ,
94
- objects = objects ,
107
+ objects = objects + precompiled_fortran_objects ,
95
108
output_name = ctx .attr .name ,
96
109
)
97
110
98
- providers = [DefaultInfo (
99
- executable = output ,
100
- )]
111
+ providers = [
112
+ DefaultInfo (
113
+ executable = output ,
114
+ ),
115
+ FortranInfo (
116
+ compiled_objects = objects ,
117
+ ),
118
+ ]
101
119
102
120
if ctx .attr .linkshared :
103
121
providers .append (OutputGroupInfo (
@@ -133,6 +151,13 @@ fortran_binary = rule(
133
151
134
152
def _fortran_library_impl (ctx ):
135
153
(fortran_toolchain , feature_configuration ) = _get_configuration (ctx )
154
+ fortran_sources = []
155
+ precompiled_fortran_objects = []
156
+ for src in ctx .attr .srcs :
157
+ if FortranInfo in src :
158
+ precompiled_fortran_objects .extend (src [FortranInfo ].compiled_objects )
159
+ else :
160
+ fortran_sources .extend (src [DefaultInfo ].files .to_list ())
136
161
objects = _compile (
137
162
actions = ctx .actions ,
138
163
defines = ctx .attr .defines ,
@@ -141,13 +166,13 @@ def _fortran_library_impl(ctx):
141
166
fopts = ctx .attr .fopts ,
142
167
fortran_toolchain = fortran_toolchain ,
143
168
includes = ctx .files .includes ,
144
- srcs = ctx . files . srcs ,
169
+ srcs = fortran_sources ,
145
170
)
146
171
output = _archive (
147
172
actions = ctx .actions ,
148
173
feature_configuration = feature_configuration ,
149
174
fortran_toolchain = fortran_toolchain ,
150
- objects = objects ,
175
+ objects = objects + precompiled_fortran_objects ,
151
176
output_name = ctx .attr .name ,
152
177
)
153
178
@@ -157,6 +182,9 @@ def _fortran_library_impl(ctx):
157
182
files = files ,
158
183
runfiles = ctx .runfiles (transitive_files = files ),
159
184
),
185
+ FortranInfo (
186
+ compiled_objects = objects ,
187
+ ),
160
188
OutputGroupInfo (
161
189
archive = depset ([output ]),
162
190
includes = ctx .files .includes ,
@@ -216,6 +244,8 @@ def _compile(
216
244
actions .declare_file (paths .replace_extension (src .path , ".o" ))
217
245
for src in srcs
218
246
]
247
+ if len (objects ) == 0 :
248
+ return []
219
249
deps_includes = []
220
250
for dep in deps :
221
251
if hasattr (dep .output_groups , "includes" ):
0 commit comments