Skip to content

Commit 1eefe22

Browse files
Merge branch 'feature/msvc-win32-support' into develop
2 parents efe4b45 + 8c9adaf commit 1eefe22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+725
-358
lines changed

.clang-format

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,41 @@ SpaceBeforeAssignmentOperators: true
6262
ContinuationIndentWidth: 4
6363
CommentPragmas: "^ IWYU pragma:"
6464
SpaceBeforeParens: ControlStatements
65+
SortIncludes: CaseSensitive
6566
DisableFormat: false
6667
IncludeBlocks: Regroup
6768
IncludeCategories:
6869
- Regex: '"\.' # Relative headers
6970
Priority: 10
7071
- Regex: '<amongoc/|"amongoc/' # amongoc headers first
7172
Priority: 20
72-
- Regex: '<bson/|"bson/' # bson headers next
73+
- Regex: "<bson/" # bson headers next
7374
Priority: 25
74-
- Regex: '<mlib/|"mlib/' # mlib headers next
75+
- Regex: "<mlib/" # mlib headers next
7576
Priority: 30
7677
- Regex: '<.*/.*\.h>|<.*/.*\.hpp>' # Other non-standard headres
7778
Priority: 40
7879
- Regex: '<.*\.h>|<.*\.hpp>' # Other non-standard headres
7980
Priority: 50
8081
- Regex: '<[[:alnum:]_.]+>|<std.*\.h>' # Standard headres
8182
Priority: 60
83+
# Unix platform headers
84+
- Regex: '<(unistd|netdb|pthread|netinet/.*|sys/.*|netdb|fcntl|arpa/.*|resolv|sched|poll)\.h>'
85+
Priority: 90
86+
# Darwin platform headers
87+
- Regex: '<(Availability|AvailabilityMacros|TargetConditionals|CommonCrypto/.*|Security/.*|CoreFoundation/.*)\.h>'
88+
CaseSensitive: false
89+
Priority: 90
90+
# Windows platform headers
91+
- Regex: '<(win(dows|sock2|error|trnl|error|crypt|ver|dns)|sspi|ws2tcpip|ntstatus|mstcpip|process)\.h>'
92+
CaseSensitive: false
93+
Priority: 90
94+
# Standard library headers are near last
95+
- Regex: "<(assert|string|ctype|limits|time|math|errno|inttypes|signal).h>|<std|<[^.]+>"
96+
Priority: 100
97+
# Other -I headers get sorted before platform headers
98+
- Regex: "<"
99+
Priority: 80
82100
InsertNewlineAtEOF: true
83101
IfMacros:
84102
- mlib_math_catch

CMakeLists.txt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ project(amongoc VERSION 0.1.0 DESCRIPTION "An Asynchronous MongoDB Library for C
33

44
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
55

6+
# Declare that we are compiling with C++23 before we proceed. This must be near
7+
# the top before we import other libraries that might depend on our toolchain
8+
# settings (e.g. Catch2)
9+
set(CMAKE_CXX_STANDARD 23)
10+
611
# Pull our deps with PMM+vcpkg
712
option(AMONGOC_USE_PMM "Enable PMM execution to bootstrap dependency packages automatically" ${PROJECT_IS_TOP_LEVEL})
813
if(AMONGOC_USE_PMM)
@@ -24,6 +29,12 @@ find_package(Threads REQUIRED)
2429
# Note: We respect the BUILD_TESTING option from CTest.cmake
2530
include(CTest)
2631

32+
# Tweak some options if we are added as a subdirectory project
33+
if(NOT PROJECT_IS_TOP_LEVEL)
34+
# We aren't the top-level project. Don't define test cases
35+
set(BUILD_TESTING FALSE)
36+
endif()
37+
2738
if(BUILD_TESTING)
2839
find_package(Catch2 CONFIG REQUIRED)
2940
endif()
@@ -56,14 +67,12 @@ if(PROJECT_IS_TOP_LEVEL)
5667
)
5768
endif()
5869

59-
# Tweak some options if we are added as a subdirectory project
60-
if(NOT PROJECT_IS_TOP_LEVEL)
61-
# We aren't the top-level project. Don't define test cases
62-
set(BUILD_TESTING FALSE)
63-
endif()
64-
65-
# When compiling C++ with GCC, set a deeper diagnostics depth
66-
add_compile_options($<$<AND:$<CXX_COMPILER_ID:GNU>,$<COMPILE_LANGUAGE:CXX>>:-fconcepts-diagnostics-depth=4>)
70+
add_compile_options(
71+
# When compiling C++ with GCC, set a deeper diagnostics depth
72+
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<COMPILE_LANGUAGE:CXX>>:-fconcepts-diagnostics-depth=4>
73+
# Enable large object files with MSVC
74+
$<$<CXX_COMPILER_ID:MSVC>:/bigobj>
75+
)
6776

