From 089868a244e623f75c1ffbe9297d3228bef9a8f1 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 16 Sep 2016 16:23:40 -0400 Subject: [PATCH 1/4] cmState: Record buildsystem target names in each directory Maintain in the directory state the list of target names added to be built. These are normal, non-imported targets (but do include INTERFACE libraries). --- Source/cmMakefile.cxx | 1 + Source/cmState.cxx | 8 ++++++++ Source/cmState.h | 2 ++ 3 files changed, 11 insertions(+) diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 508c6702e..50e7b3315 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -1930,6 +1930,7 @@ cmTarget* cmMakefile::AddNewTarget(cmState::TargetType type, name, cmTarget(name, type, cmTarget::VisibilityNormal, this))) .first; this->GetGlobalGenerator()->IndexTarget(&it->second); + this->GetStateSnapshot().GetDirectory().AddNormalTargetName(name); return &it->second; } diff --git a/Source/cmState.cxx b/Source/cmState.cxx index ffb104b93..2ff4516e2 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -94,6 +94,8 @@ struct cmState::BuildsystemDirectoryStateType std::vector CompileOptions; std::vector CompileOptionsBacktraces; + std::vector NormalTargetNames; + std::string ProjectName; cmPropertyMap Properties; @@ -324,6 +326,7 @@ cmState::Snapshot cmState::Reset() it->CompileOptions.clear(); it->CompileOptionsBacktraces.clear(); it->DirectoryEnd = pos; + it->NormalTargetNames.clear(); it->Properties.clear(); it->Children.clear(); } @@ -1733,6 +1736,11 @@ std::vector cmState::Directory::GetPropertyKeys() const return keys; } +void cmState::Directory::AddNormalTargetName(std::string const& name) +{ + this->DirectoryState->NormalTargetNames.push_back(name); +} + bool operator==(const cmState::Snapshot& lhs, const cmState::Snapshot& rhs) { return lhs.Position == rhs.Position; diff --git a/Source/cmState.h b/Source/cmState.h index 0fac42c57..1324f5fc6 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -175,6 +175,8 @@ public: bool GetPropertyAsBool(const std::string& prop) const; std::vector GetPropertyKeys() const; + void AddNormalTargetName(std::string const& name); + private: void ComputeRelativePathTopSource(); void ComputeRelativePathTopBinary(); From 7a4b8d0dc2f1e780f14e35e1c7ea32dde90576a4 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 19 Sep 2016 13:32:29 -0400 Subject: [PATCH 2/4] 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 +++++++++++++++ .../dev/directory-list-targets-and-subdirs.rst | 5 +++++ Source/cmState.cxx | 14 ++++++++++++++ .../get_property/directory_properties-stderr.txt | 6 +++++- .../get_property/directory_properties.cmake | 4 ++++ .../directory_properties/CMakeLists.txt | 2 ++ .../directory_properties/sub1/CMakeLists.txt | 0 .../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 0f1bfad94..855cee6ba 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 000000000..2c2ea773e --- /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 000000000..554d6d6b7 --- /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 2ff4516e2..325ca7689 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 80c9877ab..b24c7098a 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 b0a9b1b6d..c1347d3b4 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 000000000..f9ebf7811 --- /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 000000000..e69de29bb 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 000000000..e69de29bb From cbca65826c2bf49ba8c99efe8fa315d2942c4836 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 19 Sep 2016 13:33:56 -0400 Subject: [PATCH 3/4] Add directory property to list buildsystem targets Add a BUILDSYSTEM_TARGETS property to allow project code to traverse the list of its own targets in a given directory. --- Help/manual/cmake-properties.7.rst | 1 + Help/prop_dir/BUILDSYSTEM_TARGETS.rst | 11 +++++++++++ .../dev/directory-list-targets-and-subdirs.rst | 4 ++++ Source/cmState.cxx | 5 +++++ .../get_property/directory_properties-stderr.txt | 6 +++++- .../RunCMake/get_property/directory_properties.cmake | 6 ++++++ .../get_property/directory_properties/CMakeLists.txt | 4 ++++ 7 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Help/prop_dir/BUILDSYSTEM_TARGETS.rst diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index 855cee6ba..b15ca9a82 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -54,6 +54,7 @@ Properties on Directories :maxdepth: 1 /prop_dir/ADDITIONAL_MAKE_CLEAN_FILES + /prop_dir/BUILDSYSTEM_TARGETS /prop_dir/CACHE_VARIABLES /prop_dir/CLEAN_NO_CUSTOM /prop_dir/CMAKE_CONFIGURE_DEPENDS diff --git a/Help/prop_dir/BUILDSYSTEM_TARGETS.rst b/Help/prop_dir/BUILDSYSTEM_TARGETS.rst new file mode 100644 index 000000000..da907cb93 --- /dev/null +++ b/Help/prop_dir/BUILDSYSTEM_TARGETS.rst @@ -0,0 +1,11 @@ +BUILDSYSTEM_TARGETS +------------------- + +This read-only directory property contains a +:ref:`;-list ` of buildsystem targets added in the +directory by calls to the :command:`add_library`, :command:`add_executable`, +and :command:`add_custom_target` commands. The list does not include any +:ref:`Imported Targets` or :ref:`Alias Targets`, but does include +:ref:`Interface Libraries`. Each entry in the list is the logical name +of a target, suitable to pass to the :command:`get_property` command +``TARGET`` option. diff --git a/Help/release/dev/directory-list-targets-and-subdirs.rst b/Help/release/dev/directory-list-targets-and-subdirs.rst index 554d6d6b7..5bab6659f 100644 --- a/Help/release/dev/directory-list-targets-and-subdirs.rst +++ b/Help/release/dev/directory-list-targets-and-subdirs.rst @@ -1,5 +1,9 @@ directory-list-targets-and-subdirs ---------------------------------- +* A :prop_dir:`BUILDSYSTEM_TARGETS` directory property was added to + get the list of logical buildsystem target names added by the + project in a directory. + * 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 325ca7689..37e41ad3d 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -29,6 +29,7 @@ #include #include +static std::string const kBUILDSYSTEM_TARGETS = "BUILDSYSTEM_TARGETS"; static std::string const kSUBDIRECTORIES = "SUBDIRECTORIES"; struct cmState::SnapshotDataType @@ -1683,6 +1684,10 @@ const char* cmState::Directory::GetProperty(const std::string& prop, output = cmJoin(child_dirs, ";"); return output.c_str(); } + if (prop == kBUILDSYSTEM_TARGETS) { + output = cmJoin(this->DirectoryState->NormalTargetNames, ";"); + return output.c_str(); + } if (prop == "LISTFILE_STACK") { std::vector listFiles; diff --git a/Tests/RunCMake/get_property/directory_properties-stderr.txt b/Tests/RunCMake/get_property/directory_properties-stderr.txt index b24c7098a..5fb79e14c 100644 --- a/Tests/RunCMake/get_property/directory_properties-stderr.txt +++ b/Tests/RunCMake/get_property/directory_properties-stderr.txt @@ -7,4 +7,8 @@ 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<--$ +get_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties/sub1;[^<;]*Tests/RunCMake/get_property/directory_properties/sub2<-- +get_directory_property: -->CustomTop;InterfaceTop<-- +get_property: -->CustomTop;InterfaceTop<-- +get_directory_property: -->CustomSub;InterfaceSub<-- +get_property: -->CustomSub;InterfaceSub<--$ diff --git a/Tests/RunCMake/get_property/directory_properties.cmake b/Tests/RunCMake/get_property/directory_properties.cmake index c1347d3b4..ae57282a3 100644 --- a/Tests/RunCMake/get_property/directory_properties.cmake +++ b/Tests/RunCMake/get_property/directory_properties.cmake @@ -14,6 +14,12 @@ 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_custom_target(CustomTop) +add_library(InterfaceTop INTERFACE) +add_library(my::InterfaceTop ALIAS InterfaceTop) + add_subdirectory(directory_properties) check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" SUBDIRECTORIES) check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" SUBDIRECTORIES) +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" BUILDSYSTEM_TARGETS) +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" BUILDSYSTEM_TARGETS) diff --git a/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt b/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt index f9ebf7811..7318b9777 100644 --- a/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt +++ b/Tests/RunCMake/get_property/directory_properties/CMakeLists.txt @@ -1,2 +1,6 @@ add_subdirectory(sub1) subdirs(sub2) + +add_custom_target(CustomSub) +add_library(InterfaceSub INTERFACE) +add_library(my::InterfaceSub ALIAS InterfaceSub) From d0be1e15c383f17502d0f47a36d16ba3571b0b79 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 19 Sep 2016 13:47:51 -0400 Subject: [PATCH 4/4] Add directory properties to get source and binary directories Add SOURCE_DIR and BINARY_DIR directory properties that return the absolute paths to the corresponding directories. These correspond to the target properties of the same names that we already have. --- Help/manual/cmake-properties.7.rst | 2 ++ Help/prop_dir/BINARY_DIR.rst | 5 +++++ Help/prop_dir/SOURCE_DIR.rst | 5 +++++ .../release/dev/directory-list-targets-and-subdirs.rst | 7 +++++++ Source/cmState.cxx | 10 ++++++++++ .../get_property/directory_properties-stderr.txt | 10 +++++++++- Tests/RunCMake/get_property/directory_properties.cmake | 5 +++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Help/prop_dir/BINARY_DIR.rst create mode 100644 Help/prop_dir/SOURCE_DIR.rst diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index b15ca9a82..2cb6a1a46 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -54,6 +54,7 @@ Properties on Directories :maxdepth: 1 /prop_dir/ADDITIONAL_MAKE_CLEAN_FILES + /prop_dir/BINARY_DIR /prop_dir/BUILDSYSTEM_TARGETS /prop_dir/CACHE_VARIABLES /prop_dir/CLEAN_NO_CUSTOM @@ -74,6 +75,7 @@ Properties on Directories /prop_dir/RULE_LAUNCH_COMPILE /prop_dir/RULE_LAUNCH_CUSTOM /prop_dir/RULE_LAUNCH_LINK + /prop_dir/SOURCE_DIR /prop_dir/SUBDIRECTORIES /prop_dir/TEST_INCLUDE_FILE /prop_dir/VARIABLES diff --git a/Help/prop_dir/BINARY_DIR.rst b/Help/prop_dir/BINARY_DIR.rst new file mode 100644 index 000000000..597c79aa8 --- /dev/null +++ b/Help/prop_dir/BINARY_DIR.rst @@ -0,0 +1,5 @@ +BINARY_DIR +---------- + +This read-only directory property reports absolute path to the binary +directory corresponding to the source on which it is read. diff --git a/Help/prop_dir/SOURCE_DIR.rst b/Help/prop_dir/SOURCE_DIR.rst new file mode 100644 index 000000000..ac98c3b28 --- /dev/null +++ b/Help/prop_dir/SOURCE_DIR.rst @@ -0,0 +1,5 @@ +SOURCE_DIR +---------- + +This read-only directory property reports absolute path to the source +directory on which it is read. diff --git a/Help/release/dev/directory-list-targets-and-subdirs.rst b/Help/release/dev/directory-list-targets-and-subdirs.rst index 5bab6659f..85f2c826b 100644 --- a/Help/release/dev/directory-list-targets-and-subdirs.rst +++ b/Help/release/dev/directory-list-targets-and-subdirs.rst @@ -1,6 +1,13 @@ directory-list-targets-and-subdirs ---------------------------------- +* A :prop_dir:`SOURCE_DIR` directory property was added to get the + absolute path to the source directory associated with a directory. + +* A :prop_dir:`BINARY_DIR` directory property was added to get the + absolute path to the binary directory corresponding to the source + directory on which the property is read. + * A :prop_dir:`BUILDSYSTEM_TARGETS` directory property was added to get the list of logical buildsystem target names added by the project in a directory. diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 37e41ad3d..6b37b92cc 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -29,7 +29,9 @@ #include #include +static std::string const kBINARY_DIR = "BINARY_DIR"; static std::string const kBUILDSYSTEM_TARGETS = "BUILDSYSTEM_TARGETS"; +static std::string const kSOURCE_DIR = "SOURCE_DIR"; static std::string const kSUBDIRECTORIES = "SUBDIRECTORIES"; struct cmState::SnapshotDataType @@ -1673,6 +1675,14 @@ const char* cmState::Directory::GetProperty(const std::string& prop, } return ""; } + if (prop == kBINARY_DIR) { + output = this->GetCurrentBinary(); + return output.c_str(); + } + if (prop == kSOURCE_DIR) { + output = this->GetCurrentSource(); + return output.c_str(); + } if (prop == kSUBDIRECTORIES) { std::vector child_dirs; std::vector const& children = diff --git a/Tests/RunCMake/get_property/directory_properties-stderr.txt b/Tests/RunCMake/get_property/directory_properties-stderr.txt index 5fb79e14c..6d5bcdb0d 100644 --- a/Tests/RunCMake/get_property/directory_properties-stderr.txt +++ b/Tests/RunCMake/get_property/directory_properties-stderr.txt @@ -11,4 +11,12 @@ get_property: -->[^<;]*Tests/RunCMake/get_property/directory_properties/sub1;[^< get_directory_property: -->CustomTop;InterfaceTop<-- get_property: -->CustomTop;InterfaceTop<-- get_directory_property: -->CustomSub;InterfaceSub<-- -get_property: -->CustomSub;InterfaceSub<--$ +get_property: -->CustomSub;InterfaceSub<-- +get_directory_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties-build<-- +get_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties-build<-- +get_directory_property: -->[^<;]*/RunCMake/get_property<-- +get_property: -->[^<;]*/Tests/RunCMake/get_property<-- +get_directory_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties-build/directory_properties<-- +get_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties-build/directory_properties<-- +get_directory_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties<-- +get_property: -->[^<;]*/Tests/RunCMake/get_property/directory_properties<--$ diff --git a/Tests/RunCMake/get_property/directory_properties.cmake b/Tests/RunCMake/get_property/directory_properties.cmake index ae57282a3..4e68738d8 100644 --- a/Tests/RunCMake/get_property/directory_properties.cmake +++ b/Tests/RunCMake/get_property/directory_properties.cmake @@ -23,3 +23,8 @@ check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" SUBDIRECTORIES) check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" SUBDIRECTORIES) check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" BUILDSYSTEM_TARGETS) check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" BUILDSYSTEM_TARGETS) + +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" BINARY_DIR) +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}" SOURCE_DIR) +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" BINARY_DIR) +check_directory_property("${CMAKE_CURRENT_SOURCE_DIR}/directory_properties" SOURCE_DIR)