我想禁止人们使用生成的CMake文件混乱我们的源代码树...更重要的是,不允许他们踩到Makefiles不属于我们使用CMake的同一构建过程的现有内容.(最好不要问)
我想出这样做的方法是在我的顶部有几行CMakeLists.txt,如下所示:
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed.")
endif("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
Run Code Online (Sandbox Code Playgroud)
但是,这样做似乎太冗长了.此外,如果我尝试进行源内构建,它仍然会在引发错误之前创建CMakeFiles/目录和CMakeCache.txt源树中的文件.
我错过了更好的方法吗?
And*_*nov 52
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)
Run Code Online (Sandbox Code Playgroud)
-
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.
Run Code Online (Sandbox Code Playgroud)
/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;
}
Run Code Online (Sandbox Code Playgroud)
包括像函数这个.它与您对这些差异的处理方式类似:
它封装在一个函数中,当您包含该PreventInSourceBuilds.cmake模块时会调用该函数.您的主要CMakeLists.txt必须包含它:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
include(PreventInSourceBuilds)
Run Code Online (Sandbox Code Playgroud)它使用带有REALPATH参数的get_filename_component()来比较路径之前解析符号链接.
如果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()
Run Code Online (Sandbox Code Playgroud)
我有一个cmake()shell函数.bashrc/ .zshrc类似于这个:
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
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种低级礼仪解决方案 当我们切换到CMake时,它摆脱了同事们最大的抱怨,但它并没有阻止那些真正想要进行内部/顶级树构建的人这样做 - 他们可以直接调用/usr/bin/cmake(或不完全使用包装函数).而且它的愚蠢简单.
我想我喜欢你的方式。cmake 邮件列表很好地回答了这些类型的问题。
附带说明:您可以在失败的目录中创建一个“cmake”可执行文件。取决于是否有“.” 在他们的路径中(在 Linux 上)。您甚至可以符号链接 /bin/false。
在Windows中,我不确定是否首先找到当前目录中的文件。
| 归档时间: |
|
| 查看次数: |
13793 次 |
| 最近记录: |