[CMake] Idea: generic task-driven make-like targets with CMake

Kevin Wojniak kainjow at kainjow.com
Tue Dec 8 13:04:35 EST 2015


The one thing I’ve found I still have to replace with CMake is the 
ability to specify generic “make” targets that do various tasks, 
such as a release build using the same build directory, run tests, grab 
dependencies.

For example, with Make I can do:

release:
     mkdir -p build_dir
     cd build_dir && cmake -DCMAKE_BUILD_TYPE=Release ..

And all I need to run is:

make release

But this obviously doesn’t work with native MSVC builds. So, then 
you’re stuck duplicating this logic with Batch or PowerShell scripts, 
or using a scripting language such as Python or Ruby.

Now, Ruby with Rake is a very nice replacement, so this can become:

task :release do
     FileUtils.mkdir_p “build_dir”
     Dir.chdir “build_dir” do
         sh “cmake”, “-DCMAKE_BUILD_TYPE=Release”, “..”
     end
end

And I can run on all platforms where Ruby/Rake are available:

rake release

However, Rake/Ruby is a heavy requirement to run these tasks which are 
very simple for most projects. Plus, you cannot use CMake logic such as 
compiler/platform detection if you need it outside of CMake.

Is there any way to do this cleanly with CMake tools? If not, what if a 
simple “ctask” tool was added that offered this basic functionality. 
For example, within my CMakeLists.txt file I could have tasks defined 
like:

add_task(release
     COMMAND ${CMAKE_COMMAND} -E make_directory “build_dir”
     COMMAND ${CMAKE_COMMAND} -E chdir “build_dir” ${CMAKE_COMMAND} 
“-DCMAKE_BUILD_TYPE=Release”, “..”
)

Then, this could be executed as:

ctask release

Tasks could depend on other tasks with a DEPENDS argument, just like 
normal CMake targets.

Thoughts?

Kevin


More information about the CMake mailing list