Skip to content

Commit 0dd1c75

Browse files
committed
[5.6][swift-stdlib-tool] Fix finding sibling directories
When used without passing `--source-libraries`, this tool fetches its current path and uses that to find the directories of the swift libraries near it. Since #39216 it was searching the nearby directories recursively, and only for files, instead of just looking at 1 level, only for directories. Because of this it would never discover `path/to/lib/swift-5.0/...` because it was only looking for files. This fixes this issue with a new function, and renames the previous function to be more explicit for the other use case that does want that behavior. (cherry picked from commit d1eabf1)
1 parent 95b2f80 commit 0dd1c75

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tools/swift-stdlib-tool/swift-stdlib-tool.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,21 @@ std::vector<uint8_t> query_code_signature(std::string file) {
953953
}
954954

955955
template <typename F>
956-
void enumerateDirectory(std::string directory, F &&func) {
956+
void listDirectoryContents(std::string directory, F &&func) {
957+
DIR *dir = opendir(directory.c_str());
958+
if (dir == NULL) {
959+
return;
960+
}
961+
962+
struct dirent *entry;
963+
while ((entry = readdir(dir))) {
964+
func(directory + "/" + entry->d_name);
965+
}
966+
closedir(dir);
967+
}
968+
969+
template <typename F>
970+
void recursivelyListFiles(std::string directory, F &&func) {
957971
DIR *dir = opendir(directory.c_str());
958972
if (dir == NULL) {
959973
return;
@@ -975,7 +989,7 @@ void enumerateDirectory(std::string directory, F &&func) {
975989
}
976990
closedir(dir);
977991
for (const auto &path : subpaths) {
978-
enumerateDirectory(path, func);
992+
recursivelyListFiles(path, func);
979993
}
980994
}
981995

@@ -1085,7 +1099,7 @@ int main(int argc, const char *argv[]) {
10851099
std::string root_path =
10861100
parentPath(parentPath(self_executable)) + "/" + "lib";
10871101

1088-
enumerateDirectory(root_path, [&](std::string entry) {
1102+
listDirectoryContents(root_path, [&](std::string entry) {
10891103
if (filename(entry).compare(0, strlen("swift-"), "swift-") == 0) {
10901104
src_dirs.push_back(entry + "/" + platform);
10911105
}
@@ -1119,7 +1133,7 @@ int main(int argc, const char *argv[]) {
11191133

11201134
// Collect executables from the --scan-folder locations.
11211135
for (const auto &embedDir : embedDirs) {
1122-
enumerateDirectory(embedDir, [&](std::string entry) {
1136+
recursivelyListFiles(embedDir, [&](std::string entry) {
11231137
if (0 == access(entry.c_str(), X_OK)) {
11241138
executables.push_back(entry);
11251139
} else {

0 commit comments

Comments
 (0)