]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cmake: fix "WITH_STATIC_LIBSTDCXX"
authorKefu Chai <kchai@redhat.com>
Thu, 12 Jul 2018 09:17:44 +0000 (17:17 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 28 Jul 2018 17:44:18 +0000 (01:44 +0800)
- do not link libkv with ALLOC_LIBS, it turns out that if we link
tcmalloc *before* -static-libstdc++ -static-libgcc, libstdc++ and gcc
libs will show up in `ldd` output
- add `-static-libstdc++ -static-libgcc` to CMAKE_SHARED_LINKER_FLAGS
and CMAKE_EXE_LINKER_FLAGS instead of adding them to all shared
libraries and executable. simpler this way.
- link against libtcmalloc statically, because libtcmalloc is a C++
library, linking against it dynamically and linking against C++ runtime
statically will pull in depdencies on two versions of C++ runtime, which
will bring down the app at run-time.
- do not pass '-pie' to linker when building executable if
`WITH_STATIC_LIBSTDCXX` and tcmalloc is used, because the static tcmalloc
is not compiled with PIC.
- only apply '-pie' if ENABLE_SHARED is enabled.

Signed-off-by: Kefu Chai <kchai@redhat.com>
CMakeLists.txt
src/CMakeLists.txt
src/librados/CMakeLists.txt
src/libradosstriper/CMakeLists.txt
src/librbd/CMakeLists.txt
src/mgr/CMakeLists.txt
src/osd/CMakeLists.txt

index 74ed9a0ce952e31eaa7ce38e04deb00913a4feef..f073d8e3b84920f6ce6eba20f2f6b8d0296ac90f 100644 (file)
@@ -167,7 +167,13 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ${ENABLE_SHARED})
 
 option(WITH_STATIC_LIBSTDCXX "Link against libstdc++ statically" OFF)
 if(WITH_STATIC_LIBSTDCXX)
-  if(NOT CMAKE_COMPILER_IS_GNUCXX)
+  if(CMAKE_COMPILER_IS_GNUCXX)
+    set(static_linker_flags "-static-libstdc++ -static-libgcc")
+    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${static_linker_flags}")
+    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${static_linker_flags}")
+    unset(static_linker_flags)
+    set(GPERFTOOLS_USE_STATIC_LIBS TRUE)
+  else()
     message(FATAL_ERROR "Please use GCC to enable WITH_STATIC_LIBSTDCXX")
   endif()
 endif()
@@ -337,6 +343,11 @@ else(ALLOCATOR)
   endif(gperftools_FOUND)
 endif(ALLOCATOR)
 
+if(HAVE_LIBTCMALLOC AND WITH_STATIC_LIBSTDCXX)
+  set(EXE_LINKER_USE_PIE FALSE)
+else()
+  set(EXE_LINKER_USE_PIE ${ENABLE_SHARED})
+endif()
 
 if(WITH_LIBCEPHFS OR WITH_KRBD)
   find_package(keyutils REQUIRED)
index 06b3c15c37e75a0af734ae6c39d541c7ffb7dbfa..48759883c1e4b646debe9be44002daeee8f5d7a2 100644 (file)
@@ -40,8 +40,12 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstrict-null-sentinel -Woverloaded-virtual")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-new-ttp-matching")
-  if(NOT WITH_OSD_INSTRUMENT_FUNCTIONS AND NOT HAVE_SEASTAR)
-    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
+  # cmake does not add '-pie' for executables even if
+  # CMAKE_POSITION_INDEPENDENT_CODE is TRUE.
+  if(EXE_LINKER_USE_PIE)
+    if (NOT WITH_OSD_INSTRUMENT_FUNCTIONS AND NOT HAVE_SEASTAR)
+      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
+    endif()
   endif()
 elseif(CMAKE_CXX_COMPILER_ID STREQUAL Clang)
   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_EXPORTS_C_FLAG}")
@@ -446,12 +450,6 @@ endif()
 add_library(common STATIC ${ceph_common_objs})
 target_link_libraries(common
   PRIVATE ${ceph_common_deps})
-if(WITH_STATIC_LIBSTDCXX)
-  # the apps linking against libcommon are daemons also written in C++, so we
-  # need to link them against libstdc++.
-  target_link_libraries(common
-    INTERFACE "-static-libstdc++ -static-libgcc")
-endif()
 
 add_library(ceph-common SHARED ${ceph_common_objs})
 target_link_libraries(ceph-common ${ceph_common_deps})
@@ -459,12 +457,6 @@ target_link_libraries(ceph-common ${ceph_common_deps})
 set_target_properties(ceph-common PROPERTIES
   SOVERSION 0
   INSTALL_RPATH "")
-if(WITH_STATIC_LIBSTDCXX)
-  # link libstdc++ into ceph-common to avoid including libstdc++ in every apps,
-  # to reduce the size of the app linking against ceph-common.
-  set_target_properties(ceph-common PROPERTIES
-    LINK_FLAGS "-static-libstdc++ -static-libgcc")
-endif()
 install(TARGETS ceph-common DESTINATION ${CMAKE_INSTALL_PKGLIBDIR})
 
 if(${WITH_LTTNG})
@@ -550,10 +542,12 @@ set(ceph_osd_srcs
 add_executable(ceph-osd ${ceph_osd_srcs})
 add_dependencies(ceph-osd erasure_code_plugins)
 target_link_libraries(ceph-osd osd os global-static common
-  ${BLKID_LIBRARIES})
+  ${BLKID_LIBRARIES} ${ALLOC_LIBS})
 if(WITH_FUSE)
   target_link_libraries(ceph-osd ${FUSE_LIBRARIES})
 endif()
