# Fetch compiler toolchain in toolchain file

**URL:** https://discourse.cmake.org/t/fetch-compiler-toolchain-in-toolchain-file/10490
**Category:** Code
**Created:** [March 26, 2024, 3:53pm UTC](https://discourse.cmake.org/t/fetch-compiler-toolchain-in-toolchain-file/10490 "2024-03-26T15:53:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Mathias\_B](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathias_b/32/4442_2.png) [@Mathias\_B](https://discourse.cmake.org/u/Mathias_B)
#### Post date: [March 26, 2024, 3:53pm UTC](https://discourse.cmake.org/t/fetch-compiler-toolchain-in-toolchain-file/10490/1 "2024-03-26T15:53:26Z")

</div>

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

```auto
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 :

```auto
[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,

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 26, 2024, 9:29pm UTC](https://discourse.cmake.org/t/fetch-compiler-toolchain-in-toolchain-file/10490/2 "2024-03-26T21:29:33Z")

</div>

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](https://cmake.org/cmake/help/latest/prop_gbl/IN_TRY_COMPILE.html) 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.
