1MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
2MAKEFILE_DIR  := $(dir $(MAKEFILE_PATH))
3
4filter-false = $(strip $(filter-out 0 off OFF false FALSE,$1))
5filter-true = $(strip $(filter-out 1 on ON true TRUE,$1))
6
7# See contrib/local.mk.example
8-include local.mk
9
10all: nvim
11
12CMAKE_PRG ?= $(shell (command -v cmake3 || echo cmake))
13CMAKE_BUILD_TYPE ?= Debug
14CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
15# Extra CMake flags which extend the default set
16CMAKE_EXTRA_FLAGS ?=
17NVIM_PRG := $(MAKEFILE_DIR)/build/bin/nvim
18
19# CMAKE_INSTALL_PREFIX
20#   - May be passed directly or as part of CMAKE_EXTRA_FLAGS.
21#   - `checkprefix` target checks that it matches the CMake-cached value. #9615
22ifneq (,$(CMAKE_INSTALL_PREFIX)$(CMAKE_EXTRA_FLAGS))
23CMAKE_INSTALL_PREFIX := $(shell echo $(CMAKE_EXTRA_FLAGS) | 2>/dev/null \
24    grep -o 'CMAKE_INSTALL_PREFIX=[^ ]\+' | cut -d '=' -f2)
25endif
26ifneq (,$(CMAKE_INSTALL_PREFIX))
27override CMAKE_EXTRA_FLAGS += -DCMAKE_INSTALL_PREFIX=$(CMAKE_INSTALL_PREFIX)
28
29checkprefix:
30	@if [ -f build/.ran-cmake ]; then \
31	  cached_prefix=$(shell $(CMAKE_PRG) -L -N build | 2>/dev/null grep 'CMAKE_INSTALL_PREFIX' | cut -d '=' -f2); \
32	  if ! [ "$(CMAKE_INSTALL_PREFIX)" = "$$cached_prefix" ]; then \
33	    printf "Re-running CMake: CMAKE_INSTALL_PREFIX '$(CMAKE_INSTALL_PREFIX)' does not match cached value '%s'.\n" "$$cached_prefix"; \
34	    $(RM) build/.ran-cmake; \
35	  fi \
36	fi
37else
38checkprefix: ;
39endif
40
41CMAKE_GENERATOR ?= $(shell (command -v ninja > /dev/null 2>&1 && echo "Ninja") || \
42    echo "Unix Makefiles")
43DEPS_BUILD_DIR ?= .deps
44ifneq (1,$(words [$(DEPS_BUILD_DIR)]))
45  $(error DEPS_BUILD_DIR must not contain whitespace)
46endif
47
48ifeq (,$(BUILD_TOOL))
49  ifeq (Ninja,$(CMAKE_GENERATOR))
50    ifneq ($(shell $(CMAKE_PRG) --help 2>/dev/null | grep Ninja),)
51      BUILD_TOOL = ninja
52    else
53      # User's version of CMake doesn't support Ninja
54      BUILD_TOOL = $(MAKE)
55      CMAKE_GENERATOR := Unix Makefiles
56    endif
57  else
58    BUILD_TOOL = $(MAKE)
59  endif
60endif
61
62
63# Only need to handle Ninja here.  Make will inherit the VERBOSE variable, and the -j, -l, and -n flags.
64ifeq ($(CMAKE_GENERATOR),Ninja)
65  ifneq ($(VERBOSE),)
66    BUILD_TOOL += -v
67  endif
68  BUILD_TOOL += $(shell printf '%s' '$(MAKEFLAGS)' | grep -o -- ' *-[jl][0-9]\+ *')
69  ifeq (n,$(findstring n,$(firstword -$(MAKEFLAGS))))
70    BUILD_TOOL += -n
71  endif
72endif
73
74DEPS_CMAKE_FLAGS ?=
75# Back-compat: USE_BUNDLED_DEPS was the old name.
76USE_BUNDLED ?= $(USE_BUNDLED_DEPS)
77
78ifneq (,$(USE_BUNDLED))
79  BUNDLED_CMAKE_FLAG := -DUSE_BUNDLED=$(USE_BUNDLED)
80endif
81
82ifneq (,$(findstring functionaltest-lua,$(MAKECMDGOALS)))
83  BUNDLED_LUA_CMAKE_FLAG := -DUSE_BUNDLED_LUA=ON
84  $(shell [ -x $(DEPS_BUILD_DIR)/usr/bin/lua ] || rm build/.ran-*)
85endif
86
87# For use where we want to make sure only a single job is run.  This does issue
88# a warning, but we need to keep SCRIPTS argument.
89SINGLE_MAKE = export MAKEFLAGS= ; $(MAKE)
90
91nvim: build/.ran-cmake deps
92	+$(BUILD_TOOL) -C build
93
94libnvim: build/.ran-cmake deps
95	+$(BUILD_TOOL) -C build libnvim
96
97cmake:
98	touch CMakeLists.txt
99	$(MAKE) build/.ran-cmake
100
101build/.ran-cmake: | deps
102	cd build && $(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(CMAKE_FLAGS) $(CMAKE_EXTRA_FLAGS) $(MAKEFILE_DIR)
103	touch $@
104
105deps: | build/.ran-third-party-cmake
106ifeq ($(call filter-true,$(USE_BUNDLED)),)
107	+$(BUILD_TOOL) -C $(DEPS_BUILD_DIR)
108endif
109
110ifeq ($(call filter-true,$(USE_BUNDLED)),)
111$(DEPS_BUILD_DIR):
112	mkdir -p "$@"
113build/.ran-third-party-cmake:: $(DEPS_BUILD_DIR)
114	cd $(DEPS_BUILD_DIR) && \
115		$(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(BUNDLED_CMAKE_FLAG) $(BUNDLED_LUA_CMAKE_FLAG) \
116		$(DEPS_CMAKE_FLAGS) $(MAKEFILE_DIR)/third-party
117endif
118build/.ran-third-party-cmake::
119	mkdir -p build
120	touch $@
121
122# TODO: cmake 3.2+ add_custom_target() has a USES_TERMINAL flag.
123oldtest: | nvim build/runtime/doc/tags
124	+$(SINGLE_MAKE) -C src/nvim/testdir clean
125ifeq ($(strip $(TEST_FILE)),)
126	+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) $(MAKEOVERRIDES)
127else
128	@# Handle TEST_FILE=test_foo{,.res,.vim}.
129	+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst %.vim,%,$(patsubst %.res,%,$(TEST_FILE)))
130endif
131# Build oldtest by specifying the relative .vim filename.
132.PHONY: phony_force
133src/nvim/testdir/%.vim: phony_force
134	+$(SINGLE_MAKE) -C src/nvim/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst src/nvim/testdir/%.vim,%,$@)
135
136build/runtime/doc/tags helptags: | nvim
137	+$(BUILD_TOOL) -C build runtime/doc/tags
138
139# Builds help HTML _and_ checks for invalid help tags.
140helphtml: | nvim build/runtime/doc/tags
141	+$(BUILD_TOOL) -C build doc_html
142
143functionaltest: | nvim
144	+$(BUILD_TOOL) -C build functionaltest
145
146functionaltest-lua: | nvim
147	+$(BUILD_TOOL) -C build functionaltest-lua
148
149lualint: | build/.ran-cmake deps
150	$(BUILD_TOOL) -C build lualint
151
152shlint:
153	@shellcheck --version | head -n 2
154	shellcheck scripts/vim-patch.sh
155
156_opt_shlint:
157	@command -v shellcheck && { $(MAKE) shlint; exit $$?; } \
158		|| echo "SKIP: shlint (shellcheck not found)"
159
160pylint:
161	flake8 contrib/ scripts/ src/ test/
162
163# Run pylint only if flake8 is installed.
164_opt_pylint:
165	@command -v flake8 && { $(MAKE) pylint; exit $$?; } \
166		|| echo "SKIP: pylint (flake8 not found)"
167
168commitlint:
169	$(NVIM_PRG) --clean -es +"lua require('scripts.lintcommit').main({trace=false})"
170
171_opt_commitlint:
172	@test -x build/bin/nvim && { $(MAKE) commitlint; exit $$?; } \
173		|| echo "SKIP: commitlint (build/bin/nvim not found)"
174
175unittest: | nvim
176	+$(BUILD_TOOL) -C build unittest
177
178benchmark: | nvim
179	+$(BUILD_TOOL) -C build benchmark
180
181test: functionaltest unittest
182
183clean:
184	+test -d build && $(BUILD_TOOL) -C build clean || true
185	$(MAKE) -C src/nvim/testdir clean
186	$(MAKE) -C runtime/doc clean
187	$(MAKE) -C runtime/indent clean
188
189distclean:
190	rm -rf $(DEPS_BUILD_DIR) build
191	$(MAKE) clean
192
193install: checkprefix nvim
194	+$(BUILD_TOOL) -C build install
195
196clint: build/.ran-cmake
197	+$(BUILD_TOOL) -C build clint
198
199clint-full: build/.ran-cmake
200	+$(BUILD_TOOL) -C build clint-full
201
202check-single-includes: build/.ran-cmake
203	+$(BUILD_TOOL) -C build check-single-includes
204
205generated-sources: build/.ran-cmake
206	+$(BUILD_TOOL) -C build generated-sources
207
208appimage:
209	bash scripts/genappimage.sh
210
211# Build an appimage with embedded update information.
212#   appimage-nightly: for nightly builds
213#   appimage-latest: for a release
214appimage-%:
215	bash scripts/genappimage.sh $*
216
217lint: check-single-includes clint lualint _opt_pylint _opt_shlint _opt_commitlint
218
219# Generic pattern rules, allowing for `make build/bin/nvim` etc.
220# Does not work with "Unix Makefiles".
221ifeq ($(CMAKE_GENERATOR),Ninja)
222build/%: phony_force
223	$(BUILD_TOOL) -C build $(patsubst build/%,%,$@)
224
225$(DEPS_BUILD_DIR)/%: phony_force
226	$(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@)
227endif
228
229.PHONY: test lualint pylint shlint functionaltest unittest lint clint clean distclean nvim libnvim cmake deps install appimage checkprefix commitlint
230