Skip to content

Commit 75a8204

Browse files
committed
CXX-760 CMake and package search improvements
1 parent 453ba63 commit 75a8204

25 files changed

+294
-147
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
- REPO_TYPE="precise/mongodb-enterprise/stable multiverse"
1111
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb-enterprise.list"
1212
- KEY_SERVER="hkp://keyserver.ubuntu.com:80"
13-
- CMAKE_VERSION="cmake-3.1.3-Linux-x86_64"
13+
- CMAKE_VERSION="cmake-3.2.3-Linux-x86_64"
1414
- CMAKE_BINARY="${CMAKE_VERSION}/bin/cmake"
1515

1616
matrix:
@@ -25,7 +25,7 @@ before_install:
2525
- wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -
2626

2727
# Get the latest CMake
28-
- curl -O https://cmake.org/files/v3.1/${CMAKE_VERSION}.tar.gz
28+
- curl -O https://cmake.org/files/v3.2/${CMAKE_VERSION}.tar.gz
2929

3030
# MongoDB Enterprise Edition, latest stable version
3131
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv 7F0CEB10

CMakeLists.txt

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
project(MONGO_CXX_DRIVER LANGUAGES CXX)
22

3-
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
3+
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
44

55
set (CMAKE_SKIP_BUILD_RPATH false)
66
set (CMAKE_BUILD_WITH_INSTALL_RPATH false)
77
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH true)
8+
# Ensure that RPATH is used on OSX
9+
set(CMAKE_MACOSX_RPATH 1)
810

9-
# Add in our modules
11+
# Add in our modules, we write FindX modules for libbson and libmongoc
12+
# since they don't currently install them.
1013
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
1114

1215
# Enforce the C++ standard, and disable extensions
@@ -16,19 +19,6 @@ endif()
1619

1720
set(CMAKE_CXX_EXTENSIONS OFF)
1821

19-
# CMake 3.1 doesn't know how to C++11 for clang.
20-
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
21-
if (CMAKE_CXX_STANDARD EQUAL 11)
22-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
23-
elseif (CMAKE_CXX_STANDARD EQUAL 14)
24-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
25-
else()
26-
error("Don't know how to do that standard for Clang")
27-
endif()
28-
else()
29-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
30-
endif()
31-
3222
# Include the required modules
3323
include(GenerateExportHeader)
3424
include(InstallRequiredSystemLibraries)
@@ -39,19 +29,6 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
3929
set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "default install path" FORCE)
4030
endif()
4131

42-
# Ensure we have Package Config
43-
find_package(PkgConfig)
44-
45-
# Set PKG_CONFIG_PATH
46-
if(DEFINED $ENV{PKG_CONFIG_PATH})
47-
set(PKG_CONFIG_PATH $ENV{PKG_CONFIG_PATH})
48-
else()
49-
set(PKG_CONFIG_PATH "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig")
50-
endif()
51-
52-
# Ensure that RPATH is used on OSX
53-
set(CMAKE_MACOSX_RPATH 1)
54-
5532
if(NOT CMAKE_BUILD_TYPE)
5633
message(STATUS "No build type selected, default is Release")
5734
set(CMAKE_BUILD_TYPE "Release")

cmake/FindLibBSON.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Find libbson, either via pkg-config, find-package in config mode,
2+
# or other less admirable jiggery-pokery
3+
4+
SET(LIBBSON_DIR "" CACHE STRING "Manual search path for libbson")
5+
6+
include(FindPackageHandleStandardArgs)
7+
8+
# Load up PkgConfig if we have it
9+
find_package(PkgConfig QUIET)
10+
11+
if (PKG_CONFIG_FOUND)
12+
pkg_check_modules(LIBBSON REQUIRED libbson-1.0>=${LibBSON_FIND_VERSION} )
13+
# We don't reiterate the version information here because we assume that
14+
# pkg_check_modules has honored our request.
15+
find_package_handle_standard_args(LIBBSON DEFAULT_MSG LIBBSON_FOUND)
16+
elseif(LIBBSON_DIR)
17+
# The best we can do until libbson starts installing a libbson-config.cmake file
18+
set(LIBBSON_LIBRARIES bson-1.0 CACHE INTERNAL "")
19+
set(LIBBSON_LIBRARY_DIRS ${LIBBSON_DIR}/lib CACHE INTERNAL "")
20+
set(LIBBSON_INCLUDE_DIRS ${LIBBSON_DIR}/include/libbson-1.0 CACHE INTERNAL "")
21+
find_package_handle_standard_args(LIBBSON DEFAULT_MSG LIBBSON_LIBRARIES LIBBSON_LIBRARY_DIRS LIBBSON_INCLUDE_DIRS)
22+
else()
23+
message(FATAL_ERROR "Don't know how to find libbson; please set LIBBSON_DIR to the prefix directory with which libbson was configured.")
24+
endif()

