Commit e1788a822de827a5f869321f6a90b5ad31b420ca

Steffen Jaeckel 2022-02-17T18:06:19

allow unit tests in shared library builds The test sources must be compiled with a special define and require optimisation in order to be able to run. Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8394d1f..be652dd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -49,6 +49,19 @@ target_link_libraries(test-target PRIVATE
     ${PROJECT_NAME}
 )
 
+# for the SHARED_LIBRARY build we need some special flags enabled
+# We also allow our users to override our selection by defining their own
+# `CMAKE_C_FLAGS` on generation-phase. CMake itself doesn't allow a user
+# to override settings defined in the CMakeLists.txt so we append it manually
+# again even though CMake prepended it already.
+target_compile_options(test-target BEFORE PRIVATE
+    $<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1 -DLTM_TEST_DYNAMIC>
+    ${CMAKE_C_FLAGS}
+)
+target_link_options(test-target BEFORE PRIVATE
+    $<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1>
+)
+
 #-----------------------------------------------------------------------------
 # demo target
 #-----------------------------------------------------------------------------