Skip to content

Commit db4fcdc

Browse files
afrindmeta-codesync[bot]
authored andcommitted
Fix libunwind cmake to include lzma in static builds
Summary: X-link: facebook/folly#2586 The existing FindLibUnwind.cmake uses a bare find_library which misses transitive dependencies (e.g. lzma, zlib) that static libunwind needs. Switch to preferring pkg-config which picks up the full set of static dependencies from Libs.private, with a fallback to the original find_library approach for systems without pkg-config. Reviewed By: bigfootjon Differential Revision: D93644458 fbshipit-source-id: b470df008b95497a8db3149c3ddc7b0827c1dee5
1 parent 9643d7d commit db4fcdc

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

build/fbcode_builder/CMake/FindLibUnwind.cmake

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,57 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# When using prepackaged LLVM libunwind on Ubuntu, its includes are installed in a subdirectory.
16-
find_path(LIBUNWIND_INCLUDE_DIR NAMES libunwind.h PATH_SUFFIXES libunwind)
17-
mark_as_advanced(LIBUNWIND_INCLUDE_DIR)
15+
include(FindPackageHandleStandardArgs)
16+
17+
# Prefer pkg-config: picks up transitive deps (e.g. lzma, zlib) that
18+
# static libunwind needs but a bare find_library would miss.
19+
find_package(PkgConfig QUIET)
20+
if(PKG_CONFIG_FOUND)
21+
pkg_check_modules(PC_LIBUNWIND QUIET libunwind)
22+
endif()
1823

19-
find_library(LIBUNWIND_LIBRARY NAMES unwind)
20-
mark_as_advanced(LIBUNWIND_LIBRARY)
24+
if(PC_LIBUNWIND_FOUND)
25+
find_path(LIBUNWIND_INCLUDE_DIR NAMES libunwind.h
26+
HINTS ${PC_LIBUNWIND_INCLUDE_DIRS}
27+
PATH_SUFFIXES libunwind)
28+
mark_as_advanced(LIBUNWIND_INCLUDE_DIR)
2129

22-
include(FindPackageHandleStandardArgs)
23-
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
24-
LIBUNWIND
25-
REQUIRED_VARS LIBUNWIND_LIBRARY LIBUNWIND_INCLUDE_DIR)
30+
# Resolve each library from the static set (Libs + Libs.private) to a
31+
# full path. This gives the linker everything it needs for a fully-static
32+
# link without leaking imported targets through cmake exports.
33+
set(LIBUNWIND_LIBRARIES "")
34+
foreach(_lib IN LISTS PC_LIBUNWIND_STATIC_LIBRARIES)
35+
find_library(_libunwind_dep_${_lib} NAMES ${_lib}
36+
HINTS ${PC_LIBUNWIND_STATIC_LIBRARY_DIRS})
37+
if(_libunwind_dep_${_lib})
38+
list(APPEND LIBUNWIND_LIBRARIES ${_libunwind_dep_${_lib}})
39+
else()
40+
# Fall back to bare name; the linker will resolve -l<name>.
41+
list(APPEND LIBUNWIND_LIBRARIES ${_lib})
42+
endif()
43+
unset(_libunwind_dep_${_lib} CACHE)
44+
endforeach()
45+
set(LIBUNWIND_LIBRARY "${LIBUNWIND_LIBRARIES}")
46+
47+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibUnwind
48+
REQUIRED_VARS LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIR)
49+
else()
50+
# Fallback for systems without pkg-config.
51+
# When using prepackaged LLVM libunwind on Ubuntu, its includes are
52+
# installed in a subdirectory.
53+
find_path(LIBUNWIND_INCLUDE_DIR NAMES libunwind.h PATH_SUFFIXES libunwind)
54+
mark_as_advanced(LIBUNWIND_INCLUDE_DIR)
55+
56+
find_library(LIBUNWIND_LIBRARY NAMES unwind)
57+
mark_as_advanced(LIBUNWIND_LIBRARY)
58+
59+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibUnwind
60+
REQUIRED_VARS LIBUNWIND_LIBRARY LIBUNWIND_INCLUDE_DIR)
61+
endif()
2662

27-
if(LIBUNWIND_FOUND)
28-
set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARY})
63+
if(LibUnwind_FOUND)
64+
if(NOT LIBUNWIND_LIBRARIES)
65+
set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARY})
66+
endif()
2967
set(LIBUNWIND_INCLUDE_DIRS ${LIBUNWIND_INCLUDE_DIR})
3068
endif()

0 commit comments

Comments
 (0)