+set_target_properties(ceph-osd PROPERTIES
+  POSITION_INDEPENDENT_CODE ${EXE_LINKER_USE_PIE})
 install(TARGETS ceph-osd DESTINATION bin)
 
 add_subdirectory(mds)
@@ -649,10 +643,6 @@ if(WITH_LIBCEPHFS)
   add_library(cephfs ${CEPH_SHARED} ${libcephfs_srcs})
   target_link_libraries(cephfs PRIVATE client ceph-common
     ${CRYPTO_LIBS} ${EXTRALIBS})
-  if(WITH_STATIC_LIBSTDCXX)
-    target_link_libraries(cephfs
-      INTERFACE "-static-libstdc++ -static-libgcc")
-  endif()
   if(ENABLE_SHARED)
     set_target_properties(cephfs PROPERTIES
       OUTPUT_NAME cephfs
@@ -695,7 +685,9 @@ if(WITH_FUSE)
   add_executable(ceph-fuse ${ceph_fuse_srcs})
   target_link_libraries(ceph-fuse ${ALLOC_LIBS} ${FUSE_LIBRARIES}
     client ceph-common global-static)
-  set_target_properties(ceph-fuse PROPERTIES COMPILE_FLAGS "-I${FUSE_INCLUDE_DIRS}")
+  set_target_properties(ceph-fuse PROPERTIES
+    COMPILE_FLAGS "-I${FUSE_INCLUDE_DIRS}"
+    POSITION_INDEPENDENT_CODE ${EXE_LINKER_USE_PIE})
   install(TARGETS ceph-fuse DESTINATION bin)
   install(PROGRAMS mount.fuse.ceph DESTINATION ${CMAKE_INSTALL_SBINDIR})
 endif(WITH_FUSE)
index f15aa7d3029b2706cde9fbfd8c7a2efb36cbb82d..34d6af6a9193a30a22095df46e9eb1de2cb86796 100644 (file)
@@ -30,10 +30,6 @@ endif(ENABLE_SHARED)
 target_link_libraries(librados PRIVATE
   osdc ceph-common cls_lock_client
   ${BLKID_LIBRARIES} ${CRYPTO_LIBS} ${EXTRALIBS})
-if(WITH_STATIC_LIBSTDCXX)
-  target_link_libraries(librados
-    INTERFACE "-static-libstdc++ -static-libgcc")
-endif()
 target_link_libraries(librados ${rados_libs})
 install(TARGETS librados DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
index e447dd3aeccd4669e4ef9c429410493c6bd23c11..5b2dd7566aff59aac04d57711ac6b1b564ba52e6 100644 (file)
@@ -7,10 +7,6 @@ add_library(radosstriper ${CEPH_SHARED}
   $<TARGET_OBJECTS:librados_objs>)
 target_link_libraries(radosstriper
   PRIVATE librados cls_lock_client osdc ceph-common pthread ${CRYPTO_LIBS} ${EXTRALIBS})
-if(WITH_STATIC_LIBSTDCXX)
-  target_link_libraries(radosstriper
-    INTERFACE "-static-libstdc++ -static-libgcc")
-endif()
 set_target_properties(radosstriper PROPERTIES
   OUPUT_NAME radosstriper
   VERSION 1.0.0
index 768902e55a606e7cbe9ca1b73b7a02d8054f658b..b9c08d4624fa77f2369ea9bd2761edc08b221362 100644 (file)
@@ -157,10 +157,6 @@ if(HAVE_UDEV)
   target_link_libraries(librbd PRIVATE
     udev)
 endif()
-if(WITH_STATIC_LIBSTDCXX)
-  target_link_libraries(librbd INTERFACE
-    "-static-libstdc++ -static-libgcc")
-endif()
 if(ENABLE_SHARED)
   set_target_properties(librbd PROPERTIES
     OUTPUT_NAME rbd
index a55fc3adb0ef4714a5b99abcb375a4a6f088373b..5e061bf95c65e924175b3e6028e0121437f450bb 100644 (file)
@@ -25,4 +25,6 @@ target_link_libraries(ceph-mgr
   osdc client heap_profiler
   global-static ceph-common
   Boost::python ${MGR_PYTHON_LIBRARIES} ${CMAKE_DL_LIBS} ${ALLOC_LIBS})
+set_target_properties(ceph-mgr PROPERTIES
+  POSITION_INDEPENDENT_CODE ${EXE_LINKER_USE_PIE})
 install(TARGETS ceph-mgr DESTINATION bin)
index 31dd823c58b9b6577300e43ceee76249c2e15cf6..eb62a76c279d302e8bb4ed462c20ae4181ed8a38 100644 (file)
@@ -45,7 +45,7 @@ add_library(osd STATIC ${osd_srcs}
   $<TARGET_OBJECTS:global_common_objs>)
 target_link_libraries(osd PRIVATE
   ${LEVELDB_LIBRARIES}
-  dmclock heap_profiler cpu_profiler ${CMAKE_DL_LIBS} ${ALLOC_LIBS})
+  dmclock heap_profiler cpu_profiler ${CMAKE_DL_LIBS})
 if(WITH_LTTNG)
   add_dependencies(osd osd-tp pg-tp)
 endif()