-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
108 lines (97 loc) · 3.62 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
cmake_minimum_required(VERSION 3.23)
project(
singleton
VERSION 1.6.0
DESCRIPTION "C++11/17/20 template singleton pattern"
HOMEPAGE_URL "https://github.com/jimmy-park/singleton"
LANGUAGES CXX
)
option(SINGLETON_COMPILE "Build as a static/shared library" OFF)
option(SINGLETON_INJECT_ABSTRACT_CLASS "Prevent construction of derived class itself" OFF)
option(SINGLETON_INSTALL "Install headers and CMake targets" OFF)
include(cmake/CompileOptions.cmake)
include(cmake/UseSanitizer.cmake)
if(SINGLETON_COMPILE)
set(SCOPE PUBLIC)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/singleton.cpp "#include <singleton.hpp>")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/singleton_dclp.cpp "#include <singleton_dclp.hpp>")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/singleton_atomic.cpp "#include <singleton_atomic.hpp>")
add_library(singleton ${CMAKE_CURRENT_BINARY_DIR}/singleton.cpp)
add_library(singleton-dclp ${CMAKE_CURRENT_BINARY_DIR}/singleton_dclp.cpp)
add_library(singleton-atomic ${CMAKE_CURRENT_BINARY_DIR}/singleton_atomic.cpp)
else()
set(SCOPE INTERFACE)
add_library(singleton INTERFACE)
add_library(singleton-dclp INTERFACE)
add_library(singleton-atomic INTERFACE)
endif()
# Modify source
if(MINGW OR CYGWIN)
file(READ ${CMAKE_SOURCE_DIR}/include/singleton_dclp.hpp DCLP)
string(REPLACE "#include <shared_mutex>\n" "" DCLP "${DCLP}")
string(REPLACE "std::shared_lock" "std::lock_guard" DCLP "${DCLP}")
string(REPLACE "std::shared_mutex" "std::mutex" DCLP "${DCLP}")
file(WRITE ${CMAKE_SOURCE_DIR}/include/singleton_dclp.hpp "${DCLP}")
endif()
# Create a singleton target
add_library(singleton::singleton ALIAS singleton)
target_compile_features(singleton ${SCOPE} cxx_std_11)
target_compile_definitions(singleton ${SCOPE}
$<$<BOOL:${SINGLETON_INJECT_ABSTRACT_CLASS}>:SINGLETON_INJECT_ABSTRACT_CLASS>
)
target_sources(singleton ${SCOPE}
FILE_SET HEADERS
BASE_DIRS include
FILES include/singleton.hpp
)
# Create a singleton-dclp target
add_library(singleton::singleton-dclp ALIAS singleton-dclp)
target_compile_features(singleton-dclp ${SCOPE} cxx_std_17)
target_compile_definitions(singleton-dclp ${SCOPE}
$<$<BOOL:${SINGLETON_INJECT_ABSTRACT_CLASS}>:SINGLETON_INJECT_ABSTRACT_CLASS>
)
target_sources(singleton-dclp ${SCOPE}
FILE_SET HEADERS
BASE_DIRS include
FILES include/singleton_dclp.hpp
)
# Create a singleton-atomic target
add_library(singleton::singleton-atomic ALIAS singleton-atomic)
target_compile_features(singleton-atomic ${SCOPE} cxx_std_20)
target_compile_definitions(singleton-atomic ${SCOPE}
$<$<BOOL:${SINGLETON_INJECT_ABSTRACT_CLASS}>:SINGLETON_INJECT_ABSTRACT_CLASS>
)
target_sources(singleton-atomic ${SCOPE}
FILE_SET HEADERS
BASE_DIRS include
FILES include/singleton_atomic.hpp
)
# Install/Export targets
if(SINGLETON_INSTALL)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/singleton-config-version.cmake
COMPATIBILITY SameMajorVersion
)
install(
TARGETS singleton singleton-dclp singleton-atomic
EXPORT singletonTargets
FILE_SET HEADERS
)
install(
EXPORT singletonTargets
NAMESPACE singleton::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/singleton
)
install(
FILES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/singleton-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/singleton-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/singleton
)
endif()
if(PROJECT_IS_TOP_LEVEL AND NOT CMAKE_CROSSCOMPILING)
include(CTest)
add_subdirectory(test)
endif()