Skip to content

Commit 42caa33

Browse files
committed
build: add cmake support
feat: add cmake support chore: remove old code revert: remove mbus_data_record_unit
1 parent 62ac367 commit 42caa33

9 files changed

+280
-22
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ test/Makefile.in
1010
/compile
1111
config.guess
1212
config.sub
13-
config.h.in
1413
configure
1514
/depcomp
1615
/install-sh
@@ -72,3 +71,9 @@ test/test-frames/*.xml.new
7271
test/error-frames/*.xml.new
7372
test/unsupported-frames/*.xml.new
7473

74+
/build/
75+
_build/
76+
77+
# IDE
78+
/.vscode/
79+
CMakeLists.txt.user

CMakeLists.txt

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
3+
project(libmbus LANGUAGES CXX C)
4+
5+
set(PROJECT_VERSION "0.9.0")
6+
7+
if(CMAKE_BUILD_TYPE STREQUAL "")
8+
message(STATUS "CMAKE_BUILD_TYPE empty setting to Debug")
9+
set(CMAKE_BUILD_TYPE "Debug")
10+
endif()
11+
12+
option(LIBMBUS_BUILD_EXAMPLES "build examples" OFF)
13+
option(LIBMBUS_BUILD_TESTS "build tests" OFF)
14+
15+
set(CMAKE_CXX_STANDARD 17)
16+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
17+
set(CMAKE_CXX_EXTENSIONS OFF)
18+
set(CMAKE_C_STANDARD 11)
19+
20+
# Append our module directory to CMake
21+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
22+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
23+
24+
# Set the output of the libraries and executables.
25+
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
26+
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
27+
28+
#
29+
# static analysis
30+
#
31+
32+
if(LIBMBUS_RUN_CLANG_TIDY)
33+
find_program(
34+
CLANG_TIDY_EXE
35+
NAMES "clang-tidy"
36+
DOC "/usr/bin/clang-tidy")
37+
if(NOT CLANG_TIDY_EXE)
38+
message(WARNING "clang-tidy not found.")
39+
else()
40+
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
41+
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}")
42+
endif()
43+
endif(LIBMBUS_RUN_CLANG_TIDY)
44+
45+
include(CheckIncludeFile)
46+
47+
check_include_file(dlfcn.h HAVE_DLFCN_H)
48+
check_include_file(inttypes.h HAVE_INTTYPES_H)
49+
check_include_file(memory.h HAVE_MEMORY_H)
50+
check_include_file(stdlib.h HAVE_STDINT_H)
51+
check_include_file(stdint.h HAVE_STDLIB_H)
52+
check_include_file(strings.h HAVE_STRINGS_H)
53+
check_include_file(string.h HAVE_STRING_H)
54+
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
55+
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
56+
check_include_file(unistd.h HAVE_UNISTD_H)
57+
58+
#
59+
# library
60+
#
61+
set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
62+
63+
set(PACKAGE_VERSION "${PROJECT_VERSION}")
64+
set(VERSION "${PROJECT_VERSION}")
65+
configure_file(${CMAKE_CURRENT_LIST_DIR}/mbus/config.h.in
66+
${CMAKE_CURRENT_BINARY_DIR}/mbus/config.h)
67+
68+
add_library(
69+
${PROJECT_NAME}
70+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol.c
71+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol.h
72+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-tcp.c
73+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-tcp.h
74+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus.c
75+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus.h
76+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol-aux.c
77+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-protocol-aux.h
78+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-serial.c
79+
${CMAKE_CURRENT_LIST_DIR}/mbus/mbus-serial.h)
80+
target_include_directories(
81+
${PROJECT_NAME}
82+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
83+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
84+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
85+
target_link_libraries(${PROJECT_NAME} PRIVATE m)
86+
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-pedantic)
87+
88+
if(CLANG_TIDY_EXE)
89+
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_CLANG_TIDY
90+
"${DO_CLANG_TIDY}")
91+
endif()
92+
93+
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
94+
95+
#
96+
# examples
97+
#
98+
99+
if(LIBMBUS_BUILD_EXAMPLES)
100+
message(STATUS "building examples")
101+
add_subdirectory(bin)
102+
endif()
103+
104+
#
105+
# tests
106+
#
107+
108+
if(LIBMBUS_BUILD_TESTS)
109+
message(STATUS "building tests")
110+
enable_testing()
111+
add_subdirectory(test)
112+
endif()
113+
114+
#
115+
# install
116+
#
117+
118+
include(GNUInstallDirs)
119+
include(CMakePackageConfigHelpers)
120+
121+
set(LIBMBUS_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
122+
install(
123+
TARGETS ${PROJECT_NAME}
124+
EXPORT ${PROJECT_NAME}Targets
125+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
126+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib)
127+
128+
install(
129+
EXPORT ${PROJECT_NAME}Targets
130+
DESTINATION ${LIBMBUS_CONFIG_INSTALL_DIR}
131+
NAMESPACE ${PROJECT_NAME}::
132+
COMPONENT dev)
133+
134+
configure_package_config_file(cmake/Config.cmake.in ${PROJECT_NAME}Config.cmake
135+
INSTALL_DESTINATION ${LIBMBUS_CONFIG_INSTALL_DIR})
136+
write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake
137+
COMPATIBILITY SameMajorVersion)
138+
install(
139+
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
140+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
141+
DESTINATION ${LIBMBUS_CONFIG_INSTALL_DIR}
142+
COMPONENT dev)
143+
144+
# BUG: installing empty dirs https://gitlab.kitware.com/cmake/cmake/issues/17122
145+
install(
146+
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/mbus/
147+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mbus/
148+
COMPONENT dev
149+
FILES_MATCHING
150+
PATTERN "*.h")

README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# libmbus: M-bus Library from Raditex Control (http://www.rscada.se) <span style="float:right;"><a href="https://travis-ci.org/rscada/libmbus" style="border-bottom:none">![Build Status](https://travis-ci.org/rscada/libmbus.svg?branch=master)</a></span>
1+
# libmbus: M-bus Library from Raditex Control (http://www.rscada.se) <span style="float:right;"><a href="https://travis-ci.org/rscada/libmbus" style="border-bottom:none">
2+
3+
![Build Status](https://travis-ci.org/rscada/libmbus.svg?branch=master)</a></span>
24

35
libmbus is an open source library for the M-bus (Meter-Bus) protocol.
46

@@ -8,4 +10,32 @@ signals on the M-Bus, and the protocol and data format used in transmissions on
810
the M-Bus. The role of libmbus is to decode/encode M-bus data, and to handle
911
the communication with M-Bus devices.
1012

13+
14+
## BUILD
15+
16+
with cmake
17+
18+
```bash
19+
rm -rf _build
20+
mkdir _build
21+
cd _build
22+
# configure
23+
# e.g. on linux
24+
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON
25+
# e.g. for a target device
26+
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/foo-bar-baz.cmake
27+
# compile
28+
cmake --build . -j
29+
# install - optional
30+
cmake --build . --target install
31+
```
32+
33+
## CONSUME
34+
35+
```cmake
36+
find_package(libmbus)
37+
add_executable(my_app main.cpp)
38+
target_link_libraries(my_app libmbus::libmbus)
39+
```
40+
1141
For more information see http://www.rscada.se/libmbus

bin/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function(add_example SRCS)
2+
add_executable(${SRCS} ${CMAKE_CURRENT_LIST_DIR}/${SRCS}.c)
3+
target_link_libraries(${SRCS} PRIVATE libmbus::libmbus)
4+
endfunction()
5+
6+
add_example(mbus-serial-request-data)
7+
add_example(mbus-serial-request-data-multi-reply)
8+
add_example(mbus-serial-scan)
9+
add_example(mbus-serial-scan-secondary)
10+
add_example(mbus-serial-select-secondary)
11+
add_example(mbus-serial-set-address)
12+
add_example(mbus-serial-switch-baudrate)
13+
add_example(mbus-tcp-application-reset)
14+
add_example(mbus-tcp-raw-send)
15+
add_example(mbus-tcp-request-data)
16+
add_example(mbus-tcp-request-data-multi-reply)
17+
add_example(mbus-tcp-scan)
18+
add_example(mbus-tcp-scan-secondary)
19+
add_example(mbus-tcp-select-secondary)

build.sh

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
#!/bin/sh
2-
#
32

4-
if [ -f Makefile ]; then
5-
# use existing automake files
6-
echo >> /dev/null
7-
else
8-
# regenerate automake files
9-
echo "Running autotools..."
10-
11-
autoheader \
12-
&& aclocal \
13-
&& case \
14-
$(uname) in Darwin*) glibtoolize --ltdl --copy --force ;; \
15-
*) libtoolize --ltdl --copy --force ;; esac \
16-
&& automake --add-missing --copy \
17-
&& autoconf \
18-
&& ./configure
19-
fi
20-
21-
make
3+
rm -rf _build
4+
mkdir _build
5+
cd _build
6+
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON -DLIBMBUS_BUILD_TESTS=ON
7+
cmake --build . -j

cmake/Config.cmake.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
4+
check_required_components("@PROJECT_NAME@")

mbus/config.h.in

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* config.h. Generated from config.h.in by configure. */
2+
/* config.h.in. Generated from configure.ac by autoheader. */
3+
4+
/* Define to 1 if you have the <dlfcn.h> header file. */
5+
#cmakedefine HAVE_DLFCN_H "@HAVE_DLFCN_H@"
6+
7+
/* Define to 1 if you have the <inttypes.h> header file. */
8+
#cmakedefine HAVE_INTTYPES_H "@HAVE_INTTYPES_H@"
9+
10+
/* Define to 1 if you have the <memory.h> header file. */
11+
#cmakedefine HAVE_MEMORY_H "@HAVE_MEMORY_H@"
12+
13+
/* Define to 1 if you have the <stdint.h> header file. */
14+
#cmakedefine HAVE_STDINT_H "@HAVE_STDINT_H@"
15+
16+
/* Define to 1 if you have the <stdlib.h> header file. */
17+
#cmakedefine HAVE_STDLIB_H "@HAVE_STDLIB_H@"
18+
19+
/* Define to 1 if you have the <strings.h> header file. */
20+
#cmakedefine HAVE_STRINGS_H "@HAVE_STRINGS_H@"
21+
22+
/* Define to 1 if you have the <string.h> header file. */
23+
#cmakedefine HAVE_STRING_H "@HAVE_STRING_H@"
24+
25+
/* Define to 1 if you have the <sys/stat.h> header file. */
26+
#cmakedefine HAVE_SYS_STAT_H "@HAVE_SYS_STAT_H@"
27+
28+
/* Define to 1 if you have the <sys/types.h> header file. */
29+
#cmakedefine HAVE_SYS_TYPES_H "@HAVE_SYS_TYPES_H@"
30+
31+
/* Define to 1 if you have the <unistd.h> header file. */
32+
#cmakedefine HAVE_UNISTD_H "@HAVE_UNISTD_H@"
33+
34+
/* Define to the sub-directory where libtool stores uninstalled libraries. */
35+
#define LT_OBJDIR ".libs/"
36+
37+
/* Name of package */
38+
#define PACKAGE "libmbus"
39+
40+
/* Define to the address where bug reports for this package should be sent. */
41+
#define PACKAGE_BUGREPORT "[email protected]"
42+
43+
/* Define to the full name of this package. */
44+
#define PACKAGE_NAME "libmbus"
45+
46+
/* Define to the full name and version of this package. */
47+
#cmakedefine PACKAGE_STRING "@PACKAGE_STRING@"
48+
49+
/* Define to the one symbol short name of this package. */
50+
#define PACKAGE_TARNAME "libmbus"
51+
52+
/* Define to the home page for this package. */
53+
#define PACKAGE_URL "http://www.rscada.se/libmbus/"
54+
55+
/* Define to the version of this package. */
56+
#cmakedefine PACKAGE_VERSION "@PACKAGE_VERSION@"
57+
58+
/* Version number of package */
59+
#cmakedefine VERSION "@PACKAGE_VERSION@"

mbus/mbus.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//------------------------------------------------------------------------------
1010

1111
#include "mbus-protocol.h"
12-
#include "../config.h"
12+
#include "mbus/config.h"
1313

1414
//
1515
//

test/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(mbus_parse ${CMAKE_CURRENT_LIST_DIR}/mbus_parse.c)
2+
target_link_libraries(mbus_parse PRIVATE libmbus::libmbus)
3+
4+
add_executable(mbus_parse_hex ${CMAKE_CURRENT_LIST_DIR}/mbus_parse_hex.c)
5+
target_link_libraries(mbus_parse_hex PRIVATE libmbus::libmbus)

0 commit comments

Comments
 (0)