[CMake] namespace support

Michael Hertling mhertling at online.de
Wed Feb 2 11:18:23 EST 2011


On 02/02/2011 04:21 PM, Peter Kümmel wrote:
> On 02.02.2011 16:17, Michael Wild wrote:
>>> namespace(a)
>>>      include_directories(a)
>>>      add_library(a MODULE a/a.cpp)
>>> endnamespace()
>>>
>> Put a CMakeLists.txt file in a/ and b/ and do the include_directories()
> 
> Exactly this is what I don't wanna do and why I've asked ;)
> 
> Seems it becomes a feature request.

Indeed, the restriction of INCLUDE_DIRECTORIES() is sometimes annoying;
having INCLUDE_DIRECTORIES target and source file properties available
along with the directory property - analogous to COMPILE_DEFINITIONS -
would be quite welcome, IMO. However, if your plugins are built in a
sufficiently unique manner, you might automate this process by use of
a CMake function:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(PLUGINS C)
FILE(WRITE ${CMAKE_SOURCE_DIR}/f/f.c "void f(void){}\n")
FILE(WRITE ${CMAKE_SOURCE_DIR}/g/g.c "void g(void){}\n")
FILE(WRITE ${CMAKE_SOURCE_DIR}/h/h.c "void h(void){}\n")
FUNCTION(ADD_PLUGINS)
    FOREACH(i IN LISTS ARGN)
        IF(NOT IS_ABSOLUTE ${i})
            SET(i ${CMAKE_CURRENT_SOURCE_DIR}/${i})
        ENDIF()
        GET_FILENAME_COMPONENT(t ${i} NAME_WE)
        GET_FILENAME_COMPONENT(p ${i} PATH)
        GET_FILENAME_COMPONENT(d ${p} NAME)
        FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${d}/CMakeLists.txt
        "INCLUDE_DIRECTORIES(${p})\nADD_LIBRARY(${t} MODULE ${i})\n")
       ADD_SUBDIRECTORY(${CMAKE_CURRENT_BINARY_DIR}/${d}
                        ${CMAKE_CURRENT_BINARY_DIR}/${d})
    ENDFOREACH()
ENDFUNCTION()
ADD_PLUGINS(f/f.c g/g.c h/h.c)

For each plugin, the ADD_PLUGINS() function creates a directory and
provides a CMakeLists.txt which builds the plugin with an individual
search path for header files. Possibly, that approach can be adapted
to your needs.

Regards,

Michael


More information about the CMake mailing list