Skip to content

Commit 1a9cc18

Browse files
committed
util: make the qsort_r check work on macOS
This performs a compile-check by using CMake support, to differentiate the GNU version from the BSD version of qsort_r. Module taken from 4f252abea5f1d17c60f6ff115c9c44cc0b6f1df6, which I've checked against CMake 2.8.11.
1 parent 8856337 commit 1a9cc18

File tree

5 files changed

+142
-7
lines changed

5 files changed

+142
-7
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ INCLUDE(CheckLibraryExists)
2828
INCLUDE(CheckFunctionExists)
2929
INCLUDE(CheckSymbolExists)
3030
INCLUDE(CheckStructHasMember)
31+
INCLUDE(CheckPrototypeDefinition) # Added in CMake 3.0
3132
INCLUDE(AddCFlagIfSupported)
3233
INCLUDE(FindPkgLibraries)
3334
INCLUDE(FindThreads)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@CHECK_PROTOTYPE_DEFINITION_HEADER@
2+
3+
static void cmakeRequireSymbol(int dummy, ...) {
4+
(void) dummy;
5+
}
6+
7+
static void checkSymbol(void) {
8+
#ifndef @CHECK_PROTOTYPE_DEFINITION_SYMBOL@
9+
cmakeRequireSymbol(0, &@CHECK_PROTOTYPE_DEFINITION_SYMBOL@);
10+
#endif
11+
}
12+
13+
@CHECK_PROTOTYPE_DEFINITION_PROTO@ {
14+
return @CHECK_PROTOTYPE_DEFINITION_RETURN@;
15+
}
16+
17+
#ifdef __CLASSIC_C__
18+
int main() {
19+
int ac;
20+
char*av[];
21+
#else
22+
int main(int ac, char *av[]) {
23+
#endif
24+
checkSymbol();
25+
if (ac > 1000) {
26+
return *av[0];
27+
}
28+
return 0;
29+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# - Check if the protoype we expect is correct.
2+
# check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
3+
# FUNCTION - The name of the function (used to check if prototype exists)
4+
# PROTOTYPE- The prototype to check.
5+
# RETURN - The return value of the function.
6+
# HEADER - The header files required.
7+
# VARIABLE - The variable to store the result.
8+
# Example:
9+
# check_prototype_definition(getpwent_r
10+
# "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
11+
# "NULL"
12+
# "unistd.h;pwd.h"
13+
# SOLARIS_GETPWENT_R)
14+
# The following variables may be set before calling this macro to
15+
# modify the way the check is run:
16+
#
17+
# CMAKE_REQUIRED_FLAGS = string of compile command line flags
18+
# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
19+
# CMAKE_REQUIRED_INCLUDES = list of include directories
20+
# CMAKE_REQUIRED_LIBRARIES = list of libraries to link
21+
22+
#=============================================================================
23+
# Copyright 2005-2009 Kitware, Inc.
24+
# Copyright 2010-2011 Andreas Schneider <asn@cryptomilk.org>
25+
#
26+
# Distributed under the OSI-approved BSD License (the "License");
27+
# see accompanying file Copyright.txt for details.
28+
#
29+
# This software is distributed WITHOUT ANY WARRANTY; without even the
30+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31+
# See the License for more information.
32+
#=============================================================================
33+
# (To distribute this file outside of CMake, substitute the full
34+
# License text for the above reference.)
35+
#
36+
37+
get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
38+
39+
function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
40+
41+
if ("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
42+
set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
43+
44+
set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
45+
if (CMAKE_REQUIRED_LIBRARIES)
46+
set(CHECK_PROTOTYPE_DEFINITION_LIBS
47+
"-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
48+
else(CMAKE_REQUIRED_LIBRARIES)
49+
set(CHECK_PROTOTYPE_DEFINITION_LIBS)
50+
endif(CMAKE_REQUIRED_LIBRARIES)
51+
if (CMAKE_REQUIRED_INCLUDES)
52+
set(CMAKE_SYMBOL_EXISTS_INCLUDES
53+
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
54+
else(CMAKE_REQUIRED_INCLUDES)
55+
set(CMAKE_SYMBOL_EXISTS_INCLUDES)
56+
endif(CMAKE_REQUIRED_INCLUDES)
57+
58+
foreach(_FILE ${_HEADER})
59+
set(CHECK_PROTOTYPE_DEFINITION_HEADER
60+
"${CHECK_PROTOTYPE_DEFINITION_HEADER}#include <${_FILE}>\n")
61+
endforeach(_FILE)
62+
63+
set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
64+
set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
65+
set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
66+
67+
configure_file("${__check_proto_def_dir}/CheckPrototypeDefinition.c.in"
68+
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
69+
70+
file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
71+
72+
try_compile(${_VARIABLE}
73+
${CMAKE_BINARY_DIR}
74+
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
75+
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
76+
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
77+
"${CHECK_PROTOTYPE_DEFINITION_LIBS}"
78+
"${CMAKE_SYMBOL_EXISTS_INCLUDES}"
79+
OUTPUT_VARIABLE OUTPUT)
80+
81+
if (${_VARIABLE})
82+
set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
83+
message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
84+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
85+
"Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
86+
"${OUTPUT}\n\n")
87+
else (${_VARIABLE})
88+
message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
89+
set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
90+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
91+
"Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
92+
"${OUTPUT}\n\n${_SOURCE}\n\n")
93+
endif (${_VARIABLE})
94+
endif("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
95+
96+
endfunction(CHECK_PROTOTYPE_DEFINITION)

src/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ IF (HAVE_FUTIMENS)
5858
SET(GIT_USE_FUTIMENS 1)
5959
ENDIF ()
6060

61-
CHECK_FUNCTION_EXISTS(qsort_r HAVE_QSORT_R)
62-
IF (HAVE_QSORT_R)
63-
ADD_DEFINITIONS(-DHAVE_QSORT_R)
64-
ENDIF ()
61+
CHECK_PROTOTYPE_DEFINITION(qsort_r
62+
"void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))"
63+
"" "stdlib.h" HAVE_QSORT_R_BSD)
64+
IF (HAVE_QSORT_R_BSD)
65+
ADD_DEFINITIONS(-DHAVE_QSORT_R_BSD)
66+
ENDIF()
67+
68+
CHECK_PROTOTYPE_DEFINITION(qsort_r
69+
"void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)"
70+
"" "stdlib.h" HAVE_QSORT_R_GNU)
71+
IF (HAVE_QSORT_R_GNU)
72+
ADD_DEFINITIONS(-DHAVE_QSORT_R_GNU)
73+
ENDIF()
6574

6675
CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S)
6776
IF (HAVE_QSORT_S)

src/util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ size_t git__unescape(char *str)
647647
return (pos - str);
648648
}
649649

650-
#if defined(HAVE_QSORT_S) || (defined(HAVE_QSORT_R) && defined(BSD))
650+
#if defined(HAVE_QSORT_S) || defined(HAVE_QSORT_R_BSD)
651651
typedef struct {
652652
git__sort_r_cmp cmp;
653653
void *payload;
@@ -664,10 +664,10 @@ static int GIT_STDLIB_CALL git__qsort_r_glue_cmp(
664664
void git__qsort_r(
665665
void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload)
666666
{
667-
#if defined(HAVE_QSORT_R) && defined(BSD)
667+
#if defined(HAVE_QSORT_R_BSD)
668668
git__qsort_r_glue glue = { cmp, payload };
669669
qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
670-
#elif defined(HAVE_QSORT_R) && defined(__GLIBC__)
670+
#elif defined(HAVE_QSORT_R_GNU)
671671
qsort_r(els, nel, elsize, cmp, payload);
672672
#elif defined(HAVE_QSORT_S)
673673
git__qsort_r_glue glue = { cmp, payload };

0 commit comments

Comments
 (0)