Skip to content

Commit ab7825c

Browse files
authored
[ABLD-474] Apply COMMON_TAGS and per platform tags in dd_agent_go_binary (#53947)
### What does this PR do? - Updates dd_agent_go_binary - takes gotags and adds COMMON_TAGS + fips_tags - platform excluded tags + platform tags - provides an escape for some binaries that need to exactly specify tags (e.g. systray) - Applies this to the existing dd_agent_go_binary targets. - Clean up targets which were doing their own (redundant) setting for some link flags. - Tidy up the code to group related lines together. - Adds per/os fips config settings It's not clear yet if we should also compute cgo based on platform and fips. Let's solve that as part of ABLD-525 ### Testing - CI - A complete test is blocked on https://datadoghq.atlassian.net/browse/ABLD-525, which will switch the Go compiler used for fips mode. Right now, we get the same output for `bazel build //cmd/dogstatsd:dogstatsd --//packages/agent:flavor=fips` with or without the flavor flag. Co-authored-by: tony.aiuto <tony.aiuto@datadoghq.com>
1 parent 8fcfa7f commit ab7825c

8 files changed

Lines changed: 50 additions & 61 deletions

File tree

bazel/rules/go/go_binary.bzl

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ load("@agent_volatile//:env_vars.bzl", "env_vars")
3030
load("@dd_release_json//:release_json.bzl", "release_json")
3131
load("@rules_go//go:def.bzl", "go_binary")
3232
load("//tasks:agent_payload_version.bzl", "AGENT_PAYLOAD_VERSION")
33+
load("//tasks:build_tags.bzl", "COMMON_TAGS", "DARWIN_EXCLUDED_TAGS", "FIPS_TAGS", "LINUX_ONLY_TAGS", "WINDOWS_EXCLUDED_TAGS")
3334

3435
_REPO = "github.com/DataDog/datadog-agent"
3536
_VERSION_PKG = _REPO + "/pkg/version"
@@ -66,7 +67,7 @@ def _make_agent_version_url_safe():
6667
return env_vars.PACKAGE_VERSION
6768
return release_json.get("current_milestone") + "-localbuild"
6869

69-
def dd_agent_go_binary(name, **kwargs):
70+
def dd_agent_go_binary(name, gc_linkopts = None, gotags = None, exact_gotags = None, **kwargs):
7071
"""Wrapper around go_binary that injects Datadog Agent version x_defs.
7172
7273
Accepts all go_binary attributes. x_defs and gc_linkopts are merged with
@@ -75,34 +76,27 @@ def dd_agent_go_binary(name, **kwargs):
7576
7677
Defaults applied automatically (override by passing the attribute explicitly):
7778
cgo: True on Windows (required to link .syso resource files), False elsewhere.
78-
gc_linkopts: -s -w (strip symbol table and DWARF) on release builds.
7979
8080
Args:
8181
name: target name
82+
gc_linkopts: Base set of link opts. rpath and stripping options are
83+
automatically added to these.
84+
On linux: add RPATH
85+
On release builds: add -s -w (strip symbol table and DWARF)
86+
gotags: Base set of gotags for this binary. COMMON tags are added, and
87+
per-platform adjustments are made.
88+
exact_gotags: Like gotags, but if this is specified, no other tag sets are added.
8289
**kwargs: arguments to be forwarded to go_binary
8390
"""
84-
agent_version_url_safe = _make_agent_version_url_safe()
85-
8691
# TODO: When --stamp support is in place, also inject:
8792
# _VERSION_PKG + ".Commit": "{STABLE_GIT_COMMIT}",
8893
# The value must come from a stamp file produced by a git_info repository
8994
# rule (planned: bazel/repo/git_info.bzl).
9095

91-
existing_x_defs = kwargs.pop("x_defs", {})
92-
existing_linkopts = kwargs.pop("gc_linkopts", [])
93-
94-
# cgo must be enabled on Windows to link the .syso resource file produced
95-
# by win_resource(). Callers that need additional conditions (e.g. FIPS)
96-
# should pass an explicit cgo = select({...}) which replaces this default.
97-
if "cgo" not in kwargs:
98-
kwargs["cgo"] = select({
99-
"@platforms//os:windows": True,
100-
"//conditions:default": False,
101-
})
102-
10396
# Build two complete x_defs dicts — one per //:is_release branch.
10497
# string_dict attributes do not support per-value select(); the select()
10598
# must wrap the whole dict.
99+
agent_version_url_safe = _make_agent_version_url_safe()
106100
release_x_defs = {
107101
_VERSION_PKG + ".AgentPayloadVersion": AGENT_PAYLOAD_VERSION,
108102
_VERSION_PKG + ".AgentVersion": _url_safe_to_standard(agent_version_url_safe),
@@ -115,9 +109,19 @@ def dd_agent_go_binary(name, **kwargs):
115109
_VERSION_PKG + ".AgentVersionURLSafe": agent_version_url_safe,
116110
_SETUP_PKG + ".defaultRunPath": _RUN_PATH_DEV,
117111
}
112+
existing_x_defs = kwargs.pop("x_defs", {})
118113
release_x_defs.update(existing_x_defs)
119114
dev_x_defs.update(existing_x_defs)
120115

116+
# cgo must be enabled on Windows to link the .syso resource file produced
117+
# by win_resource(). Callers that need additional conditions (e.g. FIPS)
118+
# should pass an explicit cgo = select({...}) which replaces this default.
119+
if "cgo" not in kwargs:
120+
kwargs["cgo"] = select({
121+
"@platforms//os:windows": True,
122+
"//conditions:default": False,
123+
})
124+
121125
# "-r <path>" embeds the ELF RPATH so shared libraries under the run path
122126
# are found at runtime. This flag is Linux-specific; non-Linux targets get
123127
# an empty list.
@@ -137,9 +141,21 @@ def dd_agent_go_binary(name, **kwargs):
137141
"//conditions:default": [],
138142
})
139143

