tags:

views:

144

answers:

3

What I'd like to do is write just Lazy C++ .lzz files and then have lzz run before a build to generate .cpp and .h files that will be built into the final application, sort of like how moc works with Qt.

Is there any way to do this?

+1  A: 

For make:

sourcecode.h sourcecode.cpp: sourcecode.lzz
<TAB>lazy-cpp sourcecode.lzz

fill in sourcecode.h, sourcecode.cpp, and lazy-cpp with the correct values. I don't know them.

Joshua
I do appreciate the help, but I was asking how to do this in cmake.
jjacksonRIAB
My apologies. I thought cmake was a make variant. I looked it up, it's not. Same approach required, different syntax. Although you could get really lazy and have make process these depends and invoke cmake.
Joshua
+1  A: 

Here is an example of how to do this... First you need to find the lzz program, for that use the find_program command:

find_program(LZZ_COMMAND lzz)

This sets LZZ_COMMAND to the path of the compiler. Then use a CMake custom command to compile the LZZ file to their C++ header/implementation files:

add_custom_command(
    OUTPUT ${output}
    COMMAND ${LZZ_COMMAND} -o ${CMAKE_CURRENT_BINARY_DIR} ${filename})

That generates the files in the current build directory, in case you do out-of-source builds. You will also need to specify that the outputs are generated files:

set_source_files_properties(${output} PROPERTIES GENERATED TRUE)

Put that all together and you get a CMakeLists.txt file something like this:

cmake_minimum_required(VERSION 2.8)
project(lazy_test)
find_program(LZZ_COMMAND lzz)
function(lazy_compile filename)
    get_filename_component(base ${filename} NAME_WE)
    set(base_abs ${CMAKE_CURRENT_BINARY_DIR}/${base})
    set(output ${base_abs}.cpp ${base_abs}.h)
    add_custom_command(
        OUTPUT ${output}
        COMMAND ${LZZ_COMMAND} -o ${CMAKE_CURRENT_BINARY_DIR} ${filename})
    set_source_files_properties(${output} PROPERTIES GENERATED TRUE)
endfunction()
lazy_compile(${CMAKE_CURRENT_SOURCE_DIR}/example.lzz)
add_executable(test example.cpp example.h)

You would probably also want to add include path and other options to lzz eventually. If you placed all the Lazy C++ stuff into a module file and included that from the CMakeLists.txt it would be a bit cleaner. But this is the basic idea.

rq
Thanks, this is just what I was looking for.
jjacksonRIAB
A: 

You must be joking! Where's the advantage over make syntax, if you need write so much? The original answer can be simplified to

%.cpp %.h: %.lzz
    lzz $(input)

with makepp syntax. Through makepp's automatic inference, if you compile something that includes sourcecode.h, or link sourcecode.o to a program or library, makepp will call lzz on the fly, as well as in the 2nd case the compiler to produce sourcecode.o. Let the tool do the work, not the makefile writer. KISS!

Daniel
I suppose the advantage for me is that you can use cmake to generate project files for a good deal of IDEs, compilers, and make systems out there. I've found that no one where I work uses the same IDE - some use Qt Creator, some use Code::Blocks, some use standard make + vim, some use Visual Studio. With cmake you can generate for all of them. The downside, I've found, is that you're still stuck editing cmake files every time you add a file to a project, and it can get pretty hairy, so I'm not entirely sold on it.
jjacksonRIAB