Utilities.cmake 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. file(TO_CMAKE_PATH "${DEPENDENCY_DIRECTORY}" DEPENDENCY_DIRECTORY)
  35. set(${var}
  36. ${DEPENDENCY_DIRECTORY}
  37. PARENT_SCOPE
  38. )
  39. endfunction()
  40. function(objcopy target format suffix)
  41. add_custom_command(
  42. TARGET ${target} POST_BUILD
  43. COMMAND "${CMAKE_OBJCOPY}" -O ${format} -S "$<TARGET_FILE:${target}>"
  44. "${CMAKE_CURRENT_BINARY_DIR}/${target}${suffix}"
  45. COMMENT "Generating ${format} from ${target}..."
  46. BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/${target}${suffix}"
  47. )
  48. endfunction()
  49. function(report_size target)
  50. add_custom_command(
  51. TARGET ${target} POST_BUILD
  52. COMMAND echo "" # visually separate the output
  53. COMMAND "${CMAKE_SIZE_UTIL}" -B "$<TARGET_FILE:${target}>"
  54. USES_TERMINAL
  55. )
  56. endfunction()