-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Add ability to strip binaries in cmake #35196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| BINARY_PATH=$1 | ||
| BINARY_NAME=$(basename $BINARY_PATH) | ||
| DESTINATION_STRIPPED_DIR=$2 | ||
| OBJCOPY_PATH=${3:objcopy} | ||
| READELF_PATH=${4:readelf} | ||
|
|
||
| BUILD_ID=$($READELF_PATH -n $1 | sed -n '/Build ID/ { s/.*: //p; q; }') | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now in some cases our build system depend on bash. |
||
| BUILD_ID_PREFIX=${BUILD_ID:0:2} | ||
| BUILD_ID_SUFFIX=${BUILD_ID:2} | ||
| TEMP_BINARY_PATH="${BINARY_PATH}_temp" | ||
|
|
||
| DESTINATION_DEBUG_INFO_DIR="$DESTINATION_STRIPPED_DIR/lib/debug/.build-id" | ||
| DESTINATION_STRIP_BINARY_DIR="$DESTINATION_STRIPPED_DIR/bin" | ||
|
|
||
| mkdir -p "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX" | ||
| mkdir -p "$DESTINATION_STRIP_BINARY_DIR" | ||
|
|
||
| $OBJCOPY_PATH --only-keep-debug "$BINARY_PATH" "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" | ||
|
|
||
| touch "$TEMP_BINARY_PATH" | ||
| $OBJCOPY_PATH --add-gnu-debuglink "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" "$BINARY_PATH" "$TEMP_BINARY_PATH" | ||
| $OBJCOPY_PATH --strip-all "$TEMP_BINARY_PATH" "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME" | ||
| rm -f "$TEMP_BINARY_PATH" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| macro(clickhouse_strip_binary) | ||
| set(oneValueArgs TARGET DESTINATION_DIR BINARY_PATH) | ||
|
|
||
| cmake_parse_arguments(STRIP "" "${oneValueArgs}" "" ${ARGN}) | ||
|
|
||
| if (NOT DEFINED STRIP_TARGET) | ||
| message(FATAL_ERROR "A target name must be provided for stripping binary") | ||
| endif() | ||
|
|
||
| if (NOT DEFINED STRIP_BINARY_PATH) | ||
| message(FATAL_ERROR "A binary path name must be provided for stripping binary") | ||
| endif() | ||
|
|
||
|
|
||
| if (NOT DEFINED STRIP_DESTINATION_DIR) | ||
| message(FATAL_ERROR "Destination directory for stripped binary must be provided") | ||
| endif() | ||
|
|
||
| add_custom_command(TARGET ${STRIP_TARGET} POST_BUILD | ||
| COMMAND bash ${ClickHouse_SOURCE_DIR}/cmake/strip.sh ${STRIP_BINARY_PATH} ${STRIP_DESTINATION_DIR} ${OBJCOPY_PATH} ${READELF_PATH} | ||
| COMMENT "Stripping clickhouse binary" VERBATIM | ||
| ) | ||
|
|
||
| install(PROGRAMS ${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT clickhouse) | ||
| install(DIRECTORY ${STRIP_DESTINATION_DIR}/lib/debug DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT clickhouse) | ||
| endmacro() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,3 +169,33 @@ if (OBJCOPY_PATH) | |
| else () | ||
| message (FATAL_ERROR "Cannot find objcopy.") | ||
| endif () | ||
|
|
||
| # Readelf (FIXME copypaste) | ||
|
|
||
| if (COMPILER_GCC) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copypaste |
||
| find_program (READELF_PATH NAMES "llvm-readelf" "llvm-readelf-13" "llvm-readelf-12" "llvm-readelf-11" "readelf") | ||
| else () | ||
| find_program (READELF_PATH NAMES "llvm-readelf-${COMPILER_VERSION_MAJOR}" "llvm-readelf" "readelf") | ||
| endif () | ||
|
|
||
| if (NOT READELF_PATH AND OS_DARWIN) | ||
| find_program (BREW_PATH NAMES "brew") | ||
| if (BREW_PATH) | ||
| execute_process (COMMAND ${BREW_PATH} --prefix llvm ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE LLVM_PREFIX) | ||
| if (LLVM_PREFIX) | ||
| find_program (READELF_PATH NAMES "llvm-readelf" PATHS "${LLVM_PREFIX}/bin" NO_DEFAULT_PATH) | ||
| endif () | ||
| if (NOT READELF_PATH) | ||
| execute_process (COMMAND ${BREW_PATH} --prefix binutils ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE BINUTILS_PREFIX) | ||
| if (BINUTILS_PREFIX) | ||
| find_program (READELF_PATH NAMES "readelf" PATHS "${BINUTILS_PREFIX}/bin" NO_DEFAULT_PATH) | ||
| endif () | ||
| endif () | ||
| endif () | ||
| endif () | ||
|
|
||
| if (READELF_PATH) | ||
| message (STATUS "Using readelf: ${READELF_PATH}") | ||
| else () | ||
| message (FATAL_ERROR "Cannot find readelf.") | ||
| endif () | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean that
BUILD_STRIPPED_BINARIES_PREFIXwill be always set according to https://github.com/ClickHouse/ClickHouse/pull/35196/files#diff-20dc468ff5043ade1489346dbd70f621d828a1c8c6a439ef9b0afc3bc295a0a0R178 ?I don't really think we always want strip it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Figured it out in slack