Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD] Support pkg-config #2269

Merged
merged 8 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ jobs:
run: |
./ci/do_ci.sh cmake.with_async_export.test

cmake_absel_stl_test:
cmake_abseil_stl_test:
name: CMake test (with abseil)
runs-on: ubuntu-20.04
steps:
Expand Down Expand Up @@ -442,6 +442,25 @@ jobs:
sudo ./ci/setup_grpc.sh -T
./ci/do_ci.sh cmake.exporter.otprotocol.shared_libs.with_static_grpc.test

cmake_install_test:
name: CMake install test (with abseil)
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: setup
run: |
sudo -E ./ci/setup_cmake.sh
sudo -E ./ci/setup_ci_environment.sh
- name: run cmake install (with abseil)
run: |
sudo ./ci/install_abseil.sh
./ci/do_ci.sh cmake.install.test
- name: verify packages
run: |
./ci/verify_packages.sh

plugin_test:
name: Plugin -> CMake
runs-on: ubuntu-latest
Expand Down
16 changes: 14 additions & 2 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ if(OPENTELEMETRY_INSTALL)
DESTINATION include
FILES_MATCHING
PATTERN "*.h")

unset(TARGET_DEPS)
endif()

if(BUILD_TESTING)
Expand All @@ -41,12 +43,12 @@ if(WITH_NO_DEPRECATED_CODE)
endif()

if(WITH_ABSEIL)

target_compile_definitions(opentelemetry_api INTERFACE HAVE_ABSEIL)
target_link_libraries(
opentelemetry_api INTERFACE absl::bad_variant_access absl::any absl::base
absl::bits absl::city)

list(APPEND TARGET_DEPS "absl_bad_variant_access" "absl_any absl_base"
"absl_bits" "absl_city")
endif()

