diff options
authorJunghyun Kim <>2016-08-05 07:28:18 +0900
committerJunghyun Kim <>2016-08-05 10:17:35 +0900
commit26bb5821ce7f8b493c78221ab43532a9354768a0 (patch)
parent8f57854a0c003ff2d4c278b47d13f8ef6feb6bbc (diff)
List of files are sorted when using aux_source_directory() and file(glob).submit/tizen_base/20160805.051525accepted/tizen/base/20160805.171158
If aux_source_directory(. SRCS) or file(glob SRCS "*.c") is used, files in SRCS are not sorted. This can cause different orders of files across machines. Even though it does not have any correctness issue, there is a problem. If we have a build infrastructure (e.g., OBS), the result binaries can be different on each machine. In this case, build results can be different on two different machines even if we have the same source files. For example, 1. ld -o liba.a a.o b.o SRCS=a.c b.c 2. ld -o libb.a b.o a.o SRCS=b.c a.c Then, liba.a and libb.a are different. REASON This is because of the system call readdir(). The system call readdir() returns a file in the directory in any order. SOLUTION Intead of readdir(), scandir() is used with alphasort(). This makes the list of files are sorted alphabetically. AMENDED 1. another free() is added to free malloc()ed objects (Reviewed by Chan Lee). 2. remove "#include <errno.h>" (Reviewed by Chan Lee). Change-Id: Id8aff23b2761ea04c1c49c91fcc511b9bc2b1228 Signed-off-by: Junghyun Kim <>
1 files changed, 12 insertions, 6 deletions
diff --git a/Source/kwsys/Directory.cxx b/Source/kwsys/Directory.cxx
index b88474781..c16b9139b 100644
--- a/Source/kwsys/Directory.cxx
+++ b/Source/kwsys/Directory.cxx
@@ -190,6 +190,8 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const char* name)
#include <sys/types.h>
#include <dirent.h>
+#include <stdio.h>
+#include <malloc.h>
/* There is a problem with the Portland compiler, large file
support and glibc/Linux system headers:
@@ -211,19 +213,23 @@ bool Directory::Load(const char* name)
return 0;
- DIR* dir = opendir(name);
- if (!dir)
+ struct dirent** namelist;
+ int num_entries = scandir(name, &namelist, NULL, alphasort);
+ if(num_entries == -1)
+ printf("name=%s\n", name);
+ perror("scandir");
return 0;
- for (dirent* d = readdir(dir); d; d = readdir(dir) )
+ for( int i = 0; i < num_entries; ++i )
- this->Internal->Files.push_back(d->d_name);
+ this->Internal->Files.push_back(namelist[i]->d_name);
+ free(namelist[i]);
+ free(namelist);
this->Internal->Path = name;
- closedir(dir);
return 1;