CMAKE를 사용하면 소스 내 빌드를 어떻게 비활성화 하시겠습니까?

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

  •  05-07-2019
  •  | 
  •  

문제

생성 된 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"실행 파일을 만들 수 있습니다. "여부"에 따라 그들의 길에 있습니다 (Linux). Symlink /bin /false도 할 수도 있습니다.

Windows에서는 현재 디렉토리의 파일이 먼저 발견되는지 확실하지 않습니다.

다른 팁

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;
}

같은 함수를 포함하십시오 이 하나. 이러한 차이점으로하는 것과 유사합니다.

  1. 함수에 캡슐화되며, 포함 할 때 호출됩니다. PreventInSourceBuilds.cmake 기준 치수. 주요 cmakelists.txt는 다음을 포함해야합니다.

    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
    include(PreventInSourceBuilds)
    
  2. 사용합니다 get_filename_component () 경로를 비교하기 전에 Symlinks를 해결하는 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 이것과 유사합니다 :

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 직접 (또는 래퍼 기능을 사용하지 않음). 그리고 그것은입니다 멍청한 단순한.

.bashrc 파일과 같은 구성을 구성 할 수 있습니다 이 하나

기능을보십시오 cmakekde 그리고 Kdebuild. 빌드 및 SRC Env를 설정하십시오. 귀하의 요구에 따라 변수 및 이러한 기능을 편집하십시오. 이것은 만 구축됩니다 빌드 디르 보다는 srcdir

Linux의 사람들을 위해 :

최상위 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 ..

빌드를 수행하는 사람/프로세스에서 디렉토리를 읽으십시오. 소스 컨트롤에서 디렉토리를 체크 아웃하는 별도의 프로세스가있어 (소스 컨트롤을 사용하고 있습니다.) 읽기 전용으로 만듭니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top