Skip to content

[CMAKE] Set the project version through the cmake project macro #3605

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dbarker
Copy link
Member

@dbarker dbarker commented Aug 22, 2025

The cmake project macro takes VERSION and LANGUAGE args which are not currently being set in otel-cpp CMakeLists.txt.

CMake uses the VERSION arg to set internal variables (See https://cmake.org/cmake/help/v3.31/command/project.html#options) which are used by CMake. One example is for providing more info to the user when they call find_package(opentelemetry-cpp CONFIG).

Previous cmake message when calling find_package(opentelemetry-cpp CONFIG)

-- Found opentelemetry-cpp: /usr/local/include  

New cmake message when calling find_package(opentelemetry-cpp CONFIG)

-- Found opentelemetry-cpp: /usr/local/lib/cmake/opentelemetry-cpp/opentelemetry-cpp-config.cmake (found version "1.22.0") 

Changes

  • Move reading the api version header and setting the OPENTELEMETRY_VERSION and OPENTELEMETRY_ABI_VERSION_NO variables to cmake/version.cmake
  • Set the VERSION and LANGUAGE args in project(opentelemetry-cpp ...)
  • Set the OPENTELEMETRY_VERSION and OPENTELEMETRY_ABI_VERSION_NO as cached variables so users building otel-cpp in-tree with their project can know what version and abi number has been built.
  • Add checks in the FetchContent CI test for the cached variables.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

…ndard config mode to find the project in the config.cmake file. Set cached variables for the version and abi number for in-tree builds with fetch content. Test the cached varaibles are set properly in ci
Copy link

netlify bot commented Aug 22, 2025

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit fe8ce0c
🔍 Latest deploy log https://app.netlify.com/projects/opentelemetry-cpp-api-docs/deploys/68a8fbd466df7c000868a51e

@@ -487,13 +487,11 @@ function(otel_install_cmake_config)
"${PROJECT_SOURCE_DIR}/cmake/templates/opentelemetry-cpp-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
PATH_VARS OPENTELEMETRY_ABI_VERSION_NO OPENTELEMETRY_VERSION PROJECT_NAME
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the variables set to PATH_VARS here are not paths (OPENTELEMETRY_ABI_VERSION_NO, OPENTELEMETRY_VERSION, PROJECT_NAME) and have been removed.


# Write version file for find_package(opentelemetry-cpp CONFIG)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
VERSION ${OPENTELEMETRY_VERSION}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By setting the VERSION arg in the project macro we don't need to explicitly set the version again here. CMake will use the internal variables (PROJECT_VERSION, CMAKE_PROJECT_VERSION).

FOUND_VAR ${CMAKE_FIND_PACKAGE_NAME}_FOUND
REQUIRED_VARS OPENTELEMETRY_CPP_INCLUDE_DIRS OPENTELEMETRY_CPP_LIBRARIES)
${CMAKE_FIND_PACKAGE_NAME}
CONFIG_MODE
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the CONFIG_MODE here is best and will provide a better message to the user that includes the project version found.

See https://cmake.org/cmake/help/latest/module/FindPackageHandleStandardArgs.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOUND_VAR has been deprecated and removed in this PR

Copy link

codecov bot commented Aug 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.03%. Comparing base (dfa7118) to head (fe8ce0c).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3605   +/-   ##
=======================================
  Coverage   90.03%   90.03%           
=======================================
  Files         220      220           
  Lines        7069     7069           
=======================================
  Hits         6364     6364           
  Misses        705      705           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbarker dbarker marked this pull request as ready for review August 23, 2025 00:27
@dbarker dbarker requested a review from a team as a code owner August 23, 2025 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant