From bcc86484e8675a1d74295bd4967b4037fbeba35d Mon Sep 17 00:00:00 2001 From: chuboning Date: Sat, 16 Dec 2023 11:37:38 +0800 Subject: [PATCH] Generate debug files and strip --- CMakeLists.txt | 6 +++--- build_libtorch_npu.py | 12 ++++++++++++ setup.py | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6750d9dc8f..35d680340a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,9 +133,9 @@ if (CMAKE_BUILD_TYPE MATCHES Debug) set(CMAKE_CXX_FLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -fPIE -pie ${CMAKE_CXX_FLAGS}") set(CXXFLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -fPIE -pie ${CXXFLAGS}") else() - set(CMAKE_C_FLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-s,-z,noexecstack -fPIE -pie ${CMAKE_C_FLAGS}") - set(CMAKE_CXX_FLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-s,-z,noexecstack -fPIE -pie ${CMAKE_CXX_FLAGS}") - set(CXXFLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-s,-z,noexecstack -fPIE -pie ${CXXFLAGS}") + set(CMAKE_C_FLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -fPIE -pie ${CMAKE_C_FLAGS}") + set(CMAKE_CXX_FLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -fPIE -pie ${CMAKE_CXX_FLAGS}") + set(CXXFLAGS "-fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -fPIE -pie ${CXXFLAGS}") endif() if (NOT DEFINED BUILD_LIBTORCH) diff --git a/build_libtorch_npu.py b/build_libtorch_npu.py index cd3ec920b8..9dc8d87fef 100644 --- a/build_libtorch_npu.py +++ b/build_libtorch_npu.py @@ -122,6 +122,16 @@ def check_gtest_valid(base_dir): return os.path.exists(gtest_path) +def generate_dbg_files_and_strip(): + library_dir = Path(BASE_DIR).joinpath("build/packages/torch_npu") + dbg_dir = Path(BASE_DIR).joinpath("build/dbg_libtorch") + os.makedirs(dbg_dir, exist_ok=True) + library_files = [Path(i) for i in library_dir.rglob('*.so')] + for library_file in library_files: + subprocess.check_call(["eu-strip", library_file, "-f", + str(dbg_dir.joinpath(library_file.name)) + ".debug"], cwd=BASE_DIR) # Compliant + + def run_cmake(): cmake = get_cmake_command() @@ -159,6 +169,8 @@ def run_cmake(): subprocess.check_call([cmake, BASE_DIR] + cmake_args, cwd=build_type_dir, env=os.environ) subprocess.check_call(['make'] + build_args, cwd=build_type_dir, env=os.environ) + if which('eu-strip') is not None: + generate_dbg_files_and_strip() def check_opplugin_valid(base_dir): diff --git a/setup.py b/setup.py index 0a1a94ef21..935582fd9f 100644 --- a/setup.py +++ b/setup.py @@ -207,6 +207,16 @@ def check_tensorpipe_valid(base_dir): return os.path.exists(tensorpipe_path) +def generate_dbg_files_and_strip(): + library_dir = Path(BASE_DIR).joinpath("build/packages/torch_npu") + dbg_dir = Path(BASE_DIR).joinpath("build/dbg") + os.makedirs(dbg_dir, exist_ok=True) + library_files = [Path(i) for i in library_dir.rglob('*.so')] + for library_file in library_files: + subprocess.check_call(["eu-strip", library_file, "-f", + str(dbg_dir.joinpath(library_file.name)) + ".debug"], cwd=BASE_DIR) # Compliant + + def CppExtension(name, sources, *args, **kwargs): r''' Creates a :class:`setuptools.Extension` for C++. @@ -421,6 +431,9 @@ class PythonPackageBuild(build_py, object): class BdistWheelBuild(bdist_wheel): def run(self): + if which('eu-strip') is not None: + generate_dbg_files_and_strip() + torch_dependencies = ["libc10.so", "libtorch.so", "libtorch_cpu.so", "libtorch_python.so"] cann_dependencies = ["libhccl.so", "libascendcl.so", "libacl_op_compiler.so", "libge_runner.so", "libgraph.so", "libacl_tdt_channel.so", "libfmk_parser.so", "libascend_protobuf.so"] @@ -482,7 +495,7 @@ if DEBUG: extra_link_args += ['-O0', '-g', '-Wl,-z,now'] else: extra_compile_args += ['-DNDEBUG'] - extra_link_args += ['-Wl,-z,now,-s'] + extra_link_args += ['-Wl,-z,now'] # valid manylinux tags manylinux_tags = [ -- Gitee