I am trying to list files in subdirectory with a specific pattern using CMake…
function(ListFilesInSubmodule directory_path pattern)
if(DEFINED WIN32)
execute_process(COMMAND powershell -c 'Get-ChildItem -Path "${directory_path}" -Recurse -Filter "${pattern}" -File | ForEach-Object { Write-Host $_.FullName }'
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _Files
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
execute_process(COMMAND bash -c "find '${directory_path}' -type f -name '${pattern}'"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _Files
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(NOT _Files)
message(STATUS "No files found matching pattern '${pattern}' in '${directory_path}'.")
else()
string(REPLACE "n" ";" _FilesList ${_Files})
foreach(_file IN LISTS _FilesList)
if(EXISTS "${CMAKE_SOURCE_DIR}/${_file}")
set(PLUGIN_STRUCTURES
${PLUGIN_STRUCTURES}
"${_file}"
PARENT_SCOPE
)
message(STATUS "File is found '${_file}'.")
else()
message(WARNING "File is possibly not found '${_file}'.")
endif()
endforeach()
endif()
endfunction()
i tried the function like this
ListFilesInSubmodule("libs/subhook" "*.c")
ListFilesInSubmodule("libs/subhook" "*.h")
message(STATUS "PLUGIN_STRUCTURES : ${PLUGIN_STRUCTURES}")
and it almost works but the target variable is set to the last file’s name only not all of them as expected
I tried to set a separate variable like
else()
string(REPLACE "n" ";" _FilesList ${_Files})
foreach(_file IN LISTS _FilesList)
if(EXISTS "${CMAKE_SOURCE_DIR}/${_file}")
set(_Temp ${_Temp} "${_file}")
message(STATUS "File is found '${_file}'.")
else()
message(WARNING "File is possibly not found '${_file}'.")
endif()
endforeach()
endif()
set(PLUGIN_STRUCTURES
${_Temp}
PARENT_SCOPE
)
but it didn’t work either…