Skip to content

Commit 25f6bdf

Browse files
authored
CDRIVER-4743 Define BSON_STATIC for bson_static build (#1453)
* remove `bson_obj` Separate sources built by `bson_shared` and `bson_static`. This is intended to build `bson_static` sources with no `dllexport` and `bson_shared` with `dllexport`. * define `BSON_STATIC` when building To suppress `dllexport`. When `dllexport` is defined, it appears to export the symbols in a consumer. * revise comments BSON_STATIC and MONGOC_STATIC are defined for static builds. Exporting functions in static build has an effect.
1 parent 6c42230 commit 25f6bdf

File tree

3 files changed

+41
-63
lines changed

3 files changed

+41
-63
lines changed

src/libbson/CMakeLists.txt

+33-53
Original file line numberDiff line numberDiff line change
@@ -171,84 +171,64 @@ file(GLOB_RECURSE all_sources CONFIGURE_DEPENDS
171171
"${mongo-c-driver_SOURCE_DIR}/src/common/*.c"
172172
)
173173

174-
# The default object library for all libbson translation units:
175-
add_library(bson_obj OBJECT EXCLUDE_FROM_ALL ${all_sources})
176-
177-
# The libbson object libraries that we will build
178-
set(bson_obj_libs bson_obj)
179-
180-
if(ENABLE_PIC OR WIN32)
181-
# User wants (or platform requires) static libs to use PIC code. Since we
182-
# already need PIC for the dynamic library, we can consolidate things and
183-
# use a single object library for both the static and the shared library.
184-
# No duplicate compilations necessary!
185-
set_property(TARGET bson_obj PROPERTY POSITION_INDEPENDENT_CODE TRUE)
186-
# The bson_obj_pic is just an alias of the base library:
187-
add_library(bson_obj_pic ALIAS bson_obj)
188-
else()
189-
# User does not want PIC in the static library. In that case, we just need a second object
190-
# library that has PIC enabled so it can be used in creating the dynamic library.
191-
add_library(bson_obj_pic OBJECT EXCLUDE_FROM_ALL ${all_sources})
192-
set_property(TARGET bson_obj_pic PROPERTY POSITION_INDEPENDENT_CODE TRUE)
193-
list(APPEND bson_obj_libs bson_obj_pic)
194-
endif()
195-
196-
# Set target properties for the object libraries.
197-
mongo_target_requirements(
198-
${bson_obj_libs}
199-
LINK_LIBRARIES
200-
PUBLIC
201-
_libbson_build_interface
202-
COMPILE_DEFINITIONS
203-
PRIVATE
204-
# Tell headers that they are part of compilation:
205-
BSON_COMPILATION
206-
# Enable NaN parsing in jsonsl
207-
JSONSL_PARSE_NAN
208-
# Set the name mangling scheme for the common libraries
209-
MCOMMON_NAME_PREFIX=_bson_mcommon
210-
COMPILE_OPTIONS
211-
PRIVATE
212-
# Macro constant INFINITY triggers constant arithmetic overflow warnings in
213-
# VS 2013, but VS 2013 doesn't support inline warning suppression.
214-
# Remove once support for VS 2013 is dropped.
215-
$<$<AND:$<C_COMPILER_ID:MSVC>,$<VERSION_LESS:${MSVC_VERSION},1900>>:/wd4756>
216-
)
217-
218174
# List of the primary BSON library targets that we are building
219175
set(bson_libs)
220176

221177
if(ENABLE_STATIC)
222-
add_library(bson_static STATIC)
223-
target_link_libraries(bson_static PRIVATE $<BUILD_INTERFACE:bson_obj>)
178+
add_library(bson_static STATIC ${all_sources})
224179
list(APPEND bson_libs bson_static)
180+
# Define `BSON_STATIC` when building to suppress the annotation __declspec(dllexport).
181+
# This prevents consumers of bson_static from exporting the symbols.
182+
target_compile_definitions(bson_static PRIVATE BSON_STATIC)
225183
# When consumers link against bson_static, suppress the annotation __declspec(dllimport),
226184
# since those symbols will be available immediately at the link step:
227185
target_compile_definitions(bson_static INTERFACE BSON_STATIC)
186+
187+
if(ENABLE_PIC)
188+
# User wants static libs to use PIC code.
189+
set_property(TARGET bson_static PROPERTY POSITION_INDEPENDENT_CODE TRUE)
190+
endif()
228191
endif()
229192

230193
if(ENABLE_SHARED)
231-
add_library(bson_shared SHARED)
194+
add_library(bson_shared SHARED ${all_sources})
232195
if(WIN32)
233196
# Add resource-definition script for Windows shared library (.dll).
234197
configure_file(libbson.rc.in libbson.rc)
235198
target_sources(bson_shared PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/libbson.rc)
236199
endif()
237-
target_link_libraries(bson_shared PRIVATE $<BUILD_INTERFACE:bson_obj_pic>)
238200
list(APPEND bson_libs bson_shared)
239201
endif()
240202

241203
if(NOT bson_libs)
242204
message(FATAL_ERROR "Neither bson_shared nor bson_static is going to be built. Did you mean to enable at least one of them?")
243205
endif()
244206

207+
# Set target properties for the libraries.
245208
mongo_target_requirements(
246-
${bson_libs} LINK_LIBRARIES PUBLIC
247-
# Build-local requirements:
248-
$<BUILD_INTERFACE:_libbson_build_interface>
249-
# Include in the install interface explicitly:
250-
mongo::detail::c_platform
209+
${bson_libs}
210+
LINK_LIBRARIES
211+
PUBLIC
212+
# Build-local requirements:
213+
$<BUILD_INTERFACE:_libbson_build_interface>
214+
# Include in the install interface explicitly:
215+
mongo::detail::c_platform
216+
COMPILE_DEFINITIONS
217+
PRIVATE
218+
# Tell headers that they are part of compilation:
219+
BSON_COMPILATION
220+
# Enable NaN parsing in jsonsl
221+
JSONSL_PARSE_NAN
222+
# Set the name mangling scheme for the common libraries
223+
MCOMMON_NAME_PREFIX=_bson_mcommon
224+
COMPILE_OPTIONS
225+
PRIVATE
226+
# Macro constant INFINITY triggers constant arithmetic overflow warnings in
227+
# VS 2013, but VS 2013 doesn't support inline warning suppression.
228+
# Remove once support for VS 2013 is dropped.
229+
$<$<AND:$<C_COMPILER_ID:MSVC>,$<VERSION_LESS:${MSVC_VERSION},1900>>:/wd4756>
251230
)
231+
252232
set_target_properties(${bson_libs} PROPERTIES
253233
VERSION "0.0.0"
254234
SOVERSION "0"

src/libbson/src/bson/bson-macros.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@
6666

6767

6868
/* Decorate public functions:
69-
* - if BSON_STATIC, we're compiling a program that uses libbson as a static
70-
* library, don't decorate functions
71-
* - else if BSON_COMPILATION, we're compiling a static or shared libbson, mark
72-
* public functions for export from the shared lib (which has no effect on
73-
* the static lib)
69+
* - if BSON_STATIC, we're compiling a static libbson or a program
70+
* that uses libbson as a static library. Don't decorate functions.
71+
* - else if BSON_COMPILATION, we're compiling a shared libbson, mark
72+
* public functions for export from the shared lib
7473
* - else, we're compiling a program that uses libbson as a shared library,
7574
* mark public functions as DLL imports for Microsoft Visual C
7675
*/

src/libmongoc/src/mongoc/mongoc-macros.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
#define MONGOC_MACROS_H
2121

2222
/* Decorate public functions:
23-
* - if MONGOC_STATIC, we're compiling a program that uses libmongoc as
24-
* a static library, don't decorate functions
25-
* - else if MONGOC_COMPILATION, we're compiling a static or shared libmongoc,
26-
* mark public functions for export from the shared lib (which has no effect
27-
* on the static lib)
23+
* - if MONGOC_STATIC, we're compiling a static libmongoc or a program
24+
* that uses libmongoc as a static library. Don't decorate functions
25+
* - else if MONGOC_COMPILATION, we're compiling a shared libmongoc,
26+
* mark public functions for export from the shared lib.
2827
* - else, we're compiling a program that uses libmongoc as a shared library,
2928
* mark public functions as DLL imports for Microsoft Visual C.
3029
*/

0 commit comments

Comments
 (0)