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

CMake Compatibility with FetchContent #13311

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

mprat
Copy link

@mprat mprat commented Aug 30, 2024

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
Copy link
Collaborator

maloel commented Sep 3, 2024

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
Copy link
Author

mprat commented Sep 3, 2024

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.

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.

2 participants