targets.cmake 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # Check if IDF_ENV_FPGA environment is set
  28. set(env_idf_env_fpga $ENV{IDF_ENV_FPGA})
  29. if(${env_idf_env_fpga})
  30. idf_build_set_property(__IDF_ENV_FPGA "y")
  31. message(NOTICE "IDF_ENV_FPGA is set, building for FPGA environment")
  32. endif()
  33. endmacro()
  34. #
  35. # Check that the set build target and the config target matches.
  36. #
  37. function(__target_check)
  38. # Should be called after sdkconfig CMake file has been included.
  39. idf_build_get_property(idf_target IDF_TARGET)
  40. if(NOT ${idf_target} STREQUAL ${CONFIG_IDF_TARGET})
  41. message(FATAL_ERROR "CONFIG_IDF_TARGET in sdkconfig does not match "
  42. "IDF_TARGET environment variable. To change the target, delete "
  43. "sdkconfig file and build the project again.")
  44. endif()
  45. endfunction()
  46. #
  47. # Used by the project CMake file to set the toolchain before project() call.
  48. #
  49. macro(__target_set_toolchain)
  50. idf_build_get_property(idf_path IDF_PATH)
  51. # First try to load the toolchain file from the tools/cmake/directory of IDF
  52. set(toolchain_file_global ${idf_path}/tools/cmake/toolchain-${IDF_TARGET}.cmake)
  53. if(EXISTS ${toolchain_file_global})
  54. set(CMAKE_TOOLCHAIN_FILE ${toolchain_file_global})
  55. else()
  56. __component_get_target(component_target ${IDF_TARGET})
  57. if(NOT component_target)
  58. message(FATAL_ERROR "Unable to resolve '${IDF_TARGET}' for setting toolchain file.")
  59. endif()
  60. get_property(component_dir TARGET ${component_target} PROPERTY COMPONENT_DIR)
  61. # Try to load the toolchain file from the directory of IDF_TARGET component
  62. set(toolchain_file_component ${component_dir}/toolchain-${IDF_TARGET}.cmake)
  63. if(EXISTS ${toolchain_file_component})
  64. set(CMAKE_TOOLCHAIN_FILE ${toolchain_file_component})
  65. else()
  66. message(FATAL_ERROR "Toolchain file toolchain-${IDF_TARGET}.cmake not found,"
  67. "checked ${toolchain_file_global} and ${toolchain_file_component}")
  68. endif()
  69. endif()
  70. endmacro()