Skip to content

Commit dafcacb

Browse files
committed
Makefile: set CGO_ENABLED=1 when needed
It doesn't matter whether static or dynamic linking is used, runc always needs libcontainer/nsenter, which is written in C and thus requires cgo. Same is true for libcontainer/integration. In addition, contrib/pkg/seccompagent also needs cgo (if seccomp build tag is set), as it need to be linked against libseccomp C library. By default, cgo is disabled when cross-compiling, meaning that CGO_ENABLED=1 has to be set explicitly in such cases. In all other cases (e.g. other contrib binaries) we do not need cgo. Remove CGO_ENABLED=1 from GO_BUILD_STATIC (as it does not have anything to do with static linking), and add it to all targets that require it. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 21e32d4 commit dafcacb

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,4 @@ jobs:
126126
go-version: 1.x # Latest stable
127127

128128
- name: unit test
129-
# cgo is disabled by default when cross-compiling
130-
run: sudo -E PATH="$PATH" -- make GOARCH=386 CGO_ENABLED=1 localunittest
129+
run: sudo -E PATH="$PATH" -- make GOARCH=386 localunittest

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ ifneq (,$(filter $(GOARCH),arm64 amd64))
4040
endif
4141
endif
4242
# Enable static PIE binaries on supported platforms.
43-
GO_BUILD_STATIC := CGO_ENABLED=1 $(GO) build -trimpath $(GO_BUILDMODE_STATIC) \
43+
GO_BUILD_STATIC := $(GO) build -trimpath $(GO_BUILDMODE_STATIC) \
4444
$(EXTRA_FLAGS) -tags "$(BUILDTAGS) netgo osusergo" \
4545
-ldflags "$(LDFLAGS_COMMON) $(LDFLAGS_STATIC) $(EXTRA_LDFLAGS)"
4646

4747
GPG_KEYID ?= [email protected]
4848

49+
# Some targets need cgo, which is disabled by default when cross compiling.
50+
# Enable cgo explicitly for those.
51+
# Both runc and libcontainer/integration need libcontainer/nsenter.
52+
runc static localunittest: export CGO_ENABLED=1
53+
# seccompagent needs libseccomp (when seccomp build tag is set).
54+
ifneq (,$(filter $(BUILDTAGS),seccomp))
55+
seccompagent: export CGO_ENABLED=1
56+
endif
57+
4958
.DEFAULT: runc
5059

5160
runc:

0 commit comments

Comments
 (0)