$(info ************ Build Process with Makefiles in Golang ************) # Shorten links for My Makefile GoLang # - http://makefile-go-download.ilima.xyz - # - http://makefile-go-gist.ilima.xyz - # Define Go command and flags GO = go DELVE = dlv DEBUGPORT = 8181 GODEBUGFLAGS = -gcflags "all=-N -l" GOFLAGS = -ldflags="-s -w" # Makefiles MUST be indented using TABs and not spaces or `make` will fail. # Why does make think the target is up to date? # It happens when you have a file with the same name as Makefile target name in the directory where the Makefile is present. # Makefile Tutorial # https://makefiletutorial.com/ # https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_6.html # https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_7.html # Define the target executable ifneq ($(origin target), undefined) TARGET := $(target) else TARGET = my_app endif # Other variable GO_MAKEFILE = makefile.go.json GIST_ID = e3be354c2c3aa78a2f7956d046b0bf3a MY_APP_ENVS ?= MY_APP_ARGS ?= # Change the main file if needed via the variable MY_GO_FILE/file # $> export MY_GO_FILE=main.go # $> MY_GO_FILE=main.go make # $> make file=main.go # https://www.gnu.org/software/make/manual/html_node/Origin-Function.html ifneq ($(origin MY_GO_FILE), undefined) MAIN_FILE := $(MY_GO_FILE) endif ifneq ($(origin file), undefined) MAIN_FILE := $(file) endif ifeq ($(origin MAIN_FILE), undefined) MAIN_FILE := main.go endif $(info MAIN_FILE is $(MAIN_FILE)) # Default target: build the executable all: @echo "\nPREREQUISITES needed\nVARIABLES:\n• TARGET: $(TARGET)\n• MAIN_FILE: $(MAIN_FILE) \n\nARGS:\n• file\n e.g. make file=foo.go\n• target\n e.g. make target=app-a\n\nENV VARs:\n• MY_GO_FILE: $(MY_GO_FILE)\n• MY_APP_ENVS: $(MY_APP_ENVS)\n• MY_APP_ARGS: $(MY_APP_ARGS)" ls: @grep '^[^#[:space:]].*:' Makefile # Rule to build the target executable $(TARGET): $(MAIN_FILE) $(GO) build $(GOFLAGS) -o $(TARGET) $(MAIN_FILE) # Rule to build the target executable build: $(MAIN_FILE) $(GO) build $(GOFLAGS) -o $(TARGET) $(MAIN_FILE) # Run target: build and run the target executable run: $(TARGET) $(MAIN_FILE) ifeq ($(MY_APP_ENVS),) ./$(TARGET) $(MY_APP_ARGS) else ($(MY_APP_ENVS) && $(MY_APP_ENVS) ./$(TARGET) $(MY_APP_ARGS)) endif runn: @echo "clean, build and run at once" make clean make build make run # Clean target: remove the target executable clean: $(TARGET) rm -f $(TARGET) # Test target: run Go tests for the project test: $(GO) test ./... debug-build: $(GO) build $(GODEBUGFLAGS) -o $(TARGET) $(MAIN_FILE) debug-run: ifeq ($(MY_APP_ENVS),) $(DELVE) --listen=0.0.0.0:$(DEBUGPORT) --headless=true --api-version=2 --accept-multiclient exec ./$(TARGET) --log -- $(MY_APP_ARGS) else ($(MY_APP_ENVS) && $(MY_APP_ENVS) $(DELVE) --listen=0.0.0.0:$(DEBUGPORT) --headless=true --api-version=2 --accept-multiclient exec ./$(TARGET) --log -- $(MY_APP_ARGS)) endif debug-runn: @echo "clean, build and debug at once" make clean make debug-build make debug-run debug-conn: $(DELVE) connect 0.0.0.0:$(DEBUGPORT) open-help: vim -c ":h ilima-help | only" open-help-golang: vim -c ":h ilima-help-golang | only" update-makefile: curl https://gist.githubusercontent.com/igorlima/$(GIST_ID)/raw/Makefile -o Makefile ifeq (, $(shell which jq)) @echo "no jq installed, install it to make possible to have the Makefile Go JSON" else $(eval COMMITS = $(shell curl -L \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/gists/$(GIST_ID)/commits?per_page=10)) $(shell echo '$(COMMITS)' | jq -r '.[0] | {version, committed_at}' > $(GO_MAKEFILE)) endif check-makefile: ifeq (, $(shell which jq)) $(error "no jq installed, install it to proceed") endif $(eval COMMITS = $(shell curl -L \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/gists/$(GIST_ID)/commits?per_page=10)) $(eval LAST_COMMIT_ID = $(shell echo '$(COMMITS)' | jq -r '.[0].version')) $(eval LAST_COMMIT_DATE = $(shell echo '$(COMMITS)' | jq -r '.[0].committed_at')) @echo "\nlast commit" @echo " • id: $(LAST_COMMIT_ID)" @echo " • date: $(LAST_COMMIT_DATE)" @echo " • link: https://gist.github.com/igorlima/$(GIST_ID)/$(LAST_COMMIT_ID)" ifneq ("$(wildcard $(GO_MAKEFILE))","") # alternative to check if a file exists # test -e makefile.go.json && echo -n yes || echo -n no # ifeq ($(shell test -e file_name && echo -n yes),yes) $(eval LOCAL_COMMIT_ID = $(shell cat $(GO_MAKEFILE) | jq -r '.version')) $(eval LOCAL_COMMIT_DATE = $(shell cat $(GO_MAKEFILE) | jq -r '.committed_at')) @echo "\nlocal commit" @echo " • id: $(LOCAL_COMMIT_ID)" @echo " • date: $(LOCAL_COMMIT_DATE)" @echo " • link: https://gist.github.com/igorlima/$(GIST_ID)/$(LOCAL_COMMIT_ID)" else @echo "\nno $(GO_MAKEFILE) yet, run 'make update-makefile' to create a $(GO_MAKEFILE)" endif # https://www.gnu.org/software/make/manual/html_node/Multi_002dLine.html define GIST_API # https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#list-gist-commits # List gist commits curl -L \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/gists/e3be354c2c3aa78a2f7956d046b0bf3a/commits?per_page=10 # Get a gist revision curl -L \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/gists/e3be354c2c3aa78a2f7956d046b0bf3a/d3c088a4370741f96159c12fac1a1525729c9a3f endef