Makefile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. PKG := github.com/lightningnetwork/lnd
  2. ESCPKG := github.com\/lightningnetwork\/lnd
  3. MOBILE_PKG := $(PKG)/mobile
  4. TOOLS_DIR := tools
  5. PREFIX ?= /usr/local
  6. BTCD_PKG := github.com/btcsuite/btcd
  7. GOACC_PKG := github.com/ory/go-acc
  8. GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports
  9. GO_BIN := ${GOPATH}/bin
  10. BTCD_BIN := $(GO_BIN)/btcd
  11. GOIMPORTS_BIN := $(GO_BIN)/gosimports
  12. GOMOBILE_BIN := $(GO_BIN)/gomobile
  13. GOACC_BIN := $(GO_BIN)/go-acc
  14. MOBILE_BUILD_DIR :=${GOPATH}/src/$(MOBILE_PKG)/build
  15. IOS_BUILD_DIR := $(MOBILE_BUILD_DIR)/ios
  16. IOS_BUILD := $(IOS_BUILD_DIR)/Lndmobile.xcframework
  17. ANDROID_BUILD_DIR := $(MOBILE_BUILD_DIR)/android
  18. ANDROID_BUILD := $(ANDROID_BUILD_DIR)/Lndmobile.aar
  19. COMMIT := $(shell git describe --tags --dirty)
  20. GO_VERSION := $(shell go version | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p')
  21. GO_VERSION_MINOR := $(shell echo $(GO_VERSION) | cut -d. -f2)
  22. LOOPVARFIX :=
  23. ifeq ($(shell expr $(GO_VERSION_MINOR) \>= 21), 1)
  24. LOOPVARFIX := GOEXPERIMENT=loopvar
  25. endif
  26. GOBUILD := $(LOOPVARFIX) go build -v
  27. GOINSTALL := $(LOOPVARFIX) go install -v
  28. GOTEST := $(LOOPVARFIX) go test
  29. GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -name "*pb.go" -not -name "*pb.gw.go" -not -name "*.pb.json.go")
  30. RM := rm -f
  31. CP := cp
  32. MAKE := make
  33. XARGS := xargs -L 1
  34. include make/testing_flags.mk
  35. include make/release_flags.mk
  36. include make/fuzz_flags.mk
  37. DEV_TAGS := $(if ${tags},$(DEV_TAGS) ${tags},$(DEV_TAGS))
  38. # We only return the part inside the double quote here to avoid escape issues
  39. # when calling the external release script. The second parameter can be used to
  40. # add additional ldflags if needed (currently only used for the release).
  41. make_ldflags = $(1) -X $(PKG)/build.Commit=$(COMMIT)
  42. DEV_GCFLAGS := -gcflags "all=-N -l"
  43. DEV_LDFLAGS := -ldflags "$(call make_ldflags)"
  44. # For the release, we want to remove the symbol table and debug information (-s)
  45. # and omit the DWARF symbol table (-w). Also we clear the build ID.
  46. RELEASE_LDFLAGS := $(call make_ldflags, -s -w -buildid=)
  47. # Linting uses a lot of memory, so keep it under control by limiting the number
  48. # of workers if requested.
  49. ifneq ($(workers),)
  50. LINT_WORKERS = --concurrency=$(workers)
  51. endif
  52. DOCKER_TOOLS = docker run \
  53. --rm \
  54. -v $(shell bash -c "go env GOCACHE || (mkdir -p /tmp/go-cache; echo /tmp/go-cache)"):/tmp/build/.cache \
  55. -v $(shell bash -c "go env GOMODCACHE || (mkdir -p /tmp/go-modcache; echo /tmp/go-modcache)"):/tmp/build/.modcache \
  56. -v $(shell bash -c "mkdir -p /tmp/go-lint-cache; echo /tmp/go-lint-cache"):/root/.cache/golangci-lint \
  57. -v $$(pwd):/build lnd-tools
  58. GREEN := "\\033[0;32m"
  59. NC := "\\033[0m"
  60. define print
  61. echo $(GREEN)$1$(NC)
  62. endef
  63. default: scratch
  64. all: scratch check install
  65. # ============
  66. # DEPENDENCIES
  67. # ============
  68. $(GOACC_BIN):
  69. @$(call print, "Installing go-acc.")
  70. cd $(TOOLS_DIR); go install -trimpath -tags=tools $(GOACC_PKG)
  71. $(BTCD_BIN):
  72. @$(call print, "Installing btcd.")
  73. cd $(TOOLS_DIR); go install -trimpath $(BTCD_PKG)
  74. $(GOIMPORTS_BIN):
  75. @$(call print, "Installing goimports.")
  76. cd $(TOOLS_DIR); go install -trimpath $(GOIMPORTS_PKG)
  77. # ============
  78. # INSTALLATION
  79. # ============
  80. #? build: Build lnd and lncli binaries, place them in project directory
  81. build:
  82. @$(call print, "Building debug lnd and lncli.")
  83. $(GOBUILD) -tags="$(DEV_TAGS)" -o lnd-debug $(DEV_GCFLAGS) $(DEV_LDFLAGS) $(PKG)/cmd/lnd
  84. $(GOBUILD) -tags="$(DEV_TAGS)" -o lncli-debug $(DEV_GCFLAGS) $(DEV_LDFLAGS) $(PKG)/cmd/lncli
  85. #? build-itest: Build integration test binaries, place them in itest directory
  86. build-itest:
  87. @$(call print, "Building itest btcd and lnd.")
  88. CGO_ENABLED=0 $(GOBUILD) -tags="integration" -o itest/btcd-itest$(EXEC_SUFFIX) $(DEV_LDFLAGS) $(BTCD_PKG)
  89. CGO_ENABLED=0 $(GOBUILD) -tags="$(ITEST_TAGS)" $(ITEST_COVERAGE) -o itest/lnd-itest$(EXEC_SUFFIX) $(DEV_LDFLAGS) $(PKG)/cmd/lnd
  90. @$(call print, "Building itest binary for ${backend} backend.")
  91. CGO_ENABLED=0 $(GOTEST) -v ./itest -tags="$(DEV_TAGS) $(RPC_TAGS) integration $(backend)" -c -o itest/itest.test$(EXEC_SUFFIX)
  92. #? build-itest-race: Build integration test binaries in race detector mode, place them in itest directory
  93. build-itest-race:
  94. @$(call print, "Building itest btcd and lnd with race detector.")
  95. CGO_ENABLED=0 $(GOBUILD) -tags="integration" -o itest/btcd-itest$(EXEC_SUFFIX) $(DEV_LDFLAGS) $(BTCD_PKG)
  96. CGO_ENABLED=1 $(GOBUILD) -race -tags="$(ITEST_TAGS)" -o itest/lnd-itest$(EXEC_SUFFIX) $(DEV_LDFLAGS) $(PKG)/cmd/lnd
  97. @$(call print, "Building itest binary for ${backend} backend.")
  98. CGO_ENABLED=0 $(GOTEST) -v ./itest -tags="$(DEV_TAGS) $(RPC_TAGS) integration $(backend)" -c -o itest/itest.test$(EXEC_SUFFIX)
  99. #? install-binaries: Build and install lnd and lncli binaries, place them in $GOPATH/bin
  100. install-binaries:
  101. @$(call print, "Installing lnd and lncli.")
  102. $(GOINSTALL) -tags="${tags}" -ldflags="$(RELEASE_LDFLAGS)" $(PKG)/cmd/lnd
  103. $(GOINSTALL) -tags="${tags}" -ldflags="$(RELEASE_LDFLAGS)" $(PKG)/cmd/lncli
  104. #? manpages: generate and install man pages
  105. manpages:
  106. @$(call print, "Generating man pages lncli.1 and lnd.1.")
  107. ./scripts/gen_man_pages.sh $(DESTDIR) $(PREFIX)
  108. #? install: Build and install lnd and lncli binaries and place them in $GOPATH/bin.
  109. install: install-binaries
  110. #? install-all: Performs all the same tasks as the install command along with generating and
  111. # installing the man pages for the lnd and lncli binaries. This command is useful in an
  112. # environment where a user has root access and so has write access to the man page directory.
  113. install-all: install manpages
  114. #? release-install: Build and install lnd and lncli release binaries, place them in $GOPATH/bin
  115. release-install:
  116. @$(call print, "Installing release lnd and lncli.")
  117. env CGO_ENABLED=0 $(GOINSTALL) -v -trimpath -ldflags="$(RELEASE_LDFLAGS)" -tags="$(RELEASE_TAGS)" $(PKG)/cmd/lnd
  118. env CGO_ENABLED=0 $(GOINSTALL) -v -trimpath -ldflags="$(RELEASE_LDFLAGS)" -tags="$(RELEASE_TAGS)" $(PKG)/cmd/lncli
  119. #? release: Build the full set of reproducible release binaries for all supported platforms
  120. # Make sure the generated mobile RPC stubs don't influence our vendor package
  121. # by removing them first in the clean-mobile target.
  122. release: clean-mobile
  123. @$(call print, "Releasing lnd and lncli binaries.")
  124. $(VERSION_CHECK)
  125. ./scripts/release.sh build-release "$(VERSION_TAG)" "$(BUILD_SYSTEM)" "$(RELEASE_TAGS)" "$(RELEASE_LDFLAGS)"
  126. #? docker-release: Same as release but within a docker container to support reproducible builds on BSD/MacOS platforms
  127. docker-release:
  128. @$(call print, "Building release helper docker image.")
  129. if [ "$(tag)" = "" ]; then echo "Must specify tag=<commit_or_tag>!"; exit 1; fi
  130. docker build -t lnd-release-helper -f make/builder.Dockerfile make/
  131. # Run the actual compilation inside the docker image. We pass in all flags
  132. # that we might want to overwrite in manual tests.
  133. $(DOCKER_RELEASE_HELPER) make release tag="$(tag)" sys="$(sys)" COMMIT="$(COMMIT)"
  134. docker-tools:
  135. @$(call print, "Building tools docker image.")
  136. docker build -q -t lnd-tools $(TOOLS_DIR)
  137. scratch: build
  138. # =======
  139. # TESTING
  140. # =======
  141. #? check: Run unit and integration tests
  142. check: unit itest
  143. db-instance:
  144. ifeq ($(dbbackend),postgres)
  145. # Remove a previous postgres instance if it exists.
  146. docker rm lnd-postgres --force || echo "Starting new postgres container"
  147. # Start a fresh postgres instance. Allow a maximum of 500 connections so
  148. # that multiple lnd instances with a maximum number of connections of 50
  149. # each can run concurrently.
  150. docker run --name lnd-postgres -e POSTGRES_PASSWORD=postgres -p 6432:5432 -d postgres:13-alpine -N 500
  151. docker logs -f lnd-postgres &
  152. # Wait for the instance to be started.
  153. sleep $(POSTGRES_START_DELAY)
  154. endif
  155. #? itest-only: Only run integration tests without re-building binaries
  156. itest-only: db-instance
  157. @$(call print, "Running integration tests with ${backend} backend.")
  158. rm -rf itest/*.log itest/.logs-*; date
  159. EXEC_SUFFIX=$(EXEC_SUFFIX) scripts/itest_part.sh 0 1 $(TEST_FLAGS) $(ITEST_FLAGS)
  160. $(COLLECT_ITEST_COVERAGE)
  161. #? itest: Build and run integration tests
  162. itest: build-itest itest-only
  163. #? itest-race: Build and run integration tests in race detector mode
  164. itest-race: build-itest-race itest-only
  165. #? itest-parallel: Build and run integration tests in parallel mode, running up to ITEST_PARALLELISM test tranches in parallel (default 4)
  166. itest-parallel: build-itest db-instance
  167. @$(call print, "Running tests")
  168. rm -rf itest/*.log itest/.logs-*; date
  169. EXEC_SUFFIX=$(EXEC_SUFFIX) scripts/itest_parallel.sh $(ITEST_PARALLELISM) $(NUM_ITEST_TRANCHES) $(TEST_FLAGS) $(ITEST_FLAGS)
  170. $(COLLECT_ITEST_COVERAGE)
  171. #? itest-clean: Kill all running itest processes
  172. itest-clean:
  173. @$(call print, "Cleaning old itest processes")
  174. killall lnd-itest || echo "no running lnd-itest process found";
  175. #? unit: Run unit tests
  176. unit: $(BTCD_BIN)
  177. @$(call print, "Running unit tests.")
  178. $(UNIT)
  179. #? unit-module: Run unit tests of all submodules
  180. unit-module:
  181. @$(call print, "Running submodule unit tests.")
  182. scripts/unit_test_modules.sh
  183. #? unit-debug: Run unit tests with debug log output enabled
  184. unit-debug: $(BTCD_BIN)
  185. @$(call print, "Running debug unit tests.")
  186. $(UNIT_DEBUG)
  187. #? unit-cover: Run unit tests in coverage mode
  188. unit-cover: $(GOACC_BIN)
  189. @$(call print, "Running unit coverage tests.")
  190. $(GOACC)
  191. #? unit-race: Run unit tests in race detector mode
  192. unit-race:
  193. @$(call print, "Running unit race tests.")
  194. env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" $(UNIT_RACE)
  195. #? unit-bench: Run benchmark tests
  196. unit-bench: $(BTCD_BIN)
  197. @$(call print, "Running benchmark tests.")
  198. $(UNIT_BENCH)
  199. # =============
  200. # FLAKE HUNTING
  201. # =============
  202. #? flakehunter: Run the integration tests continuously until one fails
  203. flakehunter: build-itest
  204. @$(call print, "Flake hunting ${backend} integration tests.")
  205. while [ $$? -eq 0 ]; do make itest-only icase='${icase}' backend='${backend}'; done
  206. #? flake-unit: Run the unit tests continuously until one fails
  207. flake-unit:
  208. @$(call print, "Flake hunting unit tests.")
  209. while [ $$? -eq 0 ]; do GOTRACEBACK=all $(UNIT) -count=1; done
  210. #? flakehunter-parallel: Run the integration tests continuously until one fails, running up to ITEST_PARALLELISM test tranches in parallel (default 4)
  211. flakehunter-parallel:
  212. @$(call print, "Flake hunting ${backend} integration tests in parallel.")
  213. while [ $$? -eq 0 ]; do make itest-parallel tranches=1 parallel=${ITEST_PARALLELISM} icase='${icase}' backend='${backend}'; done
  214. # =============
  215. # FUZZING
  216. # =============
  217. #? fuzz: Run the fuzzing tests
  218. fuzz:
  219. @$(call print, "Fuzzing packages '$(FUZZPKG)'.")
  220. scripts/fuzz.sh run "$(FUZZPKG)" "$(FUZZ_TEST_RUN_TIME)" "$(FUZZ_NUM_PROCESSES)"
  221. # =========
  222. # UTILITIES
  223. # =========
  224. #? fmt: Format source code and fix imports
  225. fmt: $(GOIMPORTS_BIN)
  226. @$(call print, "Fixing imports.")
  227. gosimports -w $(GOFILES_NOVENDOR)
  228. @$(call print, "Formatting source.")
  229. gofmt -l -w -s $(GOFILES_NOVENDOR)
  230. #? fmt-check: Make sure source code is formatted and imports are correct
  231. fmt-check: fmt
  232. @$(call print, "Checking fmt results.")
  233. if test -n "$$(git status --porcelain)"; then echo "code not formatted correctly, please run `make fmt` again!"; git status; git diff; exit 1; fi
  234. #? lint: Run static code analysis
  235. lint: docker-tools
  236. @$(call print, "Linting source.")
  237. $(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)
  238. #? protolint: Lint proto files using protolint
  239. protolint:
  240. @$(call print, "Linting proto files.")
  241. docker run --rm --volume "$$(pwd):/workspace" --workdir /workspace yoheimuta/protolint lint lnrpc/
  242. #? tidy-module: Run `go mod` tidy for all modules
  243. tidy-module:
  244. echo "Running 'go mod tidy' for all modules"
  245. scripts/tidy_modules.sh
  246. #? tidy-module-check: Make sure all modules are up to date
  247. tidy-module-check: tidy-module
  248. if test -n "$$(git status --porcelain)"; then echo "modules not updated, please run `make tidy-module` again!"; git status; exit 1; fi
  249. #? list: List all available make targets
  250. list:
  251. @$(call print, "Listing commands:")
  252. @$(MAKE) -qp | \
  253. awk -F':' '/^[a-zA-Z0-9][^$$#\/\t=]*:([^=]|$$)/ {split($$1,A,/ /);for(i in A)print A[i]}' | \
  254. grep -v Makefile | \
  255. sort
  256. #? help: List all available make targets with their descriptions
  257. help: Makefile
  258. @$(call print, "Listing commands:")
  259. @sed -n 's/^#?//p' $< | column -t -s ':' | sort | sed -e 's/^/ /'
  260. #? sqlc: Generate sql models and queries in Go
  261. sqlc:
  262. @$(call print, "Generating sql models and queries in Go")
  263. ./scripts/gen_sqlc_docker.sh
  264. #? sqlc-check: Make sure sql models and queries are up to date
  265. sqlc-check: sqlc
  266. @$(call print, "Verifying sql code generation.")
  267. if test -n "$$(git status --porcelain '*.go')"; then echo "SQL models not properly generated!"; git status --porcelain '*.go'; exit 1; fi
  268. #? rpc: Compile protobuf definitions and generate REST proxy stubs
  269. rpc:
  270. @$(call print, "Compiling protos.")
  271. cd ./lnrpc; ./gen_protos_docker.sh
  272. #? rpc-format: Format protobuf definition files
  273. rpc-format:
  274. @$(call print, "Formatting protos.")
  275. cd ./lnrpc; find . -name "*.proto" | xargs clang-format --style=file -i
  276. #? rpc-check: Make sure protobuf definitions are up to date
  277. rpc-check: rpc
  278. @$(call print, "Verifying protos.")
  279. cd ./lnrpc; ../scripts/check-rest-annotations.sh
  280. if test -n "$$(git status --porcelain)"; then echo "Protos not properly formatted or not compiled with v3.4.0"; git status; git diff; exit 1; fi
  281. #? rpc-js-compile: Compile protobuf definitions and generate JSON/WASM stubs
  282. rpc-js-compile:
  283. @$(call print, "Compiling JSON/WASM stubs.")
  284. GOOS=js GOARCH=wasm $(GOBUILD) -tags="$(WASM_RELEASE_TAGS)" $(PKG)/lnrpc/...
  285. #? sample-conf-check: Make sure default values in the sample-lnd.conf file are set correctly
  286. sample-conf-check:
  287. @$(call print, "Checking that default values in the sample-lnd.conf file are set correctly")
  288. scripts/check-sample-lnd-conf.sh "$(RELEASE_TAGS)"
  289. #? mobile-rpc: Compile mobile RPC stubs from the protobuf definitions
  290. mobile-rpc:
  291. @$(call print, "Creating mobile RPC from protos.")
  292. cd ./lnrpc; COMPILE_MOBILE=1 SUBSERVER_PREFIX=1 ./gen_protos_docker.sh
  293. #? vendor: Create a vendor directory with all dependencies
  294. vendor:
  295. @$(call print, "Re-creating vendor directory.")
  296. rm -r vendor/; go mod vendor
  297. #? apple: Build mobile RPC stubs and project template for iOS and macOS
  298. apple: mobile-rpc
  299. @$(call print, "Building iOS and macOS cxframework ($(IOS_BUILD)).")
  300. mkdir -p $(IOS_BUILD_DIR)
  301. $(GOMOBILE_BIN) bind -target=ios,iossimulator,macos -tags="mobile $(DEV_TAGS) $(RPC_TAGS)" -ldflags "$(RELEASE_LDFLAGS)" -v -o $(IOS_BUILD) $(MOBILE_PKG)
  302. #? ios: Build mobile RPC stubs and project template for iOS
  303. ios: mobile-rpc
  304. @$(call print, "Building iOS cxframework ($(IOS_BUILD)).")
  305. mkdir -p $(IOS_BUILD_DIR)
  306. $(GOMOBILE_BIN) bind -target=ios,iossimulator -tags="mobile $(DEV_TAGS) $(RPC_TAGS)" -ldflags "$(RELEASE_LDFLAGS)" -v -o $(IOS_BUILD) $(MOBILE_PKG)
  307. #? macos: Build mobile RPC stubs and project template for macOS
  308. macos: mobile-rpc
  309. @$(call print, "Building macOS cxframework ($(IOS_BUILD)).")
  310. mkdir -p $(IOS_BUILD_DIR)
  311. $(GOMOBILE_BIN) bind -target=macos -tags="mobile $(DEV_TAGS) $(RPC_TAGS)" -ldflags "$(RELEASE_LDFLAGS)" -v -o $(IOS_BUILD) $(MOBILE_PKG)
  312. #? android: Build mobile RPC stubs and project template for Android
  313. android: mobile-rpc
  314. @$(call print, "Building Android library ($(ANDROID_BUILD)).")
  315. mkdir -p $(ANDROID_BUILD_DIR)
  316. $(GOMOBILE_BIN) bind -target=android -androidapi 21 -tags="mobile $(DEV_TAGS) $(RPC_TAGS)" -ldflags "$(RELEASE_LDFLAGS)" -v -o $(ANDROID_BUILD) $(MOBILE_PKG)
  317. #? mobile: Build mobile RPC stubs and project templates for iOS and Android
  318. mobile: ios android
  319. #? clean: Remove all generated files
  320. clean:
  321. @$(call print, "Cleaning source.$(NC)")
  322. $(RM) ./lnd-debug ./lncli-debug
  323. $(RM) ./lnd-itest ./lncli-itest
  324. $(RM) -r ./vendor .vendor-new
  325. #? clean-mobile: Remove all generated mobile files
  326. clean-mobile:
  327. @$(call print, "Cleaning autogenerated mobile RPC stubs.")
  328. $(RM) -r mobile/build
  329. $(RM) mobile/*_generated.go
  330. .PHONY: all \
  331. btcd \
  332. default \
  333. build \
  334. install \
  335. scratch \
  336. check \
  337. help \
  338. itest-only \
  339. itest \
  340. unit \
  341. unit-debug \
  342. unit-cover \
  343. unit-race \
  344. flakehunter \
  345. flake-unit \
  346. fmt \
  347. lint \
  348. list \
  349. rpc \
  350. rpc-format \
  351. rpc-check \
  352. rpc-js-compile \
  353. mobile-rpc \
  354. vendor \
  355. ios \
  356. android \
  357. mobile \
  358. clean