GetCompileFlags.cmake 2.6 KB

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