[PATCH] D65603: Add support in CMake to statically link the C++ standard library.

Erich Keane via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 12:55:07 PDT 2019


This revision was not accepted when it landed; it landed in state "Needs Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rL368907: Add support in CMake to statically link the C++ standard library. (authored by erichkeane, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D65603?vs=212891&id=215209#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65603/new/

https://reviews.llvm.org/D65603

Files:
  llvm/trunk/CMakeLists.txt
  llvm/trunk/cmake/modules/HandleLLVMStdlib.cmake
  llvm/trunk/docs/CMake.rst


Index: llvm/trunk/cmake/modules/HandleLLVMStdlib.cmake
===================================================================
--- llvm/trunk/cmake/modules/HandleLLVMStdlib.cmake
+++ llvm/trunk/cmake/modules/HandleLLVMStdlib.cmake
@@ -31,4 +31,24 @@
       message(WARNING "Not sure how to specify libc++ for this compiler")
     endif()
   endif()
+
+  if(LLVM_STATIC_LINK_CXX_STDLIB)
+    if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
+      check_cxx_compiler_flag("-static-libstdc++"
+                              CXX_COMPILER_SUPPORTS_STATIC_STDLIB)
+      check_linker_flag("-static-libstdc++" CXX_LINKER_SUPPORTS_STATIC_STDLIB)
+      if(CXX_COMPILER_SUPPORTS_STATIC_STDLIB AND
+        CXX_LINKER_SUPPORTS_STATIC_STDLIB)
+        append("-static-libstdc++"
+          CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS
+          CMAKE_MODULE_LINKER_FLAGS)
+      else()
+        message(WARNING
+          "Can't specify static linking for the C++ standard library")
+      endif()
+    else()
+      message(WARNING "Not sure how to specify static linking of C++ standard "
+        "library for this compiler")
+    endif()
+  endif()
 endif()
Index: llvm/trunk/CMakeLists.txt
===================================================================
--- llvm/trunk/CMakeLists.txt
+++ llvm/trunk/CMakeLists.txt
@@ -390,6 +390,7 @@
   option(LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY "Compile with -fmodules-local-submodule-visibility." ON)
 endif()
 option(LLVM_ENABLE_LIBCXX "Use libc++ if available." OFF)
+option(LLVM_STATIC_LINK_CXX_STDLIB "Statically link the standard library." OFF)
 option(LLVM_ENABLE_LLD "Use lld as C and C++ linker." OFF)
 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
 option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
Index: llvm/trunk/docs/CMake.rst
===================================================================
--- llvm/trunk/docs/CMake.rst
+++ llvm/trunk/docs/CMake.rst
@@ -431,6 +431,11 @@
   passed to invocations of both so that the project is built using libc++
   instead of stdlibc++. Defaults to OFF.
 
+**LLVM_STATIC_LINK_CXX_STDLIB**:BOOL
+  Statically link to the C++ standard library if possible. This uses the flag
+  "-static-libstdc++", but a Clang host compiler will statically link to libc++
+  if used in conjuction with the **LLVM_ENABLE_LIBCXX** flag. Defaults to OFF.
+
 **LLVM_ENABLE_LLD**:BOOL
   This option is equivalent to `-DLLVM_USE_LINKER=lld`, except during a 2-stage
   build where a dependency is added from the first stage to the second ensuring


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65603.215209.patch
Type: text/x-patch
Size: 2546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190814/43712b40/attachment.bin>


More information about the llvm-commits mailing list