Skip to content

Commit

Permalink
Cmake (#13)
Browse files Browse the repository at this point in the history
* Using cmake target export

* CPack configuration

* Updated doxygen-awesome-css to v2.3.1

* Fixed wrong project title. Whoops.

* Removed "lib" prefix from project name, as CMake adds this automatically

* Updated maintainer information

* Updated README

* Added lib prefix to cpack package name

* Updated gitignore

* Added new "docs" target for HTML Doxygen generation

* Updated CPack config to generate .debs, .rpms, etc.

* Added new workflow

---------

Co-authored-by: Simon Cahill <simon@simonc.eu>
  • Loading branch information
ErniBrown and SimonCahill committed Feb 21, 2024
1 parent c520dcc commit 621383e
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 20 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/cpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CMake

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest

steps:
- name: Install system dependencies
if: runner.os == 'Linux'
run: |
sudo apt update
sudo apt-get update
sudo apt-get -y install can-utils libsocketcan-dev
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Pack
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: cpack .

10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
###
build/

###
# Documentation (Doxygen)
###
html/

###
# cpack packages
###
packages/

###
# .vscode
###
Expand Down
56 changes: 40 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.23)

project(sockcanpp LANGUAGES CXX VERSION 1.00)
project(sockcanpp LANGUAGES CXX VERSION 1.0)

option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" ON)

set(CMAKE_CXX_STANDARD 11)
include(GNUInstallDirs)

include_directories(
include/
)
add_library(${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")

file(GLOB_RECURSE FILES ${CMAKE_CURRENT_SOURCE_DIR} src/*.cpp)
add_subdirectory(include)
add_subdirectory(src)

add_library(${PROJECT_NAME} ${FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION})
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
FILE_SET HEADERS
)

target_include_directories(
${PROJECT_NAME}
install(EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME}
)

INTERFACE include/
INTERFACE include/exceptions
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_NAME}ConfigVersion.cmake"

This comment has been minimized.

Copy link
@sikmir

sikmir Mar 6, 2024

lib prefix missed. lib${PROJECT_NAME}ConfigVersion.cmake is expected bellow.

This comment has been minimized.

Copy link
@SimonCahill

SimonCahill Mar 6, 2024

Author Owner

Thanks for the head's up!

VERSION ${${PROJECT_NAME}_VERSION}
COMPATIBILITY AnyNewerVersion
)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
install(FILES "cmake/lib${PROJECT_NAME}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION lib/cmake/${PROJECT_NAME})


set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
Expand All @@ -34,3 +44,17 @@ set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
configure_file(libsockcanpp.pc.in ${CMAKE_BINARY_DIR}/libsockcanpp.pc @ONLY)
# Install pkg-config files
install(FILES ${CMAKE_BINARY_DIR}/libsockcanpp.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

include(cmake/cpack.cmake)

###
# Docs target
###
add_custom_target("docs" COMMENT "Create Doxygen documentation")
add_custom_command(
TARGET "docs"
POST_BUILD
COMMENT "Generate Doxygen documentation for publication or reading"
COMMAND doxygen ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Project related configuration options
#---------------------------------------------------------------------------
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "Waveshare 8ch Relay Board Example"
PROJECT_NAME = "Libsockcanpp"
PROJECT_NUMBER =
PROJECT_BRIEF = "A complete C++ wrapper around socketcan."
PROJECT_LOGO =
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ libsockcanpp is a socketcan wrapper library for C++, aimed to be easy to use wit

libsockcanpp was designed with use in CMake projects, but it can also easily be integrated into existing Makefile projects, as long as cmake is present on the build system.

1) clone the repository: `git clone https://github.com/Beatsleigher/libsockcanpp.git`
1) clone the repository: `git clone https://github.com/SimonCahill/libsockcanpp.git`
2) create build directory: `mkdir build && cd build`
3) generate build files: `cmake .. -DCMAKE_TOOLCHAIN_FILE=../toolchains/desired-toolchain.cmake`
4) building library: `make -j`

## Incorporating into Cmake projects:

1) clone the repository: `git clone https://github.com/Beatsleigher/libsockcanpp.git`
1) clone the repository: `git clone https://github.com/SimonCahill/libsockcanpp.git`
2) add the following to CMakeLists.txt
```cmake
if (NOT TARGET sockcanpp)
Expand Down
43 changes: 43 additions & 0 deletions cmake/cpack.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# these are cache variables, so they could be overwritten with -D,
set(CPACK_PACKAGE_NAME lib${PROJECT_NAME}
CACHE STRING "The resulting package name"
)

# which is useful in case of packing only selected components instead of the whole thing
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A socketcan wrapper library for C++"
CACHE STRING "Package description for the package metadata"
)

set(CPACK_VERBATIM_VARIABLES YES)

set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
SET(CPACK_OUTPUT_FILE_PREFIX "${CMAKE_SOURCE_DIR}/packages")

# https://unix.stackexchange.com/a/11552/254512
#set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/some")#/${CMAKE_PROJECT_VERSION}")

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_PACKAGE_CONTACT "contact@simonc.eu")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Simon Cahill")

set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")

# package name for deb. If set, then instead of some-application-0.9.2-Linux.deb
# you'll get some-application_0.9.2_amd64.deb (note the underscores too)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)

# that is if you want every group to have its own package,
# although the same will happen if this is not set (so it defaults to ONE_PER_GROUP)
# and CPACK_DEB_COMPONENT_INSTALL is set to YES
set(CPACK_COMPONENTS_GROUPING ALL_COMPONENTS_IN_ONE)#ONE_PER_GROUP)

# without this you won't be able to pack only specified component
set(CPACK_DEB_COMPONENT_INSTALL YES)

set(CPACK_GENERATOR TGZ DEB TZ STGZ RPM ZIP)

include(CPack)
3 changes: 3 additions & 0 deletions cmake/libsockcanppConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# include(CMakeFindDependencyMacro)
# find_dependency(xxx 2.0)
include(${CMAKE_CURRENT_LIST_DIR}/libsockcanppTargets.cmake)
2 changes: 1 addition & 1 deletion doxygen-awesome-css
10 changes: 10 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.23)

target_sources(${PROJECT_NAME}
PUBLIC FILE_SET HEADERS
BASE_DIRS ${CMAKE_CURRENT_LIST_DIR}
FILES
CanDriver.hpp
CanId.hpp
CanMessage.hpp
)
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target_sources(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/CanDriver.cpp
)

0 comments on commit 621383e

Please sign in to comment.