Skip to content

Commit

Permalink
build: mp-units-contracts CMakeTarget added to reuse contract defin…
Browse files Browse the repository at this point in the history
…ition in different separate targets

Refers to #581
  • Loading branch information
mpusz committed Jun 6, 2024
1 parent 14a1801 commit e382435
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
24 changes: 2 additions & 22 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <depependencies>...)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ else()
endif()

# components/modules
include(MPUnitsContracts)
add_subdirectory(core)
add_subdirectory(systems)

Expand Down
41 changes: 41 additions & 0 deletions src/cmake/MPUnitsContracts.cmake
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 1 addition & 15 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e382435

Please sign in to comment.