Is it possible to modify CMAKE_SOURCE_DIR
to 'trick' CMake into thinking the top level CMakeLists.txt
file is somewhere else?
I have the following directory structure:
\src <--- not a repository
\cmake <--- repository
\common <--- repository
\project1 <--- repository
...
CMakeLists.txt <--- would like to locate this inside the cmake repository
The problem with the above structure is that the top-level CMakeLists.txt lives outside any repository, so it is not possible to check in changes.
Is it possible to locate that file inside the cmake
directory (which contains nothing else), and still run CMake 'as if' the file was located in the src
directory?
NOTE
src
directory will of course work, but I'm looking for a solution that runs less risk of forgetting to copy the file back into a source-controlled directory.Modifying CMAKE_SOURCE_DIR
is not possible and should not be attempted.
The problem is this: If a project is supposed to be build as part of an enclosing CMake project (that is, it's being included via add_subdirectory
), it must not rely on the assumption that it is being built as a top-level project anywhere. In particular, variables like CMAKE_SOURCE_DIR
and CMAKE_BINARY_DIR
must be used with great care, as they always refer to the top-level CMake project. The corresponding PROJECT_SOURCE_DIR
and PROJECT_BINARY_DIR
should be used instead.
From what you have described, you might be in one of two situations: Either the project in question was never meant to be built as part of another enclosing CMake project, in which case you probably should not attempt to do so. Take a look at ExternalProject for an alternative for building the project stand-alone as part of your enclosing project's build process.
If, on the other hand, the project in question was supposed to support this build setup, it simply has a bug in the CMake script and you should strive to get the CMAKE_*_DIR
variables replaced by their PROJECT_*_DIR
equivalents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With