-
Notifications
You must be signed in to change notification settings - Fork 144
/
CMakeLists.txt
195 lines (169 loc) · 5.51 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
#
# Minimum version is 3.5 (this supports Ubuntu 16.04 and later, for example)
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
#
# Project version numbering using semantic versioning. See:
# https://semver.org/
#
# These are used to set VERSION and SOVERSION properties of the LibSerial
# libarary. See:
# - https://cmake.org/cmake/help/latest/prop_tgt/SOVERSION.html
# - https://cmake.org/cmake/help/latest/prop_tgt/VERSION.html
#
PROJECT(LibSerial LANGUAGES C CXX VERSION 1.0.0)
option(LIBSERIAL_ENABLE_TESTING "Enables building unit tests" ON)
option(LIBSERIAL_BUILD_EXAMPLES "Enables building example programs" ON)
option(LIBSERIAL_PYTHON_ENABLE "Enables building the library with Python SIP bindings" ON)
option(LIBSERIAL_BUILD_DOCS "Build the Doxygen docs" ON)
#
# Project specific options and variables
#
OPTION(INSTALL_STATIC "Install static library." ON)
OPTION(INSTALL_SHARED "Install shared object library." ON)
#
# LibSerial requies a C++ compiler that supports at least C++14 standard
#
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_STANDARD_REQUIRES ON)
INCLUDE(ExternalProject)
if (LIBSERIAL_ENABLE_TESTING)
ENABLE_TESTING()
endif()
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
#
# Create compile_commands.json file so that it may be used by various
# editors/plugins/IDEs that support it.
#
SET(CMAKE_EXPORT_COMPILE_COMMANDS 1)
#
# Use GNU standard installation directories. CMake will use /usr/local
# as the default install directory. Users may override this by setting
# CMAKE_INSTALL_PREFIX. For example:
#
# cd build && cmake -DCMAKE_INSTALL_PREFIX=/usr ..
#
INCLUDE(GNUInstallDirs)
#
# Prefer -pthread compiler and linker flag when using libpthread. This must
# be set before call to FIND_PACKAGE(Threads).
#
SET(THREADS_HAVE_PTHREAD_ARG 1)
if (LIBSERIAL_BUILD_DOCS)
FIND_PACKAGE(Doxygen REQUIRED)
endif()
if (LIBSERIAL_ENABLE_TESTING)
FIND_PACKAGE(Boost COMPONENTS unit_test_framework REQUIRED)
endif()
if (LIBSERIAL_PYTHON_ENABLE)
FIND_PACKAGE(PythonLibs REQUIRED)
endif()
#FIND_PACKAGE(SIP REQUIRED)
FIND_PACKAGE(Threads REQUIRED)
#
# Use -DCMAKE_BUILD_TYPE=Release or -DCMAKE_BUILD_TYPE=Debug to let CMake
# decide whether to use debug or optimization flags. We should not hard-code
# them here. Similarly, let CMake handle flags needed for shared object files
# (such as -fPIC). Additionally, "-pthread" flag will also be handled by CMake
# via the use of CMAKE_THREAD_LIBS_INIT (cmake < 3.1) or Threads::Threads.
#
ADD_DEFINITIONS(
-Wall
-Wcast-align
-Wchar-subscripts
-Wdouble-promotion
-Wextra
-Wfatal-errors
-Wformat
-Wformat-security
-Wlogical-op
-Wno-format-extra-args
-Wno-long-long
-Wno-parentheses
-Wno-psabi
-Wno-variadic-macros
-Woverlength-strings
-Wpacked
-Wpointer-arith
-Wunused-local-typedefs
-Wwrite-strings
-fstrict-aliasing
-fno-check-new
-fno-common
-fvisibility=default
-pedantic
)
if (LIBSERIAL_ENABLE_TESTING)
SET(GTEST_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/gtest")
EXTERNALPROJECT_ADD(GTestExternal
PREFIX "${GTEST_PREFIX}"
URL https://github.com/google/googletest/archive/refs/tags/v1.13.0.tar.gz
URL_HASH SHA1=bfa4b5131b6eaac06962c251742c96aab3f7aa78
INSTALL_COMMAND ""
)
SET(LIBPREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}")
SET(LIBSUFFIX "${CMAKE_STATIC_LIBRARY_SUFFIX}")
SET(GTEST_LOCATION "${GTEST_PREFIX}/src/GTestExternal-build/lib")
SET(GTEST_LIBRARY "${GTEST_LOCATION}/${LIBPREFIX}gtest${LIBSUFFIX}")
SET(GTEST_MAINLIB "${GTEST_LOCATION}/${LIBPREFIX}gtest_main${LIBSUFFIX}")
ADD_LIBRARY(GTest IMPORTED STATIC GLOBAL)
SET_TARGET_PROPERTIES(GTest
PROPERTIES
IMPORTED_LOCATION "${GTEST_LIBRARY}"
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}"
)
ADD_LIBRARY(GTestMain IMPORTED STATIC GLOBAL)
SET_TARGET_PROPERTIES(GTestMain
PROPERTIES
IMPORTED_LOCATION "${GTEST_MAINLIB}"
IMPORTED_LINK_INTERFACE_LIBRARIES "${GTEST_LIBRARY};${CMAKE_THREAD_LIBS_INIT}"
)
ADD_DEPENDENCIES(GTest GTestExternal)
ADD_DEPENDENCIES(GTestMain GTestExternal)
EXTERNALPROJECT_GET_PROPERTY(GTestExternal source_dir)
INCLUDE_DIRECTORIES(
BEFORE ${GTEST_PREFIX}/src/GTestExternal/googletest/include
${Boost_INCLUDE_DIRS}
)
endif()
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_INSTALL_MESSAGE ALWAYS)
if (LIBSERIAL_BUILD_EXAMPLES)
ADD_SUBDIRECTORY(examples)
endif()
if (LIBSERIAL_PYTHON_ENABLE)
ADD_SUBDIRECTORY(sip)
endif()
ADD_SUBDIRECTORY(src)
if (LIBSERIAL_ENABLE_TESTING)
ADD_SUBDIRECTORY(test)
endif()
#
# Create pkg-config file for cmake builds as well as autotool builds
#
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${CMAKE_INSTALL_PREFIX})
set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(includedir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
set(VERSION ${PROJECT_VERSION})
configure_file(libserial.pc.in libserial.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libserial.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
if (LIBSERIAL_BUILD_DOCS)
CONFIGURE_FILE(
${CMAKE_CURRENT_SOURCE_DIR}/doxygen.conf.in
${CMAKE_CURRENT_BINARY_DIR}/doxygen.conf.in @ONLY
)
ADD_CUSTOM_TARGET(docs ALL
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doxygen.conf.in
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif()
#
# Packaging support
#
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
add_subdirectory(packaging)
endif()