Open Lugendre opened 4 years ago
@Lugendre are you using GHC 8.10? The errors look similiar to #1413 and issues linked therein.
Hm, from your WORKSPACE file looks like you're using 8.8.3.
@mboes I'm using GHC 8.8.3. I read #1413, #593 and #1388. I tried rules_haskell 0.13 with #1388 merged into it, and I get the same error. I think it's probably due to a misconfiguration of the directory associated with c2hs, but I can't debug it because of my lack of knowledge of bazel. I forgot to write this, I'm using NixOS 20.03.
@mboes Is there any update on this? I'm also suffering from this issue (spoiler: I and @Lugendre are working at the same team).
To look into this, I made some changes to rules_haskell
to be able to see what commands are being run: https://github.com/tweag/rules_haskell/compare/master...Infinisil:command-logging
With this, the output is:
(Note that there's a lot of arguments to the right). Haven't yet been able to figure anything out with this though.
And just to test whether c2hs binaries work at all, I extended the c2hs test with a binary build: https://github.com/tweag/rules_haskell/compare/master...Infinisil:c2hs-binary-test
But that seems to build and run just fine.
@Infinisil Thank you for looking into this! One thing that stands out to me is that many of the missing symbols are defined in //:mpi-hs-c
. However, that target is only a dependency of //:mpi-hs-c2hs
but not //:mpi-hs
. c2hs_library
does not provide a CcInfo
provider meaning it does not track C library dependencies (or really any kinds of dependencies). Maybe it's already enough to add //:mpi-hs-c
as a dependency to //:mpi-hs
.
@Infinisil @aherrmann Thank you for commenting on this! I thought the same way as @aherrmann, so I added //:mpi-hs-c
as a dependency to //:mpi-hs
and built it (c.f. https://github.com/Lugendre/mpi-hs/commit/180452f594e0d320cb248ba3dd35e522795bfc60) , but I got the same error (this is also true for adding it to //:mpi-hs-version
). I think we need some kind of workaround.
The missing symbols are all about some wrapper functions generated by C2hs. I don't know in what cases C2hs generates wrapping functions, but if it does, then these wrappers end up in a .chs.c
file, with a corresponding .chs.h
header file. I don't believe that rules_haskell knows about this file yet, and is therefore not building a .o
from it and not linking that in the final shared object.
See the foreign import
declarations in the output of C2hs for a manifestation of these wrapper functions: http://hackage.haskell.org/package/mpi-hs-0.5.1.2/docs/src/Control.Distributed.MPI.html.
Thanks to the @aherrmann's pointer to CcInfo
, I got some progress! With
diff --git a/haskell/c2hs.bzl b/haskell/c2hs.bzl
index b47fdf6a..ece33b15 100644
--- a/haskell/c2hs.bzl
+++ b/haskell/c2hs.bzl
@@ -113,6 +113,9 @@ def _c2hs_library_impl(ctx):
chi_file = chi_file,
import_dir = idir,
),
+ cc_common.merge_cc_infos(
+ cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep],
+ ),
]
c2hs_library = rule(
The error only shows symbols from c2hs:
bazel build //:mpi-hs-version --override_repository=rules_haskell=/home/infinisil/src/rules_haskell
INFO: Analyzed target //:mpi-hs-version (1 packages loaded, 13 targets configured).
INFO: Found 1 target...
ERROR: /home/infinisil/src/mpi-hs/BUILD.bazel:59:15: HaskellLinkBinary mpi-hs-version failed (Exit 1) ghc_wrapper failed: error executing command bazel-out/host/bin/external/rules_haskell/haskell/ghc_wrapper bazel-out/k8-fastbuild/bin/compile_flags_mpi-hs-version_HaskellLinkBinary ... (remaining 1 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x19b58): error: undefined reference to '__c2hs_wrapped__MPI_Abort'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1a34c): error: undefined reference to '__c2hs_wrapped__MPI_Allgather'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1b240): error: undefined reference to '__c2hs_wrapped__MPI_Allreduce'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1c21c): error: undefined reference to '__c2hs_wrapped__MPI_Alltoall'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1cc34): error: undefined reference to '__c2hs_wrapped__MPI_Barrier'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1d578): error: undefined reference to '__c2hs_wrapped__MPI_Exscan'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1e630): error: undefined reference to '__c2hs_wrapped__MPI_Iallgather'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x1f764): error: undefined reference to '__c2hs_wrapped__MPI_Iallreduce'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x20978): error: undefined reference to '__c2hs_wrapped__MPI_Ialltoall'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x215d0): error: undefined reference to '__c2hs_wrapped__MPI_Ibarrier'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2210c): error: undefined reference to '__c2hs_wrapped__MPI_Iexscan'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x234dc): error: undefined reference to '__c2hs_wrapped__MPI_Iscan'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x247d0): error: undefined reference to '__c2hs_wrapped__MPI_Scan'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x25584): error: undefined reference to '__c2hs_wrapped__MPI_Get_elements'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x277e8): error: undefined reference to '__c2hs_wrapped__MPI_Comm_rank'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x27e60): error: undefined reference to '__c2hs_wrapped__MPI_Comm_size'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x284dc): error: undefined reference to '__c2hs_wrapped__MPI_Get_count'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2b57c): error: undefined reference to '__c2hs_wrapped__MPI_Comm_compare'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2bcec): error: undefined reference to '__c2hs_wrapped__MPI_Bcast'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2c7e0): error: undefined reference to '__c2hs_wrapped__MPI_Gather'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2d578): error: undefined reference to '__c2hs_wrapped__MPI_Ibcast'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2e28c): error: undefined reference to '__c2hs_wrapped__MPI_Igather'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x2f4b0): error: undefined reference to '__c2hs_wrapped__MPI_Ireduce'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x307cc): error: undefined reference to '__c2hs_wrapped__MPI_Iscatter'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x31914): error: undefined reference to '__c2hs_wrapped__MPI_Reduce'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x329f0): error: undefined reference to '__c2hs_wrapped__MPI_Scatter'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x34764): error: undefined reference to '__c2hs_wrapped__MPI_Irecv'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x353f4): error: undefined reference to '__c2hs_wrapped__MPI_Isend'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x35d28): error: undefined reference to '__c2hs_wrapped__MPI_Probe'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x36578): error: undefined reference to '__c2hs_wrapped__MPI_Probe'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x36d14): error: undefined reference to '__c2hs_wrapped__MPI_Recv'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x379fc): error: undefined reference to '__c2hs_wrapped__MPI_Recv'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x38548): error: undefined reference to '__c2hs_wrapped__MPI_Send'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x39122): error: undefined reference to '__c2hs_wrapped__MPI_Sendrecv'
bazel-out/k8-fastbuild/bin/libHSmpi-hs.a(MPI.o)(.text+0x3a672): error: undefined reference to '__c2hs_wrapped__MPI_Sendrecv'
collect2: error: ld returned 1 exit status
`cc_wrapper-python' failed in phase `Linker'. (Exit code: 1)
Target //:mpi-hs-version failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.833s, Critical Path: 0.69s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
I copied that code pretty much from https://github.com/tweag/rules_haskell/blob/e8623a8c5ea502fbe6286d8ae5fb58f4915e92c5/haskell/private/haskell_impl.bzl#L564-L569. And I feel like the other errors should be resolved by also creating a new CcInfo
from the c2hs
invocation itself.
Although no, I don't think my fix is correct. I think my fix links the symbols to the original openmpi
library, and not the ones from c2hs. I believe the reason this gets rid of part of the warnings is that c2hs wraps all the symbols of the original library, but renames them when necessary: https://github.com/haskell/c2hs/blob/20d3c8dedde83af91c4ac06486bf346316e07285/src/C2HS/Gen/Bind.hs#L1150-L1152.
Was able to make a minimal repro of the problem, extending the c2hs-binary-test branch from before: https://github.com/tweag/rules_haskell/compare/master...Infinisil:c2hs-binary-test
Trying to build it, the output is:
$ bazel build //tests/c2hs:c2hs-bin
WARNING: /home/infinisil/.cache/bazel/_bazel_infinisil/855dd477cdeab134dd737ac0f283327f/external/zlib.dev/BUILD:10:11: in linkstatic attribute of cc_library rule @zlib.dev//:zlib: setting 'linkstatic=1' is recommended if there are no object files. Since this rule was created by the macro 'cc_library', the error might have been caused by the macro implementation
INFO: Analyzed target //tests/c2hs:c2hs-bin (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /home/infinisil/src/rules_haskell/tests/c2hs/BUILD.bazel:55:15: HaskellLinkBinary tests/c2hs/c2hs-bin failed (Exit 1) ghc_wrapper failed: error executing command bazel-out/host/bin/haskell/ghc_wrapper bazel-out/k8-fastbuild/bin/tests/c2hs/compile_flags_c2hs-bin_HaskellLinkBinary ... (remaining 1 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/k8-fastbuild/bin/tests/c2hs/libHStestsZSc2hsZSc2hs.a(Code.o)(.text+0xfc): error: undefined reference to '__c2hs_wrapped__boolfun'
bazel-out/k8-fastbuild/bin/tests/c2hs/libHStestsZSc2hsZSc2hs.a(Code.o)(.text+0x47c): error: undefined reference to 'intfun'
collect2: error: ld returned 1 exit status
`cc_wrapper-python' failed in phase `Linker'. (Exit code: 1)
Target //tests/c2hs:c2hs-bin failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.762s, Critical Path: 0.70s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
Once fixed, this can be used as a test-case. I'll continue looking into it..
Edit: Updated to also test a __c2hs_wrapped
case
Unfortunately wasn't successful, but my attempt at fixing this now lives in https://github.com/tweag/rules_haskell/tree/c2hs-binary-test
I tried to build mpi-hs's binary using bazel and rules_haskell, but I got the following error and failed to build the
haskell_binary
.On the other hand, I can build the library using
haskell_library
.I don't have much knowledge of rules_haskell, so I'm struggling with this debugging. I need your wisdom.
Here are the commits I'm actually using for validation.