-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCMakeLists.txt
executable file
·190 lines (154 loc) · 5.46 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
cmake_minimum_required(VERSION 3.11)
project(ClCxx)
# Build options
# ============
option(BUILD_TESTS "Build test programs" OFF)
# Dependencies
# ============
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
# Settings
# ============
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_MODULE_PATH}/lib")
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_VERBOSE_MAKEFILE 1)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/bin") # .exe and .dll
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/lib") # .so and .dylib
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/lib") # .lib and .a
# includes
# ============
include(CheckCXXCompilerFlag)
include(CMakePackageConfigHelpers)
include(GenerateExportHeader)
include(GNUInstallDirs)
include(InstallRequiredSystemLibraries)
# Compilation flags
# =================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Liker flags
# =================
if(APPLE)
set(D_LINER_FLAGS "-undefined error" "-flat_namespace")
else()
set(D_LINER_FLAGS "-Wl,--no-undefined" "-Wl,--no-allow-shlib-undefined")
endif()
# Compiler flags
# =================
# -march=native for GCC, Clang and ICC in any version that does support it.
if((NOT CRYPTOPP_CROSS_COMPILE) AND (CMAKE_CXX_COMPILER_ID MATCHES
"Clang|GNU|Intel"))
check_cxx_compiler_flag("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
if(COMPILER_OPT_ARCH_NATIVE_SUPPORTED AND NOT CMAKE_CXX_FLAGS MATCHES
"-march=")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
endif()
endif()
set(CMAKE_INSTALL_LIBDIR "lib")
# Source files
# ============
set(CLCXX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(CLCXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
file(GLOB HEADER_GLOB ${CLCXX_INCLUDE_DIR}/clcxx/*.hpp)
file(GLOB SOURCES_GLOB ${CLCXX_SOURCE_DIR}/*.cpp)
set(CLCXX_HEADERS ${HEADER_GLOB})
set(CLCXX_SOURCES ${SOURCES_GLOB})
# Versioning
# ==========
file(STRINGS "${CLCXX_INCLUDE_DIR}/clcxx/clcxx_config.hpp" clcxx_version_defines
REGEX "#define CLCXX_VERSION_(MAJOR|MINOR|PATCH)")
foreach(ver ${clcxx_version_defines})
if(ver MATCHES "#define CLCXX_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
set(CLCXX_VERSION_${CMAKE_MATCH_1}
"${CMAKE_MATCH_2}"
CACHE INTERNAL "")
endif()
endforeach()
set(${PROJECT_NAME}_VERSION
${CLCXX_VERSION_MAJOR}.${CLCXX_VERSION_MINOR}.${CLCXX_VERSION_PATCH})
message(STATUS "${PROJECT_NAME} version: v${${PROJECT_NAME}_VERSION}")
# Output
# ======
set(CLCXX_TARGET "${PROJECT_NAME}")
add_library(${CLCXX_TARGET} SHARED ${CLCXX_SOURCES} ${CLCXX_HEADERS})
target_include_directories(
${CLCXX_TARGET} PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CLCXX_INCLUDE_DIR}>)
target_compile_options(
${CLCXX_TARGET}
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall
-O3
-Wextra>
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /EHa>)
target_link_libraries(${CLCXX_TARGET} ${D_LINER_FLAGS})
set_target_properties(
${CLCXX_TARGET} PROPERTIES PUBLIC_HEADER "${CLCXX_HEADERS}"
COMPILE_DEFINITIONS "CLCXX_EXPORTS")
set_property(TARGET ${CLCXX_TARGET} PROPERTY VERSION ${${PROJECT_NAME}_VERSION})
set_property(TARGET ${CLCXX_TARGET} PROPERTY SOVERSION ${CLCXX_VERSION_MAJOR})
set_property(
TARGET ${CLCXX_TARGET} PROPERTY INTERFACE_${CLCXX_TARGET}_MAJOR_VERSION
${CLCXX_VERSION_MAJOR})
set_property(
TARGET ${CLCXX_TARGET}
APPEND
PROPERTY COMPATIBLE_INTERFACE_STRING ${CLCXX_TARGET}_MAJOR_VERSION)
generate_export_header(${CLCXX_TARGET})
# Installation
# ============
# Use this snippet *after* PROJECT(xxx):
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX
"~/.local"
CACHE PATH "install directory" FORCE)
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
install(
TARGETS ${CLCXX_TARGET}
EXPORT ClCxxTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/clcxx)
# Uninstall
# ==========
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
add_custom_target(
uninstall COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
if(BUILD_TESTS)
# find_package(Eigen3 REQUIRED)
enable_testing()
include(FetchContent)
# set(CATCH_INSTALL_DOCS OFF)
set(CATCH_BUILD_TESTING OFF)
# set(CATCH_INSTALL_EXTRAS OFF)
# set(CATCH_DEVELOPMENT_BUILD OFF)
set(BUILD_TESTING OFF)
FetchContent_Declare(
Catch2
GIT_REPOSITORY [email protected]:catchorg/Catch2.git
GIT_TAG v3.3.1 # or a later release
)
FetchContent_MakeAvailable(Catch2)
add_executable(tests tests/test.cpp)
target_link_libraries(tests PRIVATE ${CLCXX_TARGET} Catch2::Catch2WithMain)
target_include_directories(tests PUBLIC # ${EIGEN3_INCLUDE_DIR}
${CLCXX_INCLUDE_DIR})
target_compile_options(
tests
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall
-ggdb3
-Wextra>
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /EHa>)
add_test(NAME TestBase COMMAND tests)
endif(BUILD_TESTS)