Commit ba1a5553e43c7d5853dda21b1fce2a32e0ce4fd2

Carlos Martín Nieto 2015-09-30T17:44:10

Merge pull request #3446 from ethomson/portability portability: use `CHECK_FUNCTION_EXISTS` for checking whether functions exist...

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 713640d..8f0d507 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@ CMAKE_POLICY(SET CMP0015 NEW)
 SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
 
 INCLUDE(CheckLibraryExists)
+INCLUDE(CheckFunctionExists)
 INCLUDE(AddCFlagIfSupported)
 INCLUDE(FindPkgConfig)
 
@@ -469,6 +470,21 @@ ELSE ()
 	ENDIF ()
 ENDIF()
 
+CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
+IF (HAVE_FUTIMENS)
+	ADD_DEFINITIONS(-DHAVE_FUTIMENS)
+ENDIF ()
+
+CHECK_FUNCTION_EXISTS(qsort_r HAVE_QSORT_R)
+IF (HAVE_QSORT_R)
+	ADD_DEFINITIONS(-DHAVE_QSORT_R)
+ENDIF ()
+
+CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S)
+IF (HAVE_QSORT_S)
+	ADD_DEFINITIONS(-DHAVE_QSORT_S)
+ENDIF ()
+
 IF( NOT CMAKE_CONFIGURATION_TYPES )
 	# Build Debug by default
 	IF (NOT CMAKE_BUILD_TYPE)
diff --git a/src/unix/posix.h b/src/unix/posix.h
index 7773509..6633689 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -22,7 +22,6 @@ typedef int GIT_SOCKET;
 #define p_stat(p,b) stat(p, b)
 
 #define p_utimes(f, t) utimes(f, t)
-#define p_futimes(f, t) futimes(f, t)
 
 #define p_readlink(a, b, c) readlink(a, b, c)
 #define p_symlink(o,n) symlink(o, n)
@@ -53,4 +52,18 @@ extern char *p_realpath(const char *, char *);
 #define p_localtime_r(c, r) localtime_r(c, r)
 #define p_gmtime_r(c, r) gmtime_r(c, r)
 
+#ifdef HAVE_FUTIMENS
+GIT_INLINE(int) p_futimes(int f, const struct timeval t[2])
+{
+	struct timespec s[2];
+	s[0].tv_sec = t[0].tv_sec;
+	s[0].tv_nsec = t[0].tv_usec * 1000;
+	s[1].tv_sec = t[1].tv_sec;
+	s[1].tv_nsec = t[1].tv_usec * 1000;
+	return futimens(f, s);
+}
+#else
+# define p_futimes futimes
+#endif
+
 #endif
diff --git a/src/util.c b/src/util.c
index b3929bc..9e67f43 100644
--- a/src/util.c
+++ b/src/util.c
@@ -611,7 +611,7 @@ size_t git__unescape(char *str)
 	return (pos - str);
 }
 
-#if defined(GIT_WIN32) || defined(BSD)
+#if defined(HAVE_QSORT_S) || (defined(HAVE_QSORT_R) && defined(BSD))
 typedef struct {
 	git__sort_r_cmp cmp;
 	void *payload;
@@ -628,21 +628,16 @@ static int GIT_STDLIB_CALL git__qsort_r_glue_cmp(
 void git__qsort_r(
 	void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload)
 {
-#if defined(__MINGW32__) || defined(AMIGA) || \
-	defined(__OpenBSD__) || defined(__NetBSD__) || \
-	defined(__gnu_hurd__) || defined(__ANDROID_API__) || \
-	defined(__sun) || defined(__CYGWIN__) || \
-	(__GLIBC__ == 2 && __GLIBC_MINOR__ < 8) || \
-	(defined(_MSC_VER) && _MSC_VER < 1500)
-	git__insertsort_r(els, nel, elsize, NULL, cmp, payload);
-#elif defined(GIT_WIN32)
-	git__qsort_r_glue glue = { cmp, payload };
-	qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
-#elif defined(BSD)
+#if defined(HAVE_QSORT_R) && defined(BSD)
 	git__qsort_r_glue glue = { cmp, payload };
 	qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
-#else
+#elif defined(HAVE_QSORT_R) && defined(__GLIBC__)
 	qsort_r(els, nel, elsize, cmp, payload);
+#elif defined(HAVE_QSORT_S)
+	git__qsort_r_glue glue = { cmp, payload };
+	qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
+#else
+	git__insertsort_r(els, nel, elsize, NULL, cmp, payload);
 #endif
 }