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