targets.cmake 3.2 KB

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