Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify CMAKE_SOURCE_DIR

Tags:

cmake

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

  1. I've been gifted this split-repository layout by a client, no need to tell me it causes problems.
  2. Copying the CMakeLists.txt file in and out of the 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.
  3. I've played around with modifying the CMAKE_SOURCE_DIR variable from within the script, but this doesn't actually change the working directory.
like image 962
Zero Avatar asked Mar 06 '14 03:03

Zero


1 Answers

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.

like image 175
ComicSansMS Avatar answered Dec 12 '22 05:12

ComicSansMS