144+
if exact_gotags:
145+
kwargs["gotags"] = sorted(exact_gotags)
146+
else:
147+
gotags = gotags or set()
148+
kwargs["gotags"] = select({
149+
"@platforms//os:macos": sorted((COMMON_TAGS | gotags) - LINUX_ONLY_TAGS - DARWIN_EXCLUDED_TAGS),
150+
"//packages/agent:linux_fips": sorted(COMMON_TAGS | gotags | FIPS_TAGS),
151+
"//packages/agent:windows_x86_64_fips": sorted((COMMON_TAGS | gotags | FIPS_TAGS) - LINUX_ONLY_TAGS - WINDOWS_EXCLUDED_TAGS),
152+
"//:windows_x86_64": sorted((COMMON_TAGS | gotags) - LINUX_ONLY_TAGS - WINDOWS_EXCLUDED_TAGS),
153+
"//conditions:default": sorted(COMMON_TAGS | gotags),
154+
})
155+
140156
go_binary(
141157
name = name,
142-
gc_linkopts = existing_linkopts + run_path_linkopts + strip_linkopts,
158+
gc_linkopts = (gc_linkopts or []) + run_path_linkopts + strip_linkopts,
143159
x_defs = select({
144160
"//:is_release": release_x_defs,
145161
"//conditions:default": dev_x_defs,

cmd/agent/BUILD.bazel

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
3-
load("//tasks:build_tags.bzl", "AGENT_TAGS", "COMMON_TAGS", "DARWIN_EXCLUDED_TAGS", "LINUX_ONLY_TAGS", "WINDOWS_EXCLUDED_TAGS")
3+
load("//tasks:build_tags.bzl", "AGENT_TAGS")
44

55
go_library(
66
name = "agent_lib",
@@ -158,10 +158,6 @@ dd_agent_go_binary(
158158
"//conditions:default": [],
159159
}),
160160
embed = [":agent_lib"],
161-
gotags = select({
162-
"@platforms//os:macos": sorted((COMMON_TAGS | AGENT_TAGS) - LINUX_ONLY_TAGS - DARWIN_EXCLUDED_TAGS),
163-
"@platforms//os:windows": sorted((COMMON_TAGS | AGENT_TAGS) - LINUX_ONLY_TAGS - WINDOWS_EXCLUDED_TAGS),
164-
"//conditions:default": sorted(COMMON_TAGS | AGENT_TAGS),
165-
}),
161+
gotags = AGENT_TAGS,
166162
visibility = ["//visibility:public"],
167163
)

cmd/dogstatsd/BUILD.bazel

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
3-
load("//tasks:build_tags.bzl", "COMMON_TAGS", "DARWIN_EXCLUDED_TAGS", "DOGSTATSD_TAGS", "FIPS_TAGS", "LINUX_ONLY_TAGS", "WINDOWS_EXCLUDED_TAGS")
3+
load("//tasks:build_tags.bzl", "DOGSTATSD_TAGS")
44

55
# gazelle:dd_agent_go_test on
66

@@ -134,19 +134,6 @@ dd_agent_go_binary(
134134
"//conditions:default": False,
135135
}),
136136
embed = [":dogstatsd_lib"],
137-
# TODO: Platform filtering seems like something we should move into dd_agent_go_binary.
138-
# Maybe we should 'or' in COMMON_TAGS there too.
139-
# Or perhaps we need the equivalent of build_tags.py functions as starlark in build_tags.bzl.
140-
# Then we can select on platform and have the value be something like get_build_flags("dogstatsd", "linux")
141-
# We would like to put fips tags in the sort with the others, but the select() semantics aren't that
142-
# sophisticated. What we really need is to fix rules_go/go_binary so that tags are deduped and sourced.
143-
gotags = select({
144-
"@platforms//os:macos": sorted((COMMON_TAGS | DOGSTATSD_TAGS) - LINUX_ONLY_TAGS - DARWIN_EXCLUDED_TAGS),
145-
"@platforms//os:windows": sorted((COMMON_TAGS | DOGSTATSD_TAGS) - LINUX_ONLY_TAGS - WINDOWS_EXCLUDED_TAGS),
146-
"//conditions:default": sorted(COMMON_TAGS | DOGSTATSD_TAGS),
147-
}) + select({
148-
"//packages/agent:fips_flavor": sorted(FIPS_TAGS),
149-
"//conditions:default": [],
150-
}),
137+
gotags = DOGSTATSD_TAGS,
151138
visibility = ["//visibility:public"],
152139
)

