]> granicus.if.org Git - llvm/commitdiff
Add support in CMake to statically link the C++ standard library.
authorErich Keane <erich.keane@intel.com>
Wed, 14 Aug 2019 19:55:59 +0000 (19:55 +0000)
committerErich Keane <erich.keane@intel.com>
Wed, 14 Aug 2019 19:55:59 +0000 (19:55 +0000)
It is sometimes useful to have the C++ standard library linked into the
assembly when compiling clang, particularly when distributing a compiler
onto systems that don't have a copy of stdlibc++ or libc++ installed.

This functionality should work with either GCC or Clang as the host
compiler, though statically linking libc++ (as may be required for
licensing purposes) is only possible if the host compiler is Clang with
a copy of libc++ available.

Differential Revision: https://reviews.llvm.org/D65603

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368907 91177308-0d34-0410-b5e6-96231b3b80d8

CMakeLists.txt
cmake/modules/HandleLLVMStdlib.cmake
docs/CMake.rst

index 44a269292faad206712a05d5abbdf52623f9dd6f..678f1a74bdd14c7fed7c7501f9c1ce1711bc9838 100644 (file)
@@ -390,6 +390,7 @@ else()
   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 f0e22d1a5b33427c85b04a4419efa35477c61a82..b67d87d4300503f33e3dbe2393d25ca26609c1d8 100644 (file)
@@ -31,4 +31,24 @@ if(NOT DEFINED LLVM_STDLIB_HANDLED)
       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 cbddad5746dab748e05442af68a04480722074c2..2c54fde4393909dfdb98f6d77fab8ffd3fe41ed1 100644 (file)
@@ -431,6 +431,11 @@ LLVM-specific variables
   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