From 7a4b8d0dc2f1e780f14e35e1c7ea32dde90576a4 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 19 Sep 2016 13:32:29 -0400 Subject: 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. --- Help/manual/cmake-properties.7.rst | 1 + Help/prop_dir/SUBDIRECTORIES.rst | 15 +++++++++++++++ Help/release/dev/directory-list-targets-and-subdirs.rst | 5 +++++ Source/cmState.cxx | 14 ++++++++++++++ .../RunCMake/get_property/directory_properties-stderr.txt | 6 +++++- Tests/RunCMake/get_property/directory_properties.cmake | 4 ++++ .../get_property/directory_properties/CMakeLists.txt | 2 ++ .../get_property/directory_properties/sub1/CMakeLists.txt | 0 .../get_property/directory_properties/sub2/CMakeLists.txt | 0 9 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Help/prop_dir/SUBDIRECTORIES.rst create mode 100644 Help/release/dev/directory-list-targets-and-subdirs.rst create mode 100644 Tests/RunCMake/get_property/directory_properties/CMakeLists.txt create mode 100644 Tests/RunCMake/get_property/directory_properties/sub1/CMakeLists.txt create mode 100644 Tests/RunCMake/get_property/directory_properties/sub2/CMakeLists.txt diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index 0f1bfad..855cee6 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -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 diff --git a/Help/prop_dir/SUBDIRECTORIES.rst b/Help/prop_dir/SUBDIRECTORIES.rst new file mode 100644 index 0000000..2c2ea77 --- /dev/null +++ b/Help/prop_dir/SUBDIRECTORIES.rst @@ -0,0 +1,15 @@ +SUBDIRECTORIES +-------------- + +This read-only directory property contains a +:ref:`;-list ` 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. diff --git a/Help/release/dev/directory-list-targets-and-subdirs.rst b/Help/release/dev/directory-list-targets-and-subdirs.rst new file mode 100644 index 0000000..554d6d6 --- /dev/null +++ b/Help/release/dev/directory-list-targets-and-subdirs.rst @@ -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. diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 2ff4516..325ca76 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -29,6 +29,8 @@ #include #include +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 child_dirs; + std::vector const& children = + this->DirectoryState->Children; + for (std::vector::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 listFiles; cmState::Snapshot snp = this->Snapshot_; diff --git a/Tests/RunCMake/get_property/directory_properties-stderr.txt b/Tests/RunCMake/get_property/directory_properties-stderr.txt index 80c9877..b24c709 100644 --- a/Tests/RunCMake/get_property/directory_properties-stderr.txt +++ b/Tests/RunCMake/get_property/directory_properties-stderr.txt @@ -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<--$ diff --git a/Tests/RunCMake/get_property/directory_properties.cmake b/Tests/RunCMake/get_property/directory_properties.cmake index b0a9b1b..c1347d3 100644 --- a/Tests/RunCMake/get_property/directory_properties.cmake +++ b/Tests/RunCMake/get_property/directory_properties.cmake @@ -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) diff --git a/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt b/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt new file mode 100644 index 0000000..f9ebf78 --- /dev/null +++ b/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(sub1) +subdirs(sub2) diff --git a/Tests/RunCMake/get_property/directory_properties/sub1/CMakeLists.txt b/Tests/RunCMake/get_property/directory_properties/sub1/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/get_property/directory_properties/sub2/CMakeLists.txt b/Tests/RunCMake/get_property/directory_properties/sub2/CMakeLists.txt new file mode 100644 index 0000000..e69de29 -- cgit v0.12