-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
202 lines (179 loc) · 8.52 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
191
192
193
194
195
196
197
198
199
200
201
202
cmake_minimum_required (VERSION 3.12)
project (bignum VERSION 1.0)
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag) # Check if the compiler supports the C++20 standard
check_cxx_compiler_flag("-std=c++20" COMPILER_SUPPORTS_CXX20)
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
check_cxx_compiler_flag("-std=c++11" COMPILER_SUPPORTS_CXX11)
message(STATUS "Compiler ID ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
if(COMPILER_SUPPORTS_CXX20)
message(STATUS "Compiler support C++20. Setting C++ standard to c++20")
set(CMAKE_CXX_STANDARD 20)
#elseif(COMPILER_SUPPORTS_CXX17)
# message(STATUS "Compiler support C++17. Setting C++ standard to c++17")
# set(CMAKE_CXX_STANDARD 17)
#elseif(COMPILER_SUPPORTS_CXX11)
# message(STATUS "Compiler support C++11. Setting C++ standard to c++11")
# set(CMAKE_CXX_STANDARD 11)
else()
message(FATAL_ERROR "Currently only C++20 is supported")
endif()
set(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
# These options are for user.
option(BIGNUM_BUILD_SHARED "Build shared library" OFF)
option(BIGNUM_ENABLE_EXCEPTIONS "Enable exceptions" ON)
option(BIGNUM_ENABLE_LITERAL_FLOAT_CONSTRUCTOR "Enable literal float initialization" OFF)
option(BIGNUM_ERROR_NODISCARD "Enable [[nodiscard]] attribute for ErrCode" OFF)
# These options are for developer (testing, performance tuning, etc).
option(BIGNUM_WITH_ASAN "Building with address sanitizer" OFF)
option(BIGNUM_WITH_COVERAGE "Building with coverage" OFF)
option(BIGNUM_BUILD_TESTS "Build tests" OFF)
option(BIGNUM_BUILD_BENCHMARK "Build tests" OFF)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-error=unused-parameter")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=ambiguous-reversed-operator")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=cast-function-type -Wno-error=implicit-fallthrough")
endif()
if(BIGNUM_ENABLE_EXCEPTIONS)
add_compile_definitions(BIGNUM_ENABLE_EXCEPTIONS)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
endif()
if(BIGNUM_ENABLE_LITERAL_FLOAT_CONSTRUCTOR)
add_compile_definitions(BIGNUM_ENABLE_LITERAL_FLOAT_CONSTRUCTOR)
endif()
if(BIGNUM_ERROR_NODISCARD)
add_compile_definitions(BIGNUM_ERROR_NODISCARD)
endif()
if (BIGNUM_WITH_COVERAGE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif()
endif()
if(NOT DEFINED CMAKE_CXX_COMPILER_ID)
message(FATAL_ERROR "CMAKE_CXX_COMPILER_ID not defined yet")
endif()
message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMAKE_CXX_COMPILER_VERSION: ${CMAKE_CXX_COMPILER_VERSION}")
if(BIGNUM_WITH_ASAN)
message(STATUS "Building with ASAN")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-recover=address")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libsan -Wno-uninitialized ")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan -Wno-maybe-uninitialized ")
endif()
endif()
include(cmake/gmp.cmake)
include(cmake/gtest.cmake)
include(cmake/benchmark.cmake)
find_package(Threads REQUIRED)
set(BIGNUM_SOURCE ${PROJECT_ROOT}/src/decimal.cc)
if (BIGNUM_BUILD_SHARED)
add_library(bignum SHARED ${BIGNUM_SOURCE})
target_include_directories(bignum PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(bignum PRIVATE ${GMP_INCLUDE_DIR})
set_target_properties(bignum PROPERTIES SOVERSION 1 VERSION 1.0.0)
add_dependencies(bignum gmp_shared_lib)
target_link_libraries(bignum PUBLIC ${GMP_SHARED_LIBRARIES} Threads::Threads)
set_target_properties(bignum PROPERTIES BUILD_RPATH "${GMP_LIB_DIR}")
else()
add_library(bignum STATIC ${BIGNUM_SOURCE})
target_include_directories(bignum PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(bignum PRIVATE ${GMP_INCLUDE_DIR} Threads::Threads)
add_dependencies(bignum gmp_static_lib)
target_link_libraries(bignum PUBLIC ${GMP_LIBRARIES})
endif()
# static bignum lib that use gmp only. For dev and test purpose. So build static lib only.
add_library(bignum_gmp_only STATIC ${BIGNUM_SOURCE})
target_include_directories(bignum_gmp_only PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(bignum_gmp_only PRIVATE ${GMP_INCLUDE_DIR} Threads::Threads)
target_compile_definitions(bignum_gmp_only PUBLIC BIGNUM_DEV_USE_GMP_ONLY)
add_dependencies(bignum_gmp_only gmp_static_lib)
target_link_libraries(bignum_gmp_only PRIVATE ${GMP_LIBRARIES})
# decimal_calculator
add_executable(decimal_calculator ${CMAKE_SOURCE_DIR}/src/calculator.cc)
target_link_libraries(decimal_calculator bignum)
target_include_directories(decimal_calculator PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(decimal_calculator PRIVATE ${GMP_INCLUDE_DIR})
# decimal_calculator_gmp_only
add_executable(decimal_calculator_gmp_only ${CMAKE_SOURCE_DIR}/src/calculator.cc)
target_link_libraries(decimal_calculator_gmp_only bignum_gmp_only)
target_include_directories(decimal_calculator_gmp_only PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(decimal_calculator_gmp_only PRIVATE ${GMP_INCLUDE_DIR})
if (BIGNUM_BUILD_TESTS)
set(UNITTEST_SOURCES
${PROJECT_ROOT}/tests/main.cc
${PROJECT_ROOT}/tests/op.cc
${PROJECT_ROOT}/tests/gmp.cc
${PROJECT_ROOT}/tests/issues.cc
${PROJECT_ROOT}/tests/exception_or_assert.cc
)
add_executable(unittest ${UNITTEST_SOURCES})
target_link_libraries(unittest bignum)
target_include_directories(unittest PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(unittest PRIVATE ${GMP_INCLUDE_DIR})
target_include_directories(unittest PRIVATE ${GTEST_INCLUDE_DIR})
target_link_libraries(unittest ${GTEST_LIBRARIES} Threads::Threads)
add_dependencies(unittest gtest_lib)
add_executable(unittest_gmp_only ${UNITTEST_SOURCES})
target_link_libraries(unittest_gmp_only bignum_gmp_only)
target_include_directories(unittest_gmp_only PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(unittest_gmp_only PRIVATE ${GMP_INCLUDE_DIR})
target_include_directories(unittest_gmp_only PRIVATE ${GTEST_INCLUDE_DIR})
target_compile_definitions(unittest_gmp_only PUBLIC BIGNUM_DEV_USE_GMP_ONLY)
target_link_libraries(unittest_gmp_only ${GTEST_LIBRARIES} Threads::Threads)
add_dependencies(unittest_gmp_only gtest_lib)
endif()
if (BIGNUM_BUILD_BENCHMARK)
SET(BENCHMARK_SOURCES
${PROJECT_ROOT}/benchmark/main.cc
${PROJECT_ROOT}/benchmark/op.cc
)
add_executable(benchmark ${BENCHMARK_SOURCES})
target_link_libraries(benchmark bignum)
target_include_directories(benchmark PRIVATE ${PROJECT_ROOT}/src)
target_include_directories(benchmark PRIVATE ${GMP_INCLUDE_DIR})
target_include_directories(benchmark PRIVATE ${BENCHMARK_INCLUDE_DIR})
target_link_libraries(benchmark ${BENCHMARK_LIBRARIES} Threads::Threads)
add_dependencies(benchmark benchmark_lib)
endif()
set_target_properties(
bignum
PROPERTIES
PUBLIC_HEADER
"${PROJECT_ROOT}/src/assertion.h;${PROJECT_ROOT}/src/decimal.h;${PROJECT_ROOT}/src/errcode.h;${PROJECT_ROOT}/src/gmp_wrapper.h"
)
set_target_properties(
bignum
PROPERTIES
INSTALL_RPATH "\$ORIGIN"
)
install(TARGETS bignum
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/bignum
)
if (BIGNUM_BUILD_SHARED)
install(FILES ${GMP_LIB_DIR}/libgmp.so DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(FILES ${GMP_LIB_DIR}/libgmp.so.10 DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(FILES ${GMP_LIB_DIR}/libgmp.so.10.5.0 DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
endif()
install(TARGETS decimal_calculator ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(TARGETS decimal_calculator_gmp_only ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
if (BIGNUM_BUILD_TESTS)
install(TARGETS unittest RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(TARGETS unittest_gmp_only RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
endif()
if (BIGNUM_BUILD_BENCHMARK)
install(TARGETS benchmark RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
endif()