Makefile 6.8 KB

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