From cc4046a8c73be913b758df961c40ff8c4c805f50 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Mon, 15 Dec 2014 13:08:25 -0500
Subject: [PATCH] Directory: Work around PGI problem with Linux Large File
 Support

The "/usr/include/dirent.h" header on Linux uses a glibc __REDIRECT
to map readdir to readdir64, but that is defined only when using
a GNU-like compiler.  Otherwise it defines readdir to readdir64
via the C preprocessor, but forgets to define dirent to dirent64.

The fix in commit 2f3c4192 (add support for the Portland Compiler to
CMake, 2007-09-17) does not seem to work on all machines.  Instead try
to map dirent to dirent64 whenever the preprocessor defines readdir to
readdir64 with PGI and glibc.

Change-Id: I6d4fc4cb48e5481f6ac8ed3928c0eb6c6ef0564d
---
 Directory.cxx | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/Directory.cxx b/Directory.cxx
index 741bcba..04b2866 100644
--- a/Directory.cxx
+++ b/Directory.cxx
@@ -203,13 +203,18 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& na
 #include <sys/types.h>
 #include <dirent.h>
 
-/* There is a problem with the Portland compiler, large file
-support and glibc/Linux system headers: 
-http://www.pgroup.com/userforum/viewtopic.php?
-p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
-*/
-#if defined(__PGI) && defined(__USE_FILE_OFFSET64)
-# define dirent dirent64
+// PGI with glibc has trouble with dirent and large file support:
+//  http://www.pgroup.com/userforum/viewtopic.php?
+//  p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
+// Work around the problem by mapping dirent the same way as readdir.
+#if defined(__PGI) && defined(__GLIBC__)
+# define kwsys_dirent_readdir dirent
+# define kwsys_dirent_readdir64 dirent64
+# define kwsys_dirent kwsys_dirent_lookup(readdir)
+# define kwsys_dirent_lookup(x) kwsys_dirent_lookup_delay(x)
+# define kwsys_dirent_lookup_delay(x) kwsys_dirent_##x
+#else
+# define kwsys_dirent dirent
 #endif
 
 namespace KWSYS_NAMESPACE
@@ -226,7 +231,7 @@ bool Directory::Load(const kwsys_stl::string& name)
     return 0;
     }
 
-  for (dirent* d = readdir(dir); d; d = readdir(dir) )
+  for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir) )
     {
     this->Internal->Files.push_back(d->d_name);
     }
@@ -240,7 +245,7 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& na
   DIR* dir = opendir(name.c_str());
 
   unsigned long count = 0;
-  for (dirent* d = readdir(dir); d; d = readdir(dir) )
+  for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir) )
     {
     count++;
     }
-- 
GitLab