summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Morrow <acm@mongodb.com>2018-08-21 13:07:19 -0400
committerAndrew Morrow <acm@mongodb.com>2018-08-23 14:58:19 -0400
commit3088dd59c514f2d0a1dc123b62752008536c7bbf (patch)
tree64c971d8158a4cc9e38d7b4b416e19536446ab86
parenta17bfb8c3f1c549b6b75930bff82ec821dfa7ea0 (diff)
downloadmongo-3088dd59c514f2d0a1dc123b62752008536c7bbf.tar.gz
SERVER-36766 Addd import/export macros for embedded libraries
(cherry picked from commit 2dcd4d432705970b659e9b784547f311dfc2546c)
-rw-r--r--src/mongo/embedded/SConscript38
-rw-r--r--src/mongo/embedded/capi.cpp58
-rw-r--r--src/mongo/embedded/capi.h101
-rw-r--r--src/mongo/embedded/mongoc_client.cpp10
-rw-r--r--src/mongo/embedded/mongoc_client.h47
5 files changed, 201 insertions, 53 deletions
diff --git a/src/mongo/embedded/SConscript b/src/mongo/embedded/SConscript
index 917f6bd3cfd..d93646bc61d 100644
--- a/src/mongo/embedded/SConscript
+++ b/src/mongo/embedded/SConscript
@@ -69,7 +69,24 @@ env.Library(
],
)
-env.Library(
+capiEnv = env.Clone()
+capiEnv.AppendUnique(
+ CPPDEFINES=[
+ 'MONGO_EMBEDDED_CAPI_COMPILING',
+ ],
+ SHCCFLAGS=[
+ '-fvisibility=hidden' if env.TargetOSIs('posix') else [],
+ ],
+)
+
+if get_option('link-model') == 'static':
+ capiEnv.AppendUnique(
+ CPPDEFINES=[
+ 'MONGO_EMBEDDED_CAPI_STATIC',
+ ],
+ )
+
+capiEnv.Library(
target='mongo_embedded_capi',
source=[
'capi.cpp',
@@ -147,7 +164,24 @@ if not hygienic:
if not env['MONGO_HAVE_LIBMONGOC']:
Return()
-env.Library(
+mongocClientEnv = env.Clone()
+mongocClientEnv.AppendUnique(
+ CPPDEFINES=[
+ 'MONGO_EMBEDDED_MONGOC_CLIENT_COMPILING',
+ ],
+ SHCCFLAGS=[
+ '-fvisibility=hidden' if env.TargetOSIs('posix') else [],
+ ],
+)
+
+if get_option('link-model') == 'static':
+ mongocClientEnv.AppendUnique(
+ CPPDEFINES=[
+ 'MONGO_EMBEDDED_MONGOC_CLIENT_STATIC',
+ ],
+ )
+
+mongocClientEnv.Library(
target='mongo_embedded_mongoc_client',
source=[
'mongoc_client.cpp',
diff --git a/src/mongo/embedded/capi.cpp b/src/mongo/embedded/capi.cpp
index 7ff402f835f..eb46b38c361 100644
--- a/src/mongo/embedded/capi.cpp
+++ b/src/mongo/embedded/capi.cpp
@@ -26,6 +26,8 @@
* then also delete it in the license file.
*/
+#include "mongo/platform/basic.h"
+
#include "mongo/embedded/capi.h"
#include <cstring>
@@ -49,6 +51,12 @@
#include "mongo/util/scopeguard.h"
#include "mongo/util/shared_buffer.h"
+#if defined(_WIN32)
+#define MONGO_API_CALL __cdecl
+#else
+#define MONGO_API_CALL
+#endif
+
struct mongo_embedded_v1_status {
mongo_embedded_v1_status() noexcept = default;
mongo_embedded_v1_status(const mongo_embedded_v1_error e, const int ec, std::string w)
@@ -563,78 +571,80 @@ auto enterCXX(mongo_embedded_v1_status* const statusPtr, Callable&& c) noexcept
} // namespace
extern "C" {
-mongo_embedded_v1_lib* mongo_embedded_v1_lib_init(const mongo_embedded_v1_init_params* const params,
- mongo_embedded_v1_status* const statusPtr) {
+mongo_embedded_v1_lib* MONGO_API_CALL mongo_embedded_v1_lib_init(
+ const mongo_embedded_v1_init_params* const params, mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::capi_lib_init(params, status);
});
}
-int mongo_embedded_v1_lib_fini(mongo_embedded_v1_lib* const lib,
- mongo_embedded_v1_status* const statusPtr) {
+int MONGO_API_CALL mongo_embedded_v1_lib_fini(mongo_embedded_v1_lib* const lib,
+ mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::capi_lib_fini(lib, status);
});
}
-mongo_embedded_v1_instance* mongo_embedded_v1_instance_create(
- mongo_embedded_v1_lib* lib,
- const char* const yaml_config,
- mongo_embedded_v1_status* const statusPtr) {
+mongo_embedded_v1_instance* MONGO_API_CALL
+mongo_embedded_v1_instance_create(mongo_embedded_v1_lib* lib,
+ const char* const yaml_config,
+ mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::instance_new(lib, yaml_config, status);
});
}
-int mongo_embedded_v1_instance_destroy(mongo_embedded_v1_instance* const db,
- mongo_embedded_v1_status* const statusPtr) {
+int MONGO_API_CALL mongo_embedded_v1_instance_destroy(mongo_embedded_v1_instance* const db,
+ mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::instance_destroy(db, status);
});
}
-mongo_embedded_v1_client* mongo_embedded_v1_client_create(
+mongo_embedded_v1_client* MONGO_API_CALL mongo_embedded_v1_client_create(
mongo_embedded_v1_instance* const db, mongo_embedded_v1_status* const statusPtr) {
return enterCXX(
statusPtr, [&](mongo_embedded_v1_status& status) { return mongo::client_new(db, status); });
}
-int mongo_embedded_v1_client_destroy(mongo_embedded_v1_client* const client,
- mongo_embedded_v1_status* const statusPtr) {
+int MONGO_API_CALL mongo_embedded_v1_client_destroy(mongo_embedded_v1_client* const client,
+ mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::client_destroy(client, status);
});
}
-int mongo_embedded_v1_client_invoke(mongo_embedded_v1_client* const client,
- const void* input,
- const size_t input_size,
- void** const output,
- size_t* const output_size,
- mongo_embedded_v1_status* const statusPtr) {
+int MONGO_API_CALL mongo_embedded_v1_client_invoke(mongo_embedded_v1_client* const client,
+ const void* input,
+ const size_t input_size,
+ void** const output,
+ size_t* const output_size,
+ mongo_embedded_v1_status* const statusPtr) {
return enterCXX(statusPtr, [&](mongo_embedded_v1_status& status) {
return mongo::client_wire_protocol_rpc(
client, input, input_size, output, output_size, status);
});
}
-int mongo_embedded_v1_status_get_error(const mongo_embedded_v1_status* const status) {
+int MONGO_API_CALL
+mongo_embedded_v1_status_get_error(const mongo_embedded_v1_status* const status) {
return mongo::capi_status_get_error(status);
}
-const char* mongo_embedded_v1_status_get_explanation(const mongo_embedded_v1_status* const status) {
+const char* MONGO_API_CALL
+mongo_embedded_v1_status_get_explanation(const mongo_embedded_v1_status* const status) {
return mongo::capi_status_get_what(status);
}
-int mongo_embedded_v1_status_get_code(const mongo_embedded_v1_status* const status) {
+int MONGO_API_CALL mongo_embedded_v1_status_get_code(const mongo_embedded_v1_status* const status) {
return mongo::capi_status_get_code(status);
}
-mongo_embedded_v1_status* mongo_embedded_v1_status_create(void) {
+mongo_embedded_v1_status* MONGO_API_CALL mongo_embedded_v1_status_create(void) {
return new mongo_embedded_v1_status;
}
-void mongo_embedded_v1_status_destroy(mongo_embedded_v1_status* const status) {
+void MONGO_API_CALL mongo_embedded_v1_status_destroy(mongo_embedded_v1_status* const status) {
delete status;
}
diff --git a/src/mongo/embedded/capi.h b/src/mongo/embedded/capi.h
index 6c4ff1e916f..a7029f7573a 100644
--- a/src/mongo/embedded/capi.h
+++ b/src/mongo/embedded/capi.h
@@ -31,6 +31,38 @@
#include <stddef.h>
#include <stdint.h>
+#pragma push_macro("MONGO_API_CALL")
+#undef MONGO_API_CALL
+
+#pragma push_macro("MONGO_API_IMPORT")
+#undef MONGO_API_IMPORT
+
+#pragma push_macro("MONGO_API_EXPORT")
+#undef MONGO_API_EXPORT
+
+#pragma push_macro("MONGO_EMBEDDED_CAPI_API")
+#undef MONGO_EMBEDDED_CAPI_API
+
+#if defined(_WIN32)
+#define MONGO_API_CALL __cdecl
+#define MONGO_API_IMPORT __declspec(dllimport)
+#define MONGO_API_EXPORT __declspec(dllexport)
+#else
+#define MONGO_API_CALL
+#define MONGO_API_IMPORT __attribute__((visibility("default")))
+#define MONGO_API_EXPORT __attribute__((used, visibility("default")))
+#endif
+
+#if defined(MONGO_EMBEDDED_CAPI_STATIC)
+#define MONGO_EMBEDDED_CAPI_API
+#else
+#if defined(MONGO_EMBEDDED_CAPI_COMPILING)
+#define MONGO_EMBEDDED_CAPI_API MONGO_API_EXPORT
+#else
+#define MONGO_EMBEDDED_CAPI_API MONGO_API_IMPORT
+#endif
+#endif
+
#ifdef _DOXYGEN
/**
* Embeddable MongoDB Library.
@@ -121,7 +153,8 @@ typedef struct mongo_embedded_v1_status mongo_embedded_v1_status;
*
* @note This function may be called before `mongo_embedded_v1_lib_init`.
*/
-mongo_embedded_v1_status* mongo_embedded_v1_status_create(void);
+MONGO_EMBEDDED_CAPI_API mongo_embedded_v1_status* MONGO_API_CALL
+mongo_embedded_v1_status_create(void);
/**
* Destroys a valid `mongo_embedded_v1_status` object.
@@ -149,7 +182,8 @@ mongo_embedded_v1_status* mongo_embedded_v1_status_create(void);
* including the storage referenced by functions that returned observable storage buffers from this
* status, such as strings.
*/
-void mongo_embedded_v1_status_destroy(mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API void MONGO_API_CALL
+mongo_embedded_v1_status_destroy(mongo_embedded_v1_status* status);
/**
* The error codes reported by `mongo_embedded_v1` functions will be given the symbolic names as
@@ -162,6 +196,7 @@ void mongo_embedded_v1_status_destroy(mongo_embedded_v1_status* status);
typedef enum {
MONGO_EMBEDDED_V1_ERROR_IN_REPORTING_ERROR = -2,
MONGO_EMBEDDED_V1_ERROR_UNKNOWN = -1,
+
MONGO_EMBEDDED_V1_SUCCESS = 0,
MONGO_EMBEDDED_V1_ERROR_ENOMEM = 1,
@@ -207,7 +242,8 @@ typedef enum {
* @note This function does not report its own failures.
* @note This behavior of this function is undefined unless its preconditions are met.
*/
-int mongo_embedded_v1_status_get_error(const mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL
+mongo_embedded_v1_status_get_error(const mongo_embedded_v1_status* status);
/**
* Gets a descriptive error message from a `mongo_embedded_v1_status` object.
@@ -248,7 +284,8 @@ int mongo_embedded_v1_status_get_error(const mongo_embedded_v1_status* status);
* @note This function does not report its own failures.
* @note This behavior of this function is undefined unless its preconditions are met.
*/
-const char* mongo_embedded_v1_status_get_explanation(const mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API const char* MONGO_API_CALL
+mongo_embedded_v1_status_get_explanation(const mongo_embedded_v1_status* status);
/**
* Gets a status code from a `mongo_embedded_v1_status` object.
@@ -283,7 +320,8 @@ const char* mongo_embedded_v1_status_get_explanation(const mongo_embedded_v1_sta
* @note This function does not report its own failures.
* @note This behavior of this function is undefined unless its preconditions are met.
*/
-int mongo_embedded_v1_status_get_code(const mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL
+mongo_embedded_v1_status_get_code(const mongo_embedded_v1_status* status);
/**
@@ -324,7 +362,7 @@ typedef struct mongo_embedded_v1_init_params mongo_embedded_v1_init_params;
* Log = 0
* Debug = 1 to 5
*/
-typedef void (*mongo_embedded_v1_log_callback)(
+typedef void(MONGO_API_CALL* mongo_embedded_v1_log_callback)(
void* user_data, const char* message, const char* component, const char* context, int severity);
/**
@@ -413,8 +451,8 @@ struct mongo_embedded_v1_init_params {
* @note This function may return diagnosic errors for violations of its preconditions, but this
* behavior is not guaranteed.
*/
-mongo_embedded_v1_lib* mongo_embedded_v1_lib_init(const mongo_embedded_v1_init_params* params,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API mongo_embedded_v1_lib* MONGO_API_CALL mongo_embedded_v1_lib_init(
+ const mongo_embedded_v1_init_params* params, mongo_embedded_v1_status* status);
/**
* Tears down the state of the library, all databases must be closed before calling this.
@@ -459,7 +497,8 @@ mongo_embedded_v1_lib* mongo_embedded_v1_lib_init(const mongo_embedded_v1_init_p
* function may return diagnosic errors for violations of its preconditions, but this behavior is
* not guaranteed.
*/
-int mongo_embedded_v1_lib_fini(mongo_embedded_v1_lib* lib, mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL
+mongo_embedded_v1_lib_fini(mongo_embedded_v1_lib* lib, mongo_embedded_v1_status* status);
/**
* An object which represents an instance of an Embedded MongoDB Server.
@@ -515,9 +554,10 @@ typedef struct mongo_embedded_v1_instance mongo_embedded_v1_instance;
* @note This function may return diagnosic errors for violations of its preconditions, but this
* behavior is not guaranteed.
*/
-mongo_embedded_v1_instance* mongo_embedded_v1_instance_create(mongo_embedded_v1_lib* lib,
- const char* yaml_config,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API mongo_embedded_v1_instance* MONGO_API_CALL
+mongo_embedded_v1_instance_create(mongo_embedded_v1_lib* lib,
+ const char* yaml_config,
+ mongo_embedded_v1_status* status);
/**
* Shuts down an embedded MongoDB instance.
@@ -559,8 +599,8 @@ mongo_embedded_v1_instance* mongo_embedded_v1_instance_create(mongo_embedded_v1_
* @note This function may return diagnosic errors for violations of its precondition, but this
* behavior is not guaranteed.
*/
-int mongo_embedded_v1_instance_destroy(mongo_embedded_v1_instance* instance,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL mongo_embedded_v1_instance_destroy(
+ mongo_embedded_v1_instance* instance, mongo_embedded_v1_status* status);
/**
* An object which represents "client connection" to an Embedded MongoDB Server.
@@ -598,8 +638,8 @@ typedef struct mongo_embedded_v1_client mongo_embedded_v1_client;
*
* @invariant This function is completely threadsafe, as long as its preconditions are met.
*/
-mongo_embedded_v1_client* mongo_embedded_v1_client_create(mongo_embedded_v1_instance* instance,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API mongo_embedded_v1_client* MONGO_API_CALL mongo_embedded_v1_client_create(
+ mongo_embedded_v1_instance* instance, mongo_embedded_v1_status* status);
/**
* Destroys an Embedded MongoDB Client.
@@ -634,8 +674,8 @@ mongo_embedded_v1_client* mongo_embedded_v1_client_create(mongo_embedded_v1_inst
* @note This function may return diagnosic errors for violations of its precondition, but this
* behavior is not guaranteed.
*/
-int mongo_embedded_v1_client_destroy(mongo_embedded_v1_client* client,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL mongo_embedded_v1_client_destroy(
+ mongo_embedded_v1_client* client, mongo_embedded_v1_status* status);
/**
* Makes an RPC call to the database.
@@ -691,12 +731,13 @@ int mongo_embedded_v1_client_destroy(mongo_embedded_v1_client* client,
* to be part of the specified `client` object for the purposes of thread-safety and undefined
* behavior.
*/
-int mongo_embedded_v1_client_invoke(mongo_embedded_v1_client* client,
- const void* input,
- size_t input_size,
- void** output,
- size_t* output_size,
- mongo_embedded_v1_status* status);
+MONGO_EMBEDDED_CAPI_API int MONGO_API_CALL
+mongo_embedded_v1_client_invoke(mongo_embedded_v1_client* client,
+ const void* input,
+ size_t input_size,
+ void** output,
+ size_t* output_size,
+ mongo_embedded_v1_status* status);
#ifdef __cplusplus
} // extern "C"
@@ -707,4 +748,16 @@ int mongo_embedded_v1_client_invoke(mongo_embedded_v1_client* client,
} // namespace mongo
#endif
+#undef MONGO_EMBEDDED_CAPI_API
+#pragma pop_macro("MONGO_EMBEDDED_CAPI_API")
+
+#undef MONGO_API_EXPORT
+#pragma push_macro("MONGO_API_EXPORT")
+
+#undef MONGO_API_IMPORT
+#pragma push_macro("MONGO_API_IMPORT")
+
+#undef MONGO_API_CALL
+#pragma pop_macro("MONGO_API_CALL")
+
#endif // HEADERUUID_5E967FCD_63BD_4374_88CC_58C091BA61C0_DEFINED
diff --git a/src/mongo/embedded/mongoc_client.cpp b/src/mongo/embedded/mongoc_client.cpp
index 1c2bc911251..48cdbf192f3 100644
--- a/src/mongo/embedded/mongoc_client.cpp
+++ b/src/mongo/embedded/mongoc_client.cpp
@@ -41,6 +41,12 @@
#include "mongo/embedded/capi.h"
#include "mongo/platform/endian.h"
+#if defined(_WIN32)
+#define MONGO_API_CALL __cdecl
+#else
+#define MONGO_API_CALL
+#endif
+
// Macro to trick the linter into accepting assert.
#define mongoc_client_assert assert
@@ -300,8 +306,8 @@ struct ClientDeleter {
}
};
-extern "C" mongoc_client_t* mongo_embedded_v1_mongoc_client_create(
- mongo_embedded_v1_instance* db) try {
+extern "C" mongoc_client_t* MONGO_API_CALL
+mongo_embedded_v1_mongoc_client_create(mongo_embedded_v1_instance* db) try {
if (!db) {
errno = EINVAL;
return nullptr;
diff --git a/src/mongo/embedded/mongoc_client.h b/src/mongo/embedded/mongoc_client.h
index b327117c910..2db787b10c1 100644
--- a/src/mongo/embedded/mongoc_client.h
+++ b/src/mongo/embedded/mongoc_client.h
@@ -34,6 +34,38 @@
#include <mongo/embedded/capi.h>
#include <mongoc.h>
+#pragma push_macro("MONGO_API_CALL")
+#undef MONGO_API_CALL
+
+#pragma push_macro("MONGO_API_IMPORT")
+#undef MONGO_API_IMPORT
+
+#pragma push_macro("MONGO_API_EXPORT")
+#undef MONGO_API_EXPORT
+
+#pragma push_macro("MONGO_EMBEDDED_MONGOC_CLIENT_API")
+#undef MONGO_EMBEDDED_MONGOC_CLIENT_API
+
+#if defined(_WIN32)
+#define MONGO_API_CALL __cdecl
+#define MONGO_API_IMPORT __declspec(dllimport)
+#define MONGO_API_EXPORT __declspec(dllexport)
+#else
+#define MONGO_API_CALL
+#define MONGO_API_IMPORT __attribute__((visibility("default")))
+#define MONGO_API_EXPORT __attribute__((used, visibility("default")))
+#endif
+
+#if defined(MONGO_EMBEDDED_MONGOC_CLIENT_STATIC)
+#define MONGO_EMBEDDED_MONGOC_CLIENT_API
+#else
+#if defined(MONGO_EMBEDDED_MONGOC_CLIENT_COMPILING)
+#define MONGO_EMBEDDED_MONGOC_CLIENT_API MONGO_API_EXPORT
+#else
+#define MONGO_EMBEDDED_MONGOC_CLIENT_API MONGO_API_IMPORT
+#endif
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -43,10 +75,23 @@ extern "C" {
* @param db must be a valid instance handle created by `mongo_embedded_v1_instance_create`
* @returns a mongoc client or `NULL` on error
*/
-mongoc_client_t* mongo_embedded_v1_mongoc_client_create(mongo_embedded_v1_instance* instance);
+MONGO_EMBEDDED_MONGOC_CLIENT_API mongoc_client_t* MONGO_API_CALL
+mongo_embedded_v1_mongoc_client_create(mongo_embedded_v1_instance* instance);
#ifdef __cplusplus
} // extern "C"
#endif
+#undef MONGO_EMBEDDED_MONGOC_CLIENT_API
+#pragma pop_macro("MONGO_EMBEDDED_MONGOC_CLIENT_API")
+
+#undef MONGO_API_EXPORT
+#pragma push_macro("MONGO_API_EXPORT")
+
+#undef MONGO_API_IMPORT
+#pragma push_macro("MONGO_API_IMPORT")
+
+#undef MONGO_API_CALL
+#pragma pop_macro("MONGO_API_CALL")
+
#endif // HEADERUUID_8CAAB40D_AC65_46CF_9FA9_B48825C825DC_DEFINED