Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reuse precompiled objects of targets in srcs #176

Merged
merged 2 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions rules_fortran/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//toolchain/fortran:action_names.bzl", "ACTION_NAMES")

FortranInfo = provider(
"Information from a Fortran rule.",
fields = {"compiled_objects": "The list of compiled Fortran objects."},
)

_attrs = {
"defines": attr.string_list(
default = [],
Expand All @@ -45,8 +50,9 @@ _attrs = {
),
"srcs": attr.label_list(
allow_files = True,
default = [],
doc = "The list of Fortran source files.",
mandatory = True,
mandatory = False,
),
"linkopts": attr.string_list(
default = [],
Expand All @@ -73,6 +79,13 @@ _binary_attrs = {

def _fortran_binary_impl(ctx):
(fortran_toolchain, feature_configuration) = _get_configuration(ctx)
fortran_sources = []
precompiled_fortran_objects = []
for src in ctx.attr.srcs:
if FortranInfo in src:
precompiled_fortran_objects.extend(src[FortranInfo].compiled_objects)
else:
fortran_sources.extend(src[DefaultInfo].files.to_list())
objects = _compile(
actions = ctx.actions,
defines = ctx.attr.defines,
Expand All @@ -81,7 +94,7 @@ def _fortran_binary_impl(ctx):
fopts = ctx.attr.fopts,
fortran_toolchain = fortran_toolchain,
includes = ctx.files.includes,
srcs = ctx.files.srcs,
srcs = fortran_sources,
)
output = _link(
actions = ctx.actions,
Expand All @@ -91,13 +104,18 @@ def _fortran_binary_impl(ctx):
linkopts = ctx.attr.linkopts,
linkshared = ctx.attr.linkshared,
linkstatic = ctx.attr.linkstatic,
objects = objects,
objects = objects + precompiled_fortran_objects,
output_name = ctx.attr.name,
)

providers = [DefaultInfo(
executable = output,
)]
providers = [
DefaultInfo(
executable = output,
),
FortranInfo(
compiled_objects = objects,
),
]

if ctx.attr.linkshared:
providers.append(OutputGroupInfo(
Expand Down Expand Up @@ -133,6 +151,13 @@ fortran_binary = rule(

def _fortran_library_impl(ctx):
(fortran_toolchain, feature_configuration) = _get_configuration(ctx)
fortran_sources = []
precompiled_fortran_objects = []
for src in ctx.attr.srcs:
if FortranInfo in src:
precompiled_fortran_objects.extend(src[FortranInfo].compiled_objects)
else:
fortran_sources.extend(src[DefaultInfo].files.to_list())
objects = _compile(
actions = ctx.actions,
defines = ctx.attr.defines,
Expand All @@ -141,13 +166,13 @@ def _fortran_library_impl(ctx):
fopts = ctx.attr.fopts,
fortran_toolchain = fortran_toolchain,
includes = ctx.files.includes,
srcs = ctx.files.srcs,
srcs = fortran_sources,
)
output = _archive(
actions = ctx.actions,
feature_configuration = feature_configuration,
fortran_toolchain = fortran_toolchain,
objects = objects,
objects = objects + precompiled_fortran_objects,
output_name = ctx.attr.name,
)

Expand All @@ -157,6 +182,9 @@ def _fortran_library_impl(ctx):
files = files,
runfiles = ctx.runfiles(transitive_files = files),
),
FortranInfo(
compiled_objects = objects,
),
OutputGroupInfo(
archive = depset([output]),
includes = ctx.files.includes,
Expand Down Expand Up @@ -216,6 +244,8 @@ def _compile(
actions.declare_file(paths.replace_extension(src.path, ".o"))
for src in srcs
]
if len(objects) == 0:
return []
deps_includes = []
for dep in deps:
if hasattr(dep.output_groups, "includes"):
Expand Down
Loading