diff --git a/.github/workflows/kernel-test.yml b/.github/workflows/kernel-test.yml index b1181d941e..b70056ef85 100644 --- a/.github/workflows/kernel-test.yml +++ b/.github/workflows/kernel-test.yml @@ -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 diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index 96e88835c3..e479fd004f 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index caa59621ea..149600578e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/seed-build.yml b/.github/workflows/seed-build.yml index eb7e86775f..a3c985d8b8 100644 --- a/.github/workflows/seed-build.yml +++ b/.github/workflows/seed-build.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 9c45189356..f097cb4946 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/ @@ -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"] diff --git a/Makefile b/Makefile index 4e6afefeda..5b15e9f732 100644 --- a/Makefile +++ b/Makefile @@ -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)),) @@ -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 @@ -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 diff --git a/cgo_enabled.mk b/cgo_enabled.mk new file mode 100644 index 0000000000..df13dc9974 --- /dev/null +++ b/cgo_enabled.mk @@ -0,0 +1,32 @@ +# +# SPDX-License-Identifier: AGPL-3.0-only +# Copyright (c) 2022-2024, daeuniverse Organization +# + +ifneq ($(KEEP_CGO_ENVS),1) + ifdef CGO_ENABLED_NDEF + export CGO_ENABLED := 0 + endif + ifeq ($(CGO_ENABLED),0) + ifdef GOARCH_NDEF + ifeq ($(CC),cc) + ifneq ($(shell which musl-gcc),) + export CC := musl-gcc + else ifneq ($(shell which 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),) + 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 diff --git a/docs/en/user-guide/build-by-yourself.md b/docs/en/user-guide/build-by-yourself.md index c24c0c529f..af59de0879 100644 --- a/docs/en/user-guide/build-by-yourself.md +++ b/docs/en/user-guide/build-by-yourself.md @@ -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 diff --git a/install/musl-targets.sh b/install/musl-targets.sh new file mode 100644 index 0000000000..b599ea9cc6 --- /dev/null +++ b/install/musl-targets.sh @@ -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 \ No newline at end of file