Fetch compiler toolchain in toolchain file

Hello,

I have been trying to setup a C/C++/CMake project, with the ability to configure the cmake project with several toolchain files through the CMAKE_TOOLCHAIN_FILE CMake variable.

I have a set of toolchain files (toolchain_gcc_cm7.cmake, toolchain_gcc_cm4.cmake, toolchain_host.cmake, etc.) and i can build the project with any of these toolchain files perfectly.

I would like to go a little bit further and make these toolchain files download the toolchain through the FetchContent_Declare and FetchContent_Populate function. To do this i have the following statement for my toolchain files (following example for toolchain_gcc_cm7.cmake) :

message("[INFO] Building with CM7 toolchain")

if(PROJECT_IS_TOP_LEVEL)                                                           
    include(FetchContent)                                                          
    FetchContent_Declare(                                                          
        GCC-ARM-NONE-EABI-10.3-2021.10-X86_64-LINUX                                
        URL      https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
        SOURCE_DIR ${CMAKE_BINARY_DIR}/toolchains                                  
    )                                                                              
                                                                                   
    if(NOT gcc-arm-none-eabi-10.3-2021.10-x86_64-linux_POPULATED)                  
        message("[INFO] Downloading gcc-arm-none-eabi-10.3.2021.10-x86_64-linux")
        FetchContent_Populate(GCC-ARM-NONE-EABI-10.3-2021.10-X86_64-LINUX)         
    endif()                                                                        
endif()

The problem i am facing is, at configure time, the toolchain gets downloaded three times. I think the first time is for the TRY_COMPILE stage. I don’t know why it gets downloaded an additional couple of times though :

[INFO] Building with CM7 toolchain
[INFO] Downloading gcc-arm-none-eabi-10.3.2021.10-x86_64-linux
[INFO] Building with CM7 toolchain
-- The CXX compiler identification is GNU 10.3.1
-- The C compiler identification is GNU 10.3.1
-- The ASM compiler identification is GNU
-- Found assembler: /home/mathias/my-project/build/toolchains/bin/arm-none-eabi-gcc
-- Detecting CXX compiler ABI info
[INFO] Building with CM7 toolchain
[INFO] Downloading gcc-arm-none-eabi-10.3.2021.10-x86_64-linux
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/mathias/my-project/build/toolchains/bin/arm-none-eabi-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
[INFO] Building with CM7 toolchain
[INFO] Downloading gcc-arm-none-eabi-10.3.2021.10-x86_64-linux

Is it a good idea to download the toolchain here ? If so how can i get it to work properly ?

Thanks,

Toolchain files get used by more than just the main project. When the main project enables a new language for the first time, it creates a small sub-build to test the compiler. The toolchain file is also given to that sub-build to use, which is probably what you’re seeing. The main project might also make calls to try_compile() which also set up small sub-builds, which would also receive the toolchain file too. A toolchain file can detect if it is being used in a try_compile() call by checking the IN_TRY_COMPILE global property. I don’t know what that property holds in the sub-builds used when languages are first enabled, but it might be set then too.