title: CMake: pass variables to custom toolchain file
body: <p>I am creating a toolchain file for a gcc-based C compiler for a non-popular platform. I would like users to pass a path to compiler root directory, while a toolchain file will set up all the paths to c/c++/ar/whatever, set proper compile flags.</p>
  <p>My toolchain file starts with this check:</p>
  <pre><code>IF(NOT TOOLCHAIN_PREFIX)
       MESSAGE(FATAL_ERROR &quot;No TOOLCHAIN_PREFIX specified&quot;)
  ENDIF()
  </code></pre>
  <p>All other paths are calculated based on TOOLCHAIN_PREFIX variable, which I want users to be passed as a parameter to cmake (e.g. cmake -DTOOLCHAIN_PREFIX=/path/to/compiler/root ..)</p>
  <p>Problem: during configuration process, cmake gets to 'Detecting C compiler ABI info' step, and runs compilation of a test program, and passes there a toolchain file. The issue is that my variable TOOLCHAIN_PREFIX is not passed there. As result I get error and configuration process stops.</p>
  <p>What is the proper way of passing my TOOLCHAIN_PREFIX parameter to the nested try_compile() operations?</p>
  <p>Alternative question: what is the proper way of building toolchain files so that it can search for a compiler at a specific path?</p>
  <p>UPDATE: Here is a minimal example, reproducing the issue:</p>
  <p>CMakeLists.txt</p>
  <pre><code>set(CMAKE_TOOLCHAIN_FILE Toolchain.cmake)
  project(Test CXX)
  </code></pre>
  <p>Toolchain.cmake</p>
  <pre><code>if(NOT TOOLCHAIN_PREFIX)
      message(FATAL_ERROR &quot;No TOOLCHAIN_PREFIX specified&quot;)
  endif()
  </code></pre>
  <p>Output:</p>
  <pre><code>D:\Projects\Test\build&gt;cmake -DTOOLCHAIN_PREFIX=abcde ..
  -- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19043.
  -- The CXX compiler identification is MSVC 19.29.30136.0
  -- Detecting CXX compiler ABI info
  CMake Error at D:/Projects/Test/Toolchain.cmake:2 (message):
    No TOOLCHAIN_PREFIX specified
  Call Stack (most recent call first):
    D:/Projects/Test/build/CMakeFiles/3.18.1/CMakeSystem.cmake:6 (include)
    D:/Projects/Test/build/CMakeFiles/CMakeTmp/CMakeLists.txt:3 (project)
  </code></pre>