1# Get sources
2
3set(LIBUNWIND_CXX_SOURCES
4    libunwind.cpp
5    Unwind-EHABI.cpp
6    Unwind-seh.cpp
7    )
8if(APPLE)
9  list(APPEND LIBUNWIND_CXX_SOURCES
10    Unwind_AppleExtras.cpp
11    )
12endif()
13
14if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
15  list(APPEND LIBUNWIND_CXX_SOURCES
16    Unwind_AIXExtras.cpp
17    )
18endif()
19
20set(LIBUNWIND_C_SOURCES
21    UnwindLevel1.c
22    UnwindLevel1-gcc-ext.c
23    Unwind-sjlj.c
24    )
25set_source_files_properties(${LIBUNWIND_C_SOURCES}
26                            PROPERTIES
27                              COMPILE_FLAGS "-std=c99")
28
29set(LIBUNWIND_ASM_SOURCES
30    UnwindRegistersRestore.S
31    UnwindRegistersSave.S
32    )
33
34# See add_asm_sources() in compiler-rt for explanation of this workaround.
35# CMake doesn't work correctly with assembly on AIX. Workaround by compiling
36# as C files as well.
37if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR
38   (MINGW AND CMAKE_VERSION VERSION_LESS 3.17) OR
39   (${CMAKE_SYSTEM_NAME} MATCHES "AIX"))
40  set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C)
41endif()
42
43set(LIBUNWIND_HEADERS
44    AddressSpace.hpp
45    assembly.h
46    CompactUnwinder.hpp
47    cet_unwind.h
48    config.h
49    dwarf2.h
50    DwarfInstructions.hpp
51    DwarfParser.hpp
52    EHHeaderParser.hpp
53    FrameHeaderCache.hpp
54    libunwind_ext.h
55    Registers.hpp
56    RWMutex.hpp
57    Unwind-EHABI.h
58    UnwindCursor.hpp
59    ../include/libunwind.h
60    ../include/unwind.h
61    ../include/unwind_itanium.h
62    ../include/unwind_arm_ehabi.h
63    )
64if(APPLE)
65  list(APPEND LIBUNWIND_HEADERS
66    ../include/mach-o/compact_unwind_encoding.h
67    )
68endif()
69
70if (MSVC_IDE)
71  # Force them all into the headers dir on MSVC, otherwise they end up at
72  # project scope because they don't have extensions.
73  source_group("Header Files" FILES ${LIBUNWIND_HEADERS})
74endif()
75
76set(LIBUNWIND_SOURCES
77    ${LIBUNWIND_CXX_SOURCES}
78    ${LIBUNWIND_C_SOURCES}
79    ${LIBUNWIND_ASM_SOURCES})
80
81# Generate library list.
82add_library_flags_if(LIBUNWIND_HAS_C_LIB c)
83if (LIBUNWIND_USE_COMPILER_RT)
84  add_library_flags("${LIBUNWIND_BUILTINS_LIBRARY}")
85else()
86  add_library_flags_if(LIBUNWIND_HAS_GCC_S_LIB gcc_s)
87  add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
88endif()
89add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
90if (LIBUNWIND_ENABLE_THREADS)
91  add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
92  add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
93endif()
94
95# Setup flags.
96add_link_flags_if(CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG --unwindlib=none)
97if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
98  add_link_flags_if_supported(-nostdlib++)
99else()
100  add_link_flags_if_supported(-nodefaultlibs)
101endif()
102
103# MINGW_LIBRARIES is defined in config-ix.cmake
104add_library_flags_if(MINGW "${MINGW_LIBRARIES}")
105
106if (LIBUNWIND_ENABLE_SHARED AND
107    NOT (CXX_SUPPORTS_FNO_EXCEPTIONS_FLAG AND
108         CXX_SUPPORTS_FUNWIND_TABLES_FLAG))
109  message(FATAL_ERROR
110          "Compiler doesn't support generation of unwind tables if exception "
111          "support is disabled.  Building libunwind DSO with runtime dependency "
112          "on C++ ABI library is not supported.")
113endif()
114
115if (APPLE)
116  add_compile_flags("-U__STRICT_ANSI__")
117  add_link_flags("-compatibility_version 1" "-install_name /usr/lib/libunwind.1.dylib")
118
119  if (CMAKE_OSX_DEPLOYMENT_TARGET STREQUAL "10.6")
120    add_link_flags("-current_version ${LIBUNWIND_VERSION}" "/usr/lib/libSystem.B.dylib")
121  endif ()
122endif ()
123
124string(REPLACE ";" " " LIBUNWIND_COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}")
125string(REPLACE ";" " " LIBUNWIND_CXX_FLAGS "${LIBUNWIND_CXX_FLAGS}")
126string(REPLACE ";" " " LIBUNWIND_C_FLAGS "${LIBUNWIND_C_FLAGS}")
127string(REPLACE ";" " " LIBUNWIND_LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}")
128
129set_property(SOURCE ${LIBUNWIND_CXX_SOURCES}
130             APPEND_STRING PROPERTY COMPILE_FLAGS " ${LIBUNWIND_CXX_FLAGS}")
131set_property(SOURCE ${LIBUNWIND_C_SOURCES}
132             APPEND_STRING PROPERTY COMPILE_FLAGS " ${LIBUNWIND_C_FLAGS}")
133
134# NOTE: avoid implicit dependencies on C++ runtimes.  libunwind uses C++ for
135# ease, but does not rely on C++ at runtime.
136set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
137
138# Build the shared library.
139if (LIBUNWIND_ENABLE_SHARED)
140  add_library(unwind_shared SHARED ${LIBUNWIND_SOURCES} ${LIBUNWIND_HEADERS})
141  if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
142    target_compile_options(unwind_shared PRIVATE /GR-)
143  else()
144    target_compile_options(unwind_shared PRIVATE -fno-rtti)
145  endif()
146  target_link_libraries(unwind_shared PRIVATE ${LIBUNWIND_LIBRARIES}
147                                      PRIVATE unwind-headers)
148  set_target_properties(unwind_shared
149    PROPERTIES
150      CXX_EXTENSIONS OFF
151      CXX_STANDARD 11
152      CXX_STANDARD_REQUIRED ON
153      COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}"
154      LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
155      LINKER_LANGUAGE C
156      OUTPUT_NAME "unwind"
157      VERSION "1.0"
158      SOVERSION "1"
159  )
160  list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_shared")
161  if (LIBUNWIND_INSTALL_SHARED_LIBRARY)
162    list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_shared")
163  endif()
164endif()
165
166# Build the static library.
167if (LIBUNWIND_ENABLE_STATIC)
168  add_library(unwind_static STATIC ${LIBUNWIND_SOURCES} ${LIBUNWIND_HEADERS})
169  if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
170    target_compile_options(unwind_static PRIVATE /GR-)
171  else()
172    target_compile_options(unwind_static PRIVATE -fno-rtti)
173  endif()
174  target_link_libraries(unwind_static PRIVATE ${LIBUNWIND_LIBRARIES}
175                                      PRIVATE unwind-headers)
176  set_target_properties(unwind_static
177    PROPERTIES
178      CXX_EXTENSIONS OFF
179      CXX_STANDARD 11
180      CXX_STANDARD_REQUIRED ON
181      COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}"
182      LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
183      LINKER_LANGUAGE C
184      OUTPUT_NAME "unwind"
185  )
186
187  if(LIBUNWIND_HIDE_SYMBOLS)
188    append_flags_if_supported(UNWIND_STATIC_LIBRARY_FLAGS -fvisibility=hidden)
189    append_flags_if_supported(UNWIND_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
190    target_compile_options(unwind_static PRIVATE ${UNWIND_STATIC_LIBRARY_FLAGS})
191    target_compile_definitions(unwind_static PRIVATE _LIBUNWIND_HIDE_SYMBOLS)
192  endif()
193
194  list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_static")
195  if (LIBUNWIND_INSTALL_STATIC_LIBRARY)
196    list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_static")
197  endif()
198endif()
199
200# Add a meta-target for both libraries.
201add_custom_target(unwind DEPENDS ${LIBUNWIND_BUILD_TARGETS})
202
203if (LIBUNWIND_INSTALL_LIBRARY)
204  install(TARGETS ${LIBUNWIND_INSTALL_TARGETS}
205    LIBRARY DESTINATION ${LIBUNWIND_INSTALL_LIBRARY_DIR} COMPONENT unwind
206    ARCHIVE DESTINATION ${LIBUNWIND_INSTALL_LIBRARY_DIR} COMPONENT unwind
207    RUNTIME DESTINATION ${LIBUNWIND_INSTALL_RUNTIME_DIR} COMPONENT unwind)
208endif()
209
210if (NOT CMAKE_CONFIGURATION_TYPES AND LIBUNWIND_INSTALL_LIBRARY)
211  add_custom_target(install-unwind
212    DEPENDS unwind
213    COMMAND "${CMAKE_COMMAND}"
214            -DCMAKE_INSTALL_COMPONENT=unwind
215            -P "${LIBUNWIND_BINARY_DIR}/cmake_install.cmake")
216  add_custom_target(install-unwind-stripped
217    DEPENDS unwind
218    COMMAND "${CMAKE_COMMAND}"
219            -DCMAKE_INSTALL_COMPONENT=unwind
220            -DCMAKE_INSTALL_DO_STRIP=1
221            -P "${LIBUNWIND_BINARY_DIR}/cmake_install.cmake")
222  if(LIBUNWIND_INSTALL_HEADERS)
223    add_dependencies(install-unwind install-unwind-headers)
224    add_dependencies(install-unwind-stripped install-unwind-headers-stripped)
225  endif()
226endif()
227