inline-rust icon indicating copy to clipboard operation
inline-rust copied to clipboard

Project-level use

Open harpocrates opened this issue 7 years ago • 0 comments

In the course of writing a test suite, I ran into the following issue: there is no good way of linking in 2 different rust-produced artifacts in different modules without hitting duplicate symbol errors. GHC needs to produce one relocatable object file per module and the foreign file feature lets you link stuff into that one file. Updating the set of libraries GHC will link in at the end is also out of the question for TH.

If we link in the complete static libraries from rustc --crate-type=static (which we want to do since rustc will manage bringing in all the dependencies), we end up with each relocatable module object file having its own copy of a bunch of functions that end up in most (all?) rustc-produced static libraries. Solutions I considered:

  • Get GHC to output one library per module instead of just one relocatable file (that way, the final linker step dedups properly the shared rust stuff). Big change, unlikely to happen.
  • Namespace all symbols in the rustc-produced static library. Turns out there isn't really any good cross-platform tooling for this. 😢

So it looks like the best way to solve the problem is to not solve it and make sure a project has only one rustc-produced static library linked in. That means that

  1. All rust produced in the project will be part of one crate
  2. In exactly one file, which is far enough in the ModuleGraph for all Rust files to have been emitted, the project will be compiled.
    • That will also mean that any compilation errors in the Rust code show up only when that module gets compiled
    • It will be necessary to pass -Wl,--whole-archive so that whatever module has the cargo entry point will still retain all the symbols (even if it doesn't use them, other modules' object files will)

TODO:

  • [x] Always build up (in a .inline-rust folder, for example) the Rust files amassed so far
  • [x] API for specifying the cargo "entry" point, and better specifying project properties
  • [ ] Documentation for end users

harpocrates avatar May 01 '18 07:05 harpocrates