jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
284 stars 64 forks source link

Building on windows fails (aligned_alloc) #247

Closed kalibera closed 1 year ago

kalibera commented 1 year ago

Please note

bson/bson-memory.c: In function '_aligned_alloc_impl':
bson/bson-memory.c:38:11: warning: implicit declaration of function 'aligned_alloc' [-Wimplicit-function-declaration]
   38 |    return aligned_alloc (alignment, num_bytes);
      |           ^~~~~~~~~~~~~
bson/bson-memory.c:26:1: note: include '<stdlib.h>' or provide a declaration of 'aligned_alloc'
   25 | #include "bson-memory.h"
  +++ |+#include <stdlib.h>
   26 | 
bson/bson-memory.c:38:11: warning: incompatible implicit declaration of built-in function 'aligned_alloc' [-Wbuiltin-declaration-mismatch]
   38 |    return aligned_alloc (alignment, num_bytes);

in the CRAN check results. Sadly the suggestion is not helpful, aligned_alloc is not in stdlib.h and it is not supported in Windows, despite that it is in C11. See https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance:

The Universal CRT doesn't implement C11 aligned_alloc, but does provide _aligned_malloc and _aligned_free. Because the Windows operating system doesn't support aligned allocations, this function is unlikely to be implemented."

Probably this has to be treated as an exception from C11 upstream when compiling on Windows: https://github.com/mongodb/mongo-c-driver/blob/fa7d499b8ca53269b008cf7c19747164f98bede2/src/libbson/src/bson/bson-memory.c#L33

jeroen commented 1 year ago

Did this surface because R changed from C-99 to C-11, or due to a change in the compiler? Can we specify a C standard in the package Makevars, like we do for CXXSTD?

cc @kevinAlbs

kalibera commented 1 year ago

I think it is the C standard, because that is what the code in bson-memory.c is checking. It uses aligned_alloc when it is being compiled in C11. I think the code needs to be patched to make an exception for Windows and use other allocation functions. One probably shouldn't be permanently be choosing pre-C11 standards for that code just becuase of aligned_alloc. But, yes, with gnu99 it would most likely work.

It cannot be directly the effect of the compiler, this is about the headers (MinGW-W64) and that is in turn about what the C runtime on Windows supports. Only indirectly re what is the (default) standard.

There is work in progress on selecting also C standards, see the NEWS file.

jeroen commented 1 year ago

workaround submitted to CRAN for now. Hopefully we can come up with a fix in upstream libmongoc.

kalibera commented 1 year ago

Thanks!