#
# SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
# SPDX-License-Identifier: Apache-2.0
#

USER_ID         = $(shell id -u)
BIN            ?= dgraph
BUILD          ?= $(shell git rev-parse --short HEAD)
BUILD_CODENAME ?= dgraph
BUILD_DATE     ?= $(shell git log -1 --format=%ci)
BUILD_BRANCH   ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILD_VERSION  ?= $(shell git describe --always --tags)

# check if DGRAPH_VERSION flag is set (used for release pipeline)
ifneq ($(DGRAPH_VERSION),)
# remove arch suffix from DGRAPH_VERSION for CD steps only
BUILD_VERSION := $(shell echo ${DGRAPH_VERSION} | sed -e 's/-amd64//' -e 's/-arm64//')
endif

GOOS          ?= $(shell go env GOOS)
GOARCH        ?= $(shell go env GOARCH)
# Only build with jemalloc on Linux, mac
ifeq ($(GOOS),$(filter $(GOOS),linux darwin))
	BUILD_TAGS ?= jemalloc
endif
GOPATH        ?= $(shell go env GOPATH)

# Build-time Go variables
dgraphVersion   = github.com/dgraph-io/dgraph/v25/x.dgraphVersion
dgraphCodename  = github.com/dgraph-io/dgraph/v25/x.dgraphCodename
gitBranch       = github.com/dgraph-io/dgraph/v25/x.gitBranch
lastCommitSHA   = github.com/dgraph-io/dgraph/v25/x.lastCommitSHA
lastCommitTime  = github.com/dgraph-io/dgraph/v25/x.lastCommitTime

EXTLDFLAGS    ?=
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)")'

# Insert build tags if specified
ifneq ($(strip $(BUILD_TAGS)),)
	BUILD_FLAGS += -tags '$(BUILD_TAGS)'
endif

# Build with compiler optimizations disabled, which will help debugging with dlv.
ifneq ($(strip $(BUILD_DEBUG)),)
	BUILD_FLAGS += -gcflags="all=-N -l"
endif

# Build with race detector enabled.
ifneq ($(strip $(BUILD_RACE)),)
	BUILD_FLAGS += -race
endif

# jemalloc stuff
# Check common package-manager install paths: /usr/local/lib (make
# install from source), /usr/lib (apt-get libjemalloc-dev), and
# /usr/lib/libjemalloc_pic.a (apk add jemalloc-dev on Alpine/Wolfi).
HAS_JEMALLOC = $(shell test -f /usr/local/lib/libjemalloc.a -o -f /usr/lib/libjemalloc.a -o -f /usr/lib/libjemalloc_pic.a && echo "jemalloc")
JEMALLOC_URL = "https://github.com/jemalloc/jemalloc/releases/download/5.3.1/jemalloc-5.3.1.tar.bz2"

# go install variables
HAS_SHA256SUM = $(shell which sha256sum)
INSTALL_TARGET = $(GOPATH)/bin/$(BIN)
ifneq ($(strip $(shell go env GOBIN)),)
	INSTALL_TARGET = $(shell go env GOBIN)/$(BIN)
endif
ifneq ($(GOOS)_$(GOARCH),$(shell go env GOHOSTOS)_$(shell go env GOHOSTARCH))
	INSTALL_TARGET = $(GOPATH)/bin/$(GOOS)_$(GOARCH)/$(BIN)
endif

.PHONY: all $(BIN)
all: $(BIN)

$(BIN): clean jemalloc
	@go mod tidy
	@go build $(BUILD_FLAGS) -o $(BIN)

test-coverage-binary: clean jemalloc
	@go test -c -covermode=atomic -coverpkg ../... $(BUILD_FLAGS) -o $(BIN)

clean:
	@rm -f $(BIN)

uninstall:
	@go clean -i -x

install: jemalloc
	@echo "Commit SHA256: `git rev-parse HEAD`"
	@if [ "$(HAS_SHA256SUM)" ] ; then \
		echo "Old SHA256:" `sha256sum $(INSTALL_TARGET) 2>/dev/null | cut -c-64` ; \
	fi
	@go mod tidy
	@# Use 'go build -o' rather than 'go install': 'go install' honors
	@# the package's default name (always "dgraph"), but we want to
	@# honor $(BIN) so renames such as a fork binary produce the
	@# correct file name.
	@go build $(BUILD_FLAGS) -o $(INSTALL_TARGET)
	@echo "Installed $(BIN) ($(GOOS)/$(GOARCH)) to $(INSTALL_TARGET)"
	@if [ "$(HAS_SHA256SUM)" ] ; then \
		echo "New SHA256:" `sha256sum $(INSTALL_TARGET) 2>/dev/null | cut -c-64` ; \
	fi

# jemalloc 5.3.1 source compat patch: libstdc++ 16+ removed
# std::__throw_bad_alloc from the public ABI, breaking
# src/jemalloc_cpp.cpp on GCC 16 / libstdc++ 16 toolchains (notably
# Chainguard go-fips:v1.26.3.1-dev and later). The sed substitution
# below is a no-op on older libstdc++ (exact-match) and rewrites the
# legacy call to the standard `throw std::bad_alloc()` idiom on
# libstdc++ 16+. Drop the sed line once a tagged jemalloc release
# upstreams the fix and JEMALLOC_URL bumps past it.
jemalloc:
	@if [ -z "$(HAS_JEMALLOC)" ] ; then \
		mkdir -p /tmp/jemalloc-temp && cd /tmp/jemalloc-temp ; \
		echo "Downloading jemalloc" ; \
		curl -f -s -L ${JEMALLOC_URL} -o jemalloc.tar.bz2 ; \
		tar xjf ./jemalloc.tar.bz2 ; \
		cd jemalloc-5.3.1 ; \
		sed -i.bak 's|std::__throw_bad_alloc()|throw std::bad_alloc()|g' src/jemalloc_cpp.cpp ; \
		./configure --with-jemalloc-prefix='je_' --with-malloc-conf='background_thread:true,metadata_thp:auto'; \
		make ; \
		if [ "$(USER_ID)" = "0" ]; then \
			make install ; \
		else \
			echo "==== Need sudo access to install jemalloc" ; \
			sudo make install ; \
		fi \
	fi
