Skip to content

Commit

Permalink
feat: Replace CGO_ENABLED=0 with //go:build ignore (#950)
Browse files Browse the repository at this point in the history
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
  • Loading branch information
timraymond authored Nov 15, 2024
1 parent 030d2b1 commit 3a3ff41
Show file tree
Hide file tree
Showing 32 changed files with 63 additions and 11 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ setup-envtest: $(ENVTEST)
all: generate

generate: generate-bpf-go
CGO_ENABLED=0 go generate ./...
go generate ./...
for dir in $(GENERATE_TARGET_DIRS); do \
make -C $$dir $@; \
done

generate-bpf-go: ## generate ebpf wrappers for plugins for all archs
for arch in $(ALL_ARCH.linux); do \
CGO_ENABLED=0 GOARCH=$$arch go generate ./pkg/plugin/...; \
GOARCH=$$arch go generate ./pkg/plugin/...; \
done

.PHONY: all generate generate-bpf-go
Expand Down Expand Up @@ -174,12 +174,11 @@ retina: ## builds retina binary
$(MAKE) retina-binary

retina-binary: ## build the Retina binary
export CGO_ENABLED=0 && \
go generate ./... && \
go build -v -o $(RETINA_BUILD_DIR)/retina$(EXE_EXT) -gcflags="-dwarflocationlists=true" -ldflags "-X github.com/microsoft/retina/internal/buildinfo.Version=$(TAG) -X github.com/microsoft/retina/internal/buildinfo.ApplicationInsightsID=$(APP_INSIGHTS_ID)" $(RETINA_DIR)/main.go

retina-capture-workload: ## build the Retina capture workload
cd $(CAPTURE_WORKLOAD_DIR) && CGO_ENABLED=0 go build -v -o $(RETINA_BUILD_DIR)/captureworkload$(EXE_EXT) -gcflags="-dwarflocationlists=true" -ldflags "-X main.version=$(TAG)"
cd $(CAPTURE_WORKLOAD_DIR) && go build -v -o $(RETINA_BUILD_DIR)/captureworkload$(EXE_EXT) -gcflags="-dwarflocationlists=true" -ldflags "-X main.version=$(TAG)"

##@ Containers

Expand Down Expand Up @@ -410,7 +409,7 @@ COVER_PKG ?= .

test: $(ENVTEST) # Run unit tests.
go build -o test-summary ./test/utsummary/main.go
CGO_ENABLED=0 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use -p path)" go test -tags=unit,dashboard -skip=TestE2E* -coverprofile=coverage.out -v -json ./... | ./test-summary --progress --verbose
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use -p path)" go test -tags=unit,dashboard -skip=TestE2E* -coverprofile=coverage.out -v -json ./... | ./test-summary --progress --verbose

