Makefile 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. BUILD_TOOL = ninja
  43. else
  44. BUILD_TOOL = $(MAKE)
  45. endif
  46. endif
  47. # Only need to handle Ninja here. Make will inherit the VERBOSE variable, and the -j, -l, and -n flags.
  48. ifeq ($(CMAKE_GENERATOR),Ninja)
  49. ifneq ($(VERBOSE),)
  50. BUILD_TOOL += -v
  51. endif
  52. BUILD_TOOL += $(shell printf '%s' '$(MAKEFLAGS)' | grep -o -- ' *-[jl][0-9]\+ *')
  53. ifeq (n,$(findstring n,$(firstword -$(MAKEFLAGS))))
  54. BUILD_TOOL += -n
  55. endif
  56. endif
  57. DEPS_CMAKE_FLAGS ?=
  58. USE_BUNDLED ?=
  59. ifneq (,$(USE_BUNDLED))
  60. BUNDLED_CMAKE_FLAG := -DUSE_BUNDLED=$(USE_BUNDLED)
  61. endif
  62. ifneq (,$(findstring functionaltest-lua,$(MAKECMDGOALS)))
  63. BUNDLED_LUA_CMAKE_FLAG := -DUSE_BUNDLED_LUA=ON
  64. $(shell [ -x $(DEPS_BUILD_DIR)/usr/bin/lua ] || rm build/.ran-*)
  65. endif
  66. # For use where we want to make sure only a single job is run. This does issue
  67. # a warning, but we need to keep SCRIPTS argument.
  68. SINGLE_MAKE = export MAKEFLAGS= ; $(MAKE)
  69. nvim: build/.ran-cmake deps
  70. +$(BUILD_TOOL) -C build
  71. libnvim: build/.ran-cmake deps
  72. +$(BUILD_TOOL) -C build libnvim
  73. cmake:
  74. touch CMakeLists.txt
  75. $(MAKE) build/.ran-cmake
  76. build/.ran-cmake: | deps
  77. cd build && $(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(CMAKE_FLAGS) $(CMAKE_EXTRA_FLAGS) $(MAKEFILE_DIR)
  78. touch $@
  79. deps: | build/.ran-deps-cmake
  80. ifeq ($(call filter-true,$(USE_BUNDLED)),)
  81. +$(BUILD_TOOL) -C $(DEPS_BUILD_DIR)
  82. endif
  83. ifeq ($(call filter-true,$(USE_BUNDLED)),)
  84. $(DEPS_BUILD_DIR):
  85. mkdir -p "$@"
  86. build/.ran-deps-cmake:: $(DEPS_BUILD_DIR)
  87. cd $(DEPS_BUILD_DIR) && \
  88. $(CMAKE_PRG) -G '$(CMAKE_GENERATOR)' $(BUNDLED_CMAKE_FLAG) $(BUNDLED_LUA_CMAKE_FLAG) \
  89. $(DEPS_CMAKE_FLAGS) $(MAKEFILE_DIR)/cmake.deps
  90. endif
  91. build/.ran-deps-cmake::
  92. mkdir -p build
  93. touch $@
  94. # TODO: cmake 3.2+ add_custom_target() has a USES_TERMINAL flag.
  95. oldtest: | nvim build/runtime/doc/tags
  96. +$(SINGLE_MAKE) -C test/old/testdir clean
  97. ifeq ($(strip $(TEST_FILE)),)
  98. +$(SINGLE_MAKE) -C test/old/testdir NVIM_PRG=$(NVIM_PRG) $(MAKEOVERRIDES)
  99. else
  100. @# Handle TEST_FILE=test_foo{,.res,.vim}.
  101. +$(SINGLE_MAKE) -C test/old/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst %.vim,%,$(patsubst %.res,%,$(TEST_FILE)))
  102. endif
  103. # Build oldtest by specifying the relative .vim filename.
  104. .PHONY: phony_force
  105. test/old/testdir/%.vim: phony_force
  106. +$(SINGLE_MAKE) -C test/old/testdir NVIM_PRG=$(NVIM_PRG) SCRIPTS= $(MAKEOVERRIDES) $(patsubst test/old/testdir/%.vim,%,$@)
  107. functionaltest-lua: | nvim
  108. $(BUILD_TOOL) -C build $@
  109. FORMAT=formatc formatlua format
  110. LINT=lintlua lintsh lintc clang-tidy lintcommit lint
  111. TEST=functionaltest unittest
  112. generated-sources benchmark uninstall $(FORMAT) $(LINT) $(TEST): | build/.ran-cmake
  113. $(CMAKE_PRG) --build build --target $@
  114. test: $(TEST)
  115. iwyu: build/.ran-cmake
  116. cmake --preset iwyu
  117. cmake --build --preset iwyu > build/iwyu.log
  118. iwyu-fix-includes --only_re="src/nvim" --ignore_re="src/nvim/(auto|map.h|eval/encode.c)" --safe_headers < build/iwyu.log
  119. cmake -B build -U ENABLE_IWYU
  120. clean:
  121. +test -d build && $(BUILD_TOOL) -C build clean || true
  122. $(MAKE) -C test/old/testdir clean
  123. $(MAKE) -C runtime/indent clean
  124. distclean:
  125. rm -rf $(DEPS_BUILD_DIR) build
  126. $(MAKE) clean
  127. install: checkprefix nvim
  128. +$(BUILD_TOOL) -C build install
  129. appimage:
  130. bash scripts/genappimage.sh
  131. # Build an appimage with embedded update information.
  132. # appimage-nightly: for nightly builds
  133. # appimage-latest: for a release
  134. appimage-%:
  135. bash scripts/genappimage.sh $*
  136. # Generic pattern rules, allowing for `make build/bin/nvim` etc.
  137. # Does not work with "Unix Makefiles".
  138. ifeq ($(CMAKE_GENERATOR),Ninja)
  139. build/%: phony_force
  140. $(BUILD_TOOL) -C build $(patsubst build/%,%,$@)
  141. $(DEPS_BUILD_DIR)/%: phony_force
  142. $(BUILD_TOOL) -C $(DEPS_BUILD_DIR) $(patsubst $(DEPS_BUILD_DIR)/%,%,$@)
  143. endif
  144. .PHONY: test clean distclean nvim libnvim cmake deps install appimage checkprefix benchmark uninstall $(FORMAT) $(LINT) $(TEST)