Skip to content

Add rustc --emit=link-info for staticlib link-line output #31471

Open
@rillian

Description

@rillian

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions