pkgconfig.cmake 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. # Unlike functions, macros do not introduce a scope. This is an advantage when
  4. # trying to set global variables, as we do here.
  5. macro (opentelemetry_set_pkgconfig_paths)
  6. if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
  7. set(OPENTELEMETRY_PC_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
  8. else ()
  9. set(OPENTELEMETRY_PC_LIBDIR
  10. "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
  11. endif ()
  12. if (IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
  13. set(OPENTELEMETRY_PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
  14. else ()
  15. set(OPENTELEMETRY_PC_INCLUDEDIR
  16. "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
  17. endif ()
  18. endmacro ()
  19. # Create the pkgconfig configuration file (aka *.pc files) and the rules to install it.
  20. #
  21. # * library: the short name of the target, such as `api` or `resources`.
  22. # * name: the displayed name of the library, such as "OpenTelemetry API".
  23. # * description: the description of the library.
  24. # * ARGN: the names of any pkgconfig modules the generated module depends on.
  25. #
  26. function (opentelemetry_add_pkgconfig library name description)
  27. opentelemetry_set_pkgconfig_paths()
  28. set(target "opentelemetry_${library}")
  29. set(OPENTELEMETRY_PC_NAME "${name}")
  30. set(OPENTELEMETRY_PC_DESCRIPTION ${description})
  31. string(JOIN " " OPENTELEMETRY_PC_REQUIRES ${ARGN})
  32. get_target_property(target_type ${target} TYPE)
  33. if ("${target_type}" STREQUAL "INTERFACE_LIBRARY")
  34. # Interface libraries only contain headers. They do not generate lib files
  35. # to link against with `-l`.
  36. set(OPENTELEMETRY_PC_LIBS "")
  37. else ()
  38. set(OPENTELEMETRY_PC_LIBS "-l${target}")
  39. endif ()
  40. get_target_property(target_defs ${target} INTERFACE_COMPILE_DEFINITIONS)
  41. if (target_defs)
  42. foreach (def ${target_defs})
  43. string(APPEND OPENTELEMETRY_PC_CFLAGS " -D${def}")
  44. endforeach ()
  45. endif ()
  46. # Create and install the pkg-config files.
  47. configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/config.pc.in" "${target}.pc" @ONLY)
  48. install(
  49. FILES "${CMAKE_CURRENT_BINARY_DIR}/${target}.pc"
  50. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  51. endfunction()