[CMake] How to build only when file contents are actually changed?

Michael Wild themiwi at gmail.com
Tue Aug 14 01:56:13 EDT 2012


CMake really leaves the decision when to recompile something to the
backend, i.e. GNU Make, Xcode, Visual Studio, ninja etc. It merely
defines dependencies and then lets the actual build tool handle the
rest, and most of them choose to use simple time-stamps instead of
hashes. Also note that computing the hash might also incur considerable
overhead, in particular for very large projects with thousands of files.

You can alleviate the situation a bit by using e.g. ccache to speed up
unnecessary recompilations. E.g. on a system that has ccache installed
in /usr/lib/ccache (Ubuntu, Debian,...)

mkdir build
cd build
rm -rf *
PATH=/usr/lib/cchace:$PATH cmake ..

will build using ccache instead the normal compilers.

HTH

Michael

On 08/14/2012 05:22 AM, Peng Yu wrote:
> Hi,
> 
> The following command output shows that when I touch a source without
> changing the content, the source are compiled and linked, which is a
> waste.
> 
> This post shows how to use the checksum to decide whether a file is
> changed or not, if changed then update target. This feature seems to
> be missing in cmake. In case that I miss something in the document,
> could anybody let me know if this is the case?
> 
> http://blog.jgc.org/2006/04/rebuilding-when-hash-has-changed-not.html
> 
> ~/linux/test/cmake/lang/command/target_link_libraries/src$ cat.sh  *
> ==> CMakeLists.txt <==
> cmake_minimum_required(VERSION 2.8)
> add_library(print print.cpp)
> #include_directories(${HELLO_SOURCE_DIR})
> add_executable(main main.cpp)
> target_link_libraries(main print)
> 
> ==> main.cpp <==
> #include "print.hpp"
> int main() {
>   print();
> }
> 
> ==> print.cpp <==
> #include "print.hpp"
> #include <iostream>
> using namespace std;
> void print() {
>   cout << "Hello, World!" << endl;
> }
> 
> ==> print.hpp <==
> #ifndef _hello_h
> #define _hello_h
> void print();
> #endif
> 
> ~/linux/test/cmake/lang/command/target_link_libraries/build$ cmake ../src/
> -- The C compiler identification is GNU 4.2.1
> -- The CXX compiler identification is GNU 4.2.1
> -- Checking whether C compiler has -isysroot
> -- Checking whether C compiler has -isysroot - yes
> -- Checking whether C compiler supports OSX deployment target flag
> -- Checking whether C compiler supports OSX deployment target flag - yes
> -- Check for working C compiler: /usr/bin/gcc
> -- Check for working C compiler: /usr/bin/gcc -- works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Checking whether CXX compiler has -isysroot
> -- Checking whether CXX compiler has -isysroot - yes
> -- Checking whether CXX compiler supports OSX deployment target flag
> -- Checking whether CXX compiler supports OSX deployment target flag - yes
> -- Check for working CXX compiler: /usr/bin/c++
> -- Check for working CXX compiler: /usr/bin/c++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Configuring done
> -- Generating done
> -- Build files have been written to:
> /Users/pengy/linux/test/cmake/lang/command/target_link_libraries/build
> ~/linux/test/cmake/lang/command/target_link_libraries/build$ make
> Scanning dependencies of target print
> [ 50%] Building CXX object CMakeFiles/print.dir/print.cpp.o
> Linking CXX static library libprint.a
> [ 50%] Built target print
> Scanning dependencies of target main
> [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
> Linking CXX executable main
> [100%] Built target main
> ~/linux/test/cmake/lang/command/target_link_libraries/build$ touch
> ../src/print.hpp
> ~/linux/test/cmake/lang/command/target_link_libraries/build$ make
> Scanning dependencies of target print
> [ 50%] Building CXX object CMakeFiles/print.dir/print.cpp.o
> Linking CXX static library libprint.a
> [ 50%] Built target print
> Scanning dependencies of target main
> [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
> Linking CXX executable main
> [100%] Built target main
> 
> 
> 
> 



More information about the CMake mailing list