Use `-Wl,--gc-sections` when linking the shared library
Currently librsvg-2.0.so is linked without any special linker flags. This means that it will contain all of Rust's std library and all of the dependencies, including code that is not actually used.
By using -Wl,--gc-sections
all this unnecessary code (i.e. code unreachable from public symbols) would get removed:
unstripped | stripped | |
---|---|---|
current build | 19MB | 9.7MB |
-Wl,--gc-sections |
9.7MB | 4.5MB |
This is with the following change
diff --git a/Makefile.am b/Makefile.am
index 01e68f6d..b1dbccf2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -223,6 +223,7 @@ librsvg_@RSVG_API_MAJOR_VERSION@_la_LDFLAGS = \
-export-dynamic \
-no-undefined \
-export-symbols-regex "^rsvg_.*" \
+ -Wl,--gc-sections \
$(AM_LDFLAGS)
if OS_DARWIN
For Windows this is AFAIU unnecessary (and the default), for macOS you'd have to pass -dead_strip
instead.
Related rustc issue: https://github.com/rust-lang/rust/issues/111593