Utilities.cmake 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. get_filename_component(PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
  2. get_filename_component(PROJECT_ROOT_DIR "${PROJECT_CMAKE_DIR}" DIRECTORY)
  3. find_package(Python3 COMPONENTS Interpreter)
  4. if(NOT Python3_FOUND)
  5. message(FATAL_ERROR "Python3 not found.")
  6. endif()
  7. function(get_recommended_gcc_version var)
  8. execute_process(
  9. COMMAND "${Python3_EXECUTABLE}" "${PROJECT_ROOT_DIR}/utils/bootstrap.py"
  10. "--print-dependency-version" "avr-gcc"
  11. OUTPUT_VARIABLE RECOMMENDED_VERSION
  12. OUTPUT_STRIP_TRAILING_WHITESPACE
  13. RESULT_VARIABLE RETVAL
  14. )
  15. if(NOT "${RETVAL}" STREQUAL "0")
  16. message(FATAL_ERROR "Failed to obtain recommended gcc version from utils/bootstrap.py")
  17. endif()
  18. set(${var}
  19. ${RECOMMENDED_VERSION}
  20. PARENT_SCOPE
  21. )
  22. endfunction()
  23. function(get_dependency_directory dependency var)
  24. execute_process(
  25. COMMAND "${Python3_EXECUTABLE}" "${PROJECT_ROOT_DIR}/utils/bootstrap.py"
  26. "--print-dependency-directory" "${dependency}"
  27. OUTPUT_VARIABLE DEPENDENCY_DIRECTORY
  28. OUTPUT_STRIP_TRAILING_WHITESPACE
  29. RESULT_VARIABLE RETVAL
  30. )
  31. if(NOT "${RETVAL}" STREQUAL "0")
  32. message(FATAL_ERROR "Failed to find directory with ${dependency}")
  33. endif()
  34. set(${var}
  35. ${DEPENDENCY_DIRECTORY}
  36. PARENT_SCOPE
  37. )
  38. endfunction()
  39. function(objcopy target format suffix)
  40. add_custom_command(
  41. TARGET ${target} POST_BUILD
  42. COMMAND "${CMAKE_OBJCOPY}" -O ${format} -S "$<TARGET_FILE:${target}>"
  43. "${CMAKE_CURRENT_BINARY_DIR}/${target}${suffix}"
  44. COMMENT "Generating ${format} from ${target}..."
  45. BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/${target}${suffix}"
  46. )
  47. endfunction()
  48. function(report_size target)
  49. add_custom_command(
  50. TARGET ${target} POST_BUILD
  51. COMMAND echo "" # visually separate the output
  52. COMMAND "${CMAKE_SIZE_UTIL}" -B "$<TARGET_FILE:${target}>"
  53. USES_TERMINAL
  54. )
  55. endfunction()