1#
2# Set the target used for the standard project build.
3#
4macro(__target_init)
5    # Input is IDF_TARGET environement variable
6    set(env_idf_target $ENV{IDF_TARGET})
7
8    if(NOT env_idf_target)
9        # IDF_TARGET not set in environment, see if it is set in cache
10        if(IDF_TARGET)
11            set(env_idf_target ${IDF_TARGET})
12        else()
13            set(env_idf_target esp32)
14            message(STATUS "IDF_TARGET not set, using default target: ${env_idf_target}")
15        endif()
16    else()
17        # IDF_TARGET set both in environment and in cache, must be the same
18        if(NOT ${IDF_TARGET} STREQUAL ${env_idf_target})
19            message(FATAL_ERROR "IDF_TARGET in CMake cache does not match "
20                            "IDF_TARGET environment variable. To change the target, clear "
21                            "the build directory and sdkconfig file, and build the project again")
22        endif()
23    endif()
24
25    # IDF_TARGET will be used by Kconfig, make sure it is set
26    set(ENV{IDF_TARGET} ${env_idf_target})
27
28    # Finally, set IDF_TARGET in cache
29    set(IDF_TARGET ${env_idf_target} CACHE STRING "IDF Build Target")
30endmacro()
31
32#
33# Check that the set build target and the config target matches.
34#
35function(__target_check)
36    # Should be called after sdkconfig CMake file has been included.
37    idf_build_get_property(idf_target IDF_TARGET)
38    if(NOT ${idf_target} STREQUAL ${CONFIG_IDF_TARGET})
39        message(FATAL_ERROR "CONFIG_IDF_TARGET in sdkconfig does not match "
40            "IDF_TARGET environment variable. To change the target, delete "
41            "sdkconfig file and build the project again.")
42    endif()
43endfunction()
44
45#
46# Used by the project CMake file to set the toolchain before project() call.
47#
48macro(__target_set_toolchain)
49    idf_build_get_property(idf_path IDF_PATH)
50
51    # See if Clang toolchain should be used
52    set(env_idf_toolchain $ENV{IDF_TOOLCHAIN})
53    if(NOT env_idf_toolchain)
54        # IDF_TOOLCHAIN not set in environment, see if it is set in cache
55        if(IDF_TOOLCHAIN)
56            set(env_idf_toolchain ${IDF_TOOLCHAIN})
57        else()
58            set(env_idf_toolchain gcc)
59        endif()
60    else()
61        # IDF_TOOLCHAIN set both in environment and in cache, must be the same
62        if(NOT ${IDF_TOOLCHAIN} STREQUAL ${env_idf_toolchain})
63            message(FATAL_ERROR "IDF_TOOLCHAIN in CMake cache does not match "
64                    "IDF_TOOLCHAIN environment variable. To change the toolchain, clear "
65                    "the build directory and sdkconfig file, and build the project again")
66        endif()
67    endif()
68
69    # Finally, set IDF_TOOLCHAIN in cache
70    set(IDF_TOOLCHAIN ${env_idf_toolchain} CACHE STRING "IDF Build Toolchain Type")
71
72    if(${env_idf_toolchain} STREQUAL "clang")
73        set(toolchain_type "clang-")
74    endif()
75
76    # First try to load the toolchain file from the tools/cmake/directory of IDF
77    set(toolchain_file_global ${idf_path}/tools/cmake/toolchain-${toolchain_type}${IDF_TARGET}.cmake)
78    if(EXISTS ${toolchain_file_global})
79        set(CMAKE_TOOLCHAIN_FILE ${toolchain_file_global})
80    else()
81        message(FATAL_ERROR "Toolchain file ${toolchain_file_global} not found")
82    endif()
83endmacro()
84