Makefile 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # imgproxy Makefile
  2. BINARY := ./imgproxy
  3. MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
  4. GOCMD := go
  5. GOBUILD := $(GOCMD) build
  6. GOCLEAN := $(GOCMD) clean
  7. GOTEST := $(GOCMD) test
  8. GOFMT := gofmt
  9. GOLINT := golangci-lint
  10. CLANG_FORMAT := clang-format
  11. GOTESTSUM := gotestsum
  12. SRCDIR := ./cli
  13. RCFILE := ./.imgproxyrc
  14. BREW_PREFIX :=
  15. DEVROOT_TMP_DIR ?= $(MAKEFILE_DIR).tmp/_dev-root
  16. BASE_IMAGE ?= ghcr.io/imgproxy/imgproxy-base:v4-dev
  17. # Common environment setup for CGO builds
  18. ifneq ($(shell which brew),)
  19. BREW_PREFIX := $(shell brew --prefix)
  20. endif
  21. # Export CGO environment variables
  22. export CGO_LDFLAGS_ALLOW := -s|-w
  23. # Library paths for Homebrew-installed libraries on macOS
  24. ifdef BREW_PREFIX
  25. export PKG_CONFIG_PATH := $(PKG_CONFIG_PATH):$(shell brew --prefix libffi)/lib/pkgconfig
  26. export PKG_CONFIG_PATH := $(PKG_CONFIG_PATH):$(shell brew --prefix libarchive)/lib/pkgconfig
  27. export PKG_CONFIG_PATH := $(PKG_CONFIG_PATH):$(shell brew --prefix cfitsio)/lib/pkgconfig
  28. export CGO_LDFLAGS := $(CGO_LDFLAGS) -Wl,-no_warn_duplicate_libraries
  29. endif
  30. # Get build arguments
  31. ifeq (build,$(firstword $(MAKECMDGOALS)))
  32. BUILD_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
  33. endif
  34. # Get run arguments
  35. ifeq (run,$(firstword $(MAKECMDGOALS)))
  36. RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
  37. endif
  38. ifeq (build-and-run,$(firstword $(MAKECMDGOALS)))
  39. RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
  40. endif
  41. # Wrapper action to run commands in Docker
  42. .PHONY: _run-in-docker
  43. _run-in-docker:
  44. ifdef IMGPROXY_IN_BASE_CONTAINER
  45. @$(MAKE) $(DOCKERCMD)
  46. else
  47. @mkdir -p ${DEVROOT_TMP_DIR}/.cache ${DEVROOT_TMP_DIR}/go/pkg/mod
  48. @docker run --init --rm -it \
  49. -v "$(MAKEFILE_DIR):/workspaces/imgproxy" \
  50. -v "${DEVROOT_TMP_DIR}/.cache:/root/.cache" \
  51. -v "${DEVROOT_TMP_DIR}/go/pkg/mod:/root/go/pkg/mod" \
  52. -w /workspaces/imgproxy \
  53. -e IMGPROXY_IN_BASE_CONTAINER=1 \
  54. $(BASE_IMAGE) \
  55. bash -c "make $(DOCKERCMD)"
  56. endif
  57. # Default target
  58. .PHONY: all
  59. all: build
  60. # Build the binary. If -o is not provided, it defaults to $(BINARY).
  61. #
  62. # Usage:
  63. # make build -- -o output_name
  64. .PHONY: build
  65. build:
  66. @$(GOBUILD) -v -o $(BINARY) $(BUILD_ARGS) $(SRCDIR)
  67. # Clean
  68. .PHONY: clean
  69. clean:
  70. echo $$PKG_CONFIG_PATH
  71. @$(GOCLEAN)
  72. rm -f $(BINARY)
  73. # Run imgproxy binary
  74. #
  75. # Usage:
  76. # make run -- arg1 arg2
  77. #
  78. # If .imgproxyrc exists, it will be sourced before running the binary.
  79. .PHONY: run
  80. run: SHELL := bash
  81. run:
  82. ifneq (,$(wildcard $(RCFILE)))
  83. @source $(RCFILE) && $(BINARY) $(RUN_ARGS)
  84. else
  85. @$(BINARY) $(RUN_ARGS)
  86. endif
  87. .PHONY: build-and-run
  88. build-and-run: build run
  89. # Run tests
  90. #
  91. # Usage:
  92. # make test -- -run FooTest
  93. .PHONY: test _test
  94. test: DOCKERCMD := _test
  95. test: _run-in-docker
  96. _test:
  97. ifneq ($(shell which $(GOTESTSUM)),)
  98. @$(GOTESTSUM) ./...
  99. else
  100. @$(GOTEST) -v ./...
  101. endif
  102. # Format code
  103. .PHONY: fmt
  104. fmt:
  105. @$(GOFMT) -s -w .
  106. # Lint code (requires golangci-lint installed)
  107. .PHONY: lint-go _lint-go
  108. lint-go: DOCKERCMD := _lint-go
  109. lint-go: _run-in-docker
  110. _lint-go:
  111. @$(GOLINT) run
  112. # Lint C code (requires clang-format installed)
  113. .PHONE: lint-clang _lint-clang
  114. ling-clang: DOCKERCMD := _lint-clang
  115. ling-clang: _run-in-docker
  116. _lint-clang:
  117. @find . -not -path "./.tmp/*" -not -path "./.git/*" \( -iname "*.h" -o -iname "*.c" -o -iname "*.cpp" \) | xargs $(CLANG_FORMAT) --dry-run --Werror
  118. # Run all linters
  119. .PHONY: lint
  120. lint: lint-go ling-clang
  121. # Upgrade direct Go dependencies
  122. .PHONY: upgrade
  123. upgrade:
  124. @$(GOCMD) mod tidy
  125. @$(GOCMD) get $$($(GOCMD) list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
  126. @$(GOCMD) mod tidy
  127. # Run lychee
  128. .PHONY: lychee _lychee
  129. lychee: DOCKERCMD := _lychee
  130. lychee: _run-in-docker
  131. _lychee:
  132. lychee docs README.md CHANGELOG.md \
  133. --exclude localhost \
  134. --exclude twitter.com \
  135. --exclude x.com \
  136. --exclude-path docs/index.html \
  137. --max-concurrency 50
  138. .PHONY: devcontainer
  139. devcontainer:
  140. devcontainer exec --workspace-folder $(MAKEFILE_DIR) --config .devcontainer/oss/devcontainer.json bash
  141. # Make any unknown target do nothing to avoid "up to date" messages
  142. .PHONY: FORCE
  143. %: FORCE
  144. @: