From 1fb23db4f9b2aeb90155188e8b825d98f3105cc6 Mon Sep 17 00:00:00 2001 From: Ian-Woo Kim Date: Tue, 8 Oct 2024 20:52:19 -0700 Subject: [PATCH] extra_libraries. native C/C++ libraries can be provided. --- decls/haskell_common.bzl | 8 ++++++++ decls/haskell_rules.bzl | 2 ++ haskell/compile.bzl | 16 ++++++++++++++++ haskell/toolchain.bzl | 7 +++++++ 4 files changed, 33 insertions(+) diff --git a/decls/haskell_common.bzl b/decls/haskell_common.bzl index 0f704f71d..711a3c2ad 100644 --- a/decls/haskell_common.bzl +++ b/decls/haskell_common.bzl @@ -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, @@ -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, ) diff --git a/decls/haskell_rules.bzl b/decls/haskell_rules.bzl index 61d2107ca..17923f930 100644 --- a/decls/haskell_rules.bzl +++ b/decls/haskell_rules.bzl @@ -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() | @@ -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() | diff --git a/haskell/compile.bzl b/haskell/compile.bzl index b3184ce6f..cb3cdc7f3 100644 --- a/haskell/compile.bzl +++ b/haskell/compile.bzl @@ -31,6 +31,7 @@ load( "HaskellToolchainLibrary", "DynamicHaskellPackageDbInfo", "HaskellPackageDbTSet", + "NativeToolchainLibrary", ) load( "@prelude//haskell:util.bzl", @@ -417,6 +418,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, @@ -563,6 +565,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 []) @@ -680,6 +683,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") @@ -726,6 +739,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, @@ -772,6 +786,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)] @@ -846,6 +861,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, ), )) diff --git a/haskell/toolchain.bzl b/haskell/toolchain.bzl index f57d6d161..380e724e6 100644 --- a/haskell/toolchain.bzl +++ b/haskell/toolchain.bzl @@ -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), + }, +)