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

后端 未结 8 1388
说谎
说谎 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条回答
  •  梦毁少年i
    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 ..
    

提交回复
热议问题