[CMake] set_property() scope qualifier question...

Sean Chittenden sean at chittenden.org
Sat May 30 16:48:07 EDT 2009


> Is set_property(TEST foo_bar_test SOURCE foo_bar.c APPEND PROPERTY  
> COMPILE_FLAGS "-DTESTING") an an inclusive AND of the test  
> requirements (for property to be set we must be building the TEST  
> foo_bar_test *and* the SOURCE foo_bar.c) or is it an OR (for  
> property to be set we must be building either TEST foo_bar_test *or*  
> the SOURCE foo_bar.c).  I can't use set_source_file_property()  
> because there isn't a scope qualifier and I've got my own main()  
> functions elsewhere.  :)
>
> I'm not familiar with set_property(TEST foo_bar_test SOURCE  
> foo_bar.c APPEND PROPERTY COMPILE_FLAGS "-DTESTING")
>
> Have you considered just putting an
>
> #ifndef TESTING
> int main()
> {
> ...
> }
> #endif
>
> around foo.c.  Or simply refactoring the code so whatever code  
> that's in foo.c that foo_bar_test needs just goes into a separate  
> source file.  That way foo.c and foo_bar.c each have only a main()  
> function in them?

Well, right now I've gone the `patch -p0 < ...` route, but it's less  
than elegant.  *grin*


The issue is it's not my code, but it is my build infrastructure.   
Here's a mostly complete example that shows what the situation is and  
what I'd like to see happen:

// BEGIN foo.h
int foo(int a);
// END foo.h

// BEGIN foo.c
#include "foo.h"
int foo(int a) { return a + 1; }

#ifdef TESTING
int main(void) { assert(foo(1) == 2); return 0; }
#endif
// END foo.c

// BEGIN bar.c
#include "foo.h"
int bar(int b) { return foo(1) + 1; }

#ifdef TESTING
int main(void) { assert(bar(1) == 2); return 0; }
#endif



// CMakeLists.txt
add_executable(foo_test foo.c)
add_test(foo_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/foo_test)
set_property(TARGET foo_test APPEND PROPERTY COMPILE_FLAGS "-DTESTING")

add_executable(bar_test foo.c bar.c)
add_test(bar_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/bar_test)
set_property(TARGET bar_test APPEND PROPERTY COMPILE_FLAGS "-DTESTING")

// Elsewhere I use:
add_executable(my_app my_app.c foo.c bar.c)


Ideally I'd like to see cmake(1) be able to issue the following  
commands:

// Create foo_test
gcc -o foo.o -c foo.c -DTESTING
gcc -o foo_test foo.o

// Create bar_test
gcc -o foo.o -c foo.c
gcc -o bar.o -c bar.c -DTESTING
gcc -o bar_test foo.o bar.o


That help clarify?  set_target_property() still defines TESTING for  
both foo.c and bar.c and the bar_test case would break.  Ideally I'd  
like to set a source property in the bar_test case, but don't know how  
to do that.  I thought the scope property would do it.  Can I somehow  
finagle this using an if() ?

Thanks in advance.  -sc


--
Sean Chittenden
sean at chittenden.org



More information about the CMake mailing list