source_group called from different directory (not working)
Hi,
in example below...
only if SOURCE_GROUP_CALLED_LATER
is set to OFF
, does if work that mod.cpp
and mod.h
are placed in a source_group xxx
.
How can one get it to work with SOURCE_GROUP_CALLED_LATER
on ON
(which results in source_group
called from a different directory, than the one on which one want it to apply?) (is it a possible "bug").
Thanks.
# /CMakeLists.txt (toplevel)
cmake_minimum_required(VERSION 3.13)
set(SOURCE_GROUP_CALLED_LATER ON CACHE BOOL "source group called on target from a different dir" FORCE) ## set this to OFF, to get it to work
function(set_source_group target)
get_target_property(sourceDir ${target} SOURCE_DIR)
get_target_property(sourceList ${target} SOURCES)
## convert sourceList -> sourceListAbsolute
set(sourceListAbsolute "")
foreach(source IN LISTS sourceList)
if (NOT IS_ABSOLUTE "${source}")
set(source "${sourceDir}/${source}")
endif()
get_filename_component(source "${source}" ABSOLUTE)
list(APPEND sourceListAbsolute "${source}")
endforeach()
message(STATUS "##### source_group(TREE ${sourceDir} PREFIX xxx FILES ${sourceListAbsolute})")
source_group(TREE ${sourceDir} PREFIX xxx FILES ${sourceListAbsolute}) ### HERE's the important line-under-test!!!
endfunction()
project(go)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_subdirectory(mod)
if (SOURCE_GROUP_CALLED_LATER)
set_source_group(mod_target) #### this does not work!!! why?
endif()
# /mod/CMakeLists.txt
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/mod.cpp "")
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/mod.h "")
add_library(mod_target mod.cpp mod.h)
if (NOT SOURCE_GROUP_CALLED_LATER)
set_source_group(mod_target) ### this works!
endif()