forked from andershub4/libmbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
296 lines (246 loc) · 10.9 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
cmake_minimum_required(VERSION 3.5)
project(
libmbus
LANGUAGES CXX C
VERSION "0.9.0")
if(CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "CMAKE_BUILD_TYPE empty setting to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()
# ##############################################################################
# default options -> changed with e.g. cd build && cmake ..
# -DLIBMBUS_BUILD_TESTS=ON
# ##############################################################################
option(LIBMBUS_BUILD_EXAMPLES "build examples" OFF)
option(LIBMBUS_BUILD_TESTS "build tests" OFF)
option(LIBMBUS_ENABLE_COVERAGE "build with coverage support" OFF)
option(LIBMBUS_RUN_CLANG_TIDY "use Clang-Tidy for static analysis" OFF)
option(LIBMBUS_PACKAGE_DEB "build debian package" OFF)
option(LIBMBUS_PACKAGE_RPM "build rpm package" OFF)
option(LIBMBUS_BUILD_DOCS "build documentation" OFF)
option(BUILD_SHARED_LIBS "build shared lib" ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 11)
# Append our module directory to CMake
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_BINARY_DIR}")
# Set the output of the libraries and executables.
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# ##############################################################################
# static analysis
# ##############################################################################
if(LIBMBUS_RUN_CLANG_TIDY)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "/usr/bin/clang-tidy")
if(NOT CLANG_TIDY_EXE)
message(WARNING "clang-tidy not found.")
else()
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
endif()
endif(LIBMBUS_RUN_CLANG_TIDY)
if(LIBMBUS_ENABLE_COVERAGE)
if(NOT CMAKE_BUILD_TYPE MATCHES "(Debug)|(RelWithDebInfo)")
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# using Clang
message(STATUS "Not doing coverage...")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# using GCC
message(STATUS "Building with code coverage...")
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -ggdb3 -O0 --coverage -fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -ggdb3 -O0 --coverage -fprofile-arcs -ftest-coverage ")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage ")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage ")
link_libraries(-lgcov)
endif()
endif()
include(CheckIncludeFile)
check_include_file(dlfcn.h HAVE_DLFCN_H)
check_include_file(inttypes.h HAVE_INTTYPES_H)
check_include_file(memory.h HAVE_MEMORY_H)
check_include_file(stdlib.h HAVE_STDINT_H)
check_include_file(stdint.h HAVE_STDLIB_H)
check_include_file(strings.h HAVE_STRINGS_H)
check_include_file(string.h HAVE_STRING_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(unistd.h HAVE_UNISTD_H)
# ##############################################################################
# library
# ##############################################################################
set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
set(PACKAGE_VERSION "${PROJECT_VERSION}")
set(VERSION "${PROJECT_VERSION}")
configure_file(${CMAKE_CURRENT_LIST_DIR}/mbus/config.h.in ${CMAKE_CURRENT_LIST_DIR}/config.h @ONLY)
add_library(
${PROJECT_NAME}
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol.c"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol.h"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-tcp.c"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-tcp.h"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus.c"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus.h"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol-aux.c"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol-aux.h"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-serial.c"
"${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-serial.h")
target_include_directories(
${PROJECT_NAME}
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_libraries(${PROJECT_NAME} PRIVATE m)
endif()
if(NOT MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-pedantic)
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
if(CLANG_TIDY_EXE)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif()
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
# ##############################################################################
# examples
# ##############################################################################
if(LIBMBUS_BUILD_EXAMPLES)
message(STATUS "building examples")
add_subdirectory(bin)
endif()
# ##############################################################################
# tests
# ##############################################################################
if(LIBMBUS_BUILD_TESTS)
message(STATUS "building tests")
enable_testing()
add_subdirectory(test)
endif()
# ##############################################################################
# install
# ##############################################################################
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(INSTALL_PKGCONFIG_DIR
"${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig"
CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_INC_DIR
"${CMAKE_INSTALL_INCLUDEDIR}/mbus"
CACHE PATH "Installation directory for headers")
set(INSTALL_LIB_DIR
"${CMAKE_INSTALL_LIBDIR}"
CACHE PATH "Installation directory for libraries")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libmbus.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libmbus.pc @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/libmbus.pc
DESTINATION "${INSTALL_PKGCONFIG_DIR}"
COMPONENT dev)
set(LIBMBUS_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT dev
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT lib)
install(
EXPORT ${PROJECT_NAME}Targets
DESTINATION "${LIBMBUS_CONFIG_INSTALL_DIR}"
NAMESPACE ${PROJECT_NAME}::
COMPONENT dev)
configure_package_config_file(cmake/Config.cmake.in ${PROJECT_NAME}Config.cmake INSTALL_DESTINATION
"${LIBMBUS_CONFIG_INSTALL_DIR}")
write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake COMPATIBILITY SameMajorVersion)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION "${LIBMBUS_CONFIG_INSTALL_DIR}"
COMPONENT dev)
install(
DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/mbus/"
DESTINATION "${INSTALL_INC_DIR}"
COMPONENT dev
FILES_MATCHING
PATTERN "*.h")
# ##############################################################################
# package
# mkdir build ; cd build ; cmake .. -DLIBMBUS_PACKAGE_DEB=ON ; cpack ..
# ##############################################################################
include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Open source M-bus (Meter-Bus) library.")
set(CPACK_PACKAGE_DESCRIPTION
"libmbus is an open source library for the M-bus (Meter-Bus) protocol.
The Meter-Bus is a standard for reading out meter data from electricity meters,
heat meters, gas meters, etc. The M-bus standard deals with both the electrical
signals on the M-Bus, and the protocol and data format used in transmissions
on the M-Bus. The role of libmbus is to decode/encode M-bus data, and to handle
the communication with M-Bus devices.
For more information see http://www.rscada.se/libmbus")
set(CPACK_PACKAGE_VENDOR "Raditex Control AB")
set(CPACK_PACKAGE_CONTACT "Stefan Wahren <[email protected]>")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/rscada/libmbus/")
set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE)
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_PACKAGE_DEBUG ON)
set(CPACK_PACKAGE_RELEASE 1)
# create 2 components, libmbus, libmbus-dev
set(CPACK_COMPONENTS_ALL lib dev)
set(CPACK_COMPONENT_LIB_DESCRIPTION "FreeSCADA M-Bus Library.
A free and open-source library for M-Bus (Meter Bus) from the rSCADA project.")
set(CPACK_DEBIAN_LIB_PACKAGE_SECTION libs)
set(CPACK_COMPONENT_DEVEL_DESCRIPTION
"FreeSCADA M-Bus Library Development files.
A free and open-source library for M-Bus (Meter Bus) from the rSCADA project,
including development files.")
set(CPACK_DEBIAN_DEVEL_PACKAGE_SECTION libdevel)
set(CPACK_COMPONENT_DEVEL_DEPENDS lib)
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
set(CPACK_PACKAGE_FILE_NAME
"${CMAKE_PROJECT_NAME}_${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
if(LIBMBUS_PACKAGE_DEB)
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Stefan Wahren <[email protected]>")
set(CPACK_DEBIAN_PACKAGE_SECTION "Development/Languages/C and C++")
set(CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
set(CPACK_DEBIAN_PACKAGE_VERSION
"${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-${CPACK_PACKAGE_RELEASE}"
)
endif()
if(LIBMBUS_PACKAGE_RPM)
set(CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_LICENSE "BSD")
endif()
include(CPack)
# ##############################################################################
# doc
# mkdir build ; cd build ; cmake .. -DLIBMBUS_BUILD_DOCS=ON ; cmake --build . --target doc
# ##############################################################################
if(LIBMBUS_BUILD_DOCS)
message(STATUS "building with documentation")
# Generate targets for documentation
# check if Doxygen is installed
find_package(Doxygen)
if(Doxygen_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
# note the option ALL which allows to build the docs together with the application
add_custom_target(
doc ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
message(STATUS "Setup up the Doxygen documention build")
else(Doxygen_FOUND)
message(WARNING "Doxygen need to be installed to generate the doxygen documentation")
endif(Doxygen_FOUND)
endif()