cmake/FindLibMongoC.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Find libmongo-c, either via pkg-config, find-package in config mode,
2+
# or other less admirable jiggery-pokery
3+
4+
SET(LIBMONGOC_DIR "" CACHE STRING "Manual search path for libmongoc")
5+
6+
include(FindPackageHandleStandardArgs)
7+
8+
# Load up PkgConfig if we have it
9+
find_package(PkgConfig QUIET)
10+
11+
if (PKG_CONFIG_FOUND)
12+
pkg_check_modules(LIBMONGOC REQUIRED libmongoc-1.0>=${LibMongoC_FIND_VERSION} )
13+
# We don't reiterate the version information here because we assume that
14+
# pkg_check_modules has honored our request.
15+
find_package_handle_standard_args(LIBMONGOC DEFAULT_MSG LIBMONGOC_FOUND)
16+
elseif(LIBMONGOC_DIR)
17+
# The best we can do until libMONGOC starts installing a libmongoc-config.cmake file
18+
set(LIBMONGOC_LIBRARIES mongoc-1.0 CACHE INTERNAL "")
19+
set(LIBMONGOC_LIBRARY_DIRS ${LIBMONGOC_DIR}/lib CACHE INTERNAL "")
20+
set(LIBMONGOC_INCLUDE_DIRS ${LIBMONGOC_DIR}/include/libMONGOC-1.0 CACHE INTERNAL "")
21+
find_package_handle_standard_args(LIBMONGOC DEFAULT_MSG LIBMONGOC_LIBRARIES LIBMONGOC_LIBRARY_DIRS LIBMONGOC_INCLUDE_DIRS)
22+
else()
23+
message(FATAL_ERROR "Don't know how to find libmongoc; please set LIBMONGOC_DIR to the prefix directory with which libbson was configured.")
24+
endif()

cmake/Findbsoncxx.cmake

Lines changed: 0 additions & 29 deletions
This file was deleted.

cmake/Findmongocxx.cmake

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
add_custom_target(install_headers
22
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=dev -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
3-
DEPENDS bsoncxx mongocxx
3+
DEPENDS bsoncxx_built mongocxx_built
44
)
55

66
add_custom_target(install_libs
77
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=runtime -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
8-
DEPENDS bsoncxx mongocxx
8+
DEPENDS bsoncxx_built mongocxx_built
99
)
1010

1111
add_subdirectory(bsoncxx)

src/bsoncxx/CMakeLists.txt

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
project(BSONCXX)
22

3+
set(LIBBSON_REQUIRED_ABI_VERSION 1.0)
4+
find_package(LibBSON 1.3.0 REQUIRED)
5+
36
# Update these as needed.
47
# TODO: read from file
58
set(BSONCXX_VERSION_MAJOR 0)
69
set(BSONCXX_VERSION_MINOR 3)
710
set(BSONCXX_VERSION_PATCH 0)
811
set(BSONCXX_VERSION_EXTRA "-pre")
912
set(BSONCXX_ABI_VERSION 0)
10-
set(LIBBSON_REQUIRED_ABI_VERSION 1.0)
1113

1214
set(BSONCXX_VERSION ${BSONCXX_VERSION_MAJOR}.${BSONCXX_VERSION_MINOR}.${BSONCXX_VERSION_PATCH})
1315
set(BSONCXX_INLINE_NAMESPACE "v${BSONCXX_ABI_VERSION}")
@@ -22,8 +24,6 @@ option(BSONCXX_POLY_USE_MNMLSTC "Use mnmlstc/core for stdx polyfills" ON)
2224
option(BSONCXX_POLY_USE_SYSTEM_MNMLSTC "Obtain mnmlstc/core from system" OFF)
2325
option(BSONCXX_POLY_USE_BOOST "Use boost for stdx polyfills" OFF)
2426

