1# Toolchain file is processed multiple times, however, it cannot access CMake cache on some runs.
2# We store the search path in an environment variable so that we can always access it.
3if (NOT "${PICO_TOOLCHAIN_PATH}" STREQUAL "")
4    set(ENV{PICO_TOOLCHAIN_PATH} "${PICO_TOOLCHAIN_PATH}")
5endif ()
6
7# Find the compiler executable and store its path in a cache entry ${compiler_path}.
8# If not found, issue a fatal message and stop processing. PICO_TOOLCHAIN_PATH can be provided from
9# commandline as additional search path.
10function(pico_find_compiler compiler_path compiler_exe)
11    # Search user provided path first.
12    find_program(
13            ${compiler_path} ${compiler_exe}
14            PATHS ENV PICO_TOOLCHAIN_PATH
15            PATH_SUFFIXES bin
16            NO_DEFAULT_PATH
17    )
18
19    # If not then search system paths.
20    if ("${${compiler_path}}" STREQUAL "${compiler_path}-NOTFOUND")
21        if (DEFINED ENV{PICO_TOOLCHAIN_PATH})
22            message(WARNING "PICO_TOOLCHAIN_PATH specified ($ENV{PICO_TOOLCHAIN_PATH}), but ${compiler_exe} not found there")
23        endif()
24        find_program(${compiler_path} ${compiler_exe})
25    endif ()
26    if ("${${compiler_path}}" STREQUAL "${compiler_path}-NOTFOUND")
27        set(PICO_TOOLCHAIN_PATH "" CACHE PATH "Path to search for compiler.")
28        message(FATAL_ERROR "Compiler '${compiler_exe}' not found, you can specify search path with\
29            \"PICO_TOOLCHAIN_PATH\".")
30    endif ()
31endfunction()
32