aaragon
aaragon

Reputation: 2400

Prevent running cmake in the root directory

Sometimes I forget I'm in the build folder and I run the command in the root directory. Well, maybe some of you have experienced that this creates a mess in the entire file hierarchy, as you have to delete CMakeFiles/ folder, config files, and files related to CPack and CTest. Is there a way I can add something to the Makefile at the root directory that prevents me from running cmake accidentally? I tried to add a target 'cmake' but this didn't work.

aa

UPDATE

I found the same question posted in here. I ended up adopting to put a function in my .bashrc file as suggested in that page:

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
}

Upvotes: 4

Views: 1335

Answers (1)

JesperE
JesperE

Reputation: 64404

You can check in your CMakeLists.txt if the source and binary directories are the same. Put something like this as the very first thing in your CMakeLists.txt:

if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
    message(FATAL_ERROR "Source and build directories cannot be the same.")
endif()

Upvotes: 6

Related Questions