25-
pkg_check_modules(LIBBSON REQUIRED libbson-${LIBBSON_REQUIRED_ABI_VERSION})
26-
2727
add_subdirectory(third_party)
2828
add_subdirectory(config)
2929

@@ -65,10 +65,20 @@ add_library(bsoncxx_static STATIC
6565
${bsoncxx_sources}
6666
)
6767

68+
target_compile_definitions(bsoncxx_static PUBLIC BSONCXX_STATIC)
69+
6870
set_target_properties(bsoncxx_static PROPERTIES
6971
OUTPUT_NAME bsoncxx
7072
)
7173

74+
# Follow the boost convention to disambiguate the dll and static
75+
# library names
76+
if (WIN32)
77+
set_target_properties(bsoncxx_static PROPERTIES
78+
PREFIX lib
79+
)
80+
endif()
81+
7282
add_library(bsoncxx SHARED
7383
${bsoncxx_sources}
7484
)
@@ -88,7 +98,7 @@ generate_export_header(bsoncxx
8898
BASE_NAME BSONCXX
8999
EXPORT_MACRO_NAME BSONCXX_API
90100
NO_EXPORT_MACRO_NAME BSONCXX_PRIVATE
91-
EXPORT_FILE_NAME export.hpp
101+
EXPORT_FILE_NAME config/export.hpp
92102
STATIC_DEFINE BSONCXX_STATIC
93103
)
94104

@@ -114,19 +124,58 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
114124
)
115125

116126
install(FILES
117-
${CMAKE_CURRENT_BINARY_DIR}/export.hpp
118-
DESTINATION ${BSONCXX_HEADER_INSTALL_DIR}/bsoncxx
127+
${CMAKE_CURRENT_BINARY_DIR}/config/export.hpp
128+
DESTINATION ${BSONCXX_HEADER_INSTALL_DIR}/bsoncxx/config
119129
COMPONENT dev
120130
)
121131

122132
install(TARGETS
123133
bsoncxx
124134
LIBRARY DESTINATION lib COMPONENT runtime
135+
ARCHIVE DESTINATION lib COMPONENT dev
125136
)
126137

