Custom location for not found library libmpfr.so.4

I’m trying to solve the “classic” issue of compiling with gnu 4.4 on Bionic, which means that libmpfr.so.4 is not found and the build fails immediately on the compiler check. However libmpfr is available in a custom location in a tarball that I have unpacked, cmake just does not understand how to find it.

So the question is, how do I specify the custom location of this library from within the toolchain file so that:

  1. I’m using the libmpfr library in my custom location, not a system one
  2. the compiler check in the toolchain file works and
  3. I can build my library in the main CMakeLists with little or no changes there, I.e. I don’t want any toolchain specific logic there.

I have tried symlinking the lib to a location like this but as the comment never gets printed it does not seem to execute. Is there something wrong with this one?

set(COMPILER_ROOT $ENV{TOOLCHAIN})
add_custom_command(OUTPUT ${COMPILER_ROOT}/libmpfr.so.4
    COMMAND ${CMAKE_COMMAND} -E create_symlink
        lib/libmpfr.so.4
        libmpfr.so.4
    DEPENDS lib/libmpfr.so.4
    WORKING_DIRECTORY ${COMPILER_ROOT}
    COMMENT "On Bionic we need to symlink this ourselves"
)

What is working inelegantly for 2) is adding the custom location to the LD_LIBRARY_PATH like so:

set(ld_lib_path $ENV{LD_LIBRARY_PATH})
if ("${COMPILER_ROOT}/lib" IN_LIST ld_lib_path)
    message("we already have it, skipping")
else()
    set(ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}:${COMPILER_ROOT}/lib")
endif()

…but that does not work for 3) where it’s failing on building a few static libraries like this:
Hoping that I can learn something
add_library(wat_static STATIC ${WAT_SRCS} ${WAT_HEADERS})

…with “[path]/cc1: error while loading shared libraries: libmpfr.so.4: cannot open shared object file: No such file or directory” which is what I’m getting also for 1) unless the LD_LIBRARY_PATH hack.

Phew, that something so seemingly trivial gets so messy. For instance, as the toolchain file is processed many many times I need to check that I don’t add the custom location to the LD_LIBRARY_PATH more than once which is the ugliness referred to above.

Any suggestions most welcome :).