A solution that I found recently is to combine the out-of-source build concept with a Makefile wrapper.
In my top-level CMakeLists.txt file, I include the following to prevent in-source builds:
if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} )
message( FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt." )
endif()
Then, I create a top-level Makefile, and include the following:
# -----------------------------------------------------------------------------
# CMake project wrapper Makefile ----------------------------------------------
# -----------------------------------------------------------------------------
SHELL := /bin/bash
RM := rm -rf
MKDIR := mkdir -p
all: ./build/Makefile
@ $(MAKE) -C build
./build/Makefile:
@ ($(MKDIR) build > /dev/null)
@ (cd build > /dev/null 2>&1 && cmake ..)
distclean:
@ ($(MKDIR) build > /dev/null)
@ (cd build > /dev/null 2>&1 && cmake .. > /dev/null 2>&1)
@- $(MAKE) --silent -C build clean || true
@- $(RM) ./build/Makefile
@- $(RM) ./build/src
@- $(RM) ./build/test
@- $(RM) ./build/CMake*
@- $(RM) ./build/cmake.*
@- $(RM) ./build/*.cmake
@- $(RM) ./build/*.txt
ifeq ($(findstring distclean,$(MAKECMDGOALS)),)
$(MAKECMDGOALS): ./build/Makefile
@ $(MAKE) -C build $(MAKECMDGOALS)
endif
The default target all
is called by typing make
, and invokes the target ./build/Makefile
.
The first thing the target ./build/Makefile
does is to create the build
directory using $(MKDIR)
, which is a variable for mkdir -p
. The directory build
is where we will perform our out-of-source build. We provide the argument -p
to ensure that mkdir
does not scream at us for trying to create a directory that may already exist.
The second thing the target ./build/Makefile
does is to change directories to the build
directory and invoke cmake
.
Back to the all
target, we invoke $(MAKE) -C build
, where $(MAKE)
is a Makefile variable automatically generated for make
. make -C
changes the directory before doing anything. Therefore, using $(MAKE) -C build
is equivalent to doing cd build; make
.
To summarize, calling this Makefile wrapper with make all
or make
is equivalent to doing:
mkdir build
cd build
cmake ..
make
The target distclean
invokes cmake ..
, then make -C build clean
, and finally, removes all contents from the build
directory. I believe this is exactly what you requested in your question.
The last piece of the Makefile evaluates if the user-provided target is or is not distclean
. If not, it will change directories to build
before invoking it. This is very powerful because the user can type, for example, make clean
, and the Makefile will transform that into an equivalent of cd build; make clean
.
In conclusion, this Makefile wrapper, in combination with a mandatory out-of-source build CMake configuration, make it so that the user never has to interact with the command cmake
. This solution also provides an elegant method to remove all CMake output files from the build
directory.
P.S. In the Makefile, we use the prefix @
to suppress the output from a shell command, and the prefix @-
to ignore errors from a shell command. When using rm
as part of the distclean
target, the command will return an error if the files do not exist (they may have been deleted already using the command line with rm -rf build
, or they were never generated in the first place). This return error will force our Makefile to exit. We use the prefix @-
to prevent that. It is acceptable if a file was removed already; we want our Makefile to keep going and remove the rest.
Another thing to note: This Makefile may not work if you use a variable number of CMake variables to build your project, for example, cmake .. -DSOMEBUILDSUSETHIS:STRING="foo" -DSOMEOTHERBUILDSUSETHISTOO:STRING="bar"
. This Makefile assumes you invoke CMake in a consistent way, either by typing cmake ..
or by providing cmake
a consistent number of arguments (that you can include in your Makefile).
Finally, credit where credit is due. This Makefile wrapper was adapted from the Makefile provided by the C++ Application Project Template.
This answer was originally posted here. I thought it applied to your situation as well.