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

Philip Lowman philip at yhbt.com
Sat May 30 13:53:45 EDT 2009


On Sat, May 30, 2009 at 12:23 PM, Sean Chittenden <sean at chittenden.org>wrote:

> Hello.
>
> I'm incorporating an external library in to our source tree and would like
> to run the external library's regression tests.  The library is C, however,
> and the convention for doing this is:
>
> /* Lib contents above */
> #ifdef TESTING
> int main(int argc, char* argv[]) {
>  /* Do tests
> }
> #endif
>
> Where I'm having my problem is in layering two source files like this that
> are interdependent.  In the following example, both foo.c and foo_bar.c have
> #ifdef TESTING sections.  In CMakeLists.txt, I have something like:
>
> 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(foo_bar_test foo_bar.c foo.c)
> add_test(foo_bar_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/foo_bar_test)
> set_property(TARGET foo_bar_test APPEND PROPERTY COMPILE_FLAGS "-DTESTING")
>
>
> Which obviously doesn't work when building foo_bar_test because there are
> two main() functions now (foo_test works just fine).  I've tried various
> SCOPE qualifiers such as SOURCE, TARGET and TEST with no luck.  Having read
> the docs a few dozen times, the behavior of SCOPE for set_property is vague.


When setting a TARGET property (aka set_target_properties) your settings
affect only the building of the target that is specified.  For SOURCE they
will affect anywhere that source file is used in any target.

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?



-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090530/b3206e76/attachment.htm>


More information about the CMake mailing list