[lldb/Test] Fix API tests for mutli-config generators
[lldb.git] / lldb / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.4.3)
2 if(CMAKE_SYSTEM_NAME STREQUAL Windows)
3   cmake_minimum_required(VERSION 3.13)
4 endif()
5
6 if(POLICY CMP0075)
7   cmake_policy(SET CMP0075 NEW)
8 endif()
9
10 # Add path for custom modules.
11 set(CMAKE_MODULE_PATH
12   ${CMAKE_MODULE_PATH}
13   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
14   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
15   )
16
17 # If we are not building as part of LLVM, build LLDB as a standalone project,
18 # using LLVM as an external library.
19 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
20   project(lldb)
21   include(LLDBStandalone)
22
23   set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to")
24   set(CMAKE_CXX_STANDARD_REQUIRED YES)
25   set(CMAKE_CXX_EXTENSIONS NO)
26 endif()
27
28 include(LLDBConfig)
29 include(AddLLDB)
30
31 # Define the LLDB_CONFIGURATION_xxx matching the build type.
32 if( uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
33   add_definitions( -DLLDB_CONFIGURATION_DEBUG )
34 else()
35   add_definitions( -DLLDB_CONFIGURATION_RELEASE )
36 endif()
37
38 if(APPLE)
39   add_definitions(-DLLDB_USE_OS_LOG)
40 endif()
41
42 if (WIN32)
43   add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)
44 endif()
45
46 if (LLDB_ENABLE_PYTHON)
47   execute_process(
48     COMMAND ${PYTHON_EXECUTABLE}
49         -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(True, False, ''))"
50     OUTPUT_VARIABLE LLDB_PYTHON_DEFAULT_RELATIVE_PATH
51     OUTPUT_STRIP_TRAILING_WHITESPACE)
52
53   file(TO_CMAKE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH} LLDB_PYTHON_DEFAULT_RELATIVE_PATH)
54   set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
55     CACHE STRING "Path where Python modules are installed, relative to install prefix")
56 endif ()
57
58 if (LLDB_ENABLE_PYTHON OR LLDB_ENABLE_LUA)
59   add_subdirectory(bindings)
60 endif ()
61
62 # We need the headers generated by instrinsics_gen before we can compile
63 # any source file in LLDB as the imported Clang modules might include
64 # some of these generated headers. This approach is copied from Clang's main
65 # CMakeLists.txt, so it should kept in sync the code in Clang which was added
66 # in llvm-svn 308844.
67 if(LLVM_ENABLE_MODULES AND NOT LLDB_BUILT_STANDALONE)
68   list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen)
69 endif()
70
71 if(CMAKE_CROSSCOMPILING AND LLDB_BUILT_STANDALONE)
72   set(LLVM_USE_HOST_TOOLS ON)
73   include(CrossCompile)
74   if (NOT NATIVE_LLVM_DIR OR NOT NATIVE_Clang_DIR)
75     message(FATAL_ERROR
76       "Crosscompiling standalone requires the variables NATIVE_{CLANG,LLVM}_DIR
77       for building the native lldb-tblgen used during the build process.")
78   endif()
79   llvm_create_cross_target(lldb NATIVE "" Release
80     -DLLVM_DIR=${NATIVE_LLVM_DIR}
81     -DClang_DIR=${NATIVE_Clang_DIR})
82 endif()
83
84 # TableGen
85 add_subdirectory(utils/TableGen)
86
87 add_subdirectory(source)
88 add_subdirectory(tools)
89 add_subdirectory(docs)
90
91 option(LLDB_INCLUDE_TESTS "Generate build targets for the LLDB unit tests." ${LLVM_INCLUDE_TESTS})
92 if(LLDB_INCLUDE_TESTS)
93   add_subdirectory(test)
94   add_subdirectory(unittests)
95   add_subdirectory(utils/lit-cpuid)
96   add_subdirectory(utils/lldb-dotest)
97 endif()
98
99 if (LLDB_ENABLE_PYTHON)
100   get_target_property(lldb_bindings_dir swig_wrapper BINARY_DIR)
101
102   if(LLDB_BUILD_FRAMEWORK)
103     set(lldb_python_build_path "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/LLDB.framework/Resources/Python/lldb")
104   else()
105     set(lldb_python_build_path "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${LLDB_PYTHON_RELATIVE_PATH}/lldb")
106   endif()
107
108   # Add a Post-Build Event to copy over Python files and create the symlink
109   # to liblldb.so for the Python API(hardlink on Windows).
110   add_custom_target(finish_swig ALL VERBATIM
111     COMMAND ${CMAKE_COMMAND} -E make_directory ${lldb_python_build_path}
112     DEPENDS ${lldb_bindings_dir}/lldb.py
113     COMMENT "Python script sym-linking LLDB Python API")
114
115   if(NOT LLDB_USE_SYSTEM_SIX)
116     add_custom_command(TARGET finish_swig POST_BUILD VERBATIM
117       COMMAND ${CMAKE_COMMAND} -E copy
118         "${LLDB_SOURCE_DIR}/third_party/Python/module/six/six.py"
119         "${lldb_python_build_path}/../six.py")
120   endif()
121
122   add_custom_command(TARGET finish_swig POST_BUILD VERBATIM
123     COMMAND ${CMAKE_COMMAND} -E copy
124       "${lldb_bindings_dir}/lldb.py"
125       "${lldb_python_build_path}/__init__.py")
126
127   function(create_python_package pkg_dir)
128     cmake_parse_arguments(ARG "NOINIT" "" "FILES" ${ARGN})
129     if(ARG_FILES)
130       set(copy_cmd COMMAND ${CMAKE_COMMAND} -E copy ${ARG_FILES} ${pkg_dir})
131     endif()
132     if(NOT ARG_NOINIT)
133       set(init_cmd COMMAND ${PYTHON_EXECUTABLE}
134           "${LLDB_SOURCE_DIR}/bindings/python/createPythonInit.py"
135           "${pkg_dir}" ${ARG_FILES})
136     endif()
137     add_custom_command(TARGET finish_swig POST_BUILD VERBATIM
138       COMMAND ${CMAKE_COMMAND} -E make_directory ${pkg_dir}
139       ${copy_cmd}
140       ${init_cmd}
141       WORKING_DIRECTORY ${lldb_python_build_path})
142   endfunction()
143
144   add_custom_command(TARGET finish_swig POST_BUILD VERBATIM
145     COMMAND ${CMAKE_COMMAND} -E copy
146       "${LLDB_SOURCE_DIR}/source/Interpreter/embedded_interpreter.py" ${lldb_python_build_path})
147
148   # Distribute the examples as python packages.
149   create_python_package("formatters/cpp"
150     FILES "${LLDB_SOURCE_DIR}/examples/synthetic/gnu_libstdcpp.py"
151           "${LLDB_SOURCE_DIR}/examples/synthetic/libcxx.py")
152
153   create_python_package("formatters"
154     FILES "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/cache.py"
155           "${LLDB_SOURCE_DIR}/examples/summaries/synth.py"
156           "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/metrics.py"
157           "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/attrib_fromdict.py"
158           "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/Logger.py")
159
160   create_python_package("utils"
161     FILES "${LLDB_SOURCE_DIR}/examples/python/in_call_stack.py"
162           "${LLDB_SOURCE_DIR}/examples/python/symbolication.py")
163
164   if(APPLE)
165     create_python_package("macosx"
166       FILES "${LLDB_SOURCE_DIR}/examples/python/crashlog.py"
167             "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap.py")
168
169     create_python_package("macosx/heap"
170       FILES "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap/heap_find.cpp"
171             "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap/Makefile"
172             NOINIT)
173
174     create_python_package("diagnose"
175       FILES "${LLDB_SOURCE_DIR}/examples/python/diagnose_unwind.py"
176             "${LLDB_SOURCE_DIR}/examples/python/diagnose_nsstring.py")
177   endif()
178
179   function(create_relative_symlink target dest_file output_dir output_name)
180     get_filename_component(dest_file ${dest_file} ABSOLUTE)
181     get_filename_component(output_dir ${output_dir} ABSOLUTE)
182     file(RELATIVE_PATH rel_dest_file ${output_dir} ${dest_file})
183     if(CMAKE_HOST_UNIX)
184       set(LLVM_LINK_OR_COPY create_symlink)
185     else()
186       set(LLVM_LINK_OR_COPY copy)
187     endif()
188     add_custom_command(TARGET ${target} POST_BUILD VERBATIM
189       COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} ${rel_dest_file} ${output_name}
190       WORKING_DIRECTORY ${output_dir})
191   endfunction()
192
193   if(LLDB_BUILD_FRAMEWORK)
194     set(LIBLLDB_SYMLINK_DEST "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/LLDB.framework/LLDB")
195   else()
196     set(LIBLLDB_SYMLINK_DEST "${LLVM_SHLIB_OUTPUT_INTDIR}/liblldb${CMAKE_SHARED_LIBRARY_SUFFIX}")
197   endif()
198   if(WIN32)
199     if(CMAKE_BUILD_TYPE STREQUAL Debug)
200       set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb_d.pyd")
201     else()
202       set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.pyd")
203     endif()
204   else()
205     set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.so")
206   endif()
207   create_relative_symlink(finish_swig ${LIBLLDB_SYMLINK_DEST}
208                           ${lldb_python_build_path} ${LIBLLDB_SYMLINK_OUTPUT_FILE})
209
210   if(NOT LLDB_BUILD_FRAMEWORK)
211     set(LLDB_ARGDUMPER_FILENAME "lldb-argdumper${CMAKE_EXECUTABLE_SUFFIX}")
212     create_relative_symlink(finish_swig "${LLVM_RUNTIME_OUTPUT_INTDIR}/${LLDB_ARGDUMPER_FILENAME}"
213                             ${lldb_python_build_path} ${LLDB_ARGDUMPER_FILENAME})
214   endif()
215
216   add_dependencies(finish_swig swig_wrapper liblldb lldb-argdumper)
217   set_target_properties(finish_swig swig_wrapper PROPERTIES FOLDER "lldb misc")
218
219   # Ensure we do the python post-build step when building lldb.
220   add_dependencies(lldb finish_swig)
221
222   # Install the LLDB python module
223   if(LLDB_BUILD_FRAMEWORK)
224     set(LLDB_PYTHON_INSTALL_PATH ${LLDB_FRAMEWORK_INSTALL_DIR}/LLDB.framework/Resources/Python)
225   else()
226     set(LLDB_PYTHON_INSTALL_PATH ${LLDB_PYTHON_RELATIVE_PATH})
227   endif()
228   add_custom_target(lldb-python-scripts)
229   add_dependencies(lldb-python-scripts finish_swig)
230   install(DIRECTORY ${lldb_python_build_path}/../
231           DESTINATION ${LLDB_PYTHON_INSTALL_PATH}
232           COMPONENT lldb-python-scripts)
233   if (NOT LLVM_ENABLE_IDE)
234     add_llvm_install_targets(install-lldb-python-scripts
235                              COMPONENT lldb-python-scripts
236                              DEPENDS lldb-python-scripts)
237   endif()
238
239   # Add a Post-Build Event to copy the custom Python DLL to the lldb binaries dir so that Windows can find it when launching
240   # lldb.exe or any other executables that were linked with liblldb.
241   if (WIN32 AND NOT "${PYTHON_DLL}" STREQUAL "")
242     # When using the Visual Studio CMake generator the lldb binaries end up in Release/bin, Debug/bin etc.
243     file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin" LLDB_BIN_DIR)
244     file(TO_NATIVE_PATH "${PYTHON_DLL}" PYTHON_DLL_NATIVE_PATH)
245     add_custom_command(
246       TARGET finish_swig
247       POST_BUILD
248       COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_DLL_NATIVE_PATH} ${LLDB_BIN_DIR} VERBATIM
249       COMMENT "Copying Python DLL to LLDB binaries directory.")
250   endif ()
251 endif ()
252
253 if(LLDB_BUILT_STANDALONE AND NOT LLVM_ENABLE_IDE)
254   llvm_distribution_add_targets()
255 endif()