mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
bdba9d8ba6
* build: correct toolchain order * build: gazelle-update-repos * build: use pregenerated proto for dependencies * update bazeldnf * deps: tpm simulator * Update Google trillian module * cli: add stamping as alternative build info source * bazel: add go_test wrappers, mark special tests and select testing deps * deps: add libvirt deps * deps: go-libvirt patches * deps: cloudflare circl patches * bazel: add go_test wrappers, mark special tests and select testing deps * bazel: keep gazelle overrides * bazel: cleanup bazelrc * bazel: switch CMakeLists.txt to use bazel * bazel: fix injection of version information via stamping * bazel: commit all build files * dev-docs: document bazel usage * deps: upgrade zig-cc for go 1.20 * bazel: update Perl for macOS arm64 & Linux arm64 support * bazel: use static perl toolchain for OpenSSL * bazel: use static protobuf (protoc) toolchain * deps: add git and go to nix deps Co-authored-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
106 lines
2.9 KiB
Python
106 lines
2.9 KiB
Python
"""
|
|
This module contains rules and macros for building and testing Go code.
|
|
"""
|
|
|
|
load("@io_bazel_rules_go//go:def.bzl", _go_test = "go_test")
|
|
|
|
def go_test(ld = None, **kwargs):
|
|
"""go_test is a wrapper for go_test that uses default settings for Constellation.
|
|
|
|
It adds the following:
|
|
- Sets test count to 3.
|
|
- Sets race detector to on by default.
|
|
- Optionally sets the interpreter path to ld.
|
|
|
|
Args:
|
|
ld: path to interpreter to that will be written into the elf header.
|
|
**kwargs: all other arguments are passed to go_test.
|
|
"""
|
|
|
|
# Sets test count to 3.
|
|
kwargs.setdefault("args", [])
|
|
kwargs["args"].append("--test.count=3")
|
|
|
|
# enable race detector by default
|
|
kwargs.setdefault("race", "on")
|
|
|
|
# set gc_linkopts to set the interpreter path to ld.
|
|
kwargs.setdefault("gc_linkopts", [])
|
|
if ld:
|
|
kwargs["gc_linkopts"] += ["-I", ld]
|
|
|
|
_go_test(**kwargs)
|
|
|
|
def _vars_script(env, ld, cmd):
|
|
ret = ["#!/bin/sh"]
|
|
for k, v in env.items():
|
|
ret.append('export {}="{}"'.format(k, v))
|
|
ret.append('exec {} {} "$@"'.format(ld, cmd))
|
|
return "\n".join(ret) + "\n"
|
|
|
|
def _ld_binary_impl(ctx):
|
|
source_info = ctx.attr.src[DefaultInfo]
|
|
ld_info = ctx.attr.ld[DefaultInfo]
|
|
|
|
executable = None
|
|
if source_info.files_to_run and source_info.files_to_run.executable:
|
|
command = _vars_script(ctx.attr.env, ld_info.files_to_run.executable.short_path, source_info.files_to_run.executable.short_path)
|
|
executable = ctx.actions.declare_file("{}_under_ld".format(ctx.file.src.basename))
|
|
ctx.actions.write(
|
|
output = executable,
|
|
content = command,
|
|
is_executable = True,
|
|
)
|
|
|
|
runfiles = ctx.runfiles(files = ctx.files.src)
|
|
runfiles = runfiles.merge(source_info.data_runfiles)
|
|
runfiles = runfiles.merge(ctx.runfiles(files = ctx.files.ld))
|
|
runfiles = runfiles.merge(ld_info.data_runfiles)
|
|
return [DefaultInfo(
|
|
executable = executable,
|
|
files = depset([executable]),
|
|
runfiles = runfiles,
|
|
)]
|
|
|
|
_attrs = {
|
|
"env": attr.string_dict(
|
|
doc = "Environment variables for the test",
|
|
),
|
|
"ld": attr.label(
|
|
executable = True,
|
|
cfg = "exec",
|
|
doc = "ld wrapper executable",
|
|
),
|
|
"src": attr.label(
|
|
allow_single_file = True,
|
|
mandatory = True,
|
|
doc = "Target to build.",
|
|
),
|
|
}
|
|
|
|
ld_test = rule(
|
|
implementation = _ld_binary_impl,
|
|
attrs = _attrs,
|
|
executable = True,
|
|
test = True,
|
|
)
|
|
|
|
def go_ld_test(**kwargs):
|
|
"""go_ld_test is a wrapper for go_test that uses the specified ld to run the test binary under.
|
|
|
|
Args:
|
|
**kwargs: all arguments are passed to go_test.
|
|
"""
|
|
|
|
# Sets test count to 3.
|
|
kwargs.setdefault("args", [])
|
|
kwargs["args"].append("--test.count=3")
|
|
|
|
# Disable test wrapper
|
|
kwargs.setdefault("env", {})
|
|
kwargs["env"]["GO_TEST_WRAP"] = "0"
|
|
|
|
ld_test(
|
|
**kwargs
|
|
)
|