From 0407d48350cd6a7b76f12463fb572ea94d5df2bf Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Mon, 8 Apr 2024 22:27:19 +0200 Subject: [PATCH] Improve Makefile (#7) * Improve Makefile by combining all build targets for different platforms into a single target * Inject version into the binary on build --- Makefile | 99 +++++++++++++++++------------------------------- plugin/module.go | 3 +- 2 files changed, 36 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index a37858c..36c6661 100644 --- a/Makefile +++ b/Makefile @@ -1,82 +1,51 @@ -PROJECT_URL=github.com/gatewayd-io/gatewayd -CONFIG_PACKAGE=${PROJECT_URL}/config +PLUGIN_NAME=gatewayd-plugin-js +PROJECT_URL=github.com/gatewayd-io/$(PLUGIN_NAME) +CONFIG_PACKAGE=${PROJECT_URL}/plugin LAST_TAGGED_COMMIT=$(shell git rev-list --tags --max-count=1) VERSION=$(shell git describe --tags ${LAST_TAGGED_COMMIT}) -TIMESTAMP=$(shell date -u +"%FT%T%z") -VERSION_DETAILS=${TIMESTAMP}/${LAST_TAGGED_COMMIT_SHORT} -EXTRA_LDFLAGS=-X ${CONFIG_PACKAGE}.Version=${VERSION} -X ${CONFIG_PACKAGE}.VersionDetails=${VERSION_DETAILS} -FILES=gatewayd-plugin-js checksum.txt gatewayd_plugin.yaml README.md LICENSE +EXTRA_LDFLAGS=-X ${CONFIG_PACKAGE}.Version=$(shell echo ${VERSION} | sed 's/^v//') +FILES=$(PLUGIN_NAME) checksum.txt gatewayd_plugin.yaml README.md LICENSE tidy: @go mod tidy -test: - @go test -v ./... +build: tidy + @go build -ldflags "-s -w" checksum: - @sha256sum -b gatewayd-plugin-js + @sha256sum -b $(PLUGIN_NAME) update-all: @go get -u ./... + @go mod tidy build-dev: tidy - @CGO_ENABLED=0 go build + @go build + +test: + @go test -v ./... create-build-dir: @mkdir -p dist -build-linux-amd64: tidy - @echo "Building gatewayd ${VERSION} for linux-amd64" - @mkdir -p dist/linux-amd64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/linux-amd64/ - @GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/linux-amd64/gatewayd-plugin-js - @sha256sum dist/linux-amd64/gatewayd-plugin-js | sed 's/dist\/linux-amd64\///g' >> dist/linux-amd64/checksum.txt - @tar czf dist/gatewayd-plugin-js-linux-amd64-${VERSION}.tar.gz -C ./dist/linux-amd64/ ${FILES} - @sha256sum dist/gatewayd-plugin-js-linux-amd64-${VERSION}.tar.gz | sed 's/dist\///g' >> dist/checksums.txt - -build-linux-arm64: - @echo "Building gatewayd ${VERSION} for linux-arm64" - @mkdir -p dist/linux-arm64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/linux-arm64/ - @GOOS=linux GOARCH=arm64 CGO_ENABLED=0 CC=aarch64-linux-gnu-gcc go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/linux-arm64/gatewayd-plugin-js - @sha256sum dist/linux-arm64/gatewayd-plugin-js | sed 's/dist\/linux-arm64\///g' >> dist/linux-arm64/checksum.txt - @tar czf dist/gatewayd-plugin-js-linux-arm64-${VERSION}.tar.gz -C ./dist/linux-arm64/ ${FILES} - @sha256sum dist/gatewayd-plugin-js-linux-arm64-${VERSION}.tar.gz | sed 's/dist\///g' >> dist/checksums.txt - -build-darwin-amd64: - @echo "Building gatewayd ${VERSION} for darwin-arm64" - @mkdir -p dist/darwin-amd64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/darwin-amd64/ - @GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/darwin-amd64/gatewayd-plugin-js - @shasum -a 256 dist/darwin-amd64/gatewayd-plugin-js | sed 's/dist\/darwin-amd64\///g' >> dist/darwin-amd64/checksum.txt - @tar czf dist/gatewayd-plugin-js-darwin-amd64-${VERSION}.tar.gz -C ./dist/darwin-amd64/ ${FILES} - @shasum -a 256 dist/gatewayd-plugin-js-darwin-amd64-${VERSION}.tar.gz | sed 's/dist\///g' >> dist/checksums.txt - -build-darwin-arm64: - @echo "Building gatewayd ${VERSION} for darwin-arm64" - @mkdir -p dist/darwin-arm64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/darwin-arm64/ - @GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/darwin-arm64/gatewayd-plugin-js - @shasum -a 256 dist/darwin-arm64/gatewayd-plugin-js | sed 's/dist\/darwin-arm64\///g' >> dist/darwin-arm64/checksum.txt - @tar czf dist/gatewayd-plugin-js-darwin-arm64-${VERSION}.tar.gz -C ./dist/darwin-arm64/ ${FILES} - @shasum -a 256 dist/gatewayd-plugin-js-darwin-arm64-${VERSION}.tar.gz | sed 's/dist\///g' >> dist/checksums.txt - -build-windows-amd64: - @echo "Building gatewayd ${VERSION} for windows-amd64" - @mkdir -p dist/windows-amd64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/windows-amd64/ - @GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/windows-amd64/gatewayd-plugin-js.exe - @sha256sum dist/windows-amd64/gatewayd-plugin-js.exe | sed 's/dist\/windows-amd64\///g' >> dist/windows-amd64/checksum.txt - @zip -r dist/gatewayd-plugin-js-windows-amd64-${VERSION}.zip -j ./dist/windows-amd64/ - @sha256sum dist/gatewayd-plugin-js-windows-amd64-${VERSION}.zip | sed 's/dist\///g' >> dist/checksums.txt - -build-windows-arm64: - @echo "Building gatewayd ${VERSION} for windows-arm64" - @mkdir -p dist/windows-arm64 - @cp README.md LICENSE gatewayd_plugin.yaml ./dist/windows-arm64/ - @GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o dist/windows-arm64/gatewayd-plugin-js.exe - @sha256sum dist/windows-arm64/gatewayd-plugin-js.exe | sed 's/dist\/windows-arm64\///g' >> dist/windows-arm64/checksum.txt - @zip -r dist/gatewayd-plugin-js-windows-arm64-${VERSION}.zip -j ./dist/windows-arm64/ - @sha256sum dist/gatewayd-plugin-js-windows-arm64-${VERSION}.zip | sed 's/dist\///g' >> dist/checksums.txt - -build-release: tidy create-build-dir build-linux-amd64 build-linux-arm64 build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-windows-arm64 +build-release: tidy create-build-dir + @echo "Building ${PLUGIN_NAME} ${VERSION} for release" + @$(MAKE) build-platform GOOS=linux GOARCH=amd64 OUTPUT_DIR=dist/linux-amd64 + @$(MAKE) build-platform GOOS=linux GOARCH=arm64 OUTPUT_DIR=dist/linux-arm64 + @$(MAKE) build-platform GOOS=darwin GOARCH=amd64 OUTPUT_DIR=dist/darwin-amd64 + @$(MAKE) build-platform GOOS=darwin GOARCH=arm64 OUTPUT_DIR=dist/darwin-arm64 + @$(MAKE) build-platform GOOS=windows GOARCH=amd64 OUTPUT_DIR=dist/windows-amd64 + @$(MAKE) build-platform GOOS=windows GOARCH=arm64 OUTPUT_DIR=dist/windows-arm64 + +build-platform: tidy + @echo "Building ${PLUGIN_NAME} ${VERSION} for $(GOOS)-$(GOARCH)" + @mkdir -p $(OUTPUT_DIR) + @cp README.md LICENSE gatewayd_plugin.yaml $(OUTPUT_DIR)/ + @GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -trimpath -ldflags "-s -w ${EXTRA_LDFLAGS}" -o $(OUTPUT_DIR)/$(PLUGIN_NAME) + @sha256sum $(OUTPUT_DIR)/$(PLUGIN_NAME) | sed 's#$(OUTPUT_DIR)/##g' >> $(OUTPUT_DIR)/checksum.txt + @if [ "$(GOOS)" = "windows" ]; then \ + zip -q -r dist/$(PLUGIN_NAME)-$(GOOS)-$(GOARCH)-${VERSION}.zip -j $(OUTPUT_DIR)/; \ + else \ + tar czf dist/$(PLUGIN_NAME)-$(GOOS)-$(GOARCH)-${VERSION}.tar.gz -C $(OUTPUT_DIR)/ ${FILES}; \ + fi + @sha256sum dist/$(PLUGIN_NAME)-$(GOOS)-$(GOARCH)-${VERSION}.* | sed '#dist/##g' >> dist/checksums.txt diff --git a/plugin/module.go b/plugin/module.go index 39108a3..69c8339 100644 --- a/plugin/module.go +++ b/plugin/module.go @@ -7,9 +7,10 @@ import ( ) var ( + Version = "0.0.1" PluginID = v1.PluginID{ Name: "gatewayd-plugin-js", - Version: "0.0.1", + Version: Version, RemoteUrl: "github.com/gatewayd-io/gatewayd-plugin-js", } PluginMap = map[string]goplugin.Plugin{