Skip to content

Commit 50e6910

Browse files
authored
Add control for go build flags (#51)
1 parent e7bf1c1 commit 50e6910

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

makefiles/build.mk

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
GO ?= go
22

3-
# Override in app Makefile to add custom ldflags, example BUILD_LDFLAGS="-s -w"
4-
BUILD_LDFLAGS ?= ""
3+
# Override in app Makefile to add custom ldflags, example BUILD_LDFLAGS=-s -w
4+
BUILD_LDFLAGS ?=
5+
6+
# Override in app Makefile to add custom build flags, example BUILD_FLAGS=-trimpath -pgo=auto
7+
BUILD_FLAGS ?= -trimpath
58

69
# Override in app Makefile to control build target, example BUILD_PKG=./cmd/my-app
710
BUILD_PKG ?= .
@@ -12,16 +15,16 @@ BUILD_DIR ?= ./bin
1215
## Build Linux binary
1316
build-linux:
1417
@echo "Building Linux AMD64 binary, GOFLAGS: $(GOFLAGS)"
15-
@GOOS=linux GOARCH=amd64 $(GO) build -trimpath -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" -o $(BUILD_DIR)/ $(BUILD_PKG)
18+
@GOOS=linux GOARCH=amd64 $(GO) build $(BUILD_FLAGS) -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" -o $(BUILD_DIR)/ $(BUILD_PKG)
1619

1720
## Build binary
1821
build:
1922
@echo "Building binary, GOFLAGS: $(GOFLAGS)"
20-
@$(GO) build -trimpath -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" -o $(BUILD_DIR)/ $(BUILD_PKG)
23+
@$(GO) build $(BUILD_FLAGS) -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" -o $(BUILD_DIR)/ $(BUILD_PKG)
2124

2225
## Build and run binary
2326
run:
2427
@echo "Running binary"
25-
@$(GO) run -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" $(BUILD_PKG)
28+
@$(GO) run $(BUILD_FLAGS) -ldflags "$(shell bash $(DEVGO_SCRIPTS)/version-ldflags.sh && echo $(BUILD_LDFLAGS))" $(BUILD_PKG)
2629

2730
.PHONY: build-linux build run

0 commit comments

Comments
 (0)