With cmake, how would you disable in-source builds?

后端 未结 8 1241
说谎
说谎 2020-12-28 12:21

I want to disallow people from cluttering our source tree with generated CMake files... and, more importantly, disallow them from stepping on existing Makefiles

相关标签:
8条回答
  • 2020-12-28 12:36

    I think I like your way. The cmake mailing list does a good job at answering these types of questions.

    As a side note: you could create a "cmake" executable file in the directory which fails. Depending on whether or not "." is in their path (on linux). You could even symlink /bin/false.

    In windows, I am not sure if a file in your current directory is found first or not.

    0 讨论(0)
  • 2020-12-28 12:36

    Just make the directory read-only by the people/processes doing the builds. Have a separate process that checks out to the directory from source control (you are using source control, right?), then makes it read-only.

    0 讨论(0)
  • 2020-12-28 12:40

    This is still the best answer for my purposes:

    project(myproject)
    
    if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
      message(FATAL_ERROR "In-source builds are not allowed")
    endif()
    

    or allow the build, but show a warning message:

    if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
      message(WARNING "In-source builds are not recommended")
    endif()
    

    However, there does not appear to be a simple way to avoid CMakeFiles/ and CMakeCache.txt being created in the source directory.

    0 讨论(0)
  • 2020-12-28 12:41

    I have a cmake() shell function in my .bashrc/.zshrc similar to this one:

    function cmake() {
      # Don't invoke cmake from the top-of-tree
      if [ -e "CMakeLists.txt" ]
      then
        echo "CMakeLists.txt file present, cowardly refusing to invoke cmake..."
      else
        /usr/bin/cmake $*
      fi
    }
    

    I prefer this low ceremony solution. It got rid of my colleagues' biggest complaint when we switched to CMake, but it doesn't prevent people who really want to do an in-source/top-of-tree build from doing so—they can just invoke /usr/bin/cmake directly (or not use the wrapper function at all). And it's stupid simple.

    0 讨论(0)
  • 2020-12-28 12:46

    For those on Linux:

    add to top-level CMakeLists.txt:

    set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
    

    create a file 'dotme' in your top-level or add to your .bashrc (globally):

    #!/bin/bash
    cmk() { if [ ! -e $1/CMakeLists.txt ] || ! grep -q "set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)" $1/CMakeLists.txt;then /usr/bin/cmake $*;else echo "CMAKE_DISABLE_IN_SOURCE_BUILD ON";fi }
    
    alias cmake=cmk
    

    now run:

    . ./dotme
    

    when you try to run cmake in the top-level source tree:

    $ cmake .
    CMAKE_DISABLE_IN_SOURCE_BUILD ON
    

    No CMakeFiles/ or CMakeCache.txt gets generated.

    When doing out-of-source build and you need to run cmake first time just call the actual executable:

    $ cd build
    $ /usr/bin/cmake ..
    
    0 讨论(0)
  • 2020-12-28 12:53

    You can configure your .bashrc file like this one

    Look at the functions cmakekde and kdebuild. Set BUILD and SRC env. variables and edit these functions according to your needs. This will build only in buildDir rather than srcDir

    0 讨论(0)
提交回复
热议问题