@@ -14,7 +14,12 @@ endif(NOT CMAKE_BUILD_TYPE)
1414option (BUILD_TESTING "Enable test (depends on googletest)." OFF )
1515# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
1616# it prominent in the GUI.
17+ # cpu_features uses bit-fields which are - to some extends - implementation-defined (see https://en.cppreference.com/w/c/language/bit_field).
18+ # As a consequence it is discouraged to use cpu_features as a shared library because different compilers may interpret the code in different ways.
19+ # Prefer static linking from source whenever possible.
1720option (BUILD_SHARED_LIBS "Build library as shared." OFF )
21+ # PIC
22+ option (BUILD_PIC "Build with Position Independant Code." OFF ) # Default is off at least for GCC
1823
1924include (CheckIncludeFile )
2025include (CheckSymbolExists )
@@ -106,6 +111,7 @@ if(UNIX)
106111 if (HAVE_STRONG_GETAUXVAL)
107112 target_compile_definitions (unix_based_hardware_detection PRIVATE HAVE_STRONG_GETAUXVAL )
108113 endif ()
114+ set_property (TARGET unix_based_hardware_detection PROPERTY POSITION_INDEPENDENT_CODE ${BUILD_PIC} )
109115endif ()
110116
111117#
@@ -122,6 +128,7 @@ add_library(cpu_features ${CPU_FEATURES_HDRS} ${CPU_FEATURES_SRCS})
122128set_target_properties (cpu_features PROPERTIES PUBLIC_HEADER "${CPU_FEATURES_HDRS} " )
123129setup_include_and_definitions (cpu_features )
124130target_link_libraries (cpu_features PUBLIC ${CMAKE_DL_LIBS} )
131+ set_property (TARGET cpu_features PROPERTY POSITION_INDEPENDENT_CODE ${BUILD_PIC} )
125132target_include_directories (cpu_features
126133 PUBLIC $<INSTALL_INTERFACE :${CMAKE_INSTALL_INCLUDEDIR} /cpu_features >
127134)
0 commit comments