6877
# If we generate any DLLs, use a .dll.lib suffix for the importlibs, to distinguish
6978
# them from actual static libraries
@@ -125,7 +134,19 @@ target_compile_definitions(amongoc PRIVATE NEO_ENABLE_CHECKS=0)
125134
# Enable C++23 features on the target
126135
# We currently only use C++23 for `auto(). Future revisions
127136
# can remove these requirements for greater portability.
128-
target_compile_features(amongoc PRIVATE cxx_std_23)
137+
target_compile_features(amongoc PRIVATE cxx_std_23 PUBLIC cxx_std_20 c_std_11)
138+
139+
# Tweaks for MSVC/Windows
140+
target_compile_options(amongoc
141+
PUBLIC
142+
# Enable the conforming preprocessor
143+
$<$<C_COMPILER_ID:MSVC>:/Zc:preprocessor>
144+
PRIVATE
145+
# Add caret diagnostics, they're prettier
146+
$<$<CXX_COMPILER_ID:MSVC>:/diagnostics:caret>
147+
)
148+
# Set the Windows SDK target to Windows 7+
149+
add_compile_definitions($<$<PLATFORM_ID:Windows>:_WIN32_WINNT=0x601>)
129150

130151
# Link deps and platform libs.
131152
target_link_libraries(amongoc
@@ -155,7 +176,9 @@ if(BUILD_TESTING)
155176
)
156177
target_include_directories(amongoc-test PRIVATE src)
157178
catch_discover_tests(amongoc-test DISCOVERY_MODE PRE_TEST
158-
PROPERTIES SKIP_REGULAR_EXPRESSION ":[0-9]+: SKIPPED:"
179+
PROPERTIES
180+
SKIP_REGULAR_EXPRESSION ":[0-9]+: SKIPPED:"
181+
TIMEOUT 5
159182
)
160183
target_compile_features(amongoc-test PRIVATE cxx_std_23)
161184

docs/how-to/communicate.example.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <bson/mut.h>
66
#include <bson/view.h>
77