if(WITH_STL)
Expand All @@ -64,6 +66,7 @@ if(WITH_GSL)
find_package(Microsoft.GSL QUIET)
if(TARGET Microsoft.GSL::GSL)
target_link_libraries(opentelemetry_api INTERFACE Microsoft.GSL::GSL)
list(APPEND TARGET_DEPS "gsl")
else()
set(GSL_DIR third_party/ms-gsl)
target_include_directories(
Expand Down Expand Up @@ -115,3 +118,12 @@ if(WITH_METRICS_EXEMPLAR_PREVIEW)
target_compile_definitions(opentelemetry_api
INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW)
endif()

include(${PROJECT_SOURCE_DIR}/cmake/pkgconfig.cmake)

if(OPENTELEMETRY_INSTALL)
opentelemetry_add_pkgconfig(
api "OpenTelemetry API"
"A header-only library to support instrumentation with OpenTelemetry."
"${TARGET_DEPS}")
endif()
12 changes: 12 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ elif [[ "$1" == "cmake.do_not_install.test" ]]; then
make -j $(nproc)
cd exporters/otlp && make test
exit 0
elif [[ "$1" == "cmake.install.test" ]]; then
cd "${BUILD_DIR}"
rm -rf *
cmake ${CMAKE_OPTIONS[@]} \
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON \
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
-DWITH_ABSEIL=ON \
"${SRC_DIR}"
make -j $(nproc)
sudo make install
exit 0
elif [[ "$1" == "bazel.with_abseil" ]]; then
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS_ASYNC --//api:with_abseil=true //...
bazel $BAZEL_STARTUP_OPTIONS test $BAZEL_TEST_OPTIONS_ASYNC --//api:with_abseil=true //...
Expand Down
3 changes: 2 additions & 1 deletion ci/setup_ci_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ apt-get install --no-install-recommends --no-install-suggests -y \
git \
valgrind \
lcov \
iwyu
iwyu \
pkg-config
12 changes: 12 additions & 0 deletions ci/verify_packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

set -ex

PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig:${PKG_CONFIG_PATH:-}"

for library in api common logs metrics resources trace version; do
pkg-config --validate opentelemetry_${library} --print-errors
done
55 changes: 55 additions & 0 deletions cmake/pkgconfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# Unlike functions, macros do not introduce a scope. This is an advantage when
# trying to set global variables, as we do here.
macro (opentelemetry_set_pkgconfig_paths)
if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(OPENTELEMETRY_PC_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
else ()
set(OPENTELEMETRY_PC_LIBDIR
"\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
endif ()

if (IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(OPENTELEMETRY_PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
else ()
set(OPENTELEMETRY_PC_INCLUDEDIR
"\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
endif ()
endmacro ()

# Create the pkgconfig configuration file (aka *.pc files) and the rules to install it.
#
# * library: the short name of the target, such as `api` or `resources`.
# * name: the displayed name of the library, such as "OpenTelemetry API".
# * description: the description of the library.
# * ARGN: the names of any pkgconfig modules the generated module depends on.
#
function (opentelemetry_add_pkgconfig library name description)
opentelemetry_set_pkgconfig_paths()
set(target "opentelemetry_${library}")
set(OPENTELEMETRY_PC_NAME "${name}")
set(OPENTELEMETRY_PC_DESCRIPTION ${description})
string(JOIN " " OPENTELEMETRY_PC_REQUIRES ${ARGN})
get_target_property(target_type ${target} TYPE)
if ("${target_type}" STREQUAL "INTERFACE_LIBRARY")
# Interface libraries only contain headers. They do not generate lib files
# to link against with `-l`.
set(OPENTELEMETRY_PC_LIBS "")
else ()
set(OPENTELEMETRY_PC_LIBS "-l${target}")
endif ()
get_target_property(target_defs ${target} INTERFACE_COMPILE_DEFINITIONS)
if (target_defs)
foreach (def ${target_defs})
string(APPEND OPENTELEMETRY_PC_CFLAGS " -D${def}")
endforeach ()
endif ()

# Create and install the pkg-config files.
configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/config.pc.in" "${target}.pc" @ONLY)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/${target}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endfunction()
15 changes: 15 additions & 0 deletions cmake/templates/config.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

prefix=${pcfiledir}/../..
exec_prefix=${prefix}
libdir=@OPENTELEMETRY_PC_LIBDIR@
includedir=@OPENTELEMETRY_PC_INCLUDEDIR@

Name: @OPENTELEMETRY_PC_NAME@
Description: @OPENTELEMETRY_PC_DESCRIPTION@
Requires: @OPENTELEMETRY_PC_REQUIRES@
Version: @OPENTELEMETRY_VERSION@

Libs: -L${libdir} @OPENTELEMETRY_PC_LIBS@
Cflags: -I${includedir} @OPENTELEMETRY_PC_CFLAGS@
6 changes: 6 additions & 0 deletions sdk/src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
common
"OpenTelemetry SDK - Common"
"Common components for the OpenTelemetry SDK, a library for exporting telemetry."
"opentelemetry_api")
endif()
5 changes: 5 additions & 0 deletions sdk/src/logs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
logs "OpenTelemetry SDK - Logs"
"Components for exporting logs in the OpenTelemetry SDK."
"opentelemetry_resources")
endif()
5 changes: 5 additions & 0 deletions sdk/src/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
metrics "OpenTelemetry SDK - Metrics"
"Components for exporting metrics in the OpenTelemetry SDK."
"opentelemetry_resources")
endif()
5 changes: 5 additions & 0 deletions sdk/src/resource/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
resources "OpenTelemetry SDK - Resources"
"Components for resource detection in the OpenTelemetry SDK."
"opentelemetry_common")
endif()
5 changes: 5 additions & 0 deletions sdk/src/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
trace "OpenTelemetry SDK - Trace"
"Components for exporting traces in the OpenTelemetry SDK."
"opentelemetry_resources")
endif()
5 changes: 5 additions & 0 deletions sdk/src/version/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ if(OPENTELEMETRY_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

opentelemetry_add_pkgconfig(
version "OpenTelemetry SDK - Version"
"A library exporting version information for OpenTelemetry."
"opentelemetry_api")
endif()