GetCompileFlags.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function(get_compile_flags _compile_flags)
  2. # Create template akin to CMAKE_C_COMPILE_OBJECT.
  3. set(compile_flags "<CMAKE_C_COMPILER> <CFLAGS> <BUILD_TYPE_CFLAGS> <COMPILE_OPTIONS><COMPILE_DEFINITIONS> <INCLUDES>")
  4. # Get C compiler.
  5. string(REPLACE
  6. "<CMAKE_C_COMPILER>"
  7. "${CMAKE_C_COMPILER}"
  8. compile_flags
  9. "${compile_flags}")
  10. # Get flags set by add_definitions().
  11. get_property(compile_definitions DIRECTORY PROPERTY COMPILE_DEFINITIONS)
  12. get_target_property(compile_definitions_target nvim COMPILE_DEFINITIONS)
  13. if(compile_definitions_target)
  14. list(APPEND compile_definitions ${compile_definitions_target})
  15. list(REMOVE_DUPLICATES compile_definitions)
  16. endif()
  17. # NOTE: list(JOIN) requires CMake 3.12, string(CONCAT) requires CMake 3.
  18. string(REPLACE ";" " -D" compile_definitions "${compile_definitions}")
  19. if(compile_definitions)
  20. set(compile_definitions " -D${compile_definitions}")
  21. endif()
  22. string(REPLACE
  23. "<COMPILE_DEFINITIONS>"
  24. "${compile_definitions}"
  25. compile_flags
  26. "${compile_flags}")
  27. # Get flags set by add_compile_options().
  28. get_property(compile_options DIRECTORY PROPERTY COMPILE_OPTIONS)
  29. get_target_property(compile_options_target nvim COMPILE_OPTIONS)
  30. if(compile_options_target)
  31. list(APPEND compile_options ${compile_options_target})
  32. list(REMOVE_DUPLICATES compile_options)
  33. endif()
  34. # NOTE: list(JOIN) requires CMake 3.12.
  35. string(REPLACE ";" " " compile_options "${compile_options}")
  36. string(REPLACE
  37. "<COMPILE_OPTIONS>"
  38. "${compile_options}"
  39. compile_flags
  40. "${compile_flags}")
  41. # Get general C flags.
  42. string(REPLACE
  43. "<CFLAGS>"
  44. "${CMAKE_C_FLAGS}"
  45. compile_flags
  46. "${compile_flags}")
  47. # Get C flags specific to build type.
  48. string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
  49. string(REPLACE
  50. "<BUILD_TYPE_CFLAGS>"
  51. "${CMAKE_C_FLAGS_${build_type}}"
  52. compile_flags
  53. "${compile_flags}")
  54. # Get include directories.
  55. get_property(include_directories_list DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
  56. list(REMOVE_DUPLICATES include_directories_list)
  57. foreach(include_directory ${include_directories_list})
  58. set(include_directories "${include_directories} -I${include_directory}")
  59. endforeach()
  60. string(REPLACE
  61. "<INCLUDES>"
  62. "${include_directories}"
  63. compile_flags
  64. "${compile_flags}")
  65. # Clean duplicate whitespace.
  66. string(REPLACE
  67. " "
  68. " "
  69. compile_flags
  70. "${compile_flags}")
  71. set(${_compile_flags} "${compile_flags}" PARENT_SCOPE)
  72. endfunction()