Skip to content

Commit f6969b9

Browse files
committed
set_property: Deduplicate source file directory scopes
A user could specify the same directory scope to set_property() multiple times, which in conjunction with APPEND would append the property multiple times. Make sure to deduplicate scopes across both DIRECTORY and TARGET_DIRECTORY options, so that a property is only appended once in such a scenario. Fixes: #20941
1 parent 46f1fa0 commit f6969b9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Source/cmSetPropertyCommand.cxx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <set>
66
#include <sstream>
7+
#include <unordered_set>
78

89
#include "cmExecutionStatus.h"
910
#include "cmGlobalGenerator.h"
@@ -82,6 +83,8 @@ bool HandleSourceFileDirectoryScopes(
8283
std::vector<std::string>& source_file_target_directories,
8384
std::vector<cmMakefile*>& directory_makefiles)
8485
{
86+
std::unordered_set<cmMakefile*> directory_makefiles_set;
87+
8588
cmMakefile* current_dir_mf = &status.GetMakefile();
8689
if (!source_file_directories.empty()) {
8790
for (const std::string& dir_path : source_file_directories) {
@@ -94,7 +97,11 @@ bool HandleSourceFileDirectoryScopes(
9497
status.SetError(cmStrCat("given non-existent DIRECTORY ", dir_path));
9598
return false;
9699
}
97-
directory_makefiles.push_back(dir_mf);
100+
if (directory_makefiles_set.find(dir_mf) ==
101+
directory_makefiles_set.end()) {
102+
directory_makefiles.push_back(dir_mf);
103+
directory_makefiles_set.insert(dir_mf);
104+
}
98105
}
99106
}
100107

@@ -110,7 +117,12 @@ bool HandleSourceFileDirectoryScopes(
110117
cmMakefile* target_dir_mf =
111118
status.GetMakefile().GetGlobalGenerator()->FindMakefile(
112119
*target_source_dir);
113-
directory_makefiles.push_back(target_dir_mf);
120+
121+
if (directory_makefiles_set.find(target_dir_mf) ==
122+
directory_makefiles_set.end()) {
123+
directory_makefiles.push_back(target_dir_mf);
124+
directory_makefiles_set.insert(target_dir_mf);
125+
}
114126
}
115127
}
116128

Tests/Properties/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ function(check_get_property_value expected)
261261
endif()
262262
endfunction()
263263

264+
# Check that source file directory scopes are deduplicated.
265+
set_property(SOURCE "${CMAKE_CURRENT_BINARY_DIR}/src32.cpp"
266+
DIRECTORY SubDir2 SubDir2 SubDir2
267+
TARGET_DIRECTORY set_prop_lib_3 set_prop_lib_3 set_prop_lib_3
268+
APPEND
269+
PROPERTY NON_DUPLICATED_CUSTOM_PROP 1
270+
)
271+
272+
get_property(actual
273+
SOURCE "${CMAKE_CURRENT_BINARY_DIR}/src32.cpp"
274+
DIRECTORY SubDir2
275+
PROPERTY NON_DUPLICATED_CUSTOM_PROP)
276+
check_get_property_value("1")
277+
278+
get_source_file_property(actual "${CMAKE_CURRENT_BINARY_DIR}/src32.cpp"
279+
TARGET_DIRECTORY set_prop_lib_3
280+
NON_DUPLICATED_CUSTOM_PROP)
281+
check_get_property_value("1")
282+
264283
# Get property + target directory
265284
get_property(actual
266285
SOURCE "${src_prefix}/src1.cpp"

0 commit comments

Comments
 (0)