coverage: # Code coverage.
# go generate ./... && go test -tags=unit -coverprofile=coverage.out.tmp ./...
Expand Down
4 changes: 0 additions & 4 deletions controller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ FROM golang AS intermediate
ARG APP_INSIGHTS_ID # set to enable AI telemetry
ARG GOARCH=amd64 # default to amd64
ARG GOOS=linux # default to linux
ENV CGO_ENABLED=0
ENV GOARCH=${GOARCH}
ENV GOOS=${GOOS}
RUN if [ "$GOOS" = "linux" ] ; then \
Expand All @@ -52,7 +51,6 @@ ARG APP_INSIGHTS_ID # set to enable AI telemetry
ARG GOARCH=amd64 # default to amd64
ARG GOOS=linux # default to linux
ARG VERSION
ENV CGO_ENABLED=0
ENV GOARCH=${GOARCH}
ENV GOOS=${GOOS}
RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/retina/captureworkload -ldflags "-X github.com/microsoft/retina/internal/buildinfo.Version="$VERSION" -X github.com/microsoft/retina/internal/buildinfo.ApplicationInsightsID="$APP_INSIGHTS_ID"" captureworkload/main.go
Expand All @@ -64,7 +62,6 @@ ARG APP_INSIGHTS_ID # set to enable AI telemetry
ARG GOARCH=amd64 # default to amd64
ARG GOOS=linux # default to linux
ARG VERSION
ENV CGO_ENABLED=0
ENV GOARCH=${GOARCH}
ENV GOOS=${GOOS}
RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/retina/controller -ldflags "-X github.com/microsoft/retina/internal/buildinfo.Version="$VERSION" -X github.com/microsoft/retina/internal/buildinfo.ApplicationInsightsID="$APP_INSIGHTS_ID"" controller/main.go
Expand All @@ -76,7 +73,6 @@ ARG APP_INSIGHTS_ID # set to enable AI telemetry
ARG GOARCH=amd64 # default to amd64
ARG GOOS=linux # default to linux
ARG VERSION
ENV CGO_ENABLED=0
ENV GOARCH=${GOARCH}
ENV GOOS=${GOOS}
RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/retina/initretina -ldflags "-X github.com/microsoft/retina/internal/buildinfo.Version="$VERSION" -X github.com/microsoft/retina/internal/buildinfo.ApplicationInsightsID="$APP_INSIGHTS_ID"" init/retina/main_linux.go
Expand Down
1 change: 0 additions & 1 deletion controller/Dockerfile.windows-native
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ FROM --platform=windows/amd64 mcr.microsoft.com/oss/go/microsoft/golang@sha256:e
WORKDIR C:\\retina
COPY go.mod .
COPY go.sum .
ENV CGO_ENABLED=0
RUN go mod download
ADD . .
ARG VERSION
Expand Down
4 changes: 3 additions & 1 deletion operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ENV GOARCH=${GOARCH}

RUN make manifests
RUN --mount=type=cache,target="/root/.cache/go-build" \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
GOOS=linux GOARCH=amd64 go build \
-ldflags "-X github.com/microsoft/retina/internal/buildinfo.Version="$VERSION" \
-X "github.com/microsoft/retina/internal/buildinfo.ApplicationInsightsID"="$APP_INSIGHTS_ID"" \
-a -o retina-operator operator/main.go
Expand All @@ -26,6 +26,8 @@ RUN --mount=type=cache,target="/root/.cache/go-build" \
# mcr.microsoft.com/cbl-mariner/distroless/minimal:2.0
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/cbl-mariner/distroless/minimal@sha256:db87903c5d4d9d6760e86a274914efd6a3bb5914c0b5a6c6b35350ec297fea4f
WORKDIR /
COPY --from=builder /lib /lib
COPY --from=builder /usr/lib/ /usr/lib
COPY --from=builder /workspace/retina-operator .
USER 65532:65532

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/conntrack/_cprog/conntrack.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/dropreason/_cprog/drop_reason.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/filter/_cprog/retina_filter.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/bpf.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/bpf_prog_linfo.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2018 Facebook */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/btf.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2018 Facebook */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/btf_dump.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/btf_iter.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2021 Facebook */
/* Copyright (c) 2024, Oracle and/or its affiliates. */
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/btf_relocate.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024, Oracle and/or its affiliates. */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/elf.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

#ifndef _GNU_SOURCE
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/features.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
#include <linux/kernel.h>
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/gen_loader.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2021 Facebook */
#include <stdio.h>
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/hashmap.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/libbpf.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/libbpf_errno.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/libbpf_probes.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2019 Netronome Systems, Inc. */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/linker.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* BPF static linker
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/netlink.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2018 Facebook */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/nlattr.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/relo_core.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2019 Facebook */

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/ringbuf.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* Ring buffer operations.
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/str_error.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
#undef _GNU_SOURCE
#include <string.h>
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/strset.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2021 Facebook */
#include <stdint.h>
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/usdt.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include <ctype.h>
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/xsk.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

/*
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/lib/common/libbpf/_src/zip.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* Routines for dealing with .zip archives.
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/packetforward/_cprog/packetforward.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/packetparser/_cprog/packetparser.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

Expand Down

0 comments on commit 3a3ff41

Please sign in to comment.