From acfca0042edc0d57e459e572eb53dfec7659d662 Mon Sep 17 00:00:00 2001 From: Greg Beard Date: Fri, 19 Oct 2018 07:21:34 +0100 Subject: [PATCH 1/2] 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. --- CMakeLists.txt | 30 +++++++++++++++++++++++++----- README.md | 2 ++ cmake/WinApiLevels.cmake | 14 ++++++++++++++ include/mio/CMakeLists.txt | 2 +- include/mio/detail/CMakeLists.txt | 2 +- include/mio/detail/basic_mmap.hpp | 3 --- include/mio/page.hpp | 3 --- test/CMakeLists.txt | 5 +++++ 8 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 cmake/WinApiLevels.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bbdba7..339430f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 $ $) @@ -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:: diff --git a/README.md b/README.md index c3287a0..47a5de0 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/cmake/WinApiLevels.cmake b/cmake/WinApiLevels.cmake new file mode 100644 index 0000000..5f3ebad --- /dev/null +++ b/cmake/WinApiLevels.cmake @@ -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) diff --git a/include/mio/CMakeLists.txt b/include/mio/CMakeLists.txt index 57268b6..c2d495c 100644 --- a/include/mio/CMakeLists.txt +++ b/include/mio/CMakeLists.txt @@ -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 $ #ifdef _WIN32 -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif // WIN32_LEAN_AND_MEAN # include #else // ifdef _WIN32 # define INVALID_HANDLE_VALUE -1 diff --git a/include/mio/page.hpp b/include/mio/page.hpp index c2536a0..cae7377 100644 --- a/include/mio/page.hpp +++ b/include/mio/page.hpp @@ -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 #else # include diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 444d537..6bfc47e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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() From 63947b7c9a43cb44e371da518f561eeca3a1e07a Mon Sep 17 00:00:00 2001 From: Greg Beard Date: Fri, 19 Oct 2018 09:15:11 +0100 Subject: [PATCH 2/2] Changes wording/grammar of docs for extra win api target --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 47a5de0..b9128e6 100644 --- a/README.md +++ b/README.md @@ -264,7 +264,7 @@ 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. +**WINDOWS USERS**: The `mio::mio` target `#define`s `WIN32_LEAN_AND_MEAN` and `NOMINMAX`. The former ensures the imported surface area of the Win API is minimal, and the latter disables Windows' `min` and `max` macros so they don't intefere with `std::min` and `std::max`. Because *mio* is a header only library, these defintions will leak into downstream CMake builds. If their presence is causing problems with your build then you can use the alternative `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