free hit counter

Thursday, July 21, 2011

cmake and reswrap


profile for Rian Sanderson at Stack Overflow, Q&A for professional and enthusiast programmers

I was trying to package up some test data files into an object file so my unit tests wouldn't depend on anything external. StackOverflow was a bit of help, but I eventually pried the answer from the cryptic CMake documentation.

I solved it with reswrap and CMake, using add_custom_command and some additional dependency magic with set_property.


add_custom_command(
OUTPUT testData.cpp
COMMAND reswrap
ARGS testData.src > testData.cpp
DEPENDS testData.src
)
set_property(SOURCE unit-tests.cpp APPEND PROPERTY OBJECT_DEPENDS testData.cpp)

add_executable(app main.cpp)
add_executable(tests unit-tests.cpp)


So now testData.cpp will generated before unit-tests.cpp is compiled, and any time testData.src changes. If the command you're calling is really slow you get the added bonus that when you build just the app target you won't have to wait around for that command (which only the tests executable needs) to finish.

It's not shown above, but careful application of ${PROJECT_BINARY_DIR}, ${PROJECT_SOURCE_DIR} and include_directories() will keep your source tree clean of generated files.

Labels: ,

0 Comments:

Post a Comment

<< Home