Skip to content

chore/docs: enable cgo as much as possible #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/kernel-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ jobs:
go.sum
go-version: 1.22.4

- uses: goto-bus-stop/setup-zig@v2

- name: Generate and build
run: |
git submodule update --init
make GOFLAGS="-buildvcs=false" CC=clang
make GOFLAGS="-buildvcs=false"

- name: Store executable
uses: actions/upload-artifact@v4
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
go.sum
go-version: '^1.22'

- uses: goto-bus-stop/setup-zig@v2

- name: Install Dependencies
run: |
sudo apt-get update -y
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
go.sum
go-version: '^1.22'

- uses: goto-bus-stop/setup-zig@v2

- name: Install Dependencies
run: |
sudo apt-get update -y
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/seed-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ jobs:
go.sum
go-version: '^1.22'

- uses: goto-bus-stop/setup-zig@v2

- name: Install Dependencies
run: |
sudo apt-get update -y
Expand Down
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM golang:1.22-bookworm AS builder
RUN apt-get update && apt-get install -y llvm-15 clang-15 git make
RUN apt-get update && apt-get install -y llvm-15 clang-15 git make musl-tools
ENV CLANG=clang-15
WORKDIR /build/
ADD go.mod go.sum ./
RUN go mod download
ADD . .
RUN git submodule update --init
RUN make OUTPUT=dae GOFLAGS="-buildvcs=false" CC=clang CGO_ENABLED=0
RUN make OUTPUT=dae GOFLAGS="-buildvcs=false"

FROM alpine
RUN mkdir -p /usr/local/share/dae/
Expand All @@ -17,5 +17,4 @@ COPY --from=builder /build/dae /usr/local/bin
COPY --from=builder /build/install/empty.dae /etc/dae/config.dae
RUN chmod 0600 /etc/dae/config.dae

CMD ["dae"]
ENTRYPOINT ["dae", "run", "-c", "/etc/dae/config.dae"]
CMD ["dae", "run", "-c", "/etc/dae/config.dae"]
31 changes: 24 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CFLAGS := -DMAX_MATCH_SET_LEN=$(MAX_MATCH_SET_LEN) $(CFLAGS)
NOSTRIP ?= n
STRIP_PATH := $(shell command -v $(STRIP) 2>/dev/null)
BUILD_TAGS_FILE := .build_tags
KEEP_CGO_ENVS ?= 0
ifeq ($(strip $(NOSTRIP)),y)
STRIP_FLAG := -no-strip
else ifeq ($(wildcard $(STRIP_PATH)),)
Expand All @@ -24,7 +25,18 @@ else
STRIP_FLAG := -strip=$(STRIP_PATH)
endif

GOARCH ?= $(shell go env GOARCH)
ifndef CGO_ENABLED
CGO_ENABLED_NDEF := 1
CGO_ENABLED := $(shell go env CGO_ENABLED)
endif
ifndef CC
CC_NDEF := 1
CC := $(shell go env CC)
endif
ifndef GOARCH
GOARCH_NDEF := 1
GOARCH := $(shell go env GOARCH)
endif

# Do NOT remove the line below. This line is for CI.
#export GOMODCACHE=$(PWD)/go-mod
Expand All @@ -39,18 +51,23 @@ else
VERSION ?= unstable-$(date).r$(count).$(commit)
endif

BUILD_ARGS := -trimpath -ldflags "-s -w -X github.com/daeuniverse/dae/cmd.Version=$(VERSION) -X github.com/daeuniverse/dae/common/consts.MaxMatchSetLen_=$(MAX_MATCH_SET_LEN)" $(BUILD_ARGS)

GO_BUILD_ARGS = -trimpath -ldflags "-s -w -X github.com/daeuniverse/dae/cmd.Version=$(VERSION) -X github.com/daeuniverse/dae/common/consts.MaxMatchSetLen_=$(MAX_MATCH_SET_LEN) $(GO_LDFLAGS)" $(BUILD_ARGS)

.PHONY: clean-ebpf ebpf dae submodule submodules

