Correct way to set required linker-only arguments in toolchain

I have a toolchain file which needs to set -rtlib=compiler-rt as a “required” linker flag (i. e. without it, the toolchain will either not work or silently generate broken code). As far as I can tell, I have a couple options:

  • Set CMAKE_*_LINKER_FLAGS_INIT. However, it is overridable from the cmdline and there is no guarantee that the user won’t forget about compiler-rt.
  • Do set(CMAKE_C_COMPILER clang -rtlib=compiler-rt -Qunused-arguments) which is not amazing since -rtlib=compiler-rt is now passed during compilation as well.
  • Do some weird dance with setting something like MY_PROJECT_REQUIRED_LINKER_FLAGS in toolchain and then prepending to CMAKE_*_LINKER_FLAGS at the top of project’s CMakeLists.txt guarded by top-level check.
  • Use CMAKE_USER_MAKE_RULES_OVERRIDE_{C,CXX} and try to guess what CMAKE_CXX_CREATE_SHARED_LIBRARY & co. are being set to in the version of CMake that happens to be installed on the user machine.

and neither of them is particularly nice. So, what is the current best practice?

Why not add_link_options?