From e382435b9858dd7d0a755e47d07e584a6b40c01e Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 6 Jun 2024 14:07:16 +0200 Subject: [PATCH] build: `mp-units-contracts` CMakeTarget added to reuse contract definition in different separate targets Refers to #581 --- example/CMakeLists.txt | 24 ++----------------- src/CMakeLists.txt | 1 + src/cmake/MPUnitsContracts.cmake | 41 ++++++++++++++++++++++++++++++++ src/core/CMakeLists.txt | 16 +------------ 4 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 src/cmake/MPUnitsContracts.cmake diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 5325cd8d..0ce2d875 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -28,32 +28,12 @@ if(${projectPrefix}BUILD_CXX_MODULES) target_compile_features(example_utils INTERFACE cxx_std_20) target_compile_definitions(example_utils INTERFACE ${projectPrefix}MODULES) target_include_directories(example_utils INTERFACE include) + target_link_libraries(example_utils INTERFACE mp-units-contracts) endif() add_library(example_utils-headers INTERFACE) target_include_directories(example_utils-headers INTERFACE include) - -if(${projectPrefix}API_CONTRACTS STREQUAL "NONE") - target_compile_definitions(mp-units-core INTERFACE ${projectPrefix}API_CONTRACTS=0) -elseif(${projectPrefix}API_CONTRACTS STREQUAL "GSL-LITE") - if(NOT TARGET gsl::gsl-lite) - find_package(gsl-lite REQUIRED) - endif() - if(${projectPrefix}BUILD_CXX_MODULES) - target_link_libraries(example_utils INTERFACE gsl::gsl-lite) - endif() - target_link_libraries(example_utils-headers INTERFACE gsl::gsl-lite) - target_compile_definitions(mp-units-core INTERFACE ${projectPrefix}API_CONTRACTS=2) -elseif(${projectPrefix}API_CONTRACTS STREQUAL "MS-GSL") - if(NOT TARGET Microsoft.GSL::GSL) - find_package(Microsoft.GSL REQUIRED) - endif() - if(${projectPrefix}BUILD_CXX_MODULES) - target_link_libraries(example_utils INTERFACE Microsoft.GSL::GSL) - endif() - target_link_libraries(example_utils-headers INTERFACE Microsoft.GSL::GSL) - target_compile_definitions(mp-units-core INTERFACE ${projectPrefix}API_CONTRACTS=3) -endif() +target_link_libraries(example_utils-headers INTERFACE mp-units-contracts) # # add_example(target ...) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index edbaac1e..22f33c37 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -114,6 +114,7 @@ else() endif() # components/modules +include(MPUnitsContracts) add_subdirectory(core) add_subdirectory(systems) diff --git a/src/cmake/MPUnitsContracts.cmake b/src/cmake/MPUnitsContracts.cmake new file mode 100644 index 00000000..6b45c24e --- /dev/null +++ b/src/cmake/MPUnitsContracts.cmake @@ -0,0 +1,41 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Mateusz Pusz +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +add_library(mp-units-contracts INTERFACE) + +if(${projectPrefix}API_CONTRACTS STREQUAL "NONE") + target_compile_definitions(mp-units-contracts INTERFACE ${projectPrefix}API_CONTRACTS=0) +elseif(${projectPrefix}API_CONTRACTS STREQUAL "GSL-LITE") + if(NOT TARGET gsl::gsl-lite) + find_package(gsl-lite REQUIRED) + endif() + target_link_libraries(mp-units-contracts INTERFACE gsl::gsl-lite) + target_compile_definitions(mp-units-contracts INTERFACE ${projectPrefix}API_CONTRACTS=2) +elseif(${projectPrefix}API_CONTRACTS STREQUAL "MS-GSL") + if(NOT TARGET Microsoft.GSL::GSL) + find_package(Microsoft.GSL REQUIRED) + endif() + target_link_libraries(mp-units-contracts INTERFACE Microsoft.GSL::GSL) + target_compile_definitions(mp-units-contracts INTERFACE ${projectPrefix}API_CONTRACTS=3) +endif() + +install(TARGETS mp-units-contracts EXPORT mp-unitsTargets) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 45c91dd0..5ee3c155 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -108,21 +108,7 @@ if(NOT ${projectPrefix}API_FREESTANDING endif() # Contracts checking -if(${projectPrefix}API_CONTRACTS STREQUAL "NONE") - target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=0) -elseif(${projectPrefix}API_CONTRACTS STREQUAL "GSL-LITE") - if(NOT TARGET gsl::gsl-lite) - find_package(gsl-lite REQUIRED) - endif() - target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} gsl::gsl-lite) - target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=2) -elseif(${projectPrefix}API_CONTRACTS STREQUAL "MS-GSL") - if(NOT TARGET Microsoft.GSL::GSL) - find_package(Microsoft.GSL REQUIRED) - endif() - target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} Microsoft.GSL::GSL) - target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=3) -endif() +target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} mp-units-contracts) # C++20 modules if(${projectPrefix}BUILD_CXX_MODULES)