Skip to content

Commit

Permalink
Make symbol stripping controllable on go_binary (#223)
Browse files Browse the repository at this point in the history
* Make symbol stripping controllable on go_binary

* version
  • Loading branch information
peterebden authored Jan 30, 2024
1 parent 55eb217 commit 3600d5c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Version 1.16.0
--------------
* go_binary now takes a strip argument to control symbol stripping (#223)

Version 1.15.3
--------------
* Pass module coordinates into package info (#221)
* Pass module coordinates into package info (#221)

Version 1.15.2
--------------
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.15.3
1.16.0
16 changes: 12 additions & 4 deletions build_defs/go.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def _get_import_path(package="", import_path=""):

def go_binary(name:str, srcs:list=[], resources:list=None, asm_srcs:list=[], out:str=None, deps:list=[], data:list=None,
visibility:list=None, labels:list=[], test_only:bool&testonly=False, static:bool=CONFIG.GO.DEFAULT_STATIC,
filter_srcs:bool=True, definitions:str|list|dict=None, stamp:bool=False):
filter_srcs:bool=True, definitions:str|list|dict=None, stamp:bool=False, strip:bool=None):
"""Compiles a Go binary.

Args:
Expand All @@ -597,6 +597,8 @@ def go_binary(name:str, srcs:list=[], resources:list=None, asm_srcs:list=[], out
used to contruct the list of definitions passed to the linker.
stamp (bool): Allows this rule to gain access to information about SCM revision etc
via env vars. These can be useful to pass into `definitions`.
strip (bool): Determines whether the binary will be stripped of debug symbols or not.
By default it depends on the build mode.
"""
_srcs = srcs or [name + '.go']
lib = go_library(
Expand All @@ -618,7 +620,7 @@ def go_binary(name:str, srcs:list=[], resources:list=None, asm_srcs:list=[], out
deps = deps,
test_only = test_only,
)
cmds, tools = _go_binary_cmds(name, static=static, definitions=definitions, split_debug=CONFIG.GO.SPLIT_DEBUG_INFO)
cmds, tools = _go_binary_cmds(name, static=static, definitions=definitions, split_debug=CONFIG.GO.SPLIT_DEBUG_INFO, strip=strip)
if CONFIG.GO.STDLIB:
deps += _stdlib()

Expand Down Expand Up @@ -1628,7 +1630,7 @@ def _go_library_cmds(name, import_path:str="", complete=True, all_srcs=False, co
return cmds


def _go_binary_cmds(name, static=False, ldflags='', pkg_config='', definitions=None, gcov=False, split_debug=False):
def _go_binary_cmds(name, static=False, ldflags='', pkg_config='', definitions=None, gcov=False, split_debug=False, strip=None):
"""Returns the commands to run for linking a Go binary."""

_link_cmd = f'"$TOOLS_GO" tool link -importcfg importconfig -tmpdir "$TMP_DIR" -extld "$TOOLS_LD" -o "$OUT"'
Expand Down Expand Up @@ -1657,6 +1659,12 @@ def _go_binary_cmds(name, static=False, ldflags='', pkg_config='', definitions=N
else:
flags = ''

opt_strip = ''
if strip:
flags += ' -s -w'
elif strip is None:
opt_strip = '-s -w'

if len(defs) > 0:
flags += " " + defs

Expand All @@ -1674,7 +1682,7 @@ def _go_binary_cmds(name, static=False, ldflags='', pkg_config='', definitions=N

cmds = {
'dbg': f'{gen_import_cfg} && {_link_cmd} {flags} $SRCS',
'opt': f'{gen_import_cfg} && {_link_cmd} {flags} -s -w $SRCS',
'opt': f'{gen_import_cfg} && {_link_cmd} {flags} {opt_strip} $SRCS',
}
if gcov and CONFIG.GO.CPP_COVERAGE:
cmds['cover'] = f'{gen_import_cfg} && {_link_cmd} {flags} -extldflags="--coverage" $SRCS'
Expand Down

0 comments on commit 3600d5c

Please sign in to comment.