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
2024-11-08 13:30:47 +01:00
GOVERSION = 1.23
2022-01-26 09:44:23 +01:00
GO = container
2022-03-31 14:12:20 +02:00
OUT = katenary
2023-12-06 15:24:02 +01:00
BLD_CMD = go build -ldflags= " -X 'katenary/generator.Version= $( VERSION) ' " -o $( OUT) ./cmd/katenary
2022-02-16 18:32:51 +01:00
GOOS = linux
GOARCH = amd64
2023-12-06 15:24:02 +01:00
SIGNER = metal3d@gmail.com
BUILD_IMAGE = docker.io/golang:$( GOVERSION) -alpine
# SHELL=/bin/bash
# List of source files
SOURCES = $( wildcard ./*.go ./*/*.go ./*/*/*.go)
# List of binaries to build and sign
BINARIES = dist/katenary-linux-amd64 dist/katenary-linux-arm64 dist/katenary.exe dist/katenary-darwin-amd64 dist/katenary-freebsd-amd64 dist/katenary-freebsd-arm64
# List of signatures to build
ASC_BINARIES = $( patsubst %,%.asc,$( BINARIES) )
# defaults
2024-10-17 17:08:42 +02:00
BROWSER = $( shell command -v epiphany || echo xdg-open)
2023-12-06 15:24:02 +01:00
SHELL := bash
# strict mode
.SHELLFLAGS := -eu -o pipefail -c
# One session per target
.ONESHELL :
.DELETE_ON_ERROR :
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
2024-04-03 14:15:28 +02:00
.PHONY : help clean build install tests test
2022-02-16 18:32:51 +01:00
2023-12-06 15:24:02 +01:00
all : build
2022-01-26 09:43:01 +01:00
2024-10-17 17:08:42 +02:00
2021-12-01 09:09:30 +01:00
help :
2023-12-06 15:24:02 +01:00
@cat <<EOF | fold -s -w 80
2021-12-01 09:09:30 +01:00
= = = HELP = = =
To avoid you to install Go, the build is made by podman or docker.
2023-12-06 15:24:02 +01:00
Installinf ( you can use local Go by setting GO = local ) ) :
# use podman or docker to build
2021-12-01 09:09:30 +01:00
$$ make install
2023-12-06 15:24:02 +01:00
# or use local Go
$$ make install GO = local
2021-12-01 09:09:30 +01:00
This will build and install katenary inside the PREFIX( /bin) value ( default is $( PREFIX) )
2023-12-06 15:24:02 +01:00
To change the PREFIX to somewhere where only root or sudo users can save the binary, it is recommended to build before install, one more time you can use local Go by setting GO = local:
2021-12-01 09:09:30 +01:00
$$ 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:
2022-03-31 14:12:20 +02:00
$$ make build GOOS = linux GOARCH = amd64
2022-02-16 18:32:51 +01:00
This will build the binary for linux amd64.
2022-03-31 14:12:20 +02:00
$$ make build GOOS = linux GOARCH = arm
2022-02-16 18:32:51 +01:00
This will build the binary for linux arm.
2022-03-31 14:12:20 +02:00
$$ make build GOOS = windows GOARCH = amd64
2022-02-16 18:32:51 +01:00
This will build the binary for windows amd64.
2022-03-31 14:12:20 +02:00
$$ make build GOOS = darwin GOARCH = amd64
2022-02-16 18:32:51 +01:00
This will build the binary for darwin amd64.
2022-03-31 14:12:20 +02:00
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-04-01 10:47:58 +02:00
2023-12-06 15:24:02 +01:00
## Standard build
build : pull katenary
2022-02-16 18:32:51 +01:00
pull :
2022-03-31 14:12:20 +02:00
i f n e q ( $( GO ) , l o c a l )
2022-02-16 18:32:51 +01:00
@echo -e " \033[1;32mPulling $( BUILD_IMAGE) docker image\033[0m "
@$( CTN) pull $( BUILD_IMAGE)
2022-03-31 14:12:20 +02:00
e n d i f
2022-02-16 18:32:51 +01:00
2023-12-06 15:24:02 +01:00
katenary : $( SOURCES ) Makefile go .mod go .sum
i f e q ( $( GO ) , l o c a l )
@echo "=> Build on host using go"
e l s e
@echo "=> Build in container using" $( CTN)
e n d i f
echo $( BLD_CMD)
i f e q ( $( GO ) , l o c a l )
$( BLD_CMD)
e l s e i f e q ( $( CTN ) , p o d m a n )
@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 $( BUILD_IMAGE) $( BLD_CMD)
e l s e
@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 $( BUILD_IMAGE) $( BLD_CMD)
e n d i f
echo "=> Stripping if possible"
strip $( OUT) 2>/dev/null || echo "=> No strip available"
## Release build
dist : prepare $( BINARIES ) $( ASC_BINARIES )
prepare : pull
2022-02-16 18:32:51 +01:00
mkdir -p dist
dist/katenary-linux-amd64 :
2022-03-31 14:12:20 +02:00
@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 :
2022-03-31 14:12:20 +02:00
@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 :
2022-03-31 14:12:20 +02:00
@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
2022-03-31 14:12:20 +02: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-02-16 18:32:51 +01:00
2023-12-06 15:24:02 +01:00
gpg-sign :
rm -f dist/*.asc
$( MAKE) $( ASC_BINARIES)
2021-12-01 08:32:30 +01:00
2023-12-06 15:24:02 +01:00
dist/%.asc : dist /%
gpg --armor --detach-sign --default-key $( SIGNER) $< & >/dev/null || exit 1
2021-12-01 08:32:30 +01:00
2021-12-01 09:09:30 +01:00
install : build
2023-12-06 15:24:02 +01:00
install -Dm755 katenary $( PREFIX) /bin/katenary
2021-12-01 09:09:30 +01:00
uninstall :
rm -f $( PREFIX) /bin/katenary
2021-12-01 08:32:30 +01:00
clean :
2022-03-31 14:12:20 +02:00
rm -rf katenary dist/* release.id
2024-04-10 04:51:45 +02:00
serve-doc : __label_doc
@cd doc && \
[ -d venv ] || python -m venv venv; \
source venv/bin/activate && \
echo "==> Installing requirements in the virtual env..."
pip install -qq -r requirements.txt && \
echo "==> Serving doc with mkdocs..." && \
mkdocs serve
2022-03-31 14:12:20 +02:00
tests : test
test :
@echo -e " \033[1;33mTesting katenary $( VERSION) ...\033[0m "
2024-10-17 17:08:42 +02:00
go test -coverprofile= cover.out ./...
2024-11-18 17:12:12 +01:00
$( MAKE) cover
cover :
2024-10-17 17:08:42 +02:00
go tool cover -func= cover.out | grep "total:"
go tool cover -html= cover.out -o cover.html
if [ " $( BROWSER) " = "xdg-open" ] ; then
xdg-open cover.html
else
$( BROWSER) -i --new-window cover.html
fi
2021-12-01 08:32:30 +01:00
2022-03-31 14:14:54 +02:00
push-release : build -all
2022-03-31 14:12:20 +02:00
@rm -f release.id
# read personal access token from .git-credentials
TOKEN = $( shell cat .credentials)
# 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 \
2022-03-31 14:17:15 +02:00
https://uploads.github.com/repos/metal3d/katenary/releases/$$ ( cat release.id) /assets?name= $$ ( basename $$ i)
2022-03-31 14:12:20 +02:00
done
@rm -f release.id
2023-12-06 15:24:02 +01:00
__label_doc :
2024-04-10 04:51:45 +02:00
@command -v gomarkdoc || ( echo "==> We need to install gomarkdoc..." && \
go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@latest)
2023-12-06 15:24:02 +01:00
@echo "=> Generating labels doc..."
# short label doc
go run ./cmd/katenary help-labels -m | \
sed -i '
/START_LABEL_DOC/,/STOP_LABEL_DOC/{ /<!--/!d} ;
/START_LABEL_DOC/,/STOP_LABEL_DOC/r/dev/stdin
' doc/docs/labels.md
# detailed label doc
go run ./cmd/katenary help-labels -am | sed 's/^##/###/' | \
sed -i '
/START_DETAILED_DOC/,/STOP_DETAILED_DOC/{ /<!--/!d} ;
/START_DETAILED_DOC/,/STOP_DETAILED_DOC/r/dev/stdin
' doc/docs/labels.md
echo "=> Generating Code documentation..."
PACKAGES = $$ ( for f in $$ ( find . -name "*.go" -type f) ; do dirname $$ f; done | sort -u)
for pack in $$ PACKAGES; do
echo " -> Generating doc for $$ pack "
2024-04-10 04:51:45 +02:00
gomarkdoc --repository.default-branch $( shell git branch --show-current) -o doc/docs/packages/$$ pack.md $$ pack
2023-12-06 15:24:02 +01:00
sed -i '/^## Index/,/^##/ { /## Index/d; /^##/! d }' doc/docs/packages/$$ pack.md
done