pkg-kconfig.mk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ################################################################################
  2. # Kconfig package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of
  5. # package .mk files for packages that use kconfig for configuration files.
  6. # It is based on the generic-package infrastructure, and inherits all of its
  7. # features.
  8. #
  9. # See the Buildroot documentation for details on the usage of this
  10. # infrastructure.
  11. #
  12. ################################################################################
  13. # Macro to update back the custom (def)config file
  14. # $(1): file to copy from
  15. define kconfig-package-update-config
  16. @$(if $($(PKG)_KCONFIG_FRAGMENT_FILES), \
  17. echo "Unable to perform $(@) when fragment files are set"; exit 1)
  18. @$(if $($(PKG)_KCONFIG_DEFCONFIG), \
  19. echo "Unable to perform $(@) when using a defconfig rule"; exit 1)
  20. $(Q)if [ -d $($(PKG)_KCONFIG_FILE) ]; then \
  21. echo "Unable to perform $(@) when $($(PKG)_KCONFIG_FILE) is a directory"; \
  22. exit 1; \
  23. fi
  24. $(Q)mkdir -p $(dir $($(PKG)_KCONFIG_FILE))
  25. cp -f $($(PKG)_DIR)/$(1) $($(PKG)_KCONFIG_FILE)
  26. $(Q)touch --reference $($(PKG)_DIR)/$($(PKG)_KCONFIG_STAMP_DOTCONFIG) $($(PKG)_KCONFIG_FILE)
  27. endef
  28. PKG_KCONFIG_COMMON_OPTS = \
  29. HOSTCC="$(HOSTCC_NOCCACHE)"
  30. # Macro to save the defconfig file
  31. # $(1): the name of the package in upper-case letters
  32. define kconfig-package-savedefconfig
  33. $($(1)_MAKE_ENV) $($(1)_MAKE) -C $($(1)_DIR) \
  34. $(PKG_KCONFIG_COMMON_OPTS) $($(1)_KCONFIG_OPTS) savedefconfig
  35. endef
  36. # The correct way to regenerate a .config file is to use 'make olddefconfig'.
  37. # For historical reasons, the target name is 'oldnoconfig' between Linux kernel
  38. # versions 2.6.36 and 3.6, and remains as an alias in later versions.
  39. # In older versions, and in some other projects that use kconfig, the target is
  40. # not supported at all, and we use 'yes "" | make oldconfig' as a fallback
  41. # only, as this can fail in complex cases.
  42. # $(1): the name of the package in upper-case letters
  43. define kconfig-package-regen-dot-config
  44. $(if $(filter olddefconfig,$($(1)_KCONFIG_RULES)),
  45. $(Q)$($(1)_KCONFIG_MAKE) olddefconfig,
  46. $(if $(filter oldnoconfig,$($(1)_KCONFIG_RULES)),
  47. $(Q)$($(1)_KCONFIG_MAKE) oldnoconfig,
  48. $(Q)(yes "" | $($(1)_KCONFIG_MAKE) oldconfig)))
  49. endef
  50. # Macro to create a .config file where all given fragments are merged into.
  51. # $(1): the name of the package in upper-case letters
  52. # $(2): name of the .config file
  53. # $(3): fragment files to merge
  54. define kconfig-package-merge-config
  55. $(Q)$(if $($(1)_KCONFIG_DEFCONFIG),\
  56. $($(1)_KCONFIG_MAKE) $($(1)_KCONFIG_DEFCONFIG),\
  57. $(INSTALL) -m 0644 -D $($(1)_KCONFIG_FILE) $(2))
  58. $(Q)support/kconfig/merge_config.sh -m -O $(dir $(2)) $(2) $(3)
  59. $(call kconfig-package-regen-dot-config,$(1))
  60. endef
  61. ################################################################################
  62. # inner-kconfig-package -- generates the make targets needed to support a
  63. # kconfig package
  64. #
  65. # argument 1 is the lowercase package name
  66. # argument 2 is the uppercase package name, including a HOST_ prefix
  67. # for host packages
  68. # argument 3 is the uppercase package name, without the HOST_ prefix
  69. # for host packages
  70. # argument 4 is the type (target or host)
  71. ################################################################################
  72. define inner-kconfig-package
  73. # Register the kconfig dependencies as regular dependencies, so that
  74. # they are also accounted for in the generated graphs.
  75. $(2)_DEPENDENCIES += $$($(2)_KCONFIG_DEPENDENCIES)
  76. # Call the generic package infrastructure to generate the necessary
  77. # make targets.
  78. # Note: this must be done _before_ attempting to use $$($(2)_DIR) in a
  79. # dependency expression
  80. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  81. # Default values
  82. $(2)_MAKE ?= $$(MAKE)
  83. $(2)_KCONFIG_EDITORS ?= menuconfig
  84. $(2)_KCONFIG_OPTS ?=
  85. $(2)_KCONFIG_FIXUP_CMDS ?=
  86. $(2)_KCONFIG_FRAGMENT_FILES ?=
  87. $(2)_KCONFIG_DOTCONFIG ?= .config
  88. # Do not use $(2)_KCONFIG_DOTCONFIG as stamp file, because the package
  89. # buildsystem (e.g. linux >= 4.19) may touch it, thus rendering our
  90. # timestamps out of date, thus re-trigerring the build of the package.
  91. # Instead, use a specific file of our own as timestamp.
  92. $(2)_KCONFIG_STAMP_DOTCONFIG = .stamp_dotconfig
  93. # The config file as well as the fragments could be in-tree, so before
  94. # depending on them the package should be extracted (and patched) first.
  95. #
  96. # Since those files only have a order-only dependency, make would treat
  97. # any missing one as a "force" target:
  98. # https://www.gnu.org/software/make/manual/make.html#Force-Targets
  99. # and would forcibly any rule that depend on those files, causing a
  100. # rebuild of the kernel each time make is called.
  101. #
  102. # So, we provide a recipe that checks all of those files exist, to
  103. # overcome that standard make behaviour.
  104. #
  105. $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES): | $(1)-patch
  106. for f in $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES); do \
  107. if [ ! -f "$$$${f}" ]; then \
  108. printf "Kconfig fragment '%s' for '%s' does not exist\n" "$$$${f}" "$(1)"; \
  109. exit 1; \
  110. fi; \
  111. done
  112. $(2)_KCONFIG_MAKE = \
  113. $$($(2)_MAKE_ENV) $$($(2)_MAKE) -C $$($(2)_DIR) \
  114. $$(PKG_KCONFIG_COMMON_OPTS) $$($(2)_KCONFIG_OPTS)
  115. # $(2)_KCONFIG_MAKE may already rely on shell expansion. As the $() syntax
  116. # of the shell conflicts with Make's own syntax, this means that backticks
  117. # are used with those shell constructs. Unfortunately, the backtick syntax
  118. # does not nest, and we need to use Make instead of the shell to handle
  119. # conditions.
  120. # A recursively expanded variable is necessary, to be sure that the shell
  121. # command is called when the rule is processed during the build and not
  122. # when the rule is created when parsing all packages.
  123. $(2)_KCONFIG_RULES = \
  124. $$(shell $$($(2)_KCONFIG_MAKE) -pn config 2>/dev/null | \
  125. sed 's/^\([_0-9a-zA-Z]*config\):.*/\1/ p; d')
  126. # The specified source configuration file and any additional configuration file
  127. # fragments are merged together to .config, after the package has been patched.
  128. # Since the file could be a defconfig file it needs to be expanded to a
  129. # full .config first.
  130. $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES)
  131. $$(call prepare-per-package-directory,$$($(2)_KCONFIG_DEPENDENCIES))
  132. $$(call kconfig-package-merge-config,$(2),$$(@D)/$$($(2)_KCONFIG_DOTCONFIG),\
  133. $$($(2)_KCONFIG_FRAGMENT_FILES))
  134. $$(Q)touch $$(@D)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
  135. # If _KCONFIG_FILE or _KCONFIG_FRAGMENT_FILES exists, this dependency is
  136. # already implied, but if we only have a _KCONFIG_DEFCONFIG we have to add
  137. # it explicitly. It doesn't hurt to always have it though.
  138. $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): | $(1)-patch
  139. # Some packages may need additional tools to be present by the time their
  140. # kconfig structure is parsed (e.g. the linux kernel may need to call to
  141. # the compiler to test its features).
  142. $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): | $$($(2)_KCONFIG_DEPENDENCIES)
  143. # In order to get a usable, consistent configuration, some fixup may be needed.
  144. # The exact rules are specified by the package .mk file.
  145. define $(2)_FIXUP_DOT_CONFIG
  146. $$($(2)_KCONFIG_FIXUP_CMDS)
  147. $$(call kconfig-package-regen-dot-config,$(2))
  148. $$(Q)touch $$($(2)_DIR)/.stamp_kconfig_fixup_done
  149. endef
  150. $$($(2)_DIR)/.stamp_kconfig_fixup_done: PKG=$(2)
  151. $$($(2)_DIR)/.stamp_kconfig_fixup_done: $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
  152. $$($(2)_FIXUP_DOT_CONFIG)
  153. # Before running configure, the configuration file should be present and fixed
  154. $$($(2)_TARGET_CONFIGURE): $$($(2)_DIR)/.stamp_kconfig_fixup_done
  155. # Force olddefconfig again on -reconfigure
  156. $(1)-clean-for-reconfigure: $(1)-clean-kconfig-for-reconfigure
  157. $(1)-clean-kconfig-for-reconfigure:
  158. rm -f $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
  159. # Only enable the foo-*config targets when the package is actually enabled.
  160. # Note: the variable $(2)_KCONFIG_VAR is not related to the kconfig
  161. # infrastructure, but defined by pkg-generic.mk. The generic infrastructure is
  162. # already called above, so we can effectively use this variable.
  163. ifeq ($$($$($(2)_KCONFIG_VAR)),y)
  164. ifeq ($$(BR_BUILDING),y)
  165. # Either FOO_KCONFIG_FILE or FOO_KCONFIG_DEFCONFIG is required...
  166. ifeq ($$(or $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
  167. $$(error Internal error: no value specified for $(2)_KCONFIG_FILE or $(2)_KCONFIG_DEFCONFIG)
  168. endif
  169. # ... but not both:
  170. ifneq ($$(and $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
  171. $$(error Internal error: $(2)_KCONFIG_FILE and $(2)_KCONFIG_DEFCONFIG are mutually exclusive but both are defined)
  172. endif
  173. endif
  174. # For the configurators, we do want to use the system-provided host
  175. # tools, not the ones we build. This is particularly true for
  176. # pkg-config; if we use our pkg-config (from host-pkgconf), then it
  177. # would not look for the .pc from the host, but we do need them,
  178. # especially to find ncurses, GTK+, Qt (resp. for menuconfig and
  179. # nconfig, gconfig, xconfig).
  180. # So we simply remove our PATH and PKG_CONFIG_* variables.
  181. $(2)_CONFIGURATOR_MAKE_ENV = \
  182. $$(filter-out PATH=% PKG_CONFIG=% PKG_CONFIG_SYSROOT_DIR=% \
  183. PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=% PKG_CONFIG_ALLOW_SYSTEM_LIBS=% \
  184. PKG_CONFIG_LIBDIR=%,$$($(2)_MAKE_ENV)) \
  185. PKG_CONFIG_PATH="$(HOST_PKG_CONFIG_PATH)"
  186. # Configuration editors (menuconfig, ...)
  187. #
  188. # We need to apply the configuration fixups right after a configuration
  189. # editor exits, so that it is possible to save the configuration right
  190. # after exiting an editor, and so the user always sees a .config file
  191. # that is clean wrt. our requirements.
  192. #
  193. # Because commands in $(1)_FIXUP_KCONFIG are probably using $(@D), we
  194. # need to have a valid @D set. But, because the configurators rules are
  195. # not real files and do not contain the path to the package build dir,
  196. # @D would be just '.' in this case. So, we use an intermediate rule
  197. # with a stamp-like file which path is in the package build dir, so we
  198. # end up having a valid @D.
  199. #
  200. $$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS)): $(1)-%: $$($(2)_DIR)/.kconfig_editor_%
  201. $$($(2)_DIR)/.kconfig_editor_%: PKG=$(2)
  202. $$($(2)_DIR)/.kconfig_editor_%: $$($(2)_DIR)/.stamp_kconfig_fixup_done
  203. $$($(2)_CONFIGURATOR_MAKE_ENV) $$($(2)_MAKE) -C $$($(2)_DIR) \
  204. $$(PKG_KCONFIG_COMMON_OPTS) $$($(2)_KCONFIG_OPTS) $$(*)
  205. rm -f $$($(2)_DIR)/.stamp_{kconfig_fixup_done,configured,built}
  206. rm -f $$($(2)_DIR)/.stamp_{target,staging,images}_installed
  207. $$($(2)_FIXUP_DOT_CONFIG)
  208. # Saving back the configuration
  209. #
  210. # Ideally, that should directly depend on $$($(2)_DIR)/.stamp_kconfig_fixup_done,
  211. # but that breaks the use-case in PR-8156 (from a clean tree):
  212. # make menuconfig <- enable kernel, use an in-tree defconfig, save and exit
  213. # make linux-menuconfig <- enable/disable whatever option, save and exit
  214. # make menuconfig <- change to use a custom defconfig file, set a path, save and exit
  215. # make linux-update-config <- should save to the new custom defconfig file
  216. #
  217. # Because of that use-case, saving the configuration can *not* directly
  218. # depend on the stamp file, because it itself depends on the .config,
  219. # which in turn depends on the (newly-set an non-existent) custom
  220. # defconfig file.
  221. #
  222. # Instead, we use a PHONY rule that will catch that situation.
  223. #
  224. $(1)-check-configuration-done:
  225. @if [ ! -f $$($(2)_DIR)/.stamp_kconfig_fixup_done ]; then \
  226. echo "$(1) is not yet configured"; \
  227. exit 1; \
  228. fi
  229. $(1)-savedefconfig: $(1)-check-configuration-done
  230. $$(call kconfig-package-savedefconfig,$(2))
  231. # Target to copy back the configuration to the source configuration file
  232. # Even though we could use 'cp --preserve-timestamps' here, the separate
  233. # cp and 'touch --reference' is used for symmetry with $(1)-update-defconfig.
  234. $(1)-update-config: PKG=$(2)
  235. $(1)-update-config: $(1)-check-configuration-done
  236. $$(call kconfig-package-update-config,$$($(2)_KCONFIG_DOTCONFIG))
  237. # Note: make sure the timestamp of the stored configuration is not newer than
  238. # the .config to avoid a useless rebuild. Note that, contrary to
  239. # $(1)-update-config, the reference for 'touch' is _not_ the file from which
  240. # we copy.
  241. $(1)-update-defconfig: PKG=$(2)
  242. $(1)-update-defconfig: $(1)-savedefconfig
  243. $$(call kconfig-package-update-config,defconfig)
  244. # Target to output differences between the configuration obtained via the
  245. # defconfig + fragments (if any) and the current configuration.
  246. # Note: it preserves the timestamp of the current configuration when moving it
  247. # around.
  248. $(1)-diff-config: $(1)-check-configuration-done
  249. $$(Q)cp -a $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG) $$($(2)_DIR)/.config.dc.bak
  250. $$(call kconfig-package-merge-config,$(2),$$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG),\
  251. $$($(2)_KCONFIG_FRAGMENT_FILES))
  252. $$(Q)utils/diffconfig $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG) \
  253. $$($(2)_DIR)/.config.dc.bak
  254. $$(Q)cp -a $$($(2)_DIR)/.config.dc.bak $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG)
  255. $$(Q)rm -f $$($(2)_DIR)/.config.dc.bak
  256. endif # package enabled
  257. .PHONY: \
  258. $(1)-update-config \
  259. $(1)-update-defconfig \
  260. $(1)-diff-config \
  261. $(1)-savedefconfig \
  262. $(1)-check-configuration-done \
  263. $$($(2)_DIR)/.kconfig_editor_% \
  264. $$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS))
  265. endef # inner-kconfig-package
  266. ################################################################################
  267. # kconfig-package -- the target generator macro for kconfig packages
  268. ################################################################################
  269. kconfig-package = $(call inner-kconfig-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)