Skip to content

Commit

Permalink
Extract calls to attr_deps
Browse files Browse the repository at this point in the history
Summary: Just a pure refactoring to highlight that `haskell_library_impl` is incorrectly processing the link infos

Reviewed By: simonmar

Differential Revision: D52778010

fbshipit-source-id: d6cd10ad294684198108ffd9e478322dad3e4f61
  • Loading branch information
Pepe Iborra authored and facebook-github-bot committed Jan 15, 2024
1 parent f251361 commit 80ce3f6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 36 deletions.
45 changes: 9 additions & 36 deletions prelude/haskell/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ load(
load(
"@prelude//haskell:link_info.bzl",
"HaskellLinkInfo",
"HaskellProfLinkInfo",
"attr_link_style",
"cxx_toolchain_link_style",
"merge_haskell_link_infos",
Expand All @@ -65,6 +66,10 @@ load(
load(
"@prelude//haskell:util.bzl",
"attr_deps",
"attr_deps_haskell_link_infos_sans_template_deps",
"attr_deps_merged_link_infos",
"attr_deps_profiling_link_infos",
"attr_deps_shared_library_infos",
"get_artifact_suffix",
"is_haskell_src",
"output_extensions",
Expand Down Expand Up @@ -133,21 +138,6 @@ HaskellIndexInfo = provider(
},
)

# HaskellProfLinkInfo exposes the MergedLinkInfo of a target and all of its
# dependencies built for profiling. This allows top-level targets (e.g.
# `haskell_binary`) to be defined with profiling enabled by default.
HaskellProfLinkInfo = provider(
fields = {
"prof_infos": provider_field(typing.Any, default = None), # MergedLinkInfo
},
)

# --

# Disable until we have a need to call this.
# def _attr_deps_merged_link_infos(ctx: AnalysisContext) -> [MergedLinkInfo]:
# return filter(None, [d[MergedLinkInfo] for d in attr_deps(ctx)])

# This conversion is non-standard, see TODO about link style below
def _to_lib_output_style(link_style: LinkStyle) -> LibOutputStyle:
return default_output_style_for_link_strategy(to_link_strategy(link_style))
Expand Down Expand Up @@ -638,27 +628,10 @@ def haskell_library_impl(ctx: AnalysisContext) -> list[Provider]:
preferred_linkage = Linkage("static")

# Get haskell and native link infos from all deps
hlis = []
nlis = []
prof_nlis = []
shared_library_infos = []
for lib in attr_deps(ctx):
li = lib.get(HaskellLinkInfo)
if li != None:
hlis.append(li)
li = lib.get(MergedLinkInfo)
if li != None:
nlis.append(li)
if HaskellLinkInfo not in lib:
# MergedLinkInfo from non-haskell deps should be part of the
# profiling MergedLinkInfo
prof_nlis.append(li)
li = lib.get(HaskellProfLinkInfo)
if li != None:
prof_nlis.append(li.prof_infos)
li = lib.get(SharedLibraryInfo)
if li != None:
shared_library_infos.append(li)
hlis = attr_deps_haskell_link_infos_sans_template_deps(ctx)
nlis = attr_deps_merged_link_infos(ctx)
prof_nlis = attr_deps_profiling_link_infos(ctx)
shared_library_infos = attr_deps_shared_library_infos(ctx)

solibs = {}
link_infos = {}
Expand Down
9 changes: 9 additions & 0 deletions prelude/haskell/link_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ HaskellLinkInfo = provider(
},
)

# HaskellProfLinkInfo exposes the MergedLinkInfo of a target and all of its
# dependencies built for profiling. This allows top-level targets (e.g.
# `haskell_binary`) to be defined with profiling enabled by default.
HaskellProfLinkInfo = provider(
fields = {
"prof_infos": provider_field(typing.Any, default = None), # MergedLinkInfo
},
)

def merge_haskell_link_infos(deps: list[HaskellLinkInfo]) -> HaskellLinkInfo:
merged = {}
prof_merged = {}
Expand Down
43 changes: 43 additions & 0 deletions prelude/haskell/util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ load(
load(
"@prelude//haskell:link_info.bzl",
"HaskellLinkInfo",
"HaskellProfLinkInfo",
)
load(
"@prelude//linking:link_info.bzl",
"LinkStyle",
"MergedLinkInfo",
)
load(
"@prelude//linking:shared_libraries.bzl",
"SharedLibraryInfo",
)
load("@prelude//utils:platform_flavors_util.bzl", "by_platform")
load("@prelude//utils:utils.bzl", "flatten")
Expand Down Expand Up @@ -67,6 +73,16 @@ def attr_deps_haskell_link_infos(ctx: AnalysisContext) -> list[HaskellLinkInfo]:
],
)

# DONT CALL THIS FUNCTION, you want attr_deps_haskell_link_infos instead
def attr_deps_haskell_link_infos_sans_template_deps(ctx: AnalysisContext) -> list[HaskellLinkInfo]:
return filter(
None,
[
d.get(HaskellLinkInfo)
for d in attr_deps(ctx)
],
)

def attr_deps_haskell_lib_infos(
ctx: AnalysisContext,
link_style: LinkStyle,
Expand All @@ -81,6 +97,33 @@ def attr_deps_haskell_lib_infos(
])
]

def attr_deps_merged_link_infos(ctx: AnalysisContext) -> list[MergedLinkInfo]:
return filter(
None,
[
d.get(MergedLinkInfo)
for d in attr_deps(ctx)
],
)

def attr_deps_profiling_link_infos(ctx: AnalysisContext) -> list[MergedLinkInfo]:
return filter(
None,
[
d.get(HaskellProfLinkInfo).prof_infos if d.get(HaskellProfLinkInfo) else d.get(MergedLinkInfo)
for d in attr_deps(ctx)
],
)

def attr_deps_shared_library_infos(ctx: AnalysisContext) -> list[SharedLibraryInfo]:
return filter(
None,
[
d.get(SharedLibraryInfo)
for d in attr_deps(ctx)
],
)

def _link_style_extensions(link_style: LinkStyle) -> (str, str):
if link_style == LinkStyle("shared"):
return ("dyn_o", "dyn_hi")
Expand Down

0 comments on commit 80ce3f6

Please sign in to comment.