Victus Spiritus

home

An elegant ruby script for building complex c++ projects, using cmake

30 Oct 2011

Disclaimer: As a blogger I leverage the creative license to call solutions elegant, although in reality they may be far from it. Descriptive veracity is up to each reader.

"I'm mostly blogging this for my own future use, to be able to find how to do something I remember doing before. There you go, future me."

A friend and fellow blogger Denny Gentry reminded me of the value of helping out my future self by capturing a handy piece of code within a blog post.

Last week I had to back port a fairly complex project from Visual Studio 10 to Visual Studio 9. Microsoft likely has some tools laying around to achieve this, but I'm often tasked with rapidly moving our projects over to linux or other operating systems, and I decided to write a short script to solve the problem of cross platform project building once and for all.

All that's needed now are cmake, a ruby interpreter, and two lists of files. One list is for libraries (order matters, last dependency last) and another for executables. One caveat is that if the system you're ultimately moving too doesn't have cmake you may need to delete any project dependencies on CMakeLists.txt in the created project (I had to). By default cmake watches for changes to this file and remakes project files, which will lead to build errors without cmake's installation.


create_cmake.rb

</a>

create_cmake is a simple ruby script and sample lib which takes a list of libraries (with paths) and executables and generates a CMakeLists.txt file. The CMakeLists.txt file will generate build files on many platforms include linux, os x, and windows in the form of makefiles and visual studio solutions. See cmake.org for all supported build systems.

The create_cmake.rb script

With sample inputs:

library list:


  one_lib
  another/lib
  third_lib

exec list:


  test/test.cpp

In this example to generate the CMakeLists.txt file first a lib.list and an exec.list are needed. See the included repo files for examples of formats (file lists where order matters, put your latest dependency last on the lib list).

In the project directory run:

ruby create_cmake.rb lib.list exec.list test > CMakeLists.txt

Then from the project root type

cd build
cmake ..

And cmake will determine a default build system for your architecture.

You may specify targeted builds by typing:


    cmake

This yields a listing of supported platforms. Here's an example of what showed up on my home system:

The following generators are available on this platform:

    Unix Makefiles              = Generates standard UNIX makefiles.
    Xcode                       = Generate XCode project files.
    CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
    Eclipse CDT4 - Unix Makefiles = Generates Eclipse CDT 4.0 project files.
    KDevelop3                   = Generates KDevelop 3 project files.
    KDevelop3 - Unix Makefiles  = Generates KDevelop 3 project files.

To create an Xcode project enter:

cmake -G Xcode (path to CMakeLists.txt file)