Skip to content

Commit

Permalink
build: add cmake support
Browse files Browse the repository at this point in the history
feat: add cmake support

chore: remove old code

revert: remove mbus_data_record_unit

build: do not break existing building system
  • Loading branch information
gocarlos committed Mar 20, 2020
1 parent 62ac367 commit cf3b616
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 21 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ test/Makefile.in
/compile
config.guess
config.sub
config.h.in
configure
/depcomp
/install-sh
Expand Down Expand Up @@ -72,3 +71,9 @@ test/test-frames/*.xml.new
test/error-frames/*.xml.new
test/unsupported-frames/*.xml.new

/build/
_build/

# IDE
/.vscode/
CMakeLists.txt.user
150 changes: 150 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
cmake_minimum_required(VERSION 3.8)

project(libmbus LANGUAGES CXX C)

set(PROJECT_VERSION "0.9.0")

if(CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "CMAKE_BUILD_TYPE empty setting to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()

option(LIBMBUS_BUILD_EXAMPLES "build examples" OFF)
option(LIBMBUS_BUILD_TESTS "build tests" OFF)

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)
list(APPEND CMAKE_MODULE_PATH ${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}")
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif()
endif(LIBMBUS_RUN_CLANG_TIDY)

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)

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}>)
target_link_libraries(${PROJECT_NAME} PRIVATE m)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-pedantic)

if(CLANG_TIDY_EXE)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_CLANG_TIDY
"${DO_CLANG_TIDY}")
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(LIBMBUS_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 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)

# BUG: installing empty dirs https://gitlab.kitware.com/cmake/cmake/issues/17122
install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/mbus/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mbus/
COMPONENT dev
FILES_MATCHING
PATTERN "*.h")
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 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>
# 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>

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

Expand All @@ -8,4 +10,32 @@ 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.


## BUILD

with cmake

```bash
rm -rf _build
mkdir _build
cd _build
# configure
# e.g. on linux
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON
# e.g. for a target device
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/foo-bar-baz.cmake
# compile
cmake --build . -j
# install - optional
cmake --build . --target install
```

## CONSUME

```cmake
find_package(libmbus)
add_executable(my_app main.cpp)
target_link_libraries(my_app libmbus::libmbus)
```

For more information see http://www.rscada.se/libmbus
19 changes: 19 additions & 0 deletions bin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function(add_example SRCS)
add_executable(${SRCS} ${CMAKE_CURRENT_LIST_DIR}/${SRCS}.c)
target_link_libraries(${SRCS} PRIVATE libmbus::libmbus)
endfunction()

add_example(mbus-serial-request-data)
add_example(mbus-serial-request-data-multi-reply)
add_example(mbus-serial-scan)
add_example(mbus-serial-scan-secondary)
add_example(mbus-serial-select-secondary)
add_example(mbus-serial-set-address)
add_example(mbus-serial-switch-baudrate)
add_example(mbus-tcp-application-reset)
add_example(mbus-tcp-raw-send)
add_example(mbus-tcp-request-data)
add_example(mbus-tcp-request-data-multi-reply)
add_example(mbus-tcp-scan)
add_example(mbus-tcp-scan-secondary)
add_example(mbus-tcp-select-secondary)
24 changes: 5 additions & 19 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
#!/bin/sh
#

if [ -f Makefile ]; then
# use existing automake files
echo >> /dev/null
else
# regenerate automake files
echo "Running autotools..."

autoheader \
&& aclocal \
&& case \
$(uname) in Darwin*) glibtoolize --ltdl --copy --force ;; \
*) libtoolize --ltdl --copy --force ;; esac \
&& automake --add-missing --copy \
&& autoconf \
&& ./configure
fi

make
rm -rf _build
mkdir _build
cd _build
cmake .. -DLIBMBUS_BUILD_EXAMPLES=ON -DLIBMBUS_BUILD_TESTS=ON
cmake --build . -j
4 changes: 4 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
check_required_components("@PROJECT_NAME@")
59 changes: 59 additions & 0 deletions mbus/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */

/* Define to 1 if you have the <dlfcn.h> header file. */
#cmakedefine HAVE_DLFCN_H "@HAVE_DLFCN_H@"

/* Define to 1 if you have the <inttypes.h> header file. */
#cmakedefine HAVE_INTTYPES_H "@HAVE_INTTYPES_H@"

/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H "@HAVE_MEMORY_H@"

/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H "@HAVE_STDINT_H@"

/* Define to 1 if you have the <stdlib.h> header file. */
#cmakedefine HAVE_STDLIB_H "@HAVE_STDLIB_H@"

/* Define to 1 if you have the <strings.h> header file. */
#cmakedefine HAVE_STRINGS_H "@HAVE_STRINGS_H@"

/* Define to 1 if you have the <string.h> header file. */
#cmakedefine HAVE_STRING_H "@HAVE_STRING_H@"

/* Define to 1 if you have the <sys/stat.h> header file. */
#cmakedefine HAVE_SYS_STAT_H "@HAVE_SYS_STAT_H@"

/* Define to 1 if you have the <sys/types.h> header file. */
#cmakedefine HAVE_SYS_TYPES_H "@HAVE_SYS_TYPES_H@"

/* Define to 1 if you have the <unistd.h> header file. */
#cmakedefine HAVE_UNISTD_H "@HAVE_UNISTD_H@"

/* Define to the sub-directory where libtool stores uninstalled libraries. */
#define LT_OBJDIR ".libs/"

/* Name of package */
#define PACKAGE "libmbus"

/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "[email protected]"

/* Define to the full name of this package. */
#define PACKAGE_NAME "libmbus"

/* Define to the full name and version of this package. */
#cmakedefine PACKAGE_STRING "@PACKAGE_STRING@"

/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "libmbus"

/* Define to the home page for this package. */
#define PACKAGE_URL "http://www.rscada.se/libmbus/"

/* Define to the version of this package. */
#cmakedefine PACKAGE_VERSION "@PACKAGE_VERSION@"

/* Version number of package */
#cmakedefine VERSION "@PACKAGE_VERSION@"
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(mbus_parse ${CMAKE_CURRENT_LIST_DIR}/mbus_parse.c)
target_link_libraries(mbus_parse PRIVATE libmbus::libmbus)

add_executable(mbus_parse_hex ${CMAKE_CURRENT_LIST_DIR}/mbus_parse_hex.c)
target_link_libraries(mbus_parse_hex PRIVATE libmbus::libmbus)

0 comments on commit cf3b616

Please sign in to comment.