From 2388d10d2a8f5ae882288a33b756d57cc2a5b7e3 Mon Sep 17 00:00:00 2001 From: liujia178 Date: Fri, 1 Mar 2024 16:39:52 +0800 Subject: [PATCH] [lldb] Add support lzma in lldb Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8PEAT Signed-off-by: liujia178 --- lldb/cmake/modules/LLDBConfig.cmake | 6 ++ lldb/include/lldb/Host/Config.h.cmake | 2 + lldb/source/Host/common/LZMA.cpp | 97 +++++++++++++++++++++++++++ llvm-build/MakeLiblzma | 65 ++++++++++++++++++ llvm-build/build.py | 86 +++++++++++++++++++++++- 5 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 llvm-build/MakeLiblzma diff --git a/lldb/cmake/modules/LLDBConfig.cmake b/lldb/cmake/modules/LLDBConfig.cmake index 55d1aa17b46b..209f8a1cd348 100644 --- a/lldb/cmake/modules/LLDBConfig.cmake +++ b/lldb/cmake/modules/LLDBConfig.cmake @@ -58,7 +58,13 @@ endmacro() add_optional_dependency(LLDB_ENABLE_LIBEDIT "Enable editline support in LLDB" LibEdit LibEdit_FOUND) add_optional_dependency(LLDB_ENABLE_CURSES "Enable curses support in LLDB" CursesAndPanel CURSESANDPANEL_FOUND) +if (NOT LLDB_ENABLE_LZMA_7ZIP) # OHOS_LOCAL add_optional_dependency(LLDB_ENABLE_LZMA "Enable LZMA compression support in LLDB" LibLZMA LIBLZMA_FOUND) +# OHOS_LOCAL begin +else() +message(STATUS "Enable LZMA-7zip compression support in LLDB: TRUE") +endif() +# OHOS_LOCAL end add_optional_dependency(LLDB_ENABLE_LUA "Enable Lua scripting support in LLDB" LuaAndSwig LUAANDSWIG_FOUND) add_optional_dependency(LLDB_ENABLE_PYTHON "Enable Python scripting support in LLDB" PythonAndSwig PYTHONANDSWIG_FOUND) add_optional_dependency(LLDB_ENABLE_LIBXML2 "Enable Libxml 2 support in LLDB" LibXml2 LIBXML2_FOUND VERSION 2.8) diff --git a/lldb/include/lldb/Host/Config.h.cmake b/lldb/include/lldb/Host/Config.h.cmake index 9ee0b9dcd04f..ad69d0bfe743 100644 --- a/lldb/include/lldb/Host/Config.h.cmake +++ b/lldb/include/lldb/Host/Config.h.cmake @@ -32,6 +32,8 @@ #cmakedefine01 LLDB_ENABLE_TERMIOS #cmakedefine01 LLDB_ENABLE_LZMA +// OHOS_LOCAL +#cmakedefine01 LLDB_ENABLE_LZMA_7ZIP #cmakedefine01 LLDB_ENABLE_CURSES diff --git a/lldb/source/Host/common/LZMA.cpp b/lldb/source/Host/common/LZMA.cpp index 5b457f07afca..c94c636b517e 100644 --- a/lldb/source/Host/common/LZMA.cpp +++ b/lldb/source/Host/common/LZMA.cpp @@ -11,7 +11,16 @@ #include "llvm/Support/Error.h" #if LLDB_ENABLE_LZMA +// OHOS_LOCAL begin +#if !LLDB_ENABLE_LZMA_7ZIP #include +#else +#include <7zCrc.h> +#include +#include +#define EXPAND_FACTOR 2 +#endif +// OHOS_LOCAL end #endif // LLDB_ENABLE_LZMA namespace lldb_private { @@ -34,6 +43,8 @@ llvm::Error uncompress(llvm::ArrayRef InputBuffer, bool isAvailable() { return true; } +// OHOS_LOCAL +#if !LLDB_ENABLE_LZMA_7ZIP static const char *convertLZMACodeToString(lzma_ret Code) { switch (Code) { case LZMA_STREAM_END: @@ -139,7 +150,93 @@ llvm::Error uncompress(llvm::ArrayRef InputBuffer, return llvm::Error::success(); } +// OHOS_LOCAL begin +#else +static const char *convertLZMACodeToString(SRes Code) { + switch (Code) { + case SZ_ERROR_DATA: + return "lzma error: SZ_ERROR_DATA"; + case SZ_ERROR_MEM: + return "lzma error: SZ_ERROR_MEM"; + case SZ_ERROR_CRC: + return "lzma error: SZ_ERROR_CRC"; + case SZ_ERROR_UNSUPPORTED: + return "lzma error: SZ_ERROR_UNSUPPORTED"; + case SZ_ERROR_PARAM: + return "lzma error: SZ_ERROR_PARAM"; + case SZ_ERROR_INPUT_EOF: + return "lzma error: SZ_ERROR_INPUT_EOF"; + case SZ_ERROR_OUTPUT_EOF: + return "lzma error: SZ_ERROR_OUTPUT_EOF"; + case SZ_ERROR_READ: + return "lzma error: SZ_ERROR_READ"; + case SZ_ERROR_WRITE: + return "lzma error: SZ_ERROR_WRITE"; + case SZ_ERROR_PROGRESS: + return "lzma error: SZ_ERROR_PROGRESS"; + case SZ_ERROR_FAIL: + return "lzma error: SZ_ERROR_FAIL"; + case SZ_ERROR_THREAD: + return "lzma error: SZ_ERROR_THREAD"; + case SZ_ERROR_ARCHIVE: + return "lzma error: SZ_ERROR_ARCHIVE"; + case SZ_ERROR_NO_ARCHIVE: + return "lzma error: SZ_ERROR_NO_ARCHIVE"; + default: + llvm_unreachable("unknown or unexpected lzma status code"); + } +} + +static void *XzAlloc(ISzAllocPtr, size_t size) { + return malloc(size); +} +static void XzFree(ISzAllocPtr, void *address) { + free(address); +} + +llvm::Error uncompress(llvm::ArrayRef InputBuffer, + llvm::SmallVectorImpl &Uncompressed) { + const uint8_t *src = InputBuffer.data(); + ISzAlloc alloc; + CXzUnpacker state; + alloc.Alloc = XzAlloc; + alloc.Free = XzFree; + XzUnpacker_Construct(&state, &alloc); + CrcGenerateTable(); + Crc64GenerateTable(); + size_t srcOff = 0; + size_t dstOff = 0; + size_t srcLen = InputBuffer.size(); + std::vector dst(srcLen, 0); + ECoderStatus status = CODER_STATUS_NOT_FINISHED; + while (status == CODER_STATUS_NOT_FINISHED) { + dst.resize(dst.size() * EXPAND_FACTOR); + size_t srcRemain = srcLen - srcOff; + size_t dstRemain = dst.size() - dstOff; + SRes res = XzUnpacker_Code(&state, + reinterpret_cast(&dst[dstOff]), &dstRemain, + reinterpret_cast(&src[srcOff]), &srcRemain, + true, CODER_FINISH_ANY, &status); + if (res != SZ_OK) { + XzUnpacker_Free(&state); + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "XzUnpacker_Code()=%s", convertLZMACodeToString(res)); + } + srcOff += srcRemain; + dstOff += dstRemain; + } + XzUnpacker_Free(&state); + if (!XzUnpacker_IsStreamWasFinished(&state)) { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "XzUnpacker_IsStreamWasFinished()=lzma error: return False"); + } + Uncompressed.resize(dstOff); + memcpy(Uncompressed.data(), dst.data(), dstOff); + return llvm::Error::success(); +} +#endif +// OHOS_LOCAL end #endif // LLDB_ENABLE_LZMA } // end of namespace lzma diff --git a/llvm-build/MakeLiblzma b/llvm-build/MakeLiblzma new file mode 100644 index 000000000000..b78b86f3496b --- /dev/null +++ b/llvm-build/MakeLiblzma @@ -0,0 +1,65 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +SYSROOT := +INSTALL_DIR := +TARGET_TRIPLE := +CC := +SRCS := 7zAlloc.c 7zArcIn.c 7zBuf2.c 7zBuf.c 7zCrc.c 7zCrcOpt.c 7zDec.c 7zFile.c 7zStream.c Aes.c AesOpt.c Alloc.c Bcj2.c Bra86.c Bra.c BraIA64.c CpuArch.c Delta.c LzFind.c Lzma2Dec.c Lzma2Enc.c Lzma86Dec.c Lzma86Enc.c LzmaDec.c LzmaEnc.c LzmaLib.c Ppmd7.c Ppmd7Dec.c Ppmd7Enc.c Sha256.c Sha256Opt.c Sort.c Xz.c XzCrc64.c XzCrc64Opt.c XzDec.c XzEnc.c XzIn.c +SRC_PREFIX := + +ifeq ($(TARGET_TRIPLE),linux-x86_64) +CFLAGS := --target=x86_64-unknown-linux-gnu -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -shared -fuse-ld=lld +TARGET := liblzma.so +else +ifeq ($(TARGET_TRIPLE),windows-x86_64) +CFLAGS := --target=x86_64-pc-windows-gnu --sysroot=$(SYSROOT) -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -shared -fuse-ld=lld --rtlib=compiler-rt -Wl,--out-implib=liblzma.dll.a +TARGET := liblzma.dll +TARGET_A := liblzma.dll.a +else +ifeq ($(findstring darwin,$(TARGET_TRIPLE)),darwin) +SDKROOT := $(shell xcrun --sdk macosx --show-sdk-path) +CFLAGS := -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -dynamiclib -fuse-ld=lld -Wl,-syslibroot,$(SDKROOT) -install_name @rpath/liblzma.dylib +TARGET := liblzma.dylib +else +$(warning *** warning: TARGET_TRIPLE $(TARGET_TRIPLE) has not been set in rights) +endif +endif +endif + +lzma_header_install: + @echo "begin header install" + mkdir -p $(INSTALL_DIR)/include + cp -rf $(SRC_PREFIX)* $(INSTALL_DIR)/include + +$(TARGET): lzma_header_install + $(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(addprefix $(SRC_PREFIX), $(SRCS)) + +.PHONY: clean + +clean: + rm -f $(TARGET) $(SRC_PREFIX)*.o + +.PHONY:install + +install: $(TARGET) + @echo "begin install" + mkdir -p $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) + mv $(TARGET) $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) +ifeq ($(TARGET_TRIPLE),windows-x86_64) + mv $(TARGET_A) $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) +endif + @echo "install success!" diff --git a/llvm-build/build.py b/llvm-build/build.py index 6782645fc3f1..07577568b845 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -65,6 +65,7 @@ class BuildConfig(): self.build_libxml2 = args.build_libxml2 self.lldb_timeout = args.lldb_timeout self.enable_monitoring = args.enable_monitoring + self.enable_lzma_7zip = args.enable_lzma_7zip self.build_libs = args.build_libs self.build_libs_flags = args.build_libs_flags self.compression_format = args.compression_format @@ -224,6 +225,12 @@ class BuildConfig(): default=False, help='Build libxml2 tool') + parser.add_argument( + '--enable-lzma-7zip', + action='store_true', + default=False, + help='Build 7zip tool and enable LZMA compression support in LLDB') + parser.add_argument( '--lldb-timeout', action='store_true', @@ -631,6 +638,9 @@ class LlvmCore(BuildUtils): llvm_defines['CURSES_LIBRARIES'] = ncurses_libs llvm_defines['PANEL_LIBRARIES'] = ncurses_libs + if self.build_config.enable_lzma_7zip: + llvm_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', self.use_platform(), 'liblzma.dylib') + if self.build_config.build_libedit: llvm_defines['LibEdit_LIBRARIES'] = os.path.join(self.get_prebuilts_dir('libedit'), 'lib', 'libedit.0.dylib') @@ -672,6 +682,9 @@ class LlvmCore(BuildUtils): llvm_defines['CURSES_LIBRARIES'] = ncurses_libs llvm_defines['PANEL_LIBRARIES'] = ncurses_libs + if self.build_config.enable_lzma_7zip: + llvm_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', 'linux-x86_64', 'liblzma.so') + if self.build_config.build_libedit: llvm_defines['LibEdit_LIBRARIES'] = os.path.join(self.get_prebuilts_dir('libedit'), 'lib', 'libedit.so.0.0.68') @@ -717,6 +730,12 @@ class LlvmCore(BuildUtils): if self.build_config.build_ncurses and self.get_ncurses_version() is not None: llvm_defines['LLDB_ENABLE_CURSES'] = 'ON' llvm_defines['CURSES_INCLUDE_DIRS'] = os.path.join(self.get_prebuilts_dir('ncurses'), 'include') + + if self.build_config.enable_lzma_7zip: + llvm_defines['LLDB_ENABLE_LZMA'] = 'ON' + llvm_defines['LLDB_ENABLE_LZMA_7ZIP'] = 'ON' + llvm_defines['LIBLZMA_INCLUDE_DIRS'] = self.merge_out_path('lzma', 'include') + if self.build_config.build_libedit: llvm_defines['LLDB_ENABLE_LIBEDIT'] = 'ON' llvm_defines['LibEdit_INCLUDE_DIRS'] = os.path.join(self.get_prebuilts_dir('libedit'), 'include') @@ -849,6 +868,12 @@ class LlvmCore(BuildUtils): if self.build_config.enable_monitoring: windows_defines['LLDB_ENABLE_PERFORMANCE'] = 'ON' + if self.build_config.enable_lzma_7zip: + windows_defines['LLDB_ENABLE_LZMA'] = 'ON' + windows_defines['LLDB_ENABLE_LZMA_7ZIP'] = 'ON' + windows_defines['LIBLZMA_INCLUDE_DIRS'] = self.merge_out_path('lzma', 'include') + windows_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', 'windows-x86_64', 'liblzma.dll.a') + def llvm_compile_windows_flags(self, windows_defines, windowstool_path, @@ -1086,7 +1111,6 @@ class SysrootComposer(BuildUtils): gn_args += ' is_legacy=true musl_target_multilib=nanlegacy' multi_lib_dir = os.path.join(ohos_lib_dir, 'nanlegacy') sysroot_multi_lib_dir = os.path.join(sysroot_lib_dir, 'nanlegacy') - self.run_hb_build(product_name, target_cpu, target_name, gn_args) ld_musl_lib = os.path.join(sysroot_multi_lib_dir, 'ld-musl-{}.so.1'.format(ld_arch)) self.build_musl_libs(product_name, target_cpu, target_name, multi_lib_dir, sysroot_multi_lib_dir, ld_musl_lib, gn_args) @@ -1710,6 +1734,36 @@ class LlvmLibs(BuildUtils): self.llvm_package.copy_ncurses_to_llvm(llvm_make) self.llvm_package.copy_ncurses_to_llvm(llvm_install) + def build_lzma(self, llvm_install): + self.logger().info('Building lzma') + target_triple = self.use_platform() + liblzma_build_path = self.merge_out_path('lzma') + llvm_clang_prebuilts = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, + 'prebuilts', 'clang', 'ohos', self.use_platform(), + 'clang-%s' % self.build_config.CLANG_VERSION, 'bin', 'clang')) + src_dir = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, 'third_party', 'lzma', 'C')) + cmd = [ 'make', + 'install', + 'CC=%s' % llvm_clang_prebuilts, + 'SRC_PREFIX=%s/' % src_dir, + 'TARGET_TRIPLE=%s' % target_triple, + 'INSTALL_DIR=%s' % liblzma_build_path, + '-f', + 'MakeLiblzma'] + os.chdir(self.build_config.LLVM_BUILD_DIR) + self.check_call(cmd) + + self.logger().info('Copy lzma to llvm install dir is %s', llvm_install) + if self.host_is_darwin(): + shlib_ext = '.dylib' + if self.host_is_linux(): + shlib_ext = '.so' + lzma_file = os.path.join(liblzma_build_path, 'lib', target_triple, 'liblzma' + shlib_ext) + lib_dst_path = os.path.join(llvm_install, 'lib') + if not os.path.exists(lib_dst_path): + os.makedirs(lib_dst_path) + self.check_copy_file(lzma_file, lib_dst_path + '/liblzma' + shlib_ext) + def build_libedit(self, llvm_make, llvm_install): self.logger().info('Building libedit') @@ -2077,6 +2131,12 @@ class LlvmPackage(BuildUtils): if self.build_config.build_libxml2: windows_additional_bin_files += ['libxml2%s' % shlib_ext] + if self.build_config.enable_lzma_7zip: + windows_additional_bin_files += ['liblzma%s' % shlib_ext] + bin_root = os.path.join(install_dir, 'bin') + prebuild_dir = self.merge_out_path('lzma', 'lib', 'windows-x86_64', 'liblzma.dll') + shutil.copyfile(prebuild_dir, os.path.join(bin_root, 'liblzma.dll')) + new_necessary_bin_files = list(set(necessary_bin_files) - set(windows_forbidden_list_bin_files)) new_necessary_bin_files.extend(windows_additional_bin_files) del necessary_bin_files[:] @@ -2370,6 +2430,9 @@ def main(): if build_config.build_ncurses: llvm_libs.build_ncurses(llvm_make, llvm_install) + if build_config.enable_lzma_7zip: + llvm_libs.build_lzma(llvm_install) + if build_config.build_libedit: llvm_libs.build_libedit(llvm_make, llvm_install) @@ -2428,6 +2491,27 @@ def main(): windows_python_builder.build() windows_python_builder.prepare_for_package() llvm_core.set_mingw_python_dir(windows_python_builder.install_dir) + + if build_config.enable_lzma_7zip: + build_utils.logger().info('build windows lzma') + target_triple = 'windows-x86_64' + build_dir = build_utils.merge_out_path('lzma') + build_utils.check_create_dir(build_dir) + clang_path = build_utils.merge_out_path('llvm-install', 'bin', 'clang') + src_dir = os.path.abspath(os.path.join(build_config.REPOROOT_DIR, 'third_party', 'lzma', 'C')) + windows_sysroot = build_utils.merge_out_path('mingw', build_config.MINGW_TRIPLE) + cmd = [ 'make', + 'install', + 'CC=%s' % clang_path, + 'SRC_PREFIX=%s/' % src_dir, + 'SYSROOT=%s' % windows_sysroot, + 'TARGET_TRIPLE=%s' % target_triple, + 'INSTALL_DIR=%s' % build_dir, + '-f', + 'MakeLiblzma'] + os.chdir(build_config.LLVM_BUILD_DIR) + build_utils.check_call(cmd) + llvm_core.llvm_compile_for_windows(build_config.TARGETS, build_config.enable_assertions, build_config.build_name) -- Gitee