From 48f3e65bdf82738c8c20c4b76d6e5cda47868c92 Mon Sep 17 00:00:00 2001 From: mpilman Date: Wed, 20 Mar 2019 20:27:10 -0700 Subject: [PATCH] Strip debug symbols with `make packages` When compiling on Linux/MacOS `make packages` will now create a `lib` and a `bin` directory in the packages directory. In there it will put stripped versions of all executables and shared libraries (i.e. the binaries without debug symbols). If this is run on Linux, it will additionally copy the debug symbols of all executables into .debug files. --- bindings/c/CMakeLists.txt | 3 +++ cmake/FlowCommands.cmake | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/bindings/c/CMakeLists.txt b/bindings/c/CMakeLists.txt index 6f70ac0d4a..2807b11b9b 100644 --- a/bindings/c/CMakeLists.txt +++ b/bindings/c/CMakeLists.txt @@ -34,6 +34,7 @@ if(OPEN_FOR_IDE) add_library(fdb_c OBJECT ${FDB_C_SRCS} ${asm_file}) else() add_library(fdb_c SHARED ${FDB_C_SRCS} ${asm_file}) + strip_debug_symbols(fdb_c) endif() add_dependencies(fdb_c fdb_c_generated fdb_c_options) target_link_libraries(fdb_c PUBLIC fdbclient) @@ -54,6 +55,8 @@ if(NOT WIN32) else() add_executable(fdb_c_performance_test test/performance_test.c test/test.h) add_executable(fdb_c_ryw_benchmark test/ryw_benchmark.c test/test.h) + strip_debug_symbols(fdb_c_performance_test) + strip_debug_symbols(fdb_c_ryw_benchmark) endif() target_link_libraries(fdb_c_performance_test PRIVATE fdb_c) target_link_libraries(fdb_c_ryw_benchmark PRIVATE fdb_c) diff --git a/cmake/FlowCommands.cmake b/cmake/FlowCommands.cmake index 7e9e444e1a..b6c02af930 100644 --- a/cmake/FlowCommands.cmake +++ b/cmake/FlowCommands.cmake @@ -9,6 +9,7 @@ define_property(TARGET PROPERTY COVERAGE_FILTERS expression in this list will be ignored when the coverage.target.xml file is \ generated. This property is set through the add_flow_target function.") + function(generate_coverage_xml) if(NOT (${ARGC} EQUAL "1")) message(FATAL_ERROR "generate_coverage_xml expects one argument") @@ -86,6 +87,55 @@ function(assert_no_version_h target) add_dependencies(${target} ${target_name}) endfunction() +add_custom_target(strip_targets) +add_dependencies(packages strip_targets) + +function(strip_debug_symbols target) + if (WIN32) + return() + endif() + get_target_property(target_type ${target} TYPE) + if(target_type STREQUAL "EXECUTABLE") + set(path ${CMAKE_BINARY_DIR}/packages/bin) + set(is_exec ON) + else() + set(path ${CMAKE_BINARY_DIR}/packages/lib) + endif() + file(MAKE_DIRECTORY "${path}") + set(strip_command strip) + set(out_name ${target}) + if(APPLE) + if(NOT is_exec) + set(out_name "lib${target}.dylib") + list(APPEND strip_command -S -x) + endif() + else() + if(is_exec) + list(APPEND strip_command --strip-debug --strip-unneeded) + else() + set(out_name "lib${target}.so") + list(APPEND strip_command --strip-all) + endif() + endif() + set(out_file "${path}/${out_name}") + list(APPEND strip_command -o "${out_file}") + add_custom_command(OUTPUT "${out_file}" + COMMAND ${strip_command} $ + COMMENT "Stripping symbols from ${target}") + set(out_files "${out_file}") + if(is_exec AND NOT APPLE) + add_custom_command(OUTPUT "${out_file}.debug" + COMMAND objcopy --only-keep-debug $ "${out_file}.debug" && + objcopy --add-gnu-debuglink="${out_file}.debug" ${out_file} + DEPENDS "${out_file}" + COMMENT "Copy debug symbols to ${out_name}.debug") + list(APPEND out_files "${out_file}.debug") + endif() + add_custom_target(strip_${target} DEPENDS ${out_files}) + add_dependencies(strip_${target} ${target}) + add_dependencies(strip_targets strip_${target}) +endfunction() + function(add_flow_target) set(options EXECUTABLE STATIC_LIBRARY DYNAMIC_LIBRARY) @@ -135,6 +185,7 @@ function(add_flow_target) endif() endforeach() if(AFT_EXECUTABLE) + set(strip_target ON) set(target_type exec) add_executable(${AFT_NAME} ${sources}) endif() @@ -148,6 +199,7 @@ function(add_flow_target) if(target_type) message(FATAL_ERROR "add_flow_target can only be of one type") endif() + set(strip_target ON) add_library(${AFT_NAME} DYNAMIC ${sources}) endif() @@ -158,6 +210,9 @@ function(add_flow_target) add_dependencies(${AFT_NAME} ${AFT_NAME}_actors) assert_no_version_h(${AFT_NAME}_actors) generate_coverage_xml(${AFT_NAME}) + if(strip_target) + strip_debug_symbols(${AFT_NAME}) + endif() endif() target_include_directories(${AFT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) endfunction()