cmd/host-profiler/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:dd_agent_go_test.bzl", "dd_agent_go_test")
33
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
4-
load("//tasks:build_tags.bzl", "COMMON_TAGS", "HOST_PROFILER_TAGS")
4+
load("//tasks:build_tags.bzl", "HOST_PROFILER_TAGS")
55

66
# gazelle:dd_agent_go_test on
77

@@ -33,7 +33,7 @@ go_library(
3333
dd_agent_go_binary(
3434
name = "host-profiler",
3535
embed = [":host-profiler_lib"],
36-
gotags = sorted(COMMON_TAGS | HOST_PROFILER_TAGS),
36+
gotags = HOST_PROFILER_TAGS,
3737
target_compatible_with = ["@platforms//os:linux"],
3838
visibility = ["//visibility:public"],
3939
)

cmd/installer/BUILD.bazel

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
load("@rules_go//go:def.bzl", "go_library", "go_test")
22
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
3-
load("//tasks:build_tags.bzl", "COMMON_TAGS", "INSTALLER_TAGS")
3+
load("//tasks:build_tags.bzl", "INSTALLER_TAGS")
4+
5+
# gazelle:dd_agent_go_test off
46

57
go_library(
68
name = "installer_lib",
@@ -31,15 +33,15 @@ dd_agent_go_binary(
3133
"//conditions:default": False,
3234
}),
3335
embed = [":installer_lib"],
34-
gotags = sorted(COMMON_TAGS | INSTALLER_TAGS),
36+
gotags = INSTALLER_TAGS,
3537
visibility = ["//visibility:public"],
3638
)
3739

3840
go_test(
3941
name = "installer_test",
4042
srcs = ["main_test.go"],
4143
embed = [":installer_lib"],
42-
gotags = ["test"],
44+
gotags = sorted(["test"] + list(INSTALLER_TAGS)),
4345
deps = [
4446
"//pkg/fleet/installer/commands",
4547
"@com_github_spf13_cobra//:cobra",

cmd/process-agent/BUILD.bazel

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
3-
load("//tasks:build_tags.bzl", "COMMON_TAGS", "DARWIN_EXCLUDED_TAGS", "LINUX_ONLY_TAGS", "PROCESS_AGENT_TAGS", "WINDOWS_EXCLUDED_TAGS")
3+
load("//tasks:build_tags.bzl", "PROCESS_AGENT_TAGS")
44

55
go_library(
66
name = "process-agent_lib",
@@ -23,10 +23,6 @@ dd_agent_go_binary(
2323
"//conditions:default": [],
2424
}),
2525
embed = [":process-agent_lib"],
26-
gotags = select({
27-
"@platforms//os:macos": sorted((COMMON_TAGS | PROCESS_AGENT_TAGS) - LINUX_ONLY_TAGS - DARWIN_EXCLUDED_TAGS),
28-
"@platforms//os:windows": sorted((COMMON_TAGS | PROCESS_AGENT_TAGS) - LINUX_ONLY_TAGS - WINDOWS_EXCLUDED_TAGS),
29-
"//conditions:default": sorted(COMMON_TAGS | PROCESS_AGENT_TAGS),
30-
}),
26+
gotags = PROCESS_AGENT_TAGS,
3127
visibility = ["//visibility:public"],
3228
)

cmd/systray/BUILD.bazel

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,14 @@ dd_agent_go_binary(
4444
out = "ddtray.exe",
4545
cgo = True,
4646
embed = [":systray_lib"],
47+
exact_gotags = ["grpcnotrace"],
4748
# Subtle: This should be passed to the C linker, so the -Wl is redundant,
4849
# but it is handled by the mingw driver instead of ld, so we still need
4950
# -Wl to pass it on a second layer deep.
50-
# TODO: Move selection of strip flags (-s -w) into dd_agent_go_binary. But that
51-
# requires refactoring to merge fixed and variable parts.
5251
gc_linkopts = [
5352
"-linkmode=external",
5453
"-extldflags=-Wl,--subsystem,%s" % SUBSYSTEM,
55-
] + select({
56-
"//:is_release": [
57-
"-s",
58-
"-w",
59-
],
60-
"//conditions:default": [],
61-
}),
62-
gotags = ["grpcnotrace"],
54+
],
6355
target_compatible_with = ["@platforms//os:windows"],
6456
visibility = ["//visibility:public"],
6557
x_defs = {

cmd/trace-agent/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
3-
load("//tasks:build_tags.bzl", "COMMON_TAGS", "TRACE_AGENT_TAGS")
3+
load("//tasks:build_tags.bzl", "TRACE_AGENT_TAGS")
44

55
go_library(
66
name = "trace-agent_lib",
@@ -30,6 +30,6 @@ dd_agent_go_binary(
3030
"//conditions:default": [],
3131
}),
3232
embed = [":trace-agent_lib"],
33-
gotags = sorted(COMMON_TAGS | TRACE_AGENT_TAGS),
33+
gotags = TRACE_AGENT_TAGS,
3434
visibility = ["//visibility:public"],
3535
)

0 commit comments

Comments
 (0)