diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index a21adcfbdbd6035380b9b785d8cecf59610f5330..ba5ce543de818093e0f5e4c597228c5fc24efb77 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -89,8 +89,10 @@ namespace { // save off initial state at the beginning, and restore it at the end struct InitializePythonRAII { public: - InitializePythonRAII() { - InitializePythonHome(); + // OHOS_LOCAL begin + InitializePythonRAII(bool running_ut): m_running_ut(running_ut) { + InitializePythonHome(m_running_ut); + // OHOS_LOCAL end #ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE // Python's readline is incompatible with libedit being linked into lldb. @@ -136,11 +138,21 @@ public: } private: - void InitializePythonHome() { + // OHOS_LOCAL begin + bool m_running_ut = false; + void InitializePythonHome(bool running_ut = false) { + // OHOS_LOCAL end #if LLDB_EMBED_PYTHON_HOME typedef wchar_t *str_type; - static str_type g_python_home = []() -> str_type { + static str_type g_python_home = [&]() -> str_type { const char *lldb_python_home = LLDB_PYTHON_HOME; + // OHOS_LOCAL begin + if (running_ut) { + // python path: llvm/out/llvm_make/python3 + // test cases path: llvm/out/llvm_make/tools/lldb/unittests/ScriptInterpreter/Python/ + lldb_python_home = "../../../../../python3"; + } + // OHOS_LOCAL end const char *absolute_python_home = nullptr; llvm::SmallString<64> path; if (llvm::sys::path::is_absolute(lldb_python_home)) { @@ -3143,14 +3155,14 @@ ScriptInterpreterPythonImpl::AcquireInterpreterLock() { return py_lock; } -void ScriptInterpreterPythonImpl::Initialize() { +void ScriptInterpreterPythonImpl::Initialize(bool running_ut) { // OHOS_LOCAL LLDB_SCOPED_TIMER(); // RAII-based initialization which correctly handles multiple-initialization, // version- specific differences among Python 2 and Python 3, and saving and // restoring various other pieces of state that can get mucked with during // initialization. - InitializePythonRAII initialize_guard; + InitializePythonRAII initialize_guard(running_ut); // OHOS_LOCAL LLDBSwigPyInit(); diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h index 3b80c67d201ad99a0cbf3af2809dc0a086091944..3ab22ecbb2bb3a65646005144becca17dc7c28b6 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h @@ -341,7 +341,7 @@ public: static bool WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id); - static void Initialize(); + static void Initialize(bool running_ut = false); // OHOS_LOCAL class SynchronicityHandler { private: diff --git a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp index 6ac4606b5a845a539f5713440ccc2565dcbb7aeb..58b161fd151324d00581637119d7ea600c0b3556 100644 --- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp +++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp @@ -29,7 +29,7 @@ void PythonTestSuite::SetUp() { HostInfoBase::Initialize(); // ScriptInterpreterPython::Initialize() depends on HostInfo being // initializedso it can compute the python directory etc. - TestScriptInterpreterPython::Initialize(); + TestScriptInterpreterPython::Initialize(true); // OHOS_LOCAL // Although we don't care about concurrency for the purposes of running // this test suite, Python requires the GIL to be locked even for diff --git a/llvm-build/build.py b/llvm-build/build.py index 1d9a012b113e6b43f84de2d552de436c3d57f494..5cda52ad9a2ef7aefc77e6f56c1127c1594ed9e6 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -61,6 +61,8 @@ class BuildConfig(): self.OPENHOS_SFX = '-linux-ohos' self.LITEOS_SFX = '-liteos-ohos' self.LLDB_PY_VERSION = '3.10' + self.LLDB_PY_DIR = 'python3' + self.LLDB_PY_DETAILED_VERSION = self.LLDB_PY_VERSION + '.2' self.CLANG_VERSION = prebuilts_clang_version self.MINGW_TRIPLE = 'x86_64-windows-gnu' logging.basicConfig(level=logging.INFO) @@ -380,6 +382,14 @@ class BuildUtils(object): defines['COMPILER_RT_BUILD_XRAY'] = 'OFF' return defines + def get_python_dir(self): + python_dir = os.path.join(self.build_config.REPOROOT_DIR, 'prebuilts', self.build_config.LLDB_PY_DIR) + if self.host_is_linux(): + python_dir = os.path.join(python_dir, 'linux-x86') + if self.host_is_darwin(): + python_dir = os.path.join(python_dir, 'darwin-x86') + python_dir = os.path.join(python_dir, self.build_config.LLDB_PY_DETAILED_VERSION) + return python_dir class LlvmCore(BuildUtils): @@ -439,10 +449,11 @@ class LlvmCore(BuildUtils): llvm_defines['LIBUNWIND_ENABLE_SHARED'] = 'OFF' llvm_defines['LLDB_ENABLE_LIBEDIT'] = 'OFF' llvm_defines['LLDB_NO_DEBUGSERVER'] = 'ON' - llvm_defines['LLDB_ENABLE_PYTHON'] = 'OFF' llvm_defines['COMPILER_RT_BUILD_LIBFUZZER'] = 'OFF' llvm_defines['LLVM_BUILD_EXTERNAL_COMPILER_RT'] = 'ON' llvm_defines['LLVM_ENABLE_ZSTD'] = 'OFF' + llvm_defines['Python3_LIBRARIES'] = os.path.join(self.get_python_dir(), + 'lib', 'libpython%s.dylib' % self.build_config.LLDB_PY_VERSION) def llvm_compile_linux_defines(self, llvm_defines, @@ -465,13 +476,13 @@ class LlvmCore(BuildUtils): llvm_defines['COMPILER_RT_BUILD_ORC'] = 'OFF' llvm_defines['LIBUNWIND_USE_COMPILER_RT'] = 'ON' llvm_defines['LLVM_BINUTILS_INCDIR'] = '/usr/include' - llvm_defines['LLDB_ENABLE_PYTHON'] = 'OFF' + llvm_defines['Python3_LIBRARIES'] = os.path.join(self.get_python_dir(), + 'lib', 'libpython%s.so' % self.build_config.LLDB_PY_VERSION) if not build_instrumented and not no_lto and not debug_build: llvm_defines['LLVM_ENABLE_LTO'] = 'Thin' - @staticmethod - def llvm_compile_llvm_defines(llvm_defines, llvm_root, cflags, ldflags): + def llvm_compile_llvm_defines(self, llvm_defines, llvm_root, cflags, ldflags): llvm_defines['LLVM_ENABLE_PROJECTS'] = 'clang;lld;clang-tools-extra;openmp;lldb' llvm_defines['LLVM_ENABLE_RUNTIMES'] = 'libunwind;libcxxabi;libcxx;compiler-rt' llvm_defines['LLVM_ENABLE_BINDINGS'] = 'OFF' @@ -494,6 +505,13 @@ class LlvmCore(BuildUtils): llvm_defines['CMAKE_SHARED_LINKER_FLAGS'] = ldflags llvm_defines['CMAKE_MODULE_LINKER_FLAGS'] = ldflags llvm_defines['CMAKE_POSITION_INDEPENDENT_CODE'] = 'ON' + llvm_defines['LLDB_ENABLE_PYTHON'] = 'ON' + llvm_defines['LLDB_EMBED_PYTHON_HOME'] = 'ON' + llvm_defines['LLDB_PYTHON_HOME'] = os.path.join('..', self.build_config.LLDB_PY_DIR) + llvm_defines['Python3_INCLUDE_DIRS'] = os.path.join(self.get_python_dir(), + 'include', 'python%s' % self.build_config.LLDB_PY_VERSION) + llvm_defines['Python3_EXECUTABLE'] = os.path.join(self.get_python_dir(), 'bin', self.build_config.LLDB_PY_DIR) + llvm_defines['SWIG_EXECUTABLE'] = self.find_program('swig') def llvm_compile(self, build_name, @@ -1317,9 +1335,13 @@ class LlvmPackage(BuildUtils): self.merge_tree(os.path.join(py_root, 'lib'), os.path.join(bin_root, 'python', 'lib', 'python%s' % self.build_config.LLDB_PY_VERSION)) - - - + def copy_python_to_host(self, install_dir): + if(self.host_is_linux()): + need_file = 'libpython' + self.build_config.LLDB_PY_VERSION + '.so.1.0' + if(self.host_is_darwin()): + need_file = 'libpython' + self.build_config.LLDB_PY_VERSION + '.dylib' + shutil.copyfile(os.path.join(self.get_python_dir(), "lib", need_file), os.path.join(install_dir, 'lib', need_file)) + self.check_copy_tree(self.get_python_dir(), os.path.join(install_dir, self.build_config.LLDB_PY_DIR)) def windows_lib_files_operation(self, lib_files, lib_dir, install_dir): @@ -1651,6 +1673,9 @@ def main(): build_config.no_lto, build_config.build_instrumented, build_config.xunit_xml_output) + llvm_make = build_utils.merge_out_path('llvm_make') + llvm_package.copy_python_to_host(llvm_make) + llvm_package.copy_python_to_host(llvm_install) llvm_core.set_clang_version(llvm_install) diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index 487bd1ae78423f2d7cfb08ba868b98728b34066b..945a38daf1423660349a5d55ae06a9926a6478af 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -66,6 +66,7 @@ CLANG_LINUX_BUILD=clang_linux-x86_64-025192-20230330 copy_config_linux_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},https://mirrors.huaweicloud.com/openharmony/compiler/clang/15.0.4-025192/linux/${CLANG_LINUX_BUILD}.tar.bz2 +prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz """ @@ -73,8 +74,12 @@ CLANG_DARWIN_BUILD=clang_darwin-x86_64-8e906c-20230415 copy_config_darwin_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},http://mirrors.huaweicloud.com/harmonyos/compiler/clang/15.0.4-8e906c/darwin/${CLANG_DARWIN_BUILD}.tar.bz2 +prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz """ +copy_config_darwin_arm64=""" +prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz +""" if [[ "${host_platform}" == "linux" ]]; then if [[ "${host_cpu}" == "x86_64" ]]; then @@ -88,6 +93,7 @@ elif [[ "${host_platform}" == "darwin" ]]; then copy_config+=${copy_config_darwin_x86_64} echo "add x86-64 mac here" elif [[ "${host_cpu}" == "arm64" ]]; then + copy_config+=${copy_config_darwin_arm64} echo "add m1 config here" else echo "unknwon host_cpu - ${host_cpu} for darwin"