Skip to content

Commit

Permalink
Add test coverage instrumentataion.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxerwang committed Mar 19, 2017
1 parent b4f5183 commit 42f5056
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions go/def.bzl
Expand Up @@ -60,7 +60,7 @@ def _go_prefix(ctx):
return prefix

# TODO(bazel-team): it would be nice if Bazel had this built-in.
def symlink_tree_commands(dest_dir, artifact_dict):
def symlink_tree_commands(go_tool_cover, dest_dir, artifact_dict):
"""Symlink_tree_commands returns a list of commands to create the
dest_dir, and populate it according to the given dict.
Expand All @@ -76,6 +76,7 @@ def symlink_tree_commands(dest_dir, artifact_dict):
"mkdir -p " + dest_dir,
]

count = 0
for old_path, new_path in artifact_dict.items():
pos = new_path.rfind('/')
if pos >= 0:
Expand All @@ -94,12 +95,25 @@ def symlink_tree_commands(dest_dir, artifact_dict):
up resolving to 0 path elements, so 2 segments should be removed from the
count."""
up -= 2
cmds += [
"mkdir -p %s/%s" % (dest_dir, new_dir),
"ln -s %s%s %s/%s" % ('../' * up, old_path, dest_dir, new_path),
]

cmds += ["mkdir -p %s/%s" % (dest_dir, new_dir)]

if go_tool_cover and old_path.endswith(".go"):
cmds += ["ln -s %s%s %s/%s.orig" % ('../' * up, old_path, dest_dir, new_path)]
cmds += ["%s -var=GoCover_%d -o %s/%s %s/%s.orig" % (go_tool_cover, count,
dest_dir, new_path, dest_dir, new_path)]
cmds += ["rm %s/%s.orig" % (dest_dir, new_path)]
cmds += ["cat %s/%s" % (dest_dir, new_path)]
count += 1
else:
cmds += ["ln -s %s%s %s/%s" % ('../' * up, old_path, dest_dir, new_path)]
return cmds

def get_go_tool_cover(ctx):
if ctx.coverage_instrumented():
return ' '.join([ctx.file.go_tool.path, "tool", "cover", "--mode=set"])
return None

def go_environment_vars(ctx):
"""Return a map of environment variables for use with actions, based on
the arguments. Uses the ctx.fragments.cpp.cpu attribute, if present,
Expand Down Expand Up @@ -243,13 +257,12 @@ def _emit_go_compile_action(ctx, sources, deps, out_lib,
out_depth = out_dir.count('/') + 1
if _is_external(out_dir):
out_depth -= 2
cmds = symlink_tree_commands(out_dir, tree_layout)

cmds = ['export GOROOT=$(pwd)/%s/..' % ctx.file.go_tool.dirname]
cmds += symlink_tree_commands(get_go_tool_cover(ctx), out_dir, tree_layout)

# cd into the out_dir.
cmds += [
'export GOROOT=$(pwd)/%s/..' % ctx.file.go_tool.dirname,
'cd ' + out_dir,
]
cmds += ['cd ' + out_dir]

# Filter source files using build tags.
cleaned_go_source_paths = [
Expand All @@ -267,7 +280,7 @@ def _emit_go_compile_action(ctx, sources, deps, out_lib,
'${UNFILTERED_GO_FILES[@]}',
])
cmds += [
'UNFILTERED_GO_FILES=(%s)' %
'UNFILTERED_GO_FILES=(%s)' %
' '.join(["'%s'" % f for f in cleaned_go_source_paths]),
'FILTERED_GO_FILES=(%s)' %
' '.join(["'%s'" % f for f in cleaned_cgo_source_paths]),
Expand Down Expand Up @@ -514,7 +527,7 @@ def _emit_go_link_action(ctx, importmap, transitive_libs, cgo_deps, lib,
main_archive,
]

cmds = symlink_tree_commands(out_dir, tree_layout)
cmds = []
# Avoided -s on OSX but but it requires dsymutil to be on $PATH.
# TODO(yugui) Remove this workaround once rules_go stops supporting XCode 7.2
# or earlier.
Expand All @@ -525,6 +538,8 @@ def _emit_go_link_action(ctx, importmap, transitive_libs, cgo_deps, lib,
"STAMP_XDEFS=()",
]

cmds += symlink_tree_commands(get_go_tool_cover(ctx), out_dir, tree_layout)

stamp_inputs = []
if ctx.attr.linkstamp:
# read workspace status files, converting "KEY value" lines
Expand Down Expand Up @@ -804,8 +819,8 @@ def _cgo_codegen_impl(ctx):
out_dir = (ctx.configuration.genfiles_dir.path + '/' +
p + ctx.attr.outdir)
cc = ctx.fragments.cpp.compiler_executable
cmds = symlink_tree_commands(out_dir + "/src", tree_layout) + [
"export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
cmds = ["export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/.."]
cmds += symlink_tree_commands(get_go_tool_cover(ctx), out_dir + "/src", tree_layout) + [
# We cannot use env for CC because $(CC) on OSX is relative
# and '../' does not work fine due to symlinks.
"export CC=$(cd $(dirname {cc}); pwd)/$(basename {cc})".format(cc=cc),
Expand Down

0 comments on commit 42f5056

Please sign in to comment.