build - 使用 cmake,您将如何禁用源内构建?

我想禁止人们将我们的源代码树与生成的 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}")

但是,这样做似乎太冗长了。此外,如果我尝试进行源内构建,它仍然会在引发错误之前在源树中创建 CMakeFiles/ 目录和 CMakeCache.txt 文件。

我错过了更好的方法吗?

最佳答案

CMake 有两个未记录的选项: CMAKE_DISABLE_SOURCE_CHANGESCMAKE_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;
}

https://stackoverflow.com/questions/1208681/

相关文章:

haskell - 我可以得到 `cabal install` 来使用多核吗?

performance - 如何针对构建持续时间和 RAM 使用优化 gradle 构建性能?

javascript - 使用 grunt.js 使用 RequireJS 组合 JavaScrip

xcode - 如何在 Xcode 5 中自动增加内部版本号

c# - Visual Studio : The Operation could not be co

android - java.lang.IllegalStateException : Dex ar

ios - 如何在 Xcode 中创建发布版本?

build - 学习 Gradle 构建工具的最佳资源

android - 如何将 Eclipse Android 项目转换为使用 Ant 进行构建?

c++ - 如何设置 CMake 以生成仅 header 项目?