Add a directory property to list subdirectories

Add a SUBDIRECTORIES directory property to allow project code to
traverse the directory structure of itself as CMake sees it.
This commit is contained in:
Brad King 2016-09-19 13:32:29 -04:00
parent 089868a244
commit 7a4b8d0dc2
9 changed files with 46 additions and 1 deletions

View File

@ -73,6 +73,7 @@ Properties on Directories
/prop_dir/RULE_LAUNCH_COMPILE
/prop_dir/RULE_LAUNCH_CUSTOM
/prop_dir/RULE_LAUNCH_LINK
/prop_dir/SUBDIRECTORIES
/prop_dir/TEST_INCLUDE_FILE
/prop_dir/VARIABLES
/prop_dir/VS_GLOBAL_SECTION_POST_section

View File

@ -0,0 +1,15 @@
SUBDIRECTORIES
--------------
This read-only directory property contains a
:ref:`;-list <CMake Language Lists>` of subdirectories processed so far by
the :command:`add_subdirectory` or :command:`subdirs` commands. Each entry is
the absolute path to the source directory (containing the ``CMakeLists.txt``
file). This is suitable to pass to the :command:`get_property` command
``DIRECTORY`` option.
.. note::
The :command:`subdirs` command does not process its arguments until
after the calling directory is fully processed. Therefore looking
up this property in the current directory will not see them.

View File

@ -0,0 +1,5 @@
directory-list-targets-and-subdirs
----------------------------------
* A :prop_dir:`SUBDIRECTORIES` directory property was added to
get the list of subdirectories added by a project in a directory.

View File

@ -29,6 +29,8 @@
#include <string.h>
#include <utility>
static std::string const kSUBDIRECTORIES = "SUBDIRECTORIES";
struct cmState::SnapshotDataType
{
cmState::PositionType ScopeParent;
@ -1670,6 +1672,18 @@ const char* cmState::Directory::GetProperty(const std::string& prop,
}
return "";
}
if (prop == kSUBDIRECTORIES) {
std::vector<std::string> child_dirs;
std::vector<cmState::Snapshot> const& children =
this->DirectoryState->Children;
for (std::vector<cmState::Snapshot>::const_iterator ci = children.begin();
ci != children.end(); ++ci) {
child_dirs.push_back(ci->GetDirectory().GetCurrentSource());
}
output = cmJoin(child_dirs, ";");
return output.c_str();
}
if (prop == "LISTFILE_STACK") {
std::vector<std::string> listFiles;
cmState::Snapshot snp = this->Snapshot_;

View File

@ -3,4 +3,8 @@ get_property: --><--
get_directory_property: -->value<--
get_property: -->value<--
get_directory_property: --><--
get_property: --><--$
get_property: --><--
get_directory_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties<--
get_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties<--
get_directory_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties/sub1;[^<;]*Tests/RunCMake/get_property/directory_properties/sub2<--
get_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties/sub1;[^<;]*Tests/RunCMake/get_property/directory_properties/sub2<--$

View File

@ -13,3 +13,7 @@ set_directory_properties(PROPERTIES empty "" custom value)
check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" empty)
check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" custom)
check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" noexist)
add_subdirectory(directory_properties)
check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" SUBDIRECTORIES)
check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" SUBDIRECTORIES)

View File

@ -0,0 +1,2 @@
add_subdirectory(sub1)
subdirs(sub2)