Skip to content

Commit cb87a32

Browse files
committed
fix(test): enable CGO cross-compilation for plugin support on macOS
The local-image and install targets cross-compiled the dgraph binary without CGO, producing a statically linked binary that cannot dlopen() Go plugin .so files. This caused all plugin tests to fail on macOS with "Invalid tokenizer anagram". Changes: - Add LINUX_CC variable for architecture-aware cross-compiler selection - Enable CGO_ENABLED=1 with cross-compiler in install and local-image - Use BFD linker (-fuse-ld=bfd) since gold is not in cross-toolchains - Skip jemalloc (BUILD_TAGS=) for cross-compilation (headers unavailable) - Add EXTLDFLAGS support to dgraph/Makefile for external linker flags - Split t/Makefile check into deps (tools) + check (tools + binary) - Top-level setup now calls t/deps so it no longer requires the binary
1 parent dfef2c3 commit cb87a32

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

Makefile

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ ifeq ($(GOPATH),)
1818
$(error GOPATH is not set. Please set it explicitly, e.g. export GOPATH=$$HOME/go)
1919
endif
2020

21-
# On non-Linux systems, use a separate directory for Linux binaries
21+
# On non-Linux systems, use a separate directory for Linux binaries and
22+
# a cross-compiler so that CGO is available (required for plugin support).
2223
ifeq ($(GOHOSTOS),linux)
2324
export LINUX_GOBIN ?= $(GOPATH)/bin
25+
LINUX_CC ?= gcc
2426
else
2527
export LINUX_GOBIN ?= $(GOPATH)/linux_$(GOHOSTARCH)
28+
ifeq ($(GOHOSTARCH),arm64)
29+
LINUX_CC ?= aarch64-unknown-linux-gnu-gcc
30+
else ifeq ($(GOHOSTARCH),amd64)
31+
LINUX_CC ?= x86_64-unknown-linux-gnu-gcc
32+
else
33+
LINUX_CC ?= gcc
34+
endif
2635
endif
2736

2837
######################
@@ -60,7 +69,7 @@ install: ## Install dgraph binary
6069
ifneq ($(GOHOSTOS),linux)
6170
@mkdir -p $(LINUX_GOBIN)
6271
@echo "Installing dgraph (linux/$(GOHOSTARCH))..."
63-
@GOOS=linux GOARCH=$(GOHOSTARCH) $(MAKE) -C dgraph dgraph
72+
@GOOS=linux GOARCH=$(GOHOSTARCH) CGO_ENABLED=1 CC=$(LINUX_CC) $(MAKE) -C dgraph BUILD_TAGS= EXTLDFLAGS=-fuse-ld=bfd dgraph
6473
@mv dgraph/dgraph $(LINUX_GOBIN)/dgraph
6574
@echo "Installed dgraph (linux/$(GOHOSTARCH)) to $(LINUX_GOBIN)/dgraph"
6675
endif
@@ -80,7 +89,7 @@ dgraph-installed:
8089

8190
.PHONY: deps
8291
deps: ## Check test dependencies (pass AUTO_INSTALL=true to auto-install missing ones)
83-
$(MAKE) -C t check
92+
$(MAKE) -C t deps
8493

8594
.PHONY: setup
8695
setup: ## Install all test dependencies automatically
@@ -181,7 +190,11 @@ test-benchmark: ## Go benchmarks (i.e. 'go test -bench')
181190
.PHONY: local-image
182191
local-image: ## Build local Docker image (dgraph/dgraph:local)
183192
@echo building local docker image
184-
@GOOS=linux GOARCH=amd64 $(MAKE) dgraph
193+
ifneq ($(GOHOSTOS),linux)
194+
@GOOS=linux GOARCH=$(GOHOSTARCH) CGO_ENABLED=1 CC=$(LINUX_CC) $(MAKE) BUILD_TAGS= EXTLDFLAGS=-fuse-ld=bfd dgraph
195+
else
196+
@GOOS=linux GOARCH=$(GOHOSTARCH) $(MAKE) dgraph
197+
endif
185198
@mkdir -p linux
186199
@mv ./dgraph/dgraph ./linux/dgraph
187200
@docker build -f contrib/Dockerfile -t dgraph/dgraph:local .

dgraph/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ gitBranch = github.com/dgraph-io/dgraph/v25/x.gitBranch
3232
lastCommitSHA = github.com/dgraph-io/dgraph/v25/x.lastCommitSHA
3333
lastCommitTime = github.com/dgraph-io/dgraph/v25/x.lastCommitTime
3434

35-
BUILD_FLAGS ?= -ldflags '-X ${lastCommitSHA}=${BUILD} -X "${lastCommitTime}=${BUILD_DATE}" -X "${dgraphVersion}=${BUILD_VERSION}" -X "${dgraphCodename}=${BUILD_CODENAME}" -X ${gitBranch}=${BUILD_BRANCH}'
35+
EXTLDFLAGS ?=
36+
BUILD_FLAGS ?= -ldflags '-X ${lastCommitSHA}=${BUILD} -X "${lastCommitTime}=${BUILD_DATE}" -X "${dgraphVersion}=${BUILD_VERSION}" -X "${dgraphCodename}=${BUILD_CODENAME}" -X ${gitBranch}=${BUILD_BRANCH}$(if $(EXTLDFLAGS), -extldflags "$(EXTLDFLAGS)")'
3637

3738
# Insert build tags if specified
3839
ifneq ($(strip $(BUILD_TAGS)),)

t/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ endif
1818

1919
all: test
2020

21-
.PHONY: check
22-
check: check-go check-docker check-gotestsum check-ack check-cross-compiler
21+
.PHONY: deps
22+
deps: check-go check-docker check-gotestsum check-ack check-cross-compiler
2323
@if [ "$(GOOS)" = "linux" ]; then \
2424
which protoc > /dev/null 2>&1 || (echo "Error: protoc is not installed or not in PATH" && exit 1); \
2525
fi
2626
@echo "All dependencies are installed"
27+
28+
.PHONY: check
29+
check: deps
2730
@echo "LINUX_GOBIN=$(LINUX_GOBIN)"
2831
@if [ -f "$(LINUX_GOBIN)/dgraph" ]; then \
2932
file $(LINUX_GOBIN)/dgraph | grep -q "ELF.*executable" || (echo "Error: dgraph binary at $(LINUX_GOBIN)/dgraph is not a Linux executable" && exit 1); \

0 commit comments

Comments
 (0)