Is it possible to compile recently changed files first?

Viewed 105

The following scenario happens a lot:

  • I change a header which is included in a lot of places, e.g. to add a function declaration.
  • I add a function definition to the corresponding source file, which has an error because I'm dumb.
  • I compile, and wait a long time for a bunch of irrelevant stuff to be compiled before I see the error in the code I'm working on.

If cmake would prioritize compiling recently modified files first, it would reduce my test cycle time in these cases by several minutes. Is this possible?

1 Answers

I couldn't find anything general in CMake that allows you to specify build order, but you may be able to do this with specific build system generators that allow you to compile individual .o or .obj files. For example, using the Ninja generator:

add_executable(mytarget the-suspect-src.cpp)

The generated Ninja build system lets me build the corresponding .o file by specifying it explicitly:

ninja CMakeFiles/mytarget.dir/the-suspect-src.cpp.o

So you could achieve your desired behavior with:

ninja CMakeFiles/mytarget.dir/the-suspect-src.cpp.o && ninja

Note that I don't memorize these paths to the .o files, but instead tab-complete in the terminal.


I happen to know that the Makefile generators also have a similar ability to build individual .o files, but I'm not aware of any other generators which have this ability.

Related