Files
katenary/Makefile

154 lines
4.6 KiB
Makefile
Raw Normal View History

2021-12-03 14:12:45 +01:00
CUR_SHA=$(shell git log -n1 --pretty='%h')
CUR_BRANCH=$(shell git branch --show-current)
VERSION=$(shell git describe --exact-match --tags $(CUR_SHA) 2>/dev/null || echo $(CUR_BRANCH)-$(CUR_SHA))
2021-12-01 09:09:30 +01:00
CTN:=$(shell which podman 2>&1 1>/dev/null && echo "podman" || echo "docker")
2021-12-02 12:36:32 +01:00
PREFIX=~/.local
2021-12-01 09:09:30 +01:00
2022-01-26 09:44:23 +01:00
GO=container
OUT=katenary
BLD_CMD=go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(OUT) ./cmd/*.go
2022-02-16 18:32:51 +01:00
GOOS=linux
GOARCH=amd64
BUILD_IMAGE=docker.io/golang:1.18-alpine
2022-01-26 09:43:01 +01:00
.PHONY: help clean build
2021-12-01 09:09:30 +01:00
.ONESHELL:
help:
@cat <<EOF
=== HELP ===
To avoid you to install Go, the build is made by podman or docker.
You can use:
$$ make install
This will build and install katenary inside the PREFIX(/bin) value (default is $(PREFIX))
To change the PREFIX to somewhere where only root or sudo users can save the binary, it is recommended to build before install:
$$ make build
$$ sudo make install PREFIX=/usr/local
2022-02-16 18:32:51 +01:00
Katenary is statically built (in Go), so there is no library to install.
To build for others OS:
$$ make build GOOS=linux GOARCH=amd64
2022-02-16 18:32:51 +01:00
This will build the binary for linux amd64.
$$ make build GOOS=linux GOARCH=arm
2022-02-16 18:32:51 +01:00
This will build the binary for linux arm.
$$ make build GOOS=windows GOARCH=amd64
2022-02-16 18:32:51 +01:00
This will build the binary for windows amd64.
$$ make build GOOS=darwin GOARCH=amd64
2022-02-16 18:32:51 +01:00
This will build the binary for darwin amd64.
2022-02-16 18:32:51 +01:00
Or you can build all versions:
$$ make build-all
2021-12-01 09:09:30 +01:00
EOF
2022-02-16 18:32:51 +01:00
build: pull katenary
build-all: pull dist dist/katenary-linux-amd64 dist/katenary-linux-arm64 dist/katenary.exe dist/katenary-darwin-amd64 dist/katenary-freebsd-amd64 dist/katenary-freebsd-arm64
2022-02-16 18:32:51 +01:00
pull:
ifneq ($(GO),local)
2022-02-16 18:32:51 +01:00
@echo -e "\033[1;32mPulling $(BUILD_IMAGE) docker image\033[0m"
@$(CTN) pull $(BUILD_IMAGE)
endif
2022-02-16 18:32:51 +01:00
dist:
mkdir -p dist
dist/katenary-linux-amd64:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for linux-amd64...\033[0m"
$(MAKE) katenary GOOS=linux GOARCH=amd64 OUT=$@
2022-02-16 18:32:51 +01:00
dist/katenary-linux-arm64:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for linux-arm...\033[0m"
$(MAKE) katenary GOOS=linux GOARCH=arm64 OUT=$@
2022-02-16 18:32:51 +01:00
dist/katenary.exe:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for windows...\033[0m"
$(MAKE) katenary GOOS=windows GOARCH=amd64 OUT=$@
2022-02-16 18:32:51 +01:00
dist/katenary-darwin-amd64:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for darwin...\033[0m"
$(MAKE) katenary GOOS=darwin GOARCH=amd64 OUT=$@
dist/katenary-freebsd-amd64:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for freebsd...\033[0m"
$(MAKE) katenary GOOS=freebsd GOARCH=amd64 OUT=$@
dist/katenary-freebsd-arm64:
@echo
@echo -e "\033[1;32mBuilding katenary $(VERSION) for freebsd-arm64...\033[0m"
$(MAKE) katenary GOOS=freebsd GOARCH=arm64 OUT=$@
2022-01-26 09:21:36 +01:00
katenary: $(wildcard */*.go Makefile go.mod go.sum)
2022-01-26 09:43:01 +01:00
ifeq ($(GO),local)
@echo "=> Build in host using go"
2021-12-01 09:09:30 +01:00
else
2022-01-26 09:43:01 +01:00
@echo "=> Build in container using" $(CTN)
endif
echo $(BLD_CMD)
2022-02-14 17:15:11 +01:00
ifeq ($(GO),local)
2022-01-26 09:43:01 +01:00
$(BLD_CMD)
else ifeq ($(CTN),podman)
@podman run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --userns keep-id -it docker.io/golang $(BLD_CMD)
2022-01-26 09:43:01 +01:00
else
@docker run -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \
--rm -v $(PWD):/go/src/katenary:z -w /go/src/katenary --user $(shell id -u):$(shell id -g) -e HOME=/tmp -it docker.io/golang $(BLD_CMD)
2021-12-01 09:09:30 +01:00
endif
echo "=> Stripping if possible"
strip $(OUT) 2>/dev/null || echo "=> No strip available"
2022-02-16 18:32:51 +01:00
2021-12-01 09:09:30 +01:00
install: build
cp katenary $(PREFIX)/bin/katenary
uninstall:
rm -f $(PREFIX)/bin/katenary
clean:
rm -rf katenary dist/* release.id
tests: test
test:
@echo -e "\033[1;33mTesting katenary $(VERSION)...\033[0m"
go test -v ./...
.ONESHELL:
push-release:
@rm -f release.id
# read personal access token from .git-credentials
TOKEN=$(shell cat .credentials)
# create the body of the release from latest tag
# create a new release based on current tag and get the release id
@curl -sSL -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $$TOKEN" \
-d "{\"tag_name\": \"$(VERSION)\", \"target_commitish\": \"\", \"name\": \"$(VERSION)\", \"draft\": true, \"prerelease\": true}" \
https://api.github.com/repos/metal3d/katenary/releases | jq -r '.id' > release.id
@echo "Release id: $$(cat release.id) created"
@echo "Uploading assets..."
# push all dist binary as assets to the release
@for i in $$(find dist -type f -name "katenary*"); do
curl -sSL -H "Authorization: token $$TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/octet-stream" \
--data-binary @$$i \
https://uploads.github.com/repos/metal3d/katenary/releases/$(shell cat release.id)/assets?name=$$(basename $$i)
done
@rm -f release.id