Skip to content

Commit

Permalink
extra_libraries. native C/C++ libraries can be provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
wavewave committed Oct 30, 2024
1 parent aafe518 commit 491741b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions decls/haskell_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def _use_argsfile_at_link_arg():
"""),
}

def _extra_libraries_arg():
return {
"extra_libraries": attrs.list(attrs.dep(), default = [], doc = """
Non-Haskell deps (C/C++ libraries)
"""),
}

haskell_common = struct(
srcs_arg = _srcs_arg,
deps_arg = _deps_arg,
Expand All @@ -85,4 +92,5 @@ haskell_common = struct(
external_tools_arg = _external_tools_arg,
srcs_envs_arg = _srcs_envs_arg,
use_argsfile_at_link_arg = _use_argsfile_at_link_arg,
extra_libraries_arg = _extra_libraries_arg,
)
2 changes: 2 additions & 0 deletions decls/haskell_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ haskell_binary = prelude_rule(
haskell_common.external_tools_arg() |
haskell_common.srcs_envs_arg () |
haskell_common.use_argsfile_at_link_arg () |
haskell_common.extra_libraries_arg () |
haskell_common.compiler_flags_arg() |
haskell_common.deps_arg() |
haskell_common.scripts_arg() |
Expand Down Expand Up @@ -170,6 +171,7 @@ haskell_library = prelude_rule(
haskell_common.external_tools_arg() |
haskell_common.srcs_envs_arg() |
haskell_common.use_argsfile_at_link_arg() |
haskell_common.extra_libraries_arg() |
haskell_common.compiler_flags_arg() |
haskell_common.deps_arg() |
haskell_common.scripts_arg() |
Expand Down
16 changes: 16 additions & 0 deletions haskell/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ load(
"HaskellToolchainLibrary",
"DynamicHaskellPackageDbInfo",
"HaskellPackageDbTSet",
"NativeToolchainLibrary",
)
load(
"@prelude//haskell:util.bzl",
Expand Down Expand Up @@ -426,6 +427,7 @@ def _common_compile_module_args(
label: Label,
deps: list[Dependency],
external_tool_paths: list[RunInfo],
extra_libraries: list[Dependency],
sources: list[Artifact],
direct_deps_info: list[HaskellLibraryInfoTSet],
pkgname: str | None = None,
Expand Down Expand Up @@ -570,6 +572,7 @@ def _compile_module(
aux_deps: None | list[Artifact],
src_envs: None | dict[str, ArgLike],
source_prefixes: list[str],
extra_libraries: list[Dependency],
) -> CompiledModuleTSet:
# These compiler arguments can be passed in a response file.
compile_args_for_file = cmd_args(common_args.args_for_file, hidden = aux_deps or [])
Expand Down Expand Up @@ -687,6 +690,16 @@ def _compile_module(
compile_cmd.add(cmd_args(library_deps, prepend = "-package"))
compile_cmd.add(cmd_args(toolchain_deps, prepend = "-package"))

# extra-libraries
extra_libs = [
lib[NativeToolchainLibrary]
for lib in extra_libraries
if NativeToolchainLibrary in lib
]
for l in extra_libs:
compile_cmd.add(l.lib_path)
compile_cmd.add("-l{}".format(l.name))

compile_cmd.add("-fbyte-code-and-object-code")
if enable_th:
compile_cmd.add("-fprefer-byte-code")
Expand Down Expand Up @@ -733,6 +746,7 @@ def _dynamic_do_compile_impl(actions, artifacts, dynamic_values, outputs, arg):
compiler_flags = arg.compiler_flags,
deps = arg.deps,
external_tool_paths = arg.external_tool_paths,
extra_libraries = arg.extra_libraries,
ghc_wrapper = arg.ghc_wrapper,
haskell_toolchain = arg.haskell_toolchain,
label = arg.label,
Expand Down Expand Up @@ -779,6 +793,7 @@ def _dynamic_do_compile_impl(actions, artifacts, dynamic_values, outputs, arg):
direct_deps_by_name = direct_deps_by_name,
toolchain_deps_by_name = arg.toolchain_deps_by_name,
source_prefixes = source_prefixes,
extra_libraries = arg.extra_libraries,
)

return [DynamicCompileResultInfo(modules = module_tsets)]
Expand Down Expand Up @@ -853,6 +868,7 @@ def compile(
sources_deps = ctx.attrs.srcs_deps,
srcs_envs = ctx.attrs.srcs_envs,
toolchain_deps_by_name = toolchain_deps_by_name,
extra_libraries = ctx.attrs.extra_libraries,
),
))

Expand Down
7 changes: 7 additions & 0 deletions haskell/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ HaskellPackageDbTSet = transitive_set(
DynamicHaskellPackageDbInfo = provider(fields = {
"packages": dict[str, HaskellPackageDbTSet],
})

NativeToolchainLibrary = provider(
fields = {
"name": provider_field(str),
"lib_path": provider_field(typing.Any, default = None),
},
)

0 comments on commit 491741b

Please sign in to comment.