Skip to content

Commit 36e3e1f

Browse files
authored
feat: reuse precompiled objects of targets in srcs (#176)
* feat: reuse precompiled objects of targets in srcs When a target is provided to the srcs attribute of the Fortran rules, it will now reuse precompiled objects, if available. * refactor: remove unnecessary depset
1 parent acaecee commit 36e3e1f

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

rules_fortran/defs.bzl

+38-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
load("@bazel_skylib//lib:paths.bzl", "paths")
2222
load("//toolchain/fortran:action_names.bzl", "ACTION_NAMES")
2323

24+
FortranInfo = provider(
25+
"Information from a Fortran rule.",
26+
fields = {"compiled_objects": "The list of compiled Fortran objects."},
27+
)
28+
2429
_attrs = {
2530
"defines": attr.string_list(
2631
default = [],
@@ -45,8 +50,9 @@ _attrs = {
4550
),
4651
"srcs": attr.label_list(
4752
allow_files = True,
53+
default = [],
4854
doc = "The list of Fortran source files.",
49-
mandatory = True,
55+
mandatory = False,
5056
),
5157
"linkopts": attr.string_list(
5258
default = [],
@@ -73,6 +79,13 @@ _binary_attrs = {
7379

7480
def _fortran_binary_impl(ctx):
7581
(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())
7689
objects = _compile(
7790
actions = ctx.actions,
7891
defines = ctx.attr.defines,
@@ -81,7 +94,7 @@ def _fortran_binary_impl(ctx):
8194
fopts = ctx.attr.fopts,
8295
fortran_toolchain = fortran_toolchain,
8396
includes = ctx.files.includes,
84-
srcs = ctx.files.srcs,
97+
srcs = fortran_sources,
8598
)
8699
output = _link(
87100
actions = ctx.actions,
@@ -91,13 +104,18 @@ def _fortran_binary_impl(ctx):
91104
linkopts = ctx.attr.linkopts,
92105
linkshared = ctx.attr.linkshared,
93106
linkstatic = ctx.attr.linkstatic,
94-
objects = objects,
107+
objects = objects + precompiled_fortran_objects,
95108
output_name = ctx.attr.name,
96109
)
97110

98-
providers = [DefaultInfo(
99-
executable = output,
100-
)]
111+
providers = [
112+
DefaultInfo(
113+
executable = output,
114+
),
115+
FortranInfo(
116+
compiled_objects = objects,
117+
),
118+
]
101119

102120
if ctx.attr.linkshared:
103121
providers.append(OutputGroupInfo(
@@ -133,6 +151,13 @@ fortran_binary = rule(
133151

134152
def _fortran_library_impl(ctx):
135153
(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())
136161
objects = _compile(
137162
actions = ctx.actions,
138163
defines = ctx.attr.defines,
@@ -141,13 +166,13 @@ def _fortran_library_impl(ctx):
141166
fopts = ctx.attr.fopts,
142167
fortran_toolchain = fortran_toolchain,
143168
includes = ctx.files.includes,
144-
srcs = ctx.files.srcs,
169+
srcs = fortran_sources,
145170
)
146171
output = _archive(
147172
actions = ctx.actions,
148173
feature_configuration = feature_configuration,
149174
fortran_toolchain = fortran_toolchain,
150-
objects = objects,
175+
objects = objects + precompiled_fortran_objects,
151176
output_name = ctx.attr.name,
152177
)
153178

@@ -157,6 +182,9 @@ def _fortran_library_impl(ctx):
157182
files = files,
158183
runfiles = ctx.runfiles(transitive_files = files),
159184
),
185+
FortranInfo(
186+
compiled_objects = objects,
187+
),
160188
OutputGroupInfo(
161189
archive = depset([output]),
162190
includes = ctx.files.includes,
@@ -216,6 +244,8 @@ def _compile(
216244
actions.declare_file(paths.replace_extension(src.path, ".o"))
217245
for src in srcs
218246
]
247+
if len(objects) == 0:
248+
return []
219249
deps_includes = []
220250
for dep in deps:
221251
if hasattr(dep.output_groups, "includes"):

0 commit comments

Comments
 (0)