## Begin Dae Build
dae: export GOOS=linux
ifndef CGO_ENABLED
dae: export CGO_ENABLED=0
endif
dae: ebpf
@echo $(CFLAGS)
go build -tags=$(shell cat $(BUILD_TAGS_FILE)) -o $(OUTPUT) $(BUILD_ARGS) .
$(eval include cgo_enabled.mk)
$(info CFLAGS=$(CFLAGS))
$(info KEEP_CGO_ENVS=$(KEEP_CGO_ENVS))
$(info CGO_ENABLED=$(CGO_ENABLED))
$(info GO_LDFLAGS=$(GO_LDFLAGS))
$(info CC=$(CC))
$(info GOARCH=$(GOARCH))
go build -tags=$(shell cat $(BUILD_TAGS_FILE)) -o $(OUTPUT) $(GO_BUILD_ARGS) .
@$(STRIP) $(OUTPUT) || strip $(OUTPUT) || true
## End Dae Build

## Begin Git Submodules
Expand Down
32 changes: 32 additions & 0 deletions cgo_enabled.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2022-2024, daeuniverse Organization <[email protected]>
#

ifneq ($(KEEP_CGO_ENVS),1)
ifdef CGO_ENABLED_NDEF
export CGO_ENABLED := 0
endif
Comment on lines +7 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ifdef CGO_ENABLED_NDEF
export CGO_ENABLED := 0
endif
export CGO_ENABLED ?= 0

ifeq ($(CGO_ENABLED),0)
ifdef GOARCH_NDEF
ifeq ($(CC),cc)
ifneq ($(shell which musl-gcc),)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using command -v is better than which when checking the existence of executable, as it is a UNIX-compatible built-in command.
Reference: https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script

Suggested change
ifneq ($(shell which musl-gcc),)
ifneq ($(shell command -v musl-gcc),)

export CC := musl-gcc
else ifneq ($(shell which zig),)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else ifneq ($(shell which zig),)
else ifneq ($(shell command -v zig),)

export CC := zig cc -target $(shell . install/musl-targets.sh && echo $$GOARCH_$(GOARCH))
else
$(info ! CGO_ENABLED=0 is not recommended. Please consider to install musl-gcc for static link instead. See https://github.com/daeuniverse/dae/issues/557)
endif
endif
else ifneq ($(shell which zig),)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else ifneq ($(shell which zig),)
else ifneq ($(shell command -v zig),)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command -v 在 /bin/sh 能用吗

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mzz2017 I can confirm command -v works in sh.

export CC := zig cc -target $(shell . install/musl-targets.sh && echo $$GOARCH_$(GOARCH))
else
$(info ! CGO_ENABLED=0 is not recommended. See https://github.com/daeuniverse/dae/issues/557)
endif
endif
ifneq ($(CC),cc)
export CGO_ENABLED := 1
GO_LDFLAGS += -linkmode external -extldflags=-static
$(info * Building dae with "$(CC)" static linking instead of CGO_ENABLED=0. See https://github.com/daeuniverse/dae/issues/557)
endif
endif
3 changes: 1 addition & 2 deletions docs/en/user-guide/build-by-yourself.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ git clone https://github.com/daeuniverse/dae.git
cd dae
git submodule update --init
## Minimal dependency build
make GOFLAGS="-buildvcs=false" \
CC=clang
make GOFLAGS="-buildvcs=false" CC=clang KEEP_CGO_ENVS=1

## Normal build
#make
Expand Down
9 changes: 9 additions & 0 deletions install/musl-targets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
GOARCH_386=i386-linux-musl
GOARCH_amd64=x86_64-linux-musl
GOARCH_arm=arm-linux-musleabi
GOARCH_arm64=aarch64-linux-musl
GOARCH_mips64le=mips64el-linux-musl
GOARCH_mips64=mips64-linux-musl
GOARCH_mipsle=mipsel-linux-musl
GOARCH_mips=mips-linux-musl
GOARCH_riscv64=riscv64-linux-musl
Loading