From 25dfde5c0a74186d92de522e1cb949a485a4a130 Mon Sep 17 00:00:00 2001 From: Simon Cahill Date: Thu, 23 Nov 2023 11:53:08 +0100 Subject: [PATCH 1/3] Refactored CMakeLists to add more flexibility --- CMakeLists.txt | 51 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 162db5f..2b8cfb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,17 +3,54 @@ cmake_minimum_required(VERSION 3.10) ### # Project definition ### -project(isotp LANGUAGES C) +project(isotp LANGUAGES C VERSION 1.0.0 DESCRIPTION "A platform-agnostic ISOTP implementation in C for embedded devices.") + +option(isotpc_USE_INCLUDE_DIR "Copy header files to separate include directory in current binary dir for better separation of header files to combat potential naming conflicts." OFF) +option(isotpc_STATIC_LIBRARY "Compile libisotpc as a static library, instead of a shared library." OFF) + +if (isotpc_STATIC_LIBRARY) + add_library(isotp STATIC ${CMAKE_CURRENT_SOURCE_DIR}/isotp.c) +else() + add_library(isotp SHARED ${CMAKE_CURRENT_SOURCE_DIR}/isotp.c) +endif() ### -# Get all include directories +# Strict building policies ### -include_directories( - ${CMAKE_CURRENT_SOURCE_DIR}/ +target_compile_options( + isotp PUBLIC + -fPIC + -Werror + -Wall + -Wno-unknown-pragmas # ignore unknown pragmas, such as #pragma region ) ### -# Compile isotp as Shared Lib +# Check for debug builds +### +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(isotp PUBLIC -O0 -g) # don't optimise and add debugging symbols +else() + target_compile_options(isotp PUBLIC -O2 -s) # optimise code and strip the library +endif() + +if (isotpc_USE_INCLUDE_DIR) + file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/isotp_c) + + file(GLOB HEADERS FOLLOW_SYMLINKS ${CMAKE_CURRENT_SOURCE_DIR}/*.h) + file(COPY ${HEADERS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include/isotp_c/) + + target_include_directories(isotp PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include) + +else() + target_include_directories(isotp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +endif() + +### +# Add library aliases ### -add_library(isotp SHARED - isotp.c) \ No newline at end of file +add_library(isotpc ALIAS isotp) +add_library(isotp_c ALIAS isotp) +add_library(simon_cahill::isotp ALIAS isotp) +add_library(simon_cahill::isotpc ALIAS isotp) +add_library(simon_cahill::isotp_c ALIAS isotp) From a955bd76509a9162dfbde2285bf4764ff65a8b84 Mon Sep 17 00:00:00 2001 From: Simon Cahill Date: Thu, 23 Nov 2023 11:53:26 +0100 Subject: [PATCH 2/3] Updated README to reflect changes to CMakeLists.txt --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/README.md b/README.md index 35a19e0..6c5756f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,77 @@ This library doesn't assume anything about the source of the ISO-TP messages or ### Master Build [![CMake](https://github.com/SimonCahill/isotp-c/actions/workflows/cmake.yml/badge.svg)](https://github.com/SimonCahill/isotp-c/actions/workflows/cmake.yml) +## Building ISOTP-C + +This library may be built using either straight Makefiles, or using CMake. + +### make +To build this library using Make, simply call: + +```bash +$ make all +``` + +### CMake + +The CMake build system allows for more flexibility at generation and build time, so it is recommended you use this for building this library. +Of course, if your project does not use CMake, you don't *have* to use it. +If your projects use a different build system, you are more than welcome to include it in this repository. + +The Makefile generator for isotpc will automatically detect whether or not your build system is using the `Debug` or `Release` build type and will adjust compiler parameters accordingly. + +> **NOTE**: Regardless of build type, the library will be compiled as position-independant code. + +#### Debug Build +If your project is configured to build as `Debug`, then the library will be compiled with **no** optimisations and **with** debug symbols. +`-DCMAKE_BUILD_TYPE=Debug` + +#### Release Build +If your project is configured to build as `Release`, then the library code will be **optimised** using `-O2` and will be **stripped**. +`-DCMAKE_BUILD_TYPE=Release` + +#### External Include Directories +It is generally considered good practice to segregate header files from each other, depending on the project. For this reason, you may opt in to this behaviour for this library. + +If you pass `-Disotpc_USE_INCLUDE_DIR=ON` on the command-line, or you set `set(isotpc_USE_INCLUDE_DIR ON CACHE BOOL "Use external include dir for isotp-c")` in your CMakeLists.txt, then a separate `include/` directory will +be added to the project. +This happens at generation time, and the CMake project will automatically reference `${CMAKE_CURRENT_BINARY_DIR}/include` as the include directory for the project. This will be propagated to your projects, too. + +In your code: + +```c +// if -Disotpc_USE_INCLUDE_DIR=ON +#include + +// else +#include +``` + +#### Static Library +In some cases, it is required that a static library be used instead of a shared library. +isotp-c supports this also, via options. + +Either pass `-Disotpc_STATIC_LIBRARY=ON` via command-line or `set(isotpc_STATIC_LIBRARY ON CACHE BOOL "Enable static library for isotp-c")` in your CMakeLists.txt and the library will be built as a static library (`*.a|*.lib`) for your project to include. + +#### Inclusion in your CMake project +```cmake +### +# Set your desired options +### +set(isotpc_USE_INCLUDE_DIR ON CACHE BOOL "Use external include directory for isotp-c") # optional +set(isotpc_STATIC_LIBRARY ON CACHE BOOL "Build isotp-c as a static library instead of shared") # optional + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/path/to/isotp-c) # add to current project + +target_link_libraries( + mytarget + + # ... other libs + simon_cahill::isotp_c +) +``` + + ## Usage First, create some [shim](https://en.wikipedia.org/wiki/Shim_(computing)) functions to let this library use your lower level system: From 3e5b558410077eb0e5ea1469cd2456ce9d957701 Mon Sep 17 00:00:00 2001 From: Simon Cahill Date: Thu, 23 Nov 2023 11:54:17 +0100 Subject: [PATCH 3/3] Updated version to revision 1 --- CMakeLists.txt | 2 +- vars.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b8cfb5..2c55348 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.10) ### # Project definition ### -project(isotp LANGUAGES C VERSION 1.0.0 DESCRIPTION "A platform-agnostic ISOTP implementation in C for embedded devices.") +project(isotp LANGUAGES C VERSION 1.0.1 DESCRIPTION "A platform-agnostic ISOTP implementation in C for embedded devices.") option(isotpc_USE_INCLUDE_DIR "Copy header files to separate include directory in current binary dir for better separation of header files to combat potential naming conflicts." OFF) option(isotpc_STATIC_LIBRARY "Compile libisotpc as a static library, instead of a shared library." OFF) diff --git a/vars.mk b/vars.mk index aaccb13..636f21c 100644 --- a/vars.mk +++ b/vars.mk @@ -32,7 +32,7 @@ CPPSTD := "c++0x" LIB_NAME := "libisotp.so" MAJOR_VER := "1" MINOR_VER := "0" -REVISION := "0" +REVISION := "1" OUTPUT_NAME := $(LIB_NAME).$(MAJOR_VER).$(MINOR_VER).$(REVISION) ###