Fix proper installation of shared libraries

Cherry-pick upstream patches:
- patch-configure r. 046f0a447
- patch-libr-Makefile r. 046f0a447 and r. 2f551b485