Skip to content

Commit 4a0e816

Browse files
Fix build for FreeBSD (#1243)
CDRIVER-4465 - Some standard library features need __BSD_VISIBLE set to 1 to be enabled on FreeBSD - Cmake does not properly detect res_search on FreeBSD. Since res_search has existed since 4.3 BSD (which is the predecessor of FreeBSD), it is safe to assume that this function will exist in libc on any version of FreeBSD. Co-authored-by: Kevin Albertson <[email protected]>
1 parent 4249c97 commit 4a0e816

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ endif ()
271271
add_definitions (-D_XOPEN_SOURCE=700 -D_BSD_SOURCE -D_DEFAULT_SOURCE)
272272
list (APPEND CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -D_DEFAULT_SOURCE)
273273

274+
# Enable non-standard features on FreeBSD with __BSD_VISIBLE=1
275+
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
276+
add_definitions (-D__BSD_VISIBLE=1)
277+
list (APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)
278+
endif ()
279+
274280
# https://opensource.apple.com/source/Libc/Libc-1439.40.11/gen/compat.5.auto.html
275281
# Non-POSIX extensions are required by `_SC_NPROCESSORS_ONLN`.
276282
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")

build/cmake/FindResSearch.cmake

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ if (ENABLE_SRV STREQUAL ON OR ENABLE_SRV STREQUAL AUTO)
2828
set (MONGOC_HAVE_RES_NCLOSE 0)
2929
endif ()
3030
endif ()
31+
elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
32+
# On FreeBSD, the following line does not properly detect res_search,
33+
# which is included in libc on FreeBSD:
34+
# check_symbol_exists (res_search resolv.h MONGOC_HAVE_RES_SEARCH)
35+
#
36+
# Attempting to link with libresolv on FreeBSD will fail with this error:
37+
# ld: error: unable to find library -lresolv
38+
#
39+
# Since res_search has existed since 4.3 BSD (which is the predecessor
40+
# of FreeBSD), it is safe to assume that this function will exist in
41+
# libc on FreeBSD.
42+
set (MONGOC_HAVE_RES_SEARCH 1)
43+
set (MONGOC_HAVE_RES_NSEARCH 0)
44+
set (MONGOC_HAVE_RES_NDESTROY 0)
45+
set (MONGOC_HAVE_RES_NCLOSE 0)
3146
else ()
3247
set (MONGOC_HAVE_RES_NSEARCH 0)
3348
set (MONGOC_HAVE_RES_NDESTROY 0)
@@ -48,10 +63,10 @@ else ()
4863
set (MONGOC_HAVE_RES_NSEARCH 0)
4964
set (MONGOC_HAVE_RES_NDESTROY 0)
5065
set (MONGOC_HAVE_RES_NCLOSE 0)
51-
set (MONGOC_HAVE_RES_SEARCH 0)
66+
set (MONGOC_HAVE_RES_SEARCH 0)
5267
endif ()
5368

54-
if (ENABLE_SRV STREQUAL ON AND NOT RESOLV_LIBRARIES)
69+
if (ENABLE_SRV STREQUAL ON AND NOT RESOLV_LIBRARIES AND NOT CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
5570
message (
5671
FATAL_ERROR
5772
"Cannot find libresolv or dnsapi. Try setting ENABLE_SRV=OFF")

src/libbson/tests/test-json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,8 @@ test_bson_json_double (void)
21132113
BSON_ASSERT (BSON_ITER_HOLDS_DOUBLE (&iter));
21142114
ASSERT_CMPDOUBLE (bson_iter_double (&iter), ==, 0.0);
21152115

2116-
/* check that "x" is -0.0. signbit not available on Solaris or VS 2010 */
2117-
#if !defined(__sun) && (!defined(_MSC_VER) || (_MSC_VER >= 1800))
2116+
/* check that "x" is -0.0. signbit not available on Solaris, FreeBSD, or VS 2010 */
2117+
#if !defined(__sun) && !defined(__FreeBSD__) && (!defined(_MSC_VER) || (_MSC_VER >= 1800))
21182118
BSON_ASSERT (signbit (bson_iter_double (&iter)));
21192119
#endif
21202120

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
#include <bson/bson.h>
1918
#include "mongoc-config.h"
2019
#ifdef MONGOC_HAVE_DNSAPI
@@ -25,6 +24,7 @@
2524
#else
2625
#if defined(MONGOC_HAVE_RES_NSEARCH) || defined(MONGOC_HAVE_RES_SEARCH)
2726
#include <netdb.h>
27+
#include <netinet/in.h>
2828
#include <netinet/tcp.h>
2929
#include <arpa/nameser.h>
3030
#include <resolv.h>

src/libmongoc/src/mongoc/mongoc-counters-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sys/sysinfo.h>
2929
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
3030
defined(__OpenBSD__)
31+
#include <sched.h>
3132
#include <sys/types.h>
3233
#include <sys/sysctl.h>
3334
#include <sys/param.h>

0 commit comments

Comments
 (0)