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