Using Classdesc: Method 3, inlining with CMake

Using Makefiles is ideal, as it is possible to generate Makefile dependencies automatically from the source code, and to create the generated .cd files automatically via a Makefile rule.

When using other systems, eg CMake, you will need to explicitly list the header files that need to be generated by classdesc. An example CMake function that is useful for this purpose is:

function(classdesc)
  foreach(header ${ARGV})
    get_filename_component(stem ${header} NAME_WLE)
    set(source ${CMAKE_CURRENT_SOURCE_DIR}/${stem}.h)
    set(dest ${stem}.cd)
    add_custom_command(OUTPUT ${dest} MAIN_DEPENDENCY ${source} COMMAND ${CLASSDESC} -respect_private -typeName -i ${source} ParserSerialise  ParserDeserialise >${dest})
    set(HEADERS ${HEADERS} ${stem}.cd)
    include_directories(SYSTEM ${CMAKE_CURRENT_BINARY_DIR})
  endforeach()
endfunction()    
include_directories(SYSTEM ${CMAKE_BINARY_DIR})

Within each project's CMakeLists.txt file, simply declare the header files needing to be processed via a CLASSDESC line, eg

CLASSDESC(foo.h bla.h)

The .cd files are placed within the build directory, in a tree hierarchy corresponding to where the source header files. So if foo.h is found in include/somelib/foo.h, the .cd is placed in the build directory (aka CMAKE_BINARY_DIR) under include/somelib/foo.cd. The last include_directories ensures that the cd file can be found via

#include "include/somelib/foo.cd"

The above function, which is run at cmake time, creates a single rule dependency for every header file mentioned, such that alterations to the source header file will cause the .cd file to be rebuilt, a consequently the object file that includes it via being added the the HEADERS special variable.

Classdesc has been used with other build systems too, such a Visual Studio. The strategy there is to create a separate project which all others in the solution depend on. Thus all .cd files are regenerated at the start of the run if any dependent header file is changed. This is a little wasteful, but the classdesc preprocessor is generally so quick that it doesn't impact compile times much.