gcc.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. if(CONFIG_CU_GCC_LTO_ENABLE)
  2. # Enable cmake interprocedural optimization(IPO) support to check if GCC supports link time optimization(LTO)
  3. cmake_policy(SET CMP0069 NEW)
  4. include(CheckIPOSupported)
  5. # Compare to "ar" and "ranlib", "gcc-ar" and "gcc-ranlib" integrate GCC LTO plugin
  6. set(CMAKE_AR ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar)
  7. set(CMAKE_RANLIB ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib)
  8. macro(cu_gcc_lto_set)
  9. check_ipo_supported(RESULT result)
  10. if(result)
  11. message(STATUS "GCC link time optimization(LTO) is enable")
  12. set(multi_value COMPONENTS DEPENDS)
  13. cmake_parse_arguments(LTO "" "" "${multi_value}" ${ARGN})
  14. # Use full format LTO object file
  15. set(GCC_LTO_OBJECT_TYPE "-ffat-lto-objects")
  16. # Set compression level 9(min:0, max:9)
  17. set(GCC_LTO_COMPRESSION_LEVEL "-flto-compression-level=9")
  18. # Set partition level max to removed used symbol
  19. set(GCC_LTO_PARTITION_LEVEL "-flto-partition=max")
  20. # Set mode "auto" to increase compiling speed
  21. set(GCC_LTO_COMPILE_OPTIONS "-flto=auto"
  22. ${GCC_LTO_OBJECT_TYPE}
  23. ${GCC_LTO_COMPRESSION_LEVEL})
  24. # Enable GCC LTO and plugin when linking stage
  25. set(GCC_LTO_LINK_OPTIONS "-flto"
  26. "-fuse-linker-plugin"
  27. ${GCC_LTO_OBJECT_TYPE}
  28. ${GCC_LTO_PARTITION_LEVEL})
  29. message(STATUS "GCC LTO for components: ${LTO_COMPONENTS}")
  30. foreach(c ${LTO_COMPONENTS})
  31. idf_component_get_property(t ${c} COMPONENT_LIB)
  32. target_compile_options(${t} PRIVATE ${GCC_LTO_COMPILE_OPTIONS})
  33. endforeach()
  34. message(STATUS "GCC LTO for dependencies: ${LTO_DEPENDS}")
  35. foreach(d ${LTO_DEPENDS})
  36. target_compile_options(${d} PRIVATE ${GCC_LTO_COMPILE_OPTIONS})
  37. endforeach()
  38. if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "4.4")
  39. target_link_libraries(${project_elf} PRIVATE ${GCC_LTO_LINK_OPTIONS})
  40. else()
  41. target_link_libraries(${project_elf} ${GCC_LTO_LINK_OPTIONS})
  42. endif()
  43. else()
  44. message(FATAL_ERROR "GCC link time optimization(LTO) is not supported")
  45. endif()
  46. endmacro()
  47. else()
  48. macro(cu_gcc_lto_set)
  49. message(STATUS "GCC link time optimization(LTO) is not enable")
  50. endmacro()
  51. endif()
  52. if(CONFIG_CU_GCC_STRING_1BYTE_ALIGN)
  53. macro(cu_gcc_string_1byte_align)
  54. message(STATUS "GCC string 1-byte align is enable")
  55. set(multi_value COMPONENTS DEPENDS)
  56. cmake_parse_arguments(STR_ALIGN "" "" "${multi_value}" ${ARGN})
  57. message(STATUS "GCC string 1-byte align for components: ${STR_ALIGN_COMPONENTS}")
  58. foreach(c ${STR_ALIGN_COMPONENTS})
  59. idf_component_get_property(t ${c} COMPONENT_LIB)
  60. target_compile_options(${t} PRIVATE "-malign-data=natural")
  61. endforeach()
  62. message(STATUS "GCC string 1-byte align for dependencies: ${STR_ALIGN_DEPENDS}")
  63. foreach(d ${STR_ALIGN_DEPENDS})
  64. target_compile_options(${d} PRIVATE "-malign-data=natural")
  65. endforeach()
  66. endmacro()
  67. else()
  68. macro(cu_gcc_string_1byte_align)
  69. message(STATUS "GCC string 1-byte align is not enable")
  70. endmacro()
  71. endif()