refactor(Makefile): use built-in make commands #15708

Changes to the main Makefile:
- add `MAKEFILE_PATH` and `MAKEFILE_DIR` that are set with native commands
- add `NVIM_PRG`
- rename `BUILD_TYPE` to `CMAKE_GENERATOR` to align with CMake naming
- remove the misleading `BUILD_CMD` and use `BUILD_TOOL` instead

Add the following phony target to quickly test the changes

```make
debug-print:
	@echo makefile path: $(MAKEFILE_PATH)
	@echo makefile dir: $(MAKEFILE_DIR)
	@echo build dir: $(BUILD_DIR)
	@echo cmake generator tool: $(CMAKE_GENERATOR)
	@echo build-tool: $(BUILD_TOOL)
	@echo nvim-prg: $(NVIM_PRG)
```
This commit is contained in:
kylo252 2021-09-20 00:53:48 +02:00 committed by GitHub
parent e61ea7772e
commit 39c886551b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
THIS_DIR = $(shell pwd) MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))
filter-false = $(strip $(filter-out 0 off OFF false FALSE,$1)) filter-false = $(strip $(filter-out 0 off OFF false FALSE,$1))
filter-true = $(strip $(filter-out 1 on ON true TRUE,$1)) filter-true = $(strip $(filter-out 1 on ON true TRUE,$1))
@ -12,6 +14,7 @@ CMAKE_BUILD_TYPE ?= Debug
CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
# Extra CMake flags which extend the default set # Extra CMake flags which extend the default set
CMAKE_EXTRA_FLAGS ?= CMAKE_EXTRA_FLAGS ?=
NVIM_PRG := $(MAKEFILE_DIR)/build/bin/nvim
# CMAKE_INSTALL_PREFIX # CMAKE_INSTALL_PREFIX
# - May be passed directly or as part of CMAKE_EXTRA_FLAGS. # - May be passed directly or as part of CMAKE_EXTRA_FLAGS.
@ -35,7 +38,7 @@ else
checkprefix: ; checkprefix: ;
endif endif
BUILD_TYPE ?= $(shell (type ninja > /dev/null 2>&1 && echo "Ninja") || \ CMAKE_GENERATOR ?= $(shell (command -v ninja > /dev/null 2>&1 && echo "Ninja") || \
echo "Unix Makefiles") echo "Unix Makefiles")
DEPS_BUILD_DIR ?= .deps DEPS_BUILD_DIR ?= .deps
ifneq (1,$(words [$(DEPS_BUILD_DIR)])) ifneq (1,$(words [$(DEPS_BUILD_DIR)]))
@ -43,7 +46,7 @@ ifneq (1,$(words [$(DEPS_BUILD_DIR)]))
endif endif
ifeq (,$(BUILD_TOOL)) ifeq (,$(BUILD_TOOL))
ifeq (Ninja,$(BUILD_TYPE)) ifeq (Ninja,$(CMAKE_GENERATOR))
ifneq ($(shell $(CMAKE_PRG) --help 2>/dev/null | grep Ninja),) ifneq ($(shell $(CMAKE_PRG) --help 2>/dev/null | grep Ninja),)
BUILD_TOOL := ninja BUILD_TOOL := ninja
else else
@ -56,16 +59,15 @@ ifeq (,$(BUILD_TOOL))
endif endif
endif endif
BUILD_CMD = $(BUILD_TOOL)
# Only need to handle Ninja here. Make will inherit the VERBOSE variable, and the -j and -n flags. # Only need to handle Ninja here. Make will inherit the VERBOSE variable, and the -j and -n flags.
ifeq ($(BUILD_TYPE),Ninja) ifeq ($(CMAKE_GENERATOR),Ninja)
ifneq ($(VERBOSE),) ifneq ($(VERBOSE),)
BUILD_CMD += -v BUILD_TOOL += -v
endif endif
BUILD_CMD += $(shell printf '%s' '$(MAKEFLAGS)' | grep -o -- '-j[0-9]\+') BUILD_TOOL += $(shell printf '%s' '$(MAKEFLAGS)' | grep -o -- '-j[0-9]\+')
ifeq (n,$(findstring n,$(firstword -$(MAKEFLAGS)))) ifeq (n,$(findstring n,$(firstword -$(MAKEFLAGS))))
BUILD_CMD += -n BUILD_TOOL += -n
endif endif
endif endif
@ -87,22 +89,22 @@ endif
SINGLE_MAKE = export MAKEFLAGS= ; $(MAKE) SINGLE_MAKE = export MAKEFLAGS= ; $(MAKE)
nvim: build/.ran-cmake deps nvim: build/.ran-cmake deps
+$(BUILD_CMD) -C build +$(BUILD_TOOL) -C build
libnvim: build/.ran-cmake deps libnvim: build/.ran-cmake deps
+$(BUILD_CMD) -C build libnvim +$(BUILD_TOOL) -C build libnvim
cmake: cmake:
touch CMakeLists.txt touch CMakeLists.txt
$(MAKE) build/.ran-cmake $(MAKE) build/.ran-cmake
build/.ran-cmake: | deps build/.ran-cmake: | deps
cd build && $(CMAKE_PRG) -G '$(BUILD_TYPE)' $(CMAKE_FLAGS) $(CMAKE_EXTRA_FLAGS) $(THIS_DIR) cd build && $(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(CMAKE_FLAGS) $(CMAKE_EXTRA_FLAGS) $(MAKEFILE_DIR)
touch $@ touch $@
deps: | build/.ran-third-party-cmake deps: | build/.ran-third-party-cmake
ifeq ($(call filter-true,$(USE_BUNDLED)),) ifeq ($(call filter-true,$(USE_BUNDLED)),)
+$(BUILD_CMD) -C $(DEPS_BUILD_DIR) +$(BUILD_TOOL) -C $(DEPS_BUILD_DIR)
endif endif
ifeq ($(call filter-true,$(USE_BUNDLED)),) ifeq ($(call filter-true,$(USE_BUNDLED)),)
@ -110,8 +112,8 @@ $(DEPS_BUILD_DIR):
mkdir -p "$@" mkdir -p "$@"
build/.ran-third-party-cmake:: $(DEPS_BUILD_DIR) build/.ran-third-party-cmake:: $(DEPS_BUILD_DIR)
cd $(DEPS_BUILD_DIR) && \ cd $(DEPS_BUILD_DIR) && \
$(CMAKE_PRG) -G '$(BUILD_TYPE)' $(BUNDLED_CMAKE_FLAG) $(BUNDLED_LUA_CMAKE_FLAG) \ $(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(BUNDLED_CMAKE_FLAG) $(BUNDLED_LUA_CMAKE_FLAG) \
$(DEPS_CMAKE_FLAGS) $(THIS_DIR)/third-party $(DEPS_CMAKE_FLAGS) $(MAKEFILE_DIR)/third-party
endif endif
build/.ran-third-party-cmake:: build/.ran-third-party-cmake::
mkdir -p build mkdir -p build
@ -121,31 +123,31 @@ build/.ran-third-party-cmake::
oldtest: | nvim build/runtime/doc/tags oldtest: | nvim build/runtime/doc/tags
+$(SINGLE_MAKE) -C src/nvim/testdir clean +$(SINGLE_MAKE) -C src/nvim/testdir clean
ifeq ($(strip $(TEST_FILE)),) ifeq ($(strip $(TEST_FILE)),)
+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG="$(realpath build/bin/nvim)" $(MAKEOVERRIDES) +$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) $(MAKEOVERRIDES)
else else
@# Handle TEST_FILE=test_foo{,.res,.vim}. @# Handle TEST_FILE=test_foo{,.res,.vim}.
+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG="$(realpath build/bin/nvim)" SCRIPTS= $(MAKEOVERRIDES) $(patsubst %.vim,%,$(patsubst %.res,%,$(TEST_FILE))) +$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst %.vim,%,$(patsubst %.res,%,$(TEST_FILE)))
endif endif
# Build oldtest by specifying the relative .vim filename. # Build oldtest by specifying the relative .vim filename.
.PHONY: phony_force .PHONY: phony_force
src/nvim/testdir/%.vim: phony_force src/nvim/testdir/%.vim: phony_force
+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG="$(realpath build/bin/nvim)" SCRIPTS= $(MAKEOVERRIDES) $(patsubst src/nvim/testdir/%.vim,%,$@) +$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst src/nvim/testdir/%.vim,%,$@)
build/runtime/doc/tags helptags: | nvim build/runtime/doc/tags helptags: | nvim
+$(BUILD_CMD) -C build runtime/doc/tags +$(BUILD_TOOL) -C build runtime/doc/tags
# Builds help HTML _and_ checks for invalid help tags. # Builds help HTML _and_ checks for invalid help tags.
helphtml: | nvim build/runtime/doc/tags helphtml: | nvim build/runtime/doc/tags
+$(BUILD_CMD) -C build doc_html +$(BUILD_TOOL) -C build doc_html
functionaltest: | nvim functionaltest: | nvim
+$(BUILD_CMD) -C build functionaltest +$(BUILD_TOOL) -C build functionaltest
functionaltest-lua: | nvim functionaltest-lua: | nvim
+$(BUILD_CMD) -C build functionaltest-lua +$(BUILD_TOOL) -C build functionaltest-lua
lualint: | build/.ran-cmake deps lualint: | build/.ran-cmake deps
$(BUILD_CMD) -C build lualint $(BUILD_TOOL) -C build lualint
shlint: shlint:
@shellcheck --version | head -n 2 @shellcheck --version | head -n 2
@ -171,15 +173,15 @@ _opt_commitlint:
|| echo "SKIP: commitlint (build/bin/nvim not found)" || echo "SKIP: commitlint (build/bin/nvim not found)"
unittest: | nvim unittest: | nvim
+$(BUILD_CMD) -C build unittest +$(BUILD_TOOL) -C build unittest
benchmark: | nvim benchmark: | nvim
+$(BUILD_CMD) -C build benchmark +$(BUILD_TOOL) -C build benchmark
test: functionaltest unittest test: functionaltest unittest
clean: clean:
+test -d build && $(BUILD_CMD) -C build clean || true +test -d build && $(BUILD_TOOL) -C build clean || true
$(MAKE) -C src/nvim/testdir clean $(MAKE) -C src/nvim/testdir clean
$(MAKE) -C runtime/doc clean $(MAKE) -C runtime/doc clean
$(MAKE) -C runtime/indent clean $(MAKE) -C runtime/indent clean
@ -189,19 +191,19 @@ distclean:
$(MAKE) clean $(MAKE) clean
install: checkprefix nvim install: checkprefix nvim
+$(BUILD_CMD) -C build install +$(BUILD_TOOL) -C build install
clint: build/.ran-cmake clint: build/.ran-cmake
+$(BUILD_CMD) -C build clint +$(BUILD_TOOL) -C build clint
clint-full: build/.ran-cmake clint-full: build/.ran-cmake
+$(BUILD_CMD) -C build clint-full +$(BUILD_TOOL) -C build clint-full
check-single-includes: build/.ran-cmake check-single-includes: build/.ran-cmake
+$(BUILD_CMD) -C build check-single-includes +$(BUILD_TOOL) -C build check-single-includes
generated-sources: build/.ran-cmake generated-sources: build/.ran-cmake
+$(BUILD_CMD) -C build generated-sources +$(BUILD_TOOL) -C build generated-sources
appimage: appimage:
bash scripts/genappimage.sh bash scripts/genappimage.sh
@ -216,12 +218,12 @@ lint: check-single-includes clint lualint _opt_pylint _opt_shlint _opt_commitlin
# Generic pattern rules, allowing for `make build/bin/nvim` etc. # Generic pattern rules, allowing for `make build/bin/nvim` etc.
# Does not work with "Unix Makefiles". # Does not work with "Unix Makefiles".
ifeq ($(BUILD_TYPE),Ninja) ifeq ($(CMAKE_GENERATOR),Ninja)
build/%: phony_force build/%: phony_force
$(BUILD_CMD) -C build $(patsubst build/%,%,$@) $(BUILD_TOOL) -C build $(patsubst build/%,%,$@)
$(DEPS_BUILD_DIR)/%: phony_force $(DEPS_BUILD_DIR)/%: phony_force
$(BUILD_CMD) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@) $(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@)
endif endif
.PHONY: test lualint pylint shlint functionaltest unittest lint clint clean distclean nvim libnvim cmake deps install appimage checkprefix commitlint .PHONY: test lualint pylint shlint functionaltest unittest lint clint clean distclean nvim libnvim cmake deps install appimage checkprefix commitlint