[CMake] multi-tiered config file inclusion

Michael Hertling mhertling at online.de
Mon May 31 14:26:44 EDT 2010


On 05/30/2010 11:58 PM, Hugh Sorby wrote:
> 
> So this is what I put into my OpenCASCADE config file
> 
> SET( CONFIG_FILE_CONTENTS
>     "\nGET_FILENAME_COMPONENT( SELF_DIR \"\${CMAKE_CURRENT_LIST_FILE}\" 
> PATH )"
>     "\nINCLUDE( \${SELF_DIR}/OpenCASCADE-targets.cmake )"
>     "\nGET_FILENAME_COMPONENT( OPENCASCADE_INCLUDE_DIRS 
> \"\${SELF_DIR}/../../include/OpenCASCADE\" ABSOLUTE )"
>     "\nSET( OPENCASCADE_LIBRARIES ${OCC_ToolKits_SPACED} )"
>     "\nSET( OPENCASCADE_FOUND TRUE )"
>     "\n" )
> 
> and I put this into my wxWidgets config file
> 
> SET( CONFIG_FILE_CONTENTS
>     "\nGET_FILENAME_COMPONENT( SELF_DIR \"\${CMAKE_CURRENT_LIST_FILE}\" 
> PATH )"
>     "\nINCLUDE( \${SELF_DIR}/wxWidgets-targets.cmake )"
>     "\nGET_FILENAME_COMPONENT( wxWidgets_INCLUDE_DIRS 
> \"\${SELF_DIR}/../../include\" ABSOLUTE )"
>     "\nSET( wxWidgets_LIBRARIES wxexpat ${wxaui_TARGET} ${wxxrc_TARGET} 
> ${wxadv_TARGET} ${wxhtml_TARGET} ${wxgl_TARGET} ${wxxml_TARGET} 
> ${wxbase_TARGET} ${wxcore_TARGET} ${WXWIDGETS_PLATFORM_DEPENDENT_LIBS} )"
>     "\nGET_FILENAME_COMPONENT( wxWidgets_wxrc_EXECUTABLE 
> \"\${SELF_DIR}/../../bin/wxrc${CMAKE_EXECUTABLE_SUFFIX}\" ABSOLUTE )"
>     "\nSET( wxWidgets_DEFINITIONS ${COMPILE_DEFS_SPACED} )"
>     "\nSET( wxWidgets_DEFINITIONS_DEBUG ${COMPILE_DEFS_SPACED_DEBUG} )"
>     "\nSET( wxWidgets_FOUND TRUE )"
>     "\n" )
> 
> and my project has ths config file
> 
>     SET( CONFIG_FILE_CONTENTS
>         "\nGET_FILENAME_COMPONENT( SELF_DIR 
> \"\${CMAKE_CURRENT_LIST_FILE}\" PATH )"
>         "\nINCLUDE( \${SELF_DIR}/${CMGUI_LIB_GROUP}-targets.cmake )"
>         "\nGET_FILENAME_COMPONENT( CMGUI_INCLUDE_DIRS 
> \"\${SELF_DIR}/../../include/cmgui\" ABSOLUTE )"
>         "\nSET( CMGUI_LIBRARIES ${TARGET_CMGUI_LIB} 
> ${TARGET_CMGUI_GENERAL_LIB} ${TARGET_CMGUI_CORE_FIELDS_LIB} 
> ${TARGET_CMGUI_PASS_THROUGH_LIB} )"
>         "\nSET( CMGUI_FOUND TRUE )"
>         "\n\n" )
> 
> After thinking about this over the weekend I am thinking I might have to 
> add some conditional INCLUDEs in to drag in the imported libraries from 
> OpenCASCADE, wxWidgets .e.t.c.. I am not sure about this approach 
> because it releies on libraries being stationary,  as an alternative I 
> thought I could add a FIND_PACKAGE with a HINTS to find the config 
> file.  It seems to me like I am just implementing a libtool archive file 
> in CMake, not my intention but I can see the attraction in it.

Perhaps, the following mylib-config.cmake is a suitable alternative -
rudimentary and not accounting for OpenCASCADE/wxWidget's specifics:

INCLUDE(<CMAKE_INSTALL_PREFIX>/share/mylib/mylib-targets.cmake)
# Now, imported target "mylib" and its location(s) are known.
SET(MYLIB_LIBRARIES "mylib")
SET(MYLIB_DEFINITIONS <mylib compile definitions>)
SET(MYLIB_INCLUDE_DIRS "<CMAKE_INSTALL_PREFXIX>/.../include")
FIND_PACKAGE(OpenCASCADE PATHS ... NO_DEFAULT_PATH)
IF(OpenCASCADE_FOUND)
    LIST(APPEND MYLIB_LIBRARIES ${OpenCASCADE_LIBRARIES})
    LIST(APPEND MYLIB_DEFINITIONS ${OpenCASCADE_DEFINITIONS})
    LIST(APPEND MYLIB_INCLUDE_DIRS ${OpenCASCADE_INCLUDE_DIRS})
ENDIF()
FIND_PACKAGE(wxWidgets COMPONENTS ... PATHS ... NO_DEFAULT_PATH)
IF(wxWidgets_FOUND)
    LIST(APPEND MYLIB_LIBRARIES ${wxWidgets_LIBRARIES})
    LIST(APPEND MYLIB_DEFINITIONS ${wxWidgets_DEFINITIONS})
    LIST(APPEND MYLIB_INCLUDE_DIRS ${wxWidgets_INCLUDE_DIRS})
ENDIF()
# Now, MYLIB_{LIBRARIES,DEFINITIONS,INCLUDE_DIRS}, i.e. the officially
# recommended variables, are set up and ready to be used by a project.

The imported targets along with their locations are defined as usual
in mylib-targets.cmake which will be generated and installed with

INSTALL(TARGETS mylib EXPORT mylib-targets DESTINATION ...)
INSTALL(EXPORT mylib-targets DESTINATION share/mylib ...)

in your project's CMakeLists.txt.

If your opencascade-config.cmake and wxwidgets-config.cmake behave in
the same way, a project's CMakeLists.txt could use FIND_PACKAGE(mylib)
and, thereafter, MYLIB_{LIBRARIES,DEFINITIONS,INCLUDE_DIRS} to enable
the settings of mylib *and* its prerequisites OpenCASCADE/wxWidgets:

FIND_PACKAGE(mylib)
INCLUDE_DIRECTORIES(${MYLIB_INCLUDE_DIRS})
ADD_DEFINITIONS(${MYLIB_DEFINITIONS})
[...]
TARGET_LINK_LIBRARIES(... ${MYLIB_LIBRARIES})

Would that approach work for you?

Regards,

Michael


More information about the CMake mailing list