|
| 1 | +PKG := github.com/lightningnetwork/lightning-onion |
| 2 | +TOOLS_DIR := tools |
| 3 | + |
| 4 | +GOBUILD := GO111MODULE=on go build -v |
| 5 | +GOINSTALL := GO111MODULE=on go install -v |
| 6 | + |
| 7 | +GOFILES = $(shell find . -type f -name '*.go') |
| 8 | + |
| 9 | +RM := rm -f |
| 10 | +CP := cp |
| 11 | +MAKE := make |
| 12 | +XARGS := xargs -L 1 |
| 13 | + |
| 14 | +# GO_VERSION is the Go version used for the release build, docker files, and |
| 15 | +# GitHub Actions. This is the reference version for the project. All other Go |
| 16 | +# versions are checked against this version. |
| 17 | +GO_VERSION = 1.24.6 |
| 18 | + |
| 19 | +# Linting uses a lot of memory, so keep it under control by limiting the number |
| 20 | +# of workers if requested. |
| 21 | +ifneq ($(workers),) |
| 22 | +LINT_WORKERS = --concurrency=$(workers) |
| 23 | +endif |
| 24 | + |
| 25 | +DOCKER_TOOLS = docker run \ |
| 26 | + --rm \ |
| 27 | + -v $(shell bash -c "mkdir -p /tmp/go-build-cache; echo /tmp/go-build-cache"):/root/.cache/go-build \ |
| 28 | + -v $$(pwd):/build lightning-onion-tools |
| 29 | + |
| 30 | +GREEN := "\\033[0;32m" |
| 31 | +NC := "\\033[0m" |
| 32 | +define print |
| 33 | + echo $(GREEN)$1$(NC) |
| 34 | +endef |
| 35 | + |
| 36 | +#? default: Run `make build` |
| 37 | +default: build |
| 38 | + |
| 39 | +#? all: Run `make build` and `make check` |
| 40 | +all: build check |
| 41 | + |
| 42 | +# ============ |
| 43 | +# INSTALLATION |
| 44 | +# ============ |
| 45 | + |
| 46 | +#? build: Compile and build lightning-onion |
| 47 | +build: |
| 48 | + @$(call print, "Compiling lightning-onion.") |
| 49 | + $(GOBUILD) $(PKG)/... |
| 50 | + |
| 51 | +#? sphinx-cli: Build the sphinx-cli binary |
| 52 | +sphinx-cli: |
| 53 | + @$(call print, "Building sphinx-cli.") |
| 54 | + $(GOBUILD) -o sphinx-cli ./cmd/main.go |
| 55 | + |
| 56 | +# ======= |
| 57 | +# TESTING |
| 58 | +# ======= |
| 59 | + |
| 60 | +#? check: Run `make unit` |
| 61 | +check: unit |
| 62 | + |
| 63 | +#? unit: Run unit tests |
| 64 | +unit: |
| 65 | + @$(call print, "Running unit tests.") |
| 66 | + go test -v ./... |
| 67 | + |
| 68 | +#? unit-cover: Run unit coverage tests |
| 69 | +unit-cover: |
| 70 | + @$(call print, "Running unit coverage tests.") |
| 71 | + go test -coverprofile=coverage.txt -covermode=atomic ./... |
| 72 | + |
| 73 | +#? unit-race: Run unit race tests |
| 74 | +unit-race: |
| 75 | + @$(call print, "Running unit race tests.") |
| 76 | + env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" go test -race ./... |
| 77 | + |
| 78 | +# ========= |
| 79 | +# UTILITIES |
| 80 | +# ========= |
| 81 | + |
| 82 | +#? fmt: Fix imports and format source code |
| 83 | +fmt: docker-tools |
| 84 | + @$(call print, "Fixing imports.") |
| 85 | + $(DOCKER_TOOLS) gosimports -w $(GOFILES) |
| 86 | + @$(call print, "Formatting source.") |
| 87 | + $(DOCKER_TOOLS) gofmt -l -w -s $(GOFILES) |
| 88 | + |
| 89 | +#? check-go-version-yaml: Verify that the Go version is correct in all YAML files |
| 90 | +check-go-version-yaml: |
| 91 | + @$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)") |
| 92 | + ./scripts/check-go-version-yaml.sh $(GO_VERSION) |
| 93 | + |
| 94 | +#? check-go-version-dockerfile: Verify that the Go version is correct in all Dockerfile files |
| 95 | +check-go-version-dockerfile: |
| 96 | + @$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)") |
| 97 | + ./scripts/check-go-version-dockerfile.sh $(GO_VERSION) |
| 98 | + |
| 99 | +#? check-go-version: Verify that the Go version is correct in all project files |
| 100 | +check-go-version: check-go-version-dockerfile check-go-version-yaml |
| 101 | + |
| 102 | +#? fmt-check: Make sure source code is formatted and imports are correct |
| 103 | +fmt-check: fmt |
| 104 | + @$(call print, "Checking fmt results.") |
| 105 | + if test -n "$$(git status --porcelain)"; then echo "code not formatted correctly, please run `make fmt` again!"; git status; git diff; exit 1; fi |
| 106 | + |
| 107 | +#? lint-config-check: Verify golangci-lint configuration |
| 108 | +lint-config-check: docker-tools |
| 109 | + @$(call print, "Verifying golangci-lint configuration.") |
| 110 | + $(DOCKER_TOOLS) golangci-lint config verify -v |
| 111 | + |
| 112 | +#? lint: Lint source and check errors |
| 113 | +lint: check-go-version lint-config-check |
| 114 | + @$(call print, "Linting source.") |
| 115 | + $(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS) |
| 116 | + |
| 117 | +#? docker-tools: Build tools docker image |
| 118 | +docker-tools: |
| 119 | + @$(call print, "Building tools docker image.") |
| 120 | + docker build -q -t lightning-onion-tools -f $(TOOLS_DIR)/Dockerfile . |
| 121 | + |
| 122 | +#? clean: Clean source |
| 123 | +clean: |
| 124 | + @$(call print, "Cleaning source.$(NC)") |
| 125 | + $(RM) coverage.txt |
| 126 | + |
| 127 | +#? tidy-module: Run 'go mod tidy' for all modules |
| 128 | +tidy-module: |
| 129 | + @$(call print, "Running 'go mod tidy' for main module") |
| 130 | + go mod tidy |
| 131 | + @$(call print, "Running 'go mod tidy' for tools module") |
| 132 | + cd $(TOOLS_DIR) && go mod tidy |
| 133 | + |
| 134 | +#? tidy-module-check: Run 'go mod tidy' for all modules and check results |
| 135 | +tidy-module-check: tidy-module |
| 136 | + if test -n "$$(git status --porcelain)"; then echo "modules not updated, please run `make tidy-module` again!"; git status; exit 1; fi |
| 137 | + |
| 138 | +.PHONY: all \ |
| 139 | + default \ |
| 140 | + build \ |
| 141 | + sphinx-cli \ |
| 142 | + check \ |
| 143 | + unit \ |
| 144 | + unit-cover \ |
| 145 | + unit-race \ |
| 146 | + fmt \ |
| 147 | + fmt-check \ |
| 148 | + tidy-module \ |
| 149 | + tidy-module-check \ |
| 150 | + lint \ |
| 151 | + lint-config-check \ |
| 152 | + docker-tools \ |
| 153 | + clean |
| 154 | + |
| 155 | +#? help: Get more info on make commands |
| 156 | +help: Makefile |
| 157 | + @echo " Choose a command run in lightning-onion:" |
| 158 | + @sed -n 's/^#?//p' $< | column -t -s ':' | sort | sed -e 's/^/ /' |
| 159 | + |
| 160 | +.PHONY: help |
0 commit comments