[PATCH] D158846: [CMake] Fix BUILD_SHARED_LIBS build on Solaris

Rainer Orth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 25 07:06:30 PDT 2023


ro created this revision.
ro added reviewers: phosek, finagolfin, daltenty, benlangmuir, mgorny, JDevlieghere.
Herald added subscribers: ekilmer, lebedev.ri, fedor.sergeev, hiraditya, jyknight.
Herald added a reviewer: bollu.
Herald added a reviewer: lebedev.ri.
Herald added a project: All.
ro requested review of this revision.
Herald added a subscriber: StephenFan.
Herald added a project: LLVM.

LLVM currently doesn't build with `-DBUILD_SHARED_LIBS=ON` on Solaris:

- Quite a number of libs fail to link like this:

  Undefined                       first referenced
   symbol                             in file
  ceil                                lib/DebugInfo/Symbolize/CMakeFiles/LLVMSymbolize.dir/DIPrinter.cpp.o  (symbol belongs to implicit dependency /usr/lib/amd64/libm.so.2)

This is due to linking with `-z defs` from `llvm/cmake/modules/HandleLLVMOptions.cmake`.  Solaris `ld` requires shared objects to be self-contained.  Due to the large number of affected libraries (roughly half of all), it's only practical to do this centrally.

- `libLLVMTargetParser.so` uses `libkstat` functions without linking it.
- `libbenchmark.so` likewise has a `libm` dependency which must be made explicit.  This doesn't happen in upstream `benchmark`, but is also due to linking with `z defs`, so the patch must stay local to the LLVM tree.

Tested on `amd64-pc-solaris2.11` and `sparcv9-sun-solaris2.11`.

There are a couple of regressions:

- 34 instances on Solaris/amd64 of

  LVM ERROR: Must use fast (default) register allocator for unoptimized regalloc.

which are probably due to differences in constructor ordering.

- Also a few instances of

  Polly-Unit :: DeLICM/./DeLICMTests/failed_to_discover_tests_from_gtest

which I mean to address later.  This patch is only about the build failure.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158846

Files:
  llvm/cmake/modules/AddLLVM.cmake
  llvm/lib/TargetParser/CMakeLists.txt
  third-party/benchmark/src/CMakeLists.txt


Index: third-party/benchmark/src/CMakeLists.txt
===================================================================
--- third-party/benchmark/src/CMakeLists.txt
+++ third-party/benchmark/src/CMakeLists.txt
@@ -51,6 +51,10 @@
 # We need extra libraries on Solaris
 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
   target_link_libraries(benchmark PRIVATE kstat)
+  # Solaris ld with -z defs requires libm dependency to be specified explicitly.
+  if(BUILD_SHARED_LIBS)
+    target_link_libraries(benchmark PRIVATE m)
+  endif()
 endif()
 
 # Benchmark main library
Index: llvm/lib/TargetParser/CMakeLists.txt
===================================================================
--- llvm/lib/TargetParser/CMakeLists.txt
+++ llvm/lib/TargetParser/CMakeLists.txt
@@ -8,6 +8,11 @@
   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
 endif()
 
+# Solaris code uses kstat, so specify dependency explicitly for shared builds.
+if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
+  set(system_libs kstat)
+endif()
+
 add_llvm_component_library(LLVMTargetParser
   AArch64TargetParser.cpp
   ARMTargetParserCommon.cpp
@@ -25,6 +30,9 @@
   Unix
   Windows
 
+  LINK_LIBS
+  ${system_libs}
+
   LINK_COMPONENTS
   Support
 
Index: llvm/cmake/modules/AddLLVM.cmake
===================================================================
--- llvm/cmake/modules/AddLLVM.cmake
+++ llvm/cmake/modules/AddLLVM.cmake
@@ -644,6 +644,13 @@
         SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}
         VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX})
     endif()
+
+    # Solaris ld with -z defs requires dependencies to be specified
+    # explicitly when building shared objects.  Many LLVM libs require
+    # libm, so do this globally.
+    if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
+      set_target_properties(${name} PROPERTIES INTERFACE_LINK_LIBRARIES m)
+    endif()
   endif()
 
   if(ARG_MODULE OR ARG_SHARED)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158846.553423.patch
Type: text/x-patch
Size: 1916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230825/47e2503c/attachment.bin>


More information about the llvm-commits mailing list