8+
#include <time.h>
9+
810
/**
911
* @brief Shared state for the application. This is passed through the app as pointer stored
1012
* in a box
@@ -21,8 +23,9 @@ typedef struct app_state {
2123
* @param resp_data A `bson_mut` object that contains the response message
2224
* @return amongoc_box Returns `amongoc_nil`
2325
*/
24-
amongoc_box after_hello(amongoc_box state_ptr, amongoc_status*, amongoc_box resp_data) {
25-
(void)state_ptr;
26+
amongoc_box after_hello(amongoc_box _state_ptr, amongoc_status* _status, amongoc_box resp_data) {
27+
(void)_status;
28+
(void)_state_ptr;
2629
bson_view resp = bson_view_from(amongoc_box_cast(bson_doc, resp_data));
2730
// Just print the response message
2831
fprintf(stdout, "Got response: ");
@@ -40,7 +43,9 @@ amongoc_box after_hello(amongoc_box state_ptr, amongoc_status*, amongoc_box resp
4043
* @param cl_box An `amongoc_client*`
4144
* @return amongoc_emitter
4245
*/
43-
amongoc_emitter after_connect_say_hello(amongoc_box state_ptr, amongoc_status, amongoc_box cl_box) {
46+
amongoc_emitter
47+
after_connect_say_hello(amongoc_box state_ptr, amongoc_status _status, amongoc_box cl_box) {
48+
(void)_status;
4449
printf("Connected to server\n");
4550
// Store the connection in our app state
4651
amongoc_box_take(amongoc_box_cast(app_state*, state_ptr)->client, cl_box);

docs/how-to/looping.example.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdio.h>
44
#include <stdlib.h>
5+
#include <time.h>
56

67
/**
78
* @brief State for the program
@@ -32,7 +33,7 @@ amongoc_emitter loop_step(amongoc_box state_ptr, //
3233
uint64_t sum = s->a + s->b;
3334
s->a = s->b;
3435
s->b = sum;
35-
fprintf(stderr, "%d seconds remain, current value: %lu\n", s->countdown, cur);
36+
fprintf(stderr, "%d seconds remain, current value: %" PRIu64 "\n", s->countdown, cur);
3637
// Check if we are done
3738
if (s->countdown == 0) {
3839
// No more looping to do. Return a final result
@@ -88,6 +89,6 @@ int main(int argc, char const* const* argv) {
8889
return 2;
8990
}
9091
// Get the value returned with `amongoc_just` in `loop_step`
91-
printf("Got final value: %lu\n", amongoc_box_cast(uint64_t, result));
92+
printf("Got final value: %" PRIu64 "\n", amongoc_box_cast(uint64_t, result));
9293
return 0;
9394
}

include/amongoc/box.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct _amongoc_dynamic_box {
8282
mlib_allocator alloc;
8383
amongoc_box_destructor destroy;
8484
size_t alloc_size;
85-
mlib_alignas(max_align_t) char object[1];
85+
mlib_alignas(intmax_t) char object[1];
8686
};
8787

8888
union _amongoc_box_union {
@@ -92,7 +92,7 @@ union _amongoc_box_union {
9292
};
9393

9494
struct _amongoc_box_storage {
95-
alignas(max_align_t) union _amongoc_box_union u;
95+
alignas(intmax_t) union _amongoc_box_union u;
9696
// Non-zero if the box storage is dynamically allocated
9797
unsigned char is_dynamic : 1;
9898
// Non-zero if the box has an associated destructor function

include/amongoc/collection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ mlib_extern_c_begin();
3232
*/
3333
#define amongoc_collection_new(Client, DbName, CollName) \
3434
amongoc_collection_new(Client, mlib_str_view_from(DbName), mlib_str_view_from(CollName))
35-
amongoc_collection*(amongoc_collection_new)(amongoc_client* cl,
36-
mlib_str_view db_name,
37-
mlib_str_view coll_name)mlib_noexcept;
35+
amongoc_collection*(amongoc_collection_new)(amongoc_client * cl,
36+
mlib_str_view db_name,
37+
mlib_str_view coll_name) mlib_noexcept;
3838

3939
/**
4040
* @brief Delete a collection handle. Is a no-op for null handles.

include/amongoc/handler.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class handler_stop_token {
5252
}
5353

5454
// The function object to be called
55-
[[no_unique_address]] F _fn;
55+
mlib_no_unique_address F _fn;
5656
// The stop registration cookie
5757
unique_box _reg_cookie;
5858
};
@@ -141,8 +141,8 @@ struct unique_handler : mlib::unique<::amongoc_handler> {
141141
// Implement the wrapper for invocable objects, used by from()
142142
template <typename R>
143143
struct wrapper {
144-
mlib::allocator<> _alloc;
145-
[[no_unique_address]] R _fn;
144+
mlib::allocator<> _alloc;
145+
mlib_no_unique_address R _fn;
146146
AMONGOC_TRIVIALLY_RELOCATABLE_THIS(amongoc::enable_trivially_relocatable_v<R>, wrapper);
147147

148148
static void _complete(amongoc_handler* self, status st, box result) noexcept {
@@ -169,7 +169,7 @@ struct unique_handler : mlib::unique<::amongoc_handler> {
169169
explicit wrapper(mlib::allocator<>, R&& r)
170170
: _fn(mlib_fwd(r)) {}
171171

172-
[[no_unique_address]] R _fn;
172+
mlib_no_unique_address R _fn;
173173
AMONGOC_TRIVIALLY_RELOCATABLE_THIS(amongoc::enable_trivially_relocatable_v<R>, wrapper);
174174

175175
static void _complete(amongoc_handler* self, status st, box result) noexcept {

include/bson/detail/assert.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ enum {
3535
_bson_assert_fail(#Cond, __FILE__, __LINE__); \
3636
abort(); \
3737
} else if (!(Cond)) { \
38-
__builtin_unreachable(); \
38+
MLIB_IF_MSVC(__assume(0)); \
39+
MLIB_IF_GNU_LIKE(__builtin_unreachable()); \
3940
} else \
4041
((void)0)
4142

include/bson/make.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ explicit range(R&&) -> range<R>;
266266
*/
267267
template <typename... Els>
268268
struct doc : doc<std::index_sequence_for<Els...>, Els...> {
269-
using doc<std::index_sequence_for<Els...>, Els...>::doc;
269+
template <typename... Es>
270+
explicit doc(Es&&... es)
271+
: doc<std::index_sequence_for<Els...>, Els...>(mlib_fwd(es)...) {}
270272
};
271273

272274
template <std::size_t N, typename Elem>

include/bson/mut.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ inline uint32_t bson_mut_capacity(bson_mut d) mlib_noexcept {
6464
* @param pos An element iterator
6565
*/
6666
inline bson_byte* _bson_mut_data_at(bson_mut doc, bson_iterator pos) mlib_noexcept {
67-
const ssize_t off = bson_iterator_data(pos) - bson_data(doc);
67+
const ptrdiff_t off = bson_iterator_data(pos) - bson_data(doc);
6868
return bson_mut_data(doc) + off;
6969
}
7070

@@ -97,7 +97,7 @@ inline bson_byte* _bson_splice_region(bson_mut* const mut,
9797
const bson_byte* const insert_from) mlib_noexcept {
9898
// The offset of the position. We use this to later recover a pointer upon
9999
// reallocation
100-
const ssize_t pos_offset = position - bson_data(*mut);
100+
const ptrdiff_t pos_offset = position - bson_data(*mut);
101101
// Check that we aren't splicing within our document header:
102102
BV_ASSERT(pos_offset >= 4);
103103
// Check that we aren't splicing at/beyond the doc's null terminator:

0 commit comments

Comments
 (0)