Specifying block dependencies in cmake

I’m trying setup a simple project in CLion on OS X with cmake.

This is how my project is structured. I have two blocks - CoC-Core and CoC-Assets. CoC-Assets needs a header file from CoC-Core. I’ve setup the cmake files for these two blocks based on the TUIO and OSC example (looking at the documentation here).

When I run the build, CoC-Assets complains that cocCore.h is not found.

Here are my cmake files.

## CoC-CoreConfig.cmake

if( NOT TARGET CoC-Core )
	get_filename_component( COC_CORE_SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../src" ABSOLUTE )

	FILE(GLOB COC_CORE_SOURCES ${COC_CORE_SOURCE_PATH}/*.cpp)

	add_library( CoC-Core ${COC_CORE_SOURCES} )
	target_include_directories( CoC-Core PUBLIC "${COC_CORE_SOURCE_PATH}" )

	# If Cinder included from this block:

	target_include_directories( CoC-Core SYSTEM BEFORE PUBLIC "${CINDER_PATH}/include" )

	if( NOT TARGET cinder )
		include( "${CINDER_PATH}/proj/cmake/configure.cmake" )
		find_package( cinder REQUIRED PATHS
				"${CINDER_PATH}/${CINDER_LIB_DIRECTORY}"
				"$ENV{CINDER_PATH}/${CINDER_LIB_DIRECTORY}" )
	endif()

	target_link_libraries( CoC-Core PRIVATE cinder )
	
endif()
## CoC-AssetsConfig.cmake

if( NOT TARGET CoC-Assets )
    get_filename_component( COC_ASSETS_SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../src" ABSOLUTE )

    FILE(GLOB COC_ASSETS_SOURCES ${COC_ASSETS_SOURCE_PATH}/*.cpp)

    add_library( CoC-Assets ${COC_ASSETS_SOURCES} )
    target_include_directories( CoC-Assets PUBLIC "${COC_ASSETS_SOURCE_PATH}" )

    # If Cinder included from this block:

    target_include_directories( CoC-Assets SYSTEM BEFORE PUBLIC "${CINDER_PATH}/include" )

    if( NOT TARGET cinder )
        include( "${CINDER_PATH}/proj/cmake/configure.cmake" )
        find_package( cinder REQUIRED PATHS
                "${CINDER_PATH}/${CINDER_LIB_DIRECTORY}"
                "$ENV{CINDER_PATH}/${CINDER_LIB_DIRECTORY}" )
    endif()

    # Add CoC-Core block as a dependency
    get_filename_component( COC_CORE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../CoC-Core/proj/cmake" ABSOLUTE )
    find_package( CoC-Core REQUIRED PATHS "${COC_CORE_MODULE_PATH}" )
    if (CoC-Core_FOUND)
#        target_include_directories( CoC-Assets PUBLIC ${COC_CORE_SOURCE_PATH} ABSOLUTE ) # <-- this line works
        add_dependencies( CoC-Assets CoC-Core ) # <-- this does not
    endif()

    target_link_libraries( CoC-Assets PRIVATE cinder)

endif()

I can tell that the find_package works correctly because CoC-Core_FOUND is true. However, using add_dependencies (as per the TUIO/OSC example) does not seem to get the include directories of CoC-Core. If I manually include the directories via target_include_directories, the build is successful.

I’m not sure what I’m doing wrong. Any suggestions will be much appreciated. Thanks!

Hi,

you are not really doing something wrong actually but instead its the functionality of add_dependencies which you might be misunderstanding. What this call actually does is to ensure that CoC-Core will build before CoC-Assets but it does not add it as a link or include dependency in the build process.

In order to do that you either have to manually include it as you discovered already through target_include_directories or even better just specify it as a link dependency of your CoC-Assets target i.e

if( CoC-Core_FOUND )
    add_dependencies( CoC-Assets CoC-Core )
    target_link_libraries( Coc-Assets PUBLIC CoC-Core )
endif()

This should make the includes from CoC-Core visible to CoC-Assets.

Just a note also that doing it this way you don’t have to specify then on your app’s cmake file both blocks as a dependency but instead you just specify CoC-Assets and then CMake will also pull in CoC-Core since we have specified it as a public link dependency of CoC-Assets.

HTH,
Petros

Thanks so much for the clarification Petros! That works perfectly.