[cmake-developers] [PATCH] cmFileCommand: sort list of files from glob command

Reiner Herrmann reiner at reiner-h.de
Sat May 14 06:30:36 EDT 2016


file(GLOB ...) is often used to find source files. As it uses
readdir(), the list of files will be unsorted.
This list is often passed directly to add_executable / add_library.
Linking binaries with an unsorted list will make it unreproducible,
which means that the produced binary will differ depending on the
unpredictable readdir() order.

To solve those reproducibility issues in a lot of programs (which
don't explicitely sort the list manually), this change sorts the
resulting list of the file GLOB command.

A more detailed rationale about reproducible builds is available at:
 https://reproducible-builds.org/
---
 Help/command/file.rst    | 3 +--
 Source/cmFileCommand.cxx | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/Help/command/file.rst b/Help/command/file.rst
index 96ac6c7..a502134 100644
--- a/Help/command/file.rst
+++ b/Help/command/file.rst
@@ -103,8 +103,7 @@ Generate a list of files that match the ``<globbing-expressions>`` and
 store it into the ``<variable>``.  Globbing expressions are similar to
 regular expressions, but much simpler.  If ``RELATIVE`` flag is
 specified, the results will be returned as relative paths to the given
-path.  No specific order of results is defined.  If order is important then
-sort the list explicitly (e.g. using the :command:`list(SORT)` command).
+path.  The file list will be sorted.
 
 By default ``GLOB`` lists directories - directories are omited in result if
 ``LIST_DIRECTORIES`` is set to false.
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx
index 6b6b913..4efa550 100644
--- a/Source/cmFileCommand.cxx
+++ b/Source/cmFileCommand.cxx
@@ -1026,6 +1026,7 @@ bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
 
     std::vector<std::string>::size_type cc;
     std::vector<std::string>& files = g.GetFiles();
+    std::sort(files.begin(), files.end());
     for ( cc = 0; cc < files.size(); cc ++ )
       {
       if ( !first )
-- 
2.8.1



More information about the cmake-developers mailing list