GenPkgConfig.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Generate variables that can be used to help emit a pkg-config file
  2. # using configure_file().
  3. #
  4. # Usage: gen_pkgconfig_vars(VAR_PREFIX target)
  5. #
  6. # This will set two variables in the caller scope:
  7. # ${VAR_PREFIX}_CFLAGS: set to the compile flags computed from the specified
  8. # target
  9. # ${VAR_PREFIX}_PRIVATE_LIBS: set to the linker flags needed for static
  10. # linking computed from the specified target
  11. function(gen_pkgconfig_vars)
  12. if (NOT ${ARGC} EQUAL 2)
  13. message(FATAL_ERROR "gen_pkgconfig_vars() requires exactly 2 arguments")
  14. endif()
  15. set(var_prefix "${ARGV0}")
  16. set(target "${ARGV1}")
  17. get_target_property(target_cflags "${target}" INTERFACE_COMPILE_OPTIONS)
  18. if(target_cflags)
  19. list(APPEND cflags "${target_cflags}")
  20. endif()
  21. get_target_property(
  22. target_inc_dirs "${target}" INTERFACE_INCLUDE_DIRECTORIES)
  23. if(target_inc_dirs)
  24. list(APPEND include_dirs "${target_inc_dirs}")
  25. endif()
  26. get_target_property(target_defns "${target}" INTERFACE_COMPILE_DEFINITIONS)
  27. if(target_defns)
  28. list(APPEND definitions "${target_defns}")
  29. endif()
  30. # The INTERFACE_LINK_LIBRARIES list is unfortunately somewhat awkward to
  31. # process. Entries in this list may be any of
  32. # - target names
  33. # - absolute paths to a library file
  34. # - plain library names that need "-l" prepended
  35. # - other linker flags starting with "-"
  36. #
  37. # Walk through each entry and transform it into the desired arguments
  38. get_target_property(link_libs "${target}" INTERFACE_LINK_LIBRARIES)
  39. if(link_libs)
  40. foreach(lib_arg IN LISTS link_libs)
  41. if(TARGET "${lib_arg}")
  42. # Add any compile options specified in the targets
  43. # INTERFACE_COMPILE_OPTIONS. We don't need to process its
  44. # INTERFACE_LINK_LIBRARIES property, since our INTERFACE_LINK_LIBRARIES
  45. # will already include its entries transitively.
  46. get_target_property(lib_cflags "${lib_arg}" INTERFACE_COMPILE_OPTIONS)
  47. if(lib_cflags)
  48. list(APPEND cflags "${lib_cflags}")
  49. endif()
  50. get_target_property(lib_defs "${lib_arg}"
  51. INTERFACE_COMPILE_DEFINITIONS)
  52. if(lib_defs)
  53. list(APPEND definitions "${lib_defs}")
  54. endif()
  55. elseif(lib_arg MATCHES "^[-/]")
  56. list(APPEND private_libs "${lib_arg}")
  57. else()
  58. list(APPEND private_libs "-l${lib_arg}")
  59. endif()
  60. endforeach()
  61. endif()
  62. list(APPEND cflags "${CMAKE_REQUIRED_FLAGS}")
  63. if(definitions)
  64. list(REMOVE_DUPLICATES definitions)
  65. foreach(def_arg IN LISTS definitions)
  66. list(APPEND cflags "-D${def_arg}")
  67. endforeach()
  68. endif()
  69. if(include_dirs)
  70. list(REMOVE_DUPLICATES include_dirs)
  71. foreach(inc_dir IN LISTS include_dirs)
  72. list(APPEND cflags "-I${inc_dir}")
  73. endforeach()
  74. endif()
  75. # Set the output variables
  76. string(REPLACE ";" " " cflags "${cflags}")
  77. set("${var_prefix}_CFLAGS" "${cflags}" PARENT_SCOPE)
  78. string(REPLACE ";" " " private_libs "${private_libs}")
  79. set("${var_prefix}_PRIVATE_LIBS" "${private_libs}" PARENT_SCOPE)
  80. endfunction()