• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1add_custom_command(
2  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
3  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
4  DEPENDS ${SWIG_SOURCES}
5  DEPENDS ${SWIG_INTERFACES}
6  DEPENDS ${SWIG_HEADERS}
7  COMMAND ${SWIG_EXECUTABLE}
8      ${SWIG_COMMON_FLAGS}
9      -I${CMAKE_CURRENT_SOURCE_DIR}
10      -c++
11      -shadow
12      -python
13      -threads
14      -outdir ${CMAKE_CURRENT_BINARY_DIR}
15      -o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
16      ${CMAKE_CURRENT_SOURCE_DIR}/python.swig
17  VERBATIM
18  COMMENT "Building LLDB Python wrapper")
19
20add_custom_target(swig_wrapper_python ALL DEPENDS
21  ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
22  ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
23)
24
25function(create_python_package swig_target working_dir pkg_dir)
26  cmake_parse_arguments(ARG "NOINIT" "" "FILES" ${ARGN})
27  if(ARG_FILES)
28    set(copy_cmd COMMAND ${CMAKE_COMMAND} -E copy ${ARG_FILES} ${pkg_dir})
29  endif()
30  if(NOT ARG_NOINIT)
31    set(init_cmd COMMAND ${Python3_EXECUTABLE}
32        "${LLDB_SOURCE_DIR}/bindings/python/createPythonInit.py"
33        "${pkg_dir}" ${ARG_FILES})
34  endif()
35  add_custom_command(TARGET ${swig_target} POST_BUILD VERBATIM
36    COMMAND ${CMAKE_COMMAND} -E make_directory ${pkg_dir}
37    ${copy_cmd}
38    ${init_cmd}
39    WORKING_DIRECTORY ${working_dir})
40endfunction()
41
42function(create_relative_symlink swig_target dest_file output_dir output_name)
43  get_filename_component(dest_file ${dest_file} ABSOLUTE)
44  get_filename_component(output_dir ${output_dir} ABSOLUTE)
45  file(RELATIVE_PATH rel_dest_file ${output_dir} ${dest_file})
46  if(CMAKE_HOST_UNIX)
47    set(LLVM_LINK_OR_COPY create_symlink)
48  else()
49    set(LLVM_LINK_OR_COPY copy)
50  endif()
51  add_custom_command(TARGET ${swig_target} POST_BUILD VERBATIM
52    COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} ${rel_dest_file} ${output_name}
53    WORKING_DIRECTORY ${output_dir})
54endfunction()
55
56function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_target_dir)
57  # Add a Post-Build Event to copy over Python files and create the symlink to
58  # liblldb.so for the Python API(hardlink on Windows).
59  add_custom_target(${swig_target} ALL VERBATIM
60    COMMAND ${CMAKE_COMMAND} -E make_directory ${lldb_python_target_dir}
61    DEPENDS ${lldb_python_bindings_dir}/lldb.py
62    COMMENT "Python script sym-linking LLDB Python API")
63
64  if(NOT LLDB_USE_SYSTEM_SIX)
65    add_custom_command(TARGET ${swig_target} POST_BUILD VERBATIM
66      COMMAND ${CMAKE_COMMAND} -E copy
67        "${LLDB_SOURCE_DIR}/third_party/Python/module/six/six.py"
68        "${lldb_python_target_dir}/../six.py")
69  endif()
70
71  add_custom_command(TARGET ${swig_target} POST_BUILD VERBATIM
72    COMMAND ${CMAKE_COMMAND} -E copy
73      "${lldb_python_bindings_dir}/lldb.py"
74      "${lldb_python_target_dir}/__init__.py")
75
76  add_custom_command(TARGET ${swig_target} POST_BUILD VERBATIM
77    COMMAND ${CMAKE_COMMAND} -E copy
78      "${LLDB_SOURCE_DIR}/source/Interpreter/embedded_interpreter.py"
79      "${lldb_python_target_dir}")
80
81  # Distribute the examples as python packages.
82  create_python_package(
83    ${swig_target}
84    ${lldb_python_target_dir}
85    "formatters/cpp"
86    FILES "${LLDB_SOURCE_DIR}/examples/synthetic/gnu_libstdcpp.py"
87          "${LLDB_SOURCE_DIR}/examples/synthetic/libcxx.py")
88
89  create_python_package(
90    ${swig_target}
91    ${lldb_python_target_dir}
92    "formatters"
93    FILES "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/cache.py"
94          "${LLDB_SOURCE_DIR}/examples/summaries/synth.py"
95          "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/metrics.py"
96          "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/attrib_fromdict.py"
97          "${LLDB_SOURCE_DIR}/examples/summaries/cocoa/Logger.py")
98
99  create_python_package(
100    ${swig_target}
101    ${lldb_python_target_dir}
102    "utils"
103    FILES "${LLDB_SOURCE_DIR}/examples/python/in_call_stack.py"
104          "${LLDB_SOURCE_DIR}/examples/python/symbolication.py")
105
106  if(APPLE)
107    create_python_package(
108      ${swig_target}
109      ${lldb_python_target_dir} "macosx"
110      FILES "${LLDB_SOURCE_DIR}/examples/python/crashlog.py"
111            "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap.py")
112
113    create_python_package(
114      ${swig_target}
115      ${lldb_python_target_dir} "macosx/heap"
116      FILES "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap/heap_find.cpp"
117            "${LLDB_SOURCE_DIR}/examples/darwin/heap_find/heap/Makefile"
118            NOINIT)
119
120    create_python_package(
121      ${swig_target}
122      ${lldb_python_target_dir} "diagnose"
123      FILES "${LLDB_SOURCE_DIR}/examples/python/diagnose_unwind.py"
124            "${LLDB_SOURCE_DIR}/examples/python/diagnose_nsstring.py")
125  endif()
126
127  if(LLDB_BUILD_FRAMEWORK)
128    set(LIBLLDB_SYMLINK_DEST "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/LLDB.framework/LLDB")
129  else()
130    set(LIBLLDB_SYMLINK_DEST "${LLVM_SHLIB_OUTPUT_INTDIR}/liblldb${CMAKE_SHARED_LIBRARY_SUFFIX}")
131  endif()
132  if(WIN32)
133    if(CMAKE_BUILD_TYPE STREQUAL Debug)
134      set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb_d.pyd")
135    else()
136      set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.pyd")
137    endif()
138  else()
139    set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.so")
140  endif()
141  create_relative_symlink(${swig_target} ${LIBLLDB_SYMLINK_DEST}
142                          ${lldb_python_target_dir} ${LIBLLDB_SYMLINK_OUTPUT_FILE})
143
144  if(NOT LLDB_BUILD_FRAMEWORK)
145    set(LLDB_ARGDUMPER_FILENAME "lldb-argdumper${CMAKE_EXECUTABLE_SUFFIX}")
146    create_relative_symlink(${swig_target} "${LLVM_RUNTIME_OUTPUT_INTDIR}/${LLDB_ARGDUMPER_FILENAME}"
147                            ${lldb_python_target_dir} ${LLDB_ARGDUMPER_FILENAME})
148  endif()
149
150  add_dependencies(${swig_target} swig_wrapper_python liblldb lldb-argdumper)
151  set_target_properties(${swig_target} swig_wrapper_python PROPERTIES FOLDER "lldb misc")
152
153  # Ensure we do the python post-build step when building lldb.
154  add_dependencies(lldb ${swig_target})
155
156  # Install the LLDB python module
157  if(LLDB_BUILD_FRAMEWORK)
158    set(LLDB_PYTHON_INSTALL_PATH ${LLDB_FRAMEWORK_INSTALL_DIR}/LLDB.framework/Resources/Python)
159  else()
160    set(LLDB_PYTHON_INSTALL_PATH ${LLDB_PYTHON_RELATIVE_PATH})
161  endif()
162  if (NOT CMAKE_CFG_INTDIR STREQUAL  ".")
163    string(REPLACE ${CMAKE_CFG_INTDIR} "\$\{CMAKE_INSTALL_CONFIG_NAME\}" LLDB_PYTHON_INSTALL_PATH ${LLDB_PYTHON_INSTALL_PATH})
164    string(REPLACE ${CMAKE_CFG_INTDIR} "\$\{CMAKE_INSTALL_CONFIG_NAME\}" lldb_python_target_dir ${lldb_python_target_dir})
165  endif()
166  set(python_scripts_target "${swig_target}-scripts")
167  set(python_scripts_install_target "install-${python_scripts_target}")
168  add_custom_target(${python_scripts_target})
169  add_dependencies(${python_scripts_target} ${swig_target})
170  install(DIRECTORY ${lldb_python_target_dir}/../
171          DESTINATION ${LLDB_PYTHON_INSTALL_PATH}
172          COMPONENT ${python_scripts_target})
173  if (NOT LLVM_ENABLE_IDE)
174    add_llvm_install_targets(${python_scripts_install_target}
175                             COMPONENT ${python_scripts_target}
176                             DEPENDS ${python_scripts_target})
177  endif()
178
179  # Add a Post-Build Event to copy the custom Python DLL to the lldb binaries dir so that Windows can find it when launching
180  # lldb.exe or any other executables that were linked with liblldb.
181  if (WIN32 AND NOT "${PYTHON_DLL}" STREQUAL "")
182    # When using the Visual Studio CMake generator the lldb binaries end up in Release/bin, Debug/bin etc.
183    file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin" LLDB_BIN_DIR)
184    file(TO_NATIVE_PATH "${PYTHON_DLL}" PYTHON_DLL_NATIVE_PATH)
185    add_custom_command(
186      TARGET ${swig_target}
187      POST_BUILD
188      COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_DLL_NATIVE_PATH} ${LLDB_BIN_DIR} VERBATIM
189      COMMENT "Copying Python DLL to LLDB binaries directory.")
190  endif()
191endfunction()
192