127138
install(TARGETS
128139
bsoncxx_static
129-
ARCHIVE DESTINATION lib COMPONENT runtime
140+
ARCHIVE DESTINATION lib COMPONENT dev
141+
)
142+
143+
set(PACKAGE_INCLUDE_INSTALL_DIRS ${BSONCXX_HEADER_INSTALL_DIR})
144+
set(PACKAGE_LIBRARY_INSTALL_DIRS lib)
145+
set(PACKAGE_LIBRARIES bsoncxx)
146+
147+
include(CMakePackageConfigHelpers)
148+
149+
configure_package_config_file(
150+
cmake/libbsoncxx-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config.cmake
151+
INSTALL_DESTINATION lib/cmake/libbsoncxx-${BSONCXX_VERSION}
152+
PATH_VARS PACKAGE_INCLUDE_INSTALL_DIRS PACKAGE_LIBRARY_INSTALL_DIRS
153+
)
154+
155+
write_basic_package_version_file(
156+
${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config-version.cmake
157+
VERSION ${BSONCXX_VERSION}
158+
COMPATIBILITY SameMajorVersion
159+
)
160+
161+
install(
162+
FILES ${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config-version.cmake
163+
DESTINATION lib/cmake/libbsoncxx-${BSONCXX_VERSION}
164+
)
165+
166+
# Add all generated targets as dependencies here. This is a proxy
167+
# to allow the examples build to be sequenced after the mongocxx build
168+
# since you can't currently depend on an install target in CMake.
169+
add_custom_target(bsoncxx_built
170+
DEPENDS
171+
bsoncxx
172+
bsoncxx_static
173+
${CMAKE_CURRENT_BINARY_DIR}/config/export.hpp
174+
${CMAKE_CURRENT_BINARY_DIR}/config/version.hpp
175+
${CMAKE_CURRENT_BINARY_DIR}/config/config.hpp
176+
${CMAKE_CURRENT_BINARY_DIR}/config/private/config.hpp
177+
${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config.cmake
178+
${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx-config-version.cmake
130179
)
131180

132181
add_subdirectory(test)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(LIBBSONCXX_VERSION_MAJOR @BSONCXX_VERSION_MAJOR@)
2+
set(LIBBSONCXX_VERSION_MINOR @BSONCXX_VERSION_MINOR@)
3+
set(LIBBSONCXX_VERSION_PATCH @BSONCXX_VERSION_PATCH@)
4+
set(LIBBSONCXX_PACKAGE_VERSION @BSONCXX_VERSION@)
5+
set(LIBBSONCXX_LIBRARIES bsoncxx)
6+
7+
@PACKAGE_INIT@
8+
9+
set_and_check(LIBBSONCXX_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/@PACKAGE_INCLUDE_INSTALL_DIRS@")
10+
set_and_check(LIBBSONCXX_LIBRARY_DIRS "${PACKAGE_PREFIX_DIR}/@PACKAGE_LIBRARY_INSTALL_DIRS@")
11+

src/bsoncxx/config/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ configure_file(
2828

2929
install(FILES
3030
"${CMAKE_CURRENT_BINARY_DIR}/libbsoncxx.pc"
31-
DESTINATION "${PKG_CONFIG_PATH}"
32-
COMPONENT runtime
31+
DESTINATION lib/pkgconfig
32+
COMPONENT dev
3333
)

src/bsoncxx/config/compiler.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#if defined(_MSC_VER)
16+
17+
// Disable MSVC warnings that cause a lot of noise related to DLL visibility
18+
// for types that we don't control (like std::unique_ptr).
19+
#pragma warning(push)
20+
#pragma warning(disable: 4251)
21+
22+
#define BSONCXX_INLINE inline __forceinline
23+
24+
#else
1525
#define BSONCXX_INLINE inline __attribute__((__visibility__("hidden"), __always_inline__))
26+
#endif

src/bsoncxx/config/libbsoncxx.pc.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ prefix=@CMAKE_INSTALL_PREFIX@
22
includedir=${prefix}/include
33
libdir=${prefix}/lib
44

5-
Name: bsoncxx
6-
Description: The BSON C++ Library
5+
Name: libbsoncxx
6+
Description: The MongoDB C++11 BSON Library
77
URL: http://github.com/mongodb/mongo-cxx-driver
88
Version: @BSONCXX_VERSION@
99
Requires.private: libbson-@LIBBSON_REQUIRED_ABI_VERSION@

src/bsoncxx/config/postlude.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// compiler.hpp
2020
#undef BSONCXX_INLINE
2121
#pragma pop_macro("BSONCXX_INLINE")
22+
#if defined(_MSC_VER)
23+
#pragma warning(pop)
24+
#endif
2225

2326
// src/bsoncxx/config/config.hpp.in
2427
#undef BSONCXX_INLINE_NAMESPACE_BEGIN
@@ -63,6 +66,8 @@ static_assert(false, "BSONCXX_ENUM must be undef'ed");
6366
#pragma pop_macro("BSONCXX_NO_DEPRECATED")
6467
#undef BSONCXX_PRIVATE
6568
#pragma pop_macro("BSONCXX_PRIVATE")
69+
#undef BSONCXX_API
70+
#pragma pop_macro("BSONCXX_API")
6671

6772
// prelude.hpp
6873
#undef BSONCXX_UNREACHABLE

src/bsoncxx/config/prelude.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161
#undef BSONCXX_NO_DEPRECATED
6262
#pragma push_macro("BSONCXX_PRIVATE")
6363
#undef BSONCXX_PRIVATE
64+
#pragma push_macro("BSONCXX_API")
65+
#undef BSONCXX_API
6466

6567
#include <bsoncxx/config/compiler.hpp>
6668
#include <bsoncxx/config/config.hpp>
6769
#include <bsoncxx/config/version.hpp>
68-
#include <bsoncxx/export.hpp>
70+
#include <bsoncxx/config/export.hpp>
6971

7072
#pragma push_macro("BSONCXX_UNREACHABLE")
7173
#undef BSONCXX_UNREACHABLE

0 commit comments

Comments
 (0)