Description
When linking rust into C++ projects, I use rustc --crate-type=staticlib
to generate a static library which I can link into the overall project. Since there's no standard for transitive dependency declaration in the C ABI for static libraries, rustc prints out a list of libraries which need to be linked along with the staticlib. (#25820 (comment))
This is very helpful in development, but not ideal for automation. I propose adding an --emit=link-info
option (--emit=libs
? --emit=ldflags
?) to write the required link line out to a file for use later, similar to --emit=dep-info
for makefile dependency generation.
Order of evaluation is a little trickier than with dep-info since it has to work the first time, but this should simplify funky stderr hacks like
libfoo.a libfoo.a.out: foo/lib.rs
rustc -g --crate-type staticlib --crate-name foo \
--emit dep-info,link=$@ $< \
2> [email protected] || cat [email protected] >&2
-include foo.d
prog: RUST_LIBS = $(shell awk '/^note: library: / {print "-l"$$3}' libmp4parse.a.out)
prog: prog.o libfoo.a
$(CXX) $(CXXFLAGS) -o $@ $^ $(RUST_LIBS)
into
libfoo.a libfoo.a.link: foo/lib.rs
rustc -g --crate-type staticlib --crate-name foo \
--emit dep-info,link=$@,[email protected] $<
-include foo.d
prog: prog.o libfoo.a
$(CXX) $(CXXFLAGS) -o $@ $^ $(shell cat libfoo.a.link)