Adds a mio::mio_full_winapi target, which defines no WinAPI flags

The extra target doesn't `#define` any of `WIN32_LEAN_AND_MEAN`,
`NOMINMAX`. The default `mio::mio` adds both of these. This commit also
removes these defines from the `.*pp` files; they're now inserted by the
compile commands.
This commit is contained in:
Greg Beard 2018-10-19 07:21:34 +01:00
parent 77125df199
commit acfca0042e
8 changed files with 48 additions and 13 deletions

View File

@ -15,6 +15,7 @@ else()
endif()
project(mio VERSION 1.0.0 LANGUAGES C CXX)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include (CTest)
include (CMakeDependentOption)
@ -40,22 +41,41 @@ CMAKE_DEPENDENT_OPTION(mio.tests
# transitively to linking targets. In our case, this amounts to an include
# directory and project header files.
#
add_library(mio INTERFACE)
add_library(mio::mio ALIAS mio)
add_library(mio_base INTERFACE)
#
# mio requires C++ 11 support, at a minimum. Setting the `cxx_std_11` compile
# features ensures that the corresponding C++ standard flag is populated in
# targets linking to mio
#
target_compile_features(mio INTERFACE cxx_std_11)
target_compile_features(mio_base INTERFACE cxx_std_11)
#
# On Windows, so as to be a "good citizen", mio offers two different
# targets that control the imported surface area of the Windows API. The
# default `mio` target sets the necessary flags for a minimal Win API
# (`WIN32_LEAN_AND_MEAN`, etc.), while the `mio_full_winapi` target sets
# none of these flags so will not disable any of the modules.
#
if(WIN32)
include(WinApiLevels)
else()
# On non-Windows systems, the `mio` and `mio_base` targets are
# effectively identical.
add_library(mio INTERFACE)
target_link_libraries(mio
INTERFACE mio_base
)
endif()
add_library(mio::mio ALIAS mio)
#
# The include directory for mio can be expected to vary between build
# and installaion. Here we use a CMake generator expression to dispatch
# on how the configuration under which this library is being consumed.
#
target_include_directories(mio INTERFACE
target_include_directories(mio_base INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>)
@ -84,7 +104,7 @@ install(DIRECTORY include/
# generate a CMake configuration file for consumption by CMake's `find_package`
# intrinsic
#
install(TARGETS mio EXPORT mioConfig)
install(TARGETS mio_base mio EXPORT mioConfig)
install(EXPORT mioConfig
FILE mioConfig.cmake
NAMESPACE mio::

View File

@ -264,6 +264,8 @@ find_package( mio REQUIRED )
target_link_libraries( MyTarget PUBLIC mio::mio )
```
**WINDOWS USERS**: The `mio::mio` target `#define`s `WIN32_LEAN_AND_MEAN` and `NOMINMAX`. The former trims down the surface area of the Win API to a minimum and the latter disables Windows' `min` and `max` definitions, so they don't intefere with `std::min` and `std::max`. Because *mio* is a header only library, these defintions will leak into downstream builds, so if their presence is causing problems with your build then, alternatively, you can use the `mio::mio_full_winapi` target, which adds none of these defintions.
If mio was installed to a non-conventional location, it may be necessary for downstream projects to specify the mio installation root directory via either
+ the `CMAKE_PREFIX_PATH` configuration option,

14
cmake/WinApiLevels.cmake Normal file
View File

@ -0,0 +1,14 @@
add_library(mio_full_winapi INTERFACE)
target_link_libraries(mio_full_winapi
INTERFACE mio_base
)
add_library(mio::mio_full_winapi ALIAS mio_full_winapi)
add_library(mio INTERFACE)
target_link_libraries(mio
INTERFACE mio_full_winapi
)
target_compile_definitions(mio
INTERFACE WIN32_LEAN_AND_MEAN NOMINMAX
)
install(TARGETS mio_full_winapi EXPORT mioConfig)

View File

@ -3,7 +3,7 @@
# doing so populates these files in the source listing when CMake is used
# to generate XCode and Visual Studios projects
#
target_sources(mio INTERFACE
target_sources(mio_base INTERFACE
$<BUILD_INTERFACE:
${CMAKE_CURRENT_LIST_DIR}/mmap.hpp
${CMAKE_CURRENT_LIST_DIR}/page.hpp

View File

@ -3,7 +3,7 @@
# detail files in the source listing for CMake-generated IDE projects
#
if(NOT subproject)
target_sources(mio INTERFACE
target_sources(mio_base INTERFACE
$<BUILD_INTERFACE:
${CMAKE_CURRENT_LIST_DIR}/basic_mmap.hpp
${CMAKE_CURRENT_LIST_DIR}/basic_mmap.ipp

View File

@ -28,9 +28,6 @@
#include <system_error>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif // WIN32_LEAN_AND_MEAN
# include <windows.h>
#else // ifdef _WIN32
# define INVALID_HANDLE_VALUE -1

View File

@ -22,9 +22,6 @@
#define MIO_PAGE_HEADER
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif // WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <unistd.h>

View File

@ -7,4 +7,9 @@ if(WIN32)
target_link_libraries(mio.unicode.test PRIVATE mio::mio)
target_compile_definitions(mio.unicode.test PRIVATE UNICODE)
add_test(NAME mio.unicode.test COMMAND mio.test)
add_executable(mio.fullwinapi.test test.cpp)
target_link_libraries(mio.fullwinapi.test
PRIVATE mio::mio_full_winapi)
add_test(NAME mio.fullwinapi.test COMMAND mio.test)
endif()