targets.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # First try to load the toolchain file from the tools/cmake/directory of IDF
  46. set(toolchain_file_global ${idf_path}/tools/cmake/toolchain-${IDF_TARGET}.cmake)
  47. if(EXISTS ${toolchain_file_global})
  48. set(CMAKE_TOOLCHAIN_FILE ${toolchain_file_global})
  49. else()
  50. __component_get_target(component_target ${IDF_TARGET})
  51. if(NOT component_target)
  52. message(FATAL_ERROR "Unable to resolve '${IDF_TARGET}' for setting toolchain file.")
  53. endif()
  54. get_property(component_dir TARGET ${component_target} PROPERTY COMPONENT_DIR)
  55. # Try to load the toolchain file from the directory of IDF_TARGET component
  56. set(toolchain_file_component ${component_dir}/toolchain-${IDF_TARGET}.cmake)
  57. if(EXISTS ${toolchain_file_component})
  58. set(CMAKE_TOOLCHAIN_FILE ${toolchain_file_component})
  59. else()
  60. message(FATAL_ERROR "Toolchain file toolchain-${IDF_TARGET}.cmake not found,"
  61. "checked ${toolchain_file_global} and ${toolchain_file_component}")
  62. endif()
  63. endif()
  64. endmacro()