1+ # Executables
2+ GO ?= $(shell which go 2>/dev/null)
3+ DOCKER ?= $(shell which docker 2>/dev/null)
4+
5+ # Locations
6+ BUILD_DIR ?= build
7+ CMD_DIR := $(wildcard cmd/* )
8+
9+ # VERBOSE=1
10+ ifneq ($(VERBOSE ) ,)
11+ VERBOSE_FLAG = -v
12+ else
13+ VERBOSE_FLAG =
14+ endif
15+
16+ # Set OS and Architecture
17+ ARCH ?= $(shell arch | tr A-Z a-z | sed 's/x86_64/amd64/' | sed 's/i386/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/')
18+ OS ?= $(shell uname | tr A-Z a-z)
19+ VERSION ?= $(shell git describe --tags --always | sed 's/^v//')
20+
21+ # Set build flags
22+ BUILD_MODULE = $(shell cat go.mod | head -1 | cut -d ' ' -f 2)
23+ BUILD_LD_FLAGS += -X $(BUILD_MODULE ) /pkg/version.GitSource=${BUILD_MODULE}
24+ BUILD_LD_FLAGS += -X $(BUILD_MODULE ) /pkg/version.GitTag=$(shell git describe --tags --always)
25+ BUILD_LD_FLAGS += -X $(BUILD_MODULE ) /pkg/version.GitBranch=$(shell git name-rev HEAD --name-only --always)
26+ BUILD_LD_FLAGS += -X $(BUILD_MODULE ) /pkg/version.GitHash=$(shell git rev-parse HEAD)
27+ BUILD_LD_FLAGS += -X $(BUILD_MODULE ) /pkg/version.GoBuildTime=$(shell date -u '+% Y-% m-% dT% H:% M:% SZ')
28+ BUILD_FLAGS = -ldflags "-s -w ${BUILD_LD_FLAGS}"
29+
30+ # Docker
31+ DOCKER_REPO ?= ghcr.io/mutablelogic/go-llm
32+ DOCKER_SOURCE ?= ${BUILD_MODULE}
33+ DOCKER_TAG = ${DOCKER_REPO}-${OS}-${ARCH}:${VERSION}
34+
35+ # ##############################################################################
36+ # ALL
37+
38+ .PHONY : all
39+ all : clean build
40+
41+ # ##############################################################################
42+ # BUILD
43+
44+ # Build the commands in the cmd directory
45+ .PHONY : build
46+ build : tidy $(CMD_DIR )
47+
48+ $(CMD_DIR ) : go-dep mkdir
49+ @echo Build command $(notdir $@ ) GOOS=${OS} GOARCH=${ARCH}
50+ @GOOS=${OS} GOARCH=${ARCH} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR} /$(notdir $@ ) ./$@
51+
52+ # Build the docker image
53+ .PHONY : docker
54+ docker : docker-dep
55+ @echo build docker image ${DOCKER_TAG} OS=${OS} ARCH=${ARCH} SOURCE=${DOCKER_SOURCE} VERSION=${VERSION}
56+ @${DOCKER} build \
57+ --tag ${DOCKER_TAG} \
58+ --build-arg ARCH=${ARCH} \
59+ --build-arg OS=${OS} \
60+ --build-arg SOURCE=${DOCKER_SOURCE} \
61+ --build-arg VERSION=${VERSION} \
62+ -f etc/docker/Dockerfile .
63+
64+ # Push docker container
65+ .PHONY : docker-push
66+ docker-push : docker-dep
67+ @echo push docker image: ${DOCKER_TAG}
68+ @${DOCKER} push ${DOCKER_TAG}
69+
70+ # Print out the version
71+ .PHONY : docker-version
72+ docker-version : docker-dep
73+ @echo " tag=${VERSION} "
74+
75+ # ##############################################################################
76+ # TEST
77+
78+ .PHONY : test
79+ test : unit-test coverage-test
80+
81+ .PHONY : unit-test
82+ unit-test : go-dep
83+ @echo Unit Tests
84+ @${GO} test ${VERBOSE_FLAG} ./pkg/...
85+
86+ .PHONY : coverage-test
87+ coverage-test : go-dep mkdir
88+ @echo Test Coverage
89+ @${GO} test -coverprofile ${BUILD_DIR} /coverprofile.out ./pkg/...
90+
91+ # ##############################################################################
92+ # CLEAN
93+
94+ .PHONY : tidy
95+ tidy :
96+ @echo Running go mod tidy
97+ @${GO} mod tidy
98+
99+ .PHONY : mkdir
100+ mkdir :
101+ @install -d ${BUILD_DIR}
102+
103+ .PHONY : clean
104+ clean :
105+ @echo Clean
106+ @rm -fr $(BUILD_DIR )
107+ @${GO} clean
108+
109+ # ##############################################################################
110+ # DEPENDENCIES
111+
112+ .PHONY : go-dep
113+ go-dep :
114+ @test -f " ${GO} " && test -x " ${GO} " || (echo " Missing go binary" && exit 1)
115+
116+ .PHONY : docker-dep
117+ docker-dep :
118+ @test -f " ${DOCKER} " && test -x " ${DOCKER} " || (echo " Missing docker binary" && exit 1)
0 commit comments