Skip to content

CMake Compatibility with FetchContent - #13311

Merged
Nir-Az merged 4 commits into
realsenseai:developmentfrom
mprat:mprat/cmake_project_dir
Aug 28, 2025
Merged

CMake Compatibility with FetchContent#13311
Nir-Az merged 4 commits into
realsenseai:developmentfrom
mprat:mprat/cmake_project_dir

Conversation

@mprat

@mprat mprat commented Aug 30, 2024

Copy link
Copy Markdown
Contributor

To allow for projects using CMake's FetchContent framework, instances of CMAKE_SOURCE_DIR should be replaced with PROJECT_SOURCE_DIR. FetchContent uses add_subdirectory() under the cmake hood, and using CMAKE_SOURCE_DIR cannot be set manually; it is set as the parent project source directory. For correct add_subdirectory() cmake builds using librealsense as a dependency, these should be changed to PROJECT_SOURCE_DIR.

@mprat mprat changed the title replace all instances of CMAKE_SOURCE_DIR with PROJECT_SOURCE_DIR for… CMake Compatibility with FetchContent Aug 30, 2024
@Nir-Az
Nir-Az requested a review from maloel September 3, 2024 11:37
@maloel

maloel commented Sep 3, 2024

Copy link
Copy Markdown
Contributor

Hi @mprat
Thanks for the PR!

Question, just based on a really quick look:

PROJECT_SOURCE_DIR is the source directory of the most recent project() command.

So, for example, in a file like src/gl/CMakeLists.txt, we have:

project(realsense2-gl)
...
include(${CMAKE_SOURCE_DIR}/CMake/opengl_config.cmake)

This evaluated to the CMake directory in the root librealsense structure.
But now, if we replace by PROJECT_SOURCE_DIR, it will evaluate to src/gl/CMake/opengl_config.cmake?

@mprat

mprat commented Sep 3, 2024

Copy link
Copy Markdown
Contributor Author

Hi @mprat Thanks for the PR!

Question, just based on a really quick look:

PROJECT_SOURCE_DIR is the source directory of the most recent project() command.

So, for example, in a file like src/gl/CMakeLists.txt, we have:

project(realsense2-gl)
...
include(${CMAKE_SOURCE_DIR}/CMake/opengl_config.cmake)

This evaluated to the CMake directory in the root librealsense structure. But now, if we replace by PROJECT_SOURCE_DIR, it will evaluate to src/gl/CMake/opengl_config.cmake?

Not quite - the CMakeLists.txt file in src/gl/ is called by the call from add_subdirectory(src/gl) in the CMakeLists.txt in librealsense/CMakeLists.txt. According to the CMake documentation, PROJECT_SOURCE_DIR is not affected by calls to add_subdirectory() (https://cmake.org/cmake/help/latest/variable/PROJECT_SOURCE_DIR.html). This means that the previous call to project() (which appears in the top-level librealsense/CMakeLists.txt sets the PROJECT_SOURCE_DIR to librealsense/. The line in src/gl/CMakeLists.txt then will evaluate to librealsense/CMake/opengl_config.cmake, as it expected.

If you want to get rid of this ambiguity, the opengl_config.cmake can be moved into src/gl/CMake and the use of CMAKE_SOURCE_DIR/PROJECT_SOURCE_DIR can be removed completely.

@Nir-Az
Nir-Az requested review from Copilot and removed request for maloel August 13, 2025 13:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates CMake configuration to improve compatibility with FetchContent by replacing CMAKE_SOURCE_DIR with PROJECT_SOURCE_DIR. This change ensures that when librealsense is used as a dependency via CMake's FetchContent framework, the build system correctly references the project's source directory rather than the parent project's directory.

  • Replace CMAKE_SOURCE_DIR with PROJECT_SOURCE_DIR across multiple CMakeLists.txt files
  • Update include directory paths for Python wrappers, PCL wrappers, C# bindings, and examples
  • Modify CMake include statements for OpenGL configuration

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
wrappers/python/CMakeLists.txt Updates include directories for pybackend2 target to use PROJECT_SOURCE_DIR
wrappers/pcl/pcl/CMakeLists.txt Updates GLFW include directory path for rs-pcl target
wrappers/pcl/pcl-color/CMakeLists.txt Updates GLFW include directory path for rs-pcl-color target
wrappers/csharp/Intel.RealSense/CMakeLists.txt Updates Unity bindings directory reference
tools/CMakeLists.txt Updates OpenGL configuration include path
src/gl/CMakeLists.txt Updates OpenGL configuration include path
examples/CMakeLists.txt Updates OpenGL configuration include path

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread wrappers/pcl/pcl-color/CMakeLists.txt Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@sysrsbuild

Copy link
Copy Markdown
Contributor

Can one of the admins verify this patch?

@mprat

mprat commented Aug 20, 2025

Copy link
Copy Markdown
Contributor Author

I've fixed the conflicts with the latest base; I am starting a new project with librealsense and would like to use this patch if possible. Can admins please verify?

@Nir-Az

Nir-Az commented Aug 24, 2025

Copy link
Copy Markdown
Collaborator

@mprat I will try to help you with that.
Can you please see the CI failures in GitHub actions after your changes?
image

@Nir-Az
Nir-Az self-requested a review August 28, 2025 10:43
@Nir-Az

Nir-Az commented Aug 28, 2025

Copy link
Copy Markdown
Collaborator

@mprat since we see no reggresion in our CI I will help and merge it.
Thanks for the PR,

@Nir-Az
Nir-Az merged commit 0d7bb04 into realsenseai:development Aug 28, 2025
25 checks passed
@mprat
mprat deleted the mprat/cmake_project_dir branch August 28, 2025 13:12
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.

5 participants