From 65e616728bacf70a4e907eb3c9a84bad2587bfbe Mon Sep 17 00:00:00 2001 From: Phil Greenland <40036098+pgreenland@users.noreply.github.com> Date: Wed, 21 Feb 2024 08:50:18 +0000 Subject: [PATCH] Make position independent code flag optional for static libraries (#27) * Make PIC optional for static libraries. Make compile definitions private * Correct comment * Remove note on fixed PIC --- CMakeLists.txt | 18 +++++++++++++----- README.md | 2 -- isotp_config.h | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3324654..49d0092 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ project(isotp LANGUAGES C VERSION 1.0.1 DESCRIPTION "A platform-agnostic ISOTP i 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) +option(isotpc_STATIC_LIBRARY_PIC "Make use of position independent code (PIC), when compiling as a static library (enabled automatically when building shared libraries)." OFF) option(isotpc_PAD_CAN_FRAMES "Pad CAN frames to their full size." ON) set(isotpc_CAN_FRAME_PAD_VALUE "0xAA" CACHE STRING "Padding byte value to be used in CAN frames if enabled") @@ -20,13 +21,20 @@ endif() # Strict building policies ### target_compile_options( - isotp PUBLIC - -fPIC + isotp PRIVATE -Werror -Wall -Wno-unknown-pragmas # ignore unknown pragmas, such as #pragma region ) +### +# Enable position independent code (PIC) if required +### +if (NOT isotpc_STATIC_LIBRARY OR isotpc_STATIC_LIBRARY_PIC) + # Building a shared library or a static library with PIC enabled + target_compile_options(isotp PRIVATE -fPIC) +endif() + ### # Provide padding configuration ### @@ -38,10 +46,10 @@ endif() # Check for debug builds ### if (CMAKE_BUILD_TYPE STREQUAL "Debug") - target_compile_options(isotp PUBLIC -O0 -g) # don't optimise and add debugging symbols + target_compile_options(isotp PRIVATE -O0 -g) # don't optimise and add debugging symbols else() - target_compile_options(isotp PUBLIC -O2) # optimise code - target_link_libraries(isotp PUBLIC -s) # strip the library + target_compile_options(isotp PRIVATE -O2) # optimise code + target_link_libraries(isotp PRIVATE -s) # strip the library endif() if (isotpc_USE_INCLUDE_DIR) diff --git a/README.md b/README.md index 6c5756f..5705737 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,6 @@ If your projects use a different build system, you are more than welcome to incl 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` diff --git a/isotp_config.h b/isotp_config.h index cda4bbb..13c4c91 100644 --- a/isotp_config.h +++ b/isotp_config.h @@ -23,7 +23,7 @@ /* Private: Determines if by default, padding is added to ISO-TP message frames. */ -//#define ISO_TP_FRAME_PADDING 0xAA +//#define ISO_TP_FRAME_PADDING /* Private: Value to use when padding frames if enabled by ISO_TP_FRAME_PADDING */