Using add_dune_XXX_flags with interface targets
Hi, I am currently developing a Dune module as a header-only library that depends on a few other libraries and ideally I would like to do something like this
dune_project()
dune_add_library(dunemylib INTERFACE EXPORT_NAME Dune::MyLib)
find_package(SuiteSparse REQUIRED COMPONENTS UMFPACK CHOLMOD)
add_dune_suitesparse_flags(dunemylib)
But this doesn't work because the add_dune_XXX_flags functions currently don't support interface targets. So I created a CMake function like this
function(get_target_scope target_name result_var)
get_target_property(target_type ${target_name} TYPE)
if(target_type STREQUAL "INTERFACE_LIBRARY")
# Interface libraries MUST use INTERFACE
set(${result_var} INTERFACE PARENT_SCOPE)
else()
# Standard libraries/executables usually use PUBLIC
set(${result_var} PUBLIC PARENT_SCOPE)
endif()
endfunction()
which can then be used as follows (e.g. in the add_dune_suitesparse_flags function):
diff --git a/cmake/modules/AddSuiteSparseFlags.cmake b/cmake/modules/AddSuiteSparseFlags.cmake
index 701cad9cf..06a131d95 100644
--- a/cmake/modules/AddSuiteSparseFlags.cmake
+++ b/cmake/modules/AddSuiteSparseFlags.cmake
@@ -29,8 +29,10 @@ endif()
function(add_dune_suitesparse_flags _targets)
if(SuiteSparse_FOUND)
foreach(_target ${_targets})
- target_link_libraries(${_target} PUBLIC SuiteSparse::SuiteSparse)
- target_compile_definitions(${_target} PUBLIC ENABLE_SUITESPARSE=1)
+ get_target_scope(${_target} SCOPE)
+
+ target_link_libraries(${_target} ${SCOPE} SuiteSparse::SuiteSparse)
+ target_compile_definitions(${_target} ${SCOPE} ENABLE_SUITESPARSE=1)
endforeach(_target)
endif()
endfunction(add_dune_suitesparse_flags)
As far as I can see, this does not break the old use case but would support interface targets. I'd be happy to create a PR to change this in all the add_dune_XXX_flags functions but I first wanted to create this issue because maybe there's something I am overlooking.