我想禁止人们用生成的 CMake 文件弄乱我们的源代码树...更重要的是,禁止他们踩到现有的
Makefiles
,这些不属于我们使用 CMake 的同一构建过程。 (最好不要问)
我想出的方法是在我的
CMakeLists.txt
顶部有几行,如下所示:
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed.")
endif()
编辑注意:在旧版本的
cmake
中,使用此语法是不可能的:最后一行需要阅读:
endif("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
但是,这样做似乎太冗长了。此外,如果我尝试源内构建,它仍然会在抛出错误之前在源树中创建
CMakeFiles/
目录和 CMakeCache.txt
文件。
我是否缺少更好的方法来做到这一点?
CMake 有两个未记录的选项:
CMAKE_DISABLE_SOURCE_CHANGES
和 CMAKE_DISABLE_IN_SOURCE_BUILD
cmake_minimum_required (VERSION 2.8)
# add this options before PROJECT keyword
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
project (HELLO)
add_executable (hello hello.cxx)
-
andrew@manchester:~/src% cmake .
CMake Error at /usr/local/share/cmake-2.8/Modules/CMakeDetermineSystem.cmake:160 (FILE):
file attempted to write a file: /home/andrew/src/CMakeFiles/CMakeOutput.log
into a source directory.
/home/selivanov/cmake-2.8.8/Source/cmMakefile.cxx
bool cmMakefile::CanIWriteThisFile(const char* fileName)
{
if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
{
return true;
}
// If we are doing an in-source build, than the test will always fail
if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
this->GetHomeOutputDirectory()) )
{
if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
{
return false;
}
return true;
}
// Check if this is subdirectory of the source tree but not a
// subdirectory of a build tree
if ( cmSystemTools::IsSubDirectory(fileName,
this->GetHomeDirectory()) &&
!cmSystemTools::IsSubDirectory(fileName,
this->GetHomeOutputDirectory()) )
{
return false;
}
return true;
}
包含类似 this 的函数。这与您处理这些差异的方式类似:
它被封装在一个函数中,当你包含
PreventInSourceBuilds.cmake
模块时会调用该函数。您的主 CMakeLists.txt 必须包含它:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
include(PreventInSourceBuilds)
它使用 get_filename_component() 和 REALPATH 参数,在比较路径之前解析符号链接。
如果 github 链接发生变化,这里是模块源代码(应放置在
PreventInSouceBuilds.cmake
中,在上面的示例中名为 CMake
的目录中):
#
# This function will prevent in-source builds
function(AssureOutOfSourceBuilds)
# make sure the user doesn't play dirty with symlinks
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
# disallow in-source builds
if("${srcdir}" STREQUAL "${bindir}")
message("######################################################")
message("# ITK should not be configured & built in the ITK source directory")
message("# You must run cmake in a build directory.")
message("# For example:")
message("# mkdir ITK-Sandbox ; cd ITK-sandbox")
message("# git clone http://itk.org/ITK.git # or download & unpack the source tarball")
message("# mkdir ITK-build")
message("# this will create the following directory structure")
message("#")
message("# ITK-Sandbox")
message("# +--ITK")
message("# +--ITK-build")
message("#")
message("# Then you can proceed to configure and build")
message("# by using the following commands")
message("#")
message("# cd ITK-build")
message("# cmake ../ITK # or ccmake, or cmake-gui ")
message("# make")
message("#")
message("# NOTE: Given that you already tried to make an in-source build")
message("# CMake have already created several files & directories")
message("# in your source tree. run 'git status' to find them and")
message("# remove them by doing:")
message("#")
message("# cd ITK-Sandbox/ITK")
message("# git clean -n -d")
message("# git clean -f -d")
message("# git checkout --")
message("#")
message("######################################################")
message(FATAL_ERROR "Quitting configuration")
endif()
endfunction()
AssureOutOfSourceBuilds()
我的
cmake()
/.bashrc
中有一个 .zshrc
shell 函数,与此类似:
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
}
我更喜欢这种低调的解决方案。当我们切换到 CMake 时,它消除了我同事最大的抱怨,但这并不能阻止那些“真正”想要进行源内/树顶构建的人这样做 - 他们只需调用 /usr/bin/cmake
直接(或者根本不使用包装函数)。而且这很
愚蠢简单。
附带说明:您可以在失败的目录中创建一个“cmake”可执行文件。取决于是否有“.”在他们的路径中(在 Linux 上)。您甚至可以符号链接 /bin/false。
在Windows中,我不确定是否首先找到当前目录中的文件。
一样配置您的.bashrc文件 查看函数
cmakekde和 kdebuild。设置 BUILD 和 SRC 环境。变量并根据您的需要编辑这些函数。这只会在 buildDir 而不是 srcDir 中构建
添加到顶级 CMakeLists.txt:
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
在顶层创建一个文件“dotme”或添加到您的.bashrc(全局):
#!/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
现在运行:
. ./dotme
当您尝试在顶级源代码树中运行 cmake 时:
$ cmake .
CMAKE_DISABLE_IN_SOURCE_BUILD ON
没有生成 CMakeFiles/ 或 CMakeCache.txt。
进行源外构建时,您需要第一次运行 cmake,只需调用实际的可执行文件:
$ cd build
$ /usr/bin/cmake ..
project(myproject)
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed")
endif()
或允许构建,但显示警告消息:
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(WARNING "In-source builds are not recommended")
endif()
但是,似乎没有一种简单的方法可以避免在源目录中创建
CMakeFiles/
和
CMakeCache.txt
。