BeFunctional / haskell-foreign-rust

MIT License
9 stars 3 forks source link

Unable to compile #11

Closed khazaddum closed 1 year ago

khazaddum commented 1 year ago

Hello,

I'm trying to follow this tutorial and I am getting this error trying to compile haskell-foreign-rust:

foreign-rust   > configure
foreign-rust   > Configuring foreign-rust-0.1.0...
foreign-rust   > build
foreign-rust   > Preprocessing library for foreign-rust-0.1.0..
foreign-rust   > Building library for foreign-rust-0.1.0..
foreign-rust   > [ 1 of 17] Compiling Data.Structured
foreign-rust   > 
foreign-rust   > /tmp/stack-d01f5ffda1e3e54a/foreign-rust-0.1.0/src/Data/Structured.hs:505:16: error:
foreign-rust   >     Not in scope: type constructor or class ‘Aeson.KeyMap.Key’
foreign-rust   >     Perhaps you meant ‘Aeson.KeyMap.KeyMap’ (imported from Data.Aeson.KeyMap)
foreign-rust   >     Module ‘Data.Aeson.KeyMap’ does not export ‘Key’.
foreign-rust   >     |
foreign-rust   > 505 |     goField :: Aeson.KeyMap.Key -> NonEmpty Builder -> NonEmpty Builder
foreign-rust   >     |                ^^^^^^^^^^^^^^^^
edsko commented 1 year ago

This is due to a missing lower bound on aeson, sorry. You will need aeson version 2.1 or higher; it should also not be too difficult to modify foreign-rust to support other versions; PR welcome! :-)

khazaddum commented 1 year ago

I was able to compile it through Stack by pinning aeson, borsh, vector and generically.

However, c2hs is failing at processing the example:

$ c2hs ../rust-wrapper/rust_wrapper.h GettingStarted.chs 
In file included from GettingStarted.chs.h:2:
../rust-wrapper/rust_wrapper.h:7:10: fatal error: cstdarg: No such file or directory
    7 | #include <cstdarg>
      |          ^~~~~~~~~
compilation terminated.
c2hs: Error during preprocessing custom header file
edsko commented 1 year ago

This is due to the fact that the default for the generated header is C++, not C, and you need to override that. I could have sworn I mentioned this in the blog post but I didn't -- apologies. I've submitted a PR against the blog post to fix that, but meanwhile, here's the relevant new paragraph:


Finally, we need to configure cbindgen and tell it what kind of header file we want; we don't need to call it manually (cargo cbuild will do that for us), but we do need to tell it that we want a C header file, not a C++ header file. Create a file called cbindgen.toml in the project root (alongside Cargo.toml) with the following three lines:

include_guard = "RUST_WRAPPER_H"
include_version = false
language = "C"

Incidentally, the code for the blog post is at https://github.com/well-typed/blogpost-purgatory , and you can find it there, too. (That's no excuse, I should have mentioned it in the blog post for sure, but it might help you get unstuck.)

Thanks for your questions!

khazaddum commented 1 year ago

I was able to compile and use the library using stack.

I have a few more questions but it's no longer related to compiling the code, is there a better way to post the questions?

edsko commented 1 year ago

Feel free to post them here, or else email me.