]> git-server-git.apps.pok.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>
Wed, 8 Aug 2018 12:39:39 +0000 (20:39 +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.

Fixes: http://tracker.ceph.com/issues/26880
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit d245ffb0e616d3f2dc954047389da69f7142c915)

CMakeLists.txt
src/CMakeLists.txt
src/librados/CMakeLists.txt
src/libradosstriper/CMakeLists.txt
src/librbd/CMakeLists.txt
src/osd/CMakeLists.txt

index d68c7553242dd1a745c812cbe6020a5eef17507f..046bd8bdd551d36f11ab312d8b67bb846a6e49d9 100644 (file)
@@ -167,7 +167,12 @@ 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)
+  else()
     message(FATAL_ERROR "Please use GCC to enable WITH_STATIC_LIBSTDCXX")
   endif()
 endif()
@@ -323,6 +328,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 374cbc709b274e098691c44c952b988cd9239ebd..8fca928a5bd2ec15dbeb4703663a529f7babb7b6 100644 (file)
@@ -39,8 +39,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)
-    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)
+      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}")
@@ -689,12 +693,6 @@ endif(WITH_DPDK)
 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})
@@ -702,12 +700,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})
 
 add_library(common_utf8 STATIC common/utf8.c)
@@ -877,10 +869,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 ceph-common
-  ${BLKID_LIBRARIES} ${RDMA_LIBRARIES})
+  ${BLKID_LIBRARIES} ${RDMA_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)
@@ -984,10 +978,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
@@ -1030,7 +1020,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 88682ef9c165428fe905fdfa8d3ce0e02f631234..df6521770cad654e06dfdc8911fca243c668acd4 100644 (file)
@@ -156,10 +156,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 7ed2e736905ceaa898aef7af731ebb465bc85661..16dcdb3094ef90c72b9d10d08d6b4abbfca96034 100644 (file)
@@ -44,7 +44,7 @@ add_library(osd STATIC ${osd_srcs}
   $<TARGET_OBJECTS:cls_references_objs>
   $<TARGET_OBJECTS:global_common_objs>
   $<TARGET_OBJECTS:heap_profiler_objs>)
-target_link_libraries(osd ${LEVELDB_LIBRARIES} dmclock ${CMAKE_DL_LIBS} ${ALLOC_LIBS})
+target_link_libraries(osd ${LEVELDB_LIBRARIES} dmclock ${CMAKE_DL_LIBS})
 if(WITH_LTTNG)
   add_dependencies(osd osd-tp pg-tp)
 endif()