Custom location for not found library

I’m trying to solve the “classic” issue of compiling with gnu 4.4 on Bionic, which means that 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?

add_custom_command(OUTPUT ${COMPILER_ROOT}/
    COMMAND ${CMAKE_COMMAND} -E create_symlink
    DEPENDS lib/
    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")

…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: 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 :).