package.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. #
  3. # Copyright (C) 2006-2020 OpenWrt.org
  4. __package_mk:=1
  5. all: $(if $(DUMP),dumpinfo,$(if $(CHECK),check,compile))
  6. include $(INCLUDE_DIR)/download.mk
  7. PKG_BUILD_DIR ?= $(BUILD_DIR)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
  8. PKG_INSTALL_DIR ?= $(PKG_BUILD_DIR)/ipkg-install
  9. PKG_BUILD_PARALLEL ?=
  10. PKG_SKIP_DOWNLOAD=$(USE_SOURCE_DIR)$(USE_GIT_TREE)$(USE_GIT_SRC_CHECKOUT)
  11. MAKE_J:=$(if $(MAKE_JOBSERVER),$(MAKE_JOBSERVER) $(if $(filter 3.% 4.0 4.1,$(MAKE_VERSION)),-j))
  12. PKG_SOURCE_DATE_EPOCH:=$(if $(DUMP),,$(shell $(TOPDIR)/scripts/get_source_date_epoch.sh $(CURDIR)))
  13. ifeq ($(strip $(PKG_BUILD_PARALLEL)),0)
  14. PKG_JOBS?=-j1
  15. else
  16. PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL),$(MAKE_J),-j1)
  17. endif
  18. PKG_BUILD_FLAGS?=
  19. __unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto,$(PKG_BUILD_FLAGS))
  20. ifneq ($(__unknown_flags),)
  21. $(error unknown PKG_BUILD_FLAGS: $(__unknown_flags))
  22. endif
  23. # $1=flagname, $2=default (0/1)
  24. define pkg_build_flag
  25. $(if $(filter no-$(1),$(PKG_BUILD_FLAGS)),0,$(if $(filter $(1),$(PKG_BUILD_FLAGS)),1,$(2)))
  26. endef
  27. ifeq ($(call pkg_build_flag,iremap,1),1)
  28. IREMAP_CFLAGS = $(call iremap,$(PKG_BUILD_DIR),$(notdir $(PKG_BUILD_DIR)))
  29. TARGET_CFLAGS += $(IREMAP_CFLAGS)
  30. endif
  31. ifdef CONFIG_USE_MIPS16
  32. ifeq ($(call pkg_build_flag,mips16,1),1)
  33. TARGET_ASFLAGS_DEFAULT = $(filter-out -mips16 -minterlink-mips16,$(TARGET_CFLAGS))
  34. TARGET_CFLAGS += -mips16 -minterlink-mips16
  35. TARGET_CXXFLAGS += -mips16 -minterlink-mips16
  36. endif
  37. endif
  38. ifeq ($(call pkg_build_flag,gc-sections,$(if $(CONFIG_USE_GC_SECTIONS),1,0)),1)
  39. TARGET_CFLAGS+= -ffunction-sections -fdata-sections
  40. TARGET_CXXFLAGS+= -ffunction-sections -fdata-sections
  41. TARGET_LDFLAGS+= -Wl,--gc-sections
  42. endif
  43. ifeq ($(call pkg_build_flag,lto,$(if $(CONFIG_USE_LTO),1,0)),1)
  44. TARGET_CFLAGS+= -flto=auto -fno-fat-lto-objects
  45. TARGET_CXXFLAGS+= -flto=auto -fno-fat-lto-objects
  46. TARGET_LDFLAGS+= -flto=auto -fuse-linker-plugin
  47. endif
  48. include $(INCLUDE_DIR)/hardening.mk
  49. include $(INCLUDE_DIR)/prereq.mk
  50. include $(INCLUDE_DIR)/unpack.mk
  51. include $(INCLUDE_DIR)/depends.mk
  52. ifneq ($(wildcard $(TOPDIR)/git-src/$(PKG_NAME)/.git),)
  53. USE_GIT_SRC_CHECKOUT:=1
  54. QUILT:=1
  55. endif
  56. ifneq ($(if $(CONFIG_SRC_TREE_OVERRIDE),$(wildcard ./git-src)),)
  57. USE_GIT_TREE:=1
  58. QUILT:=1
  59. endif
  60. ifdef USE_SOURCE_DIR
  61. QUILT:=1
  62. endif
  63. ifneq ($(wildcard $(PKG_BUILD_DIR)/.source_dir),)
  64. QUILT:=1
  65. endif
  66. include $(INCLUDE_DIR)/quilt.mk
  67. find_library_dependencies = \
  68. $(wildcard $(patsubst %,$(STAGING_DIR)/pkginfo/%.version, \
  69. $(filter-out $(BUILD_PACKAGES), $(sort $(foreach dep4, \
  70. $(sort $(foreach dep3, \
  71. $(sort $(foreach dep2, \
  72. $(sort $(foreach dep1, \
  73. $(sort $(foreach dep0, \
  74. $(Package/$(1)/depends), \
  75. $(Package/$(dep0)/depends) $(dep0) \
  76. )), \
  77. $(Package/$(dep1)/depends) $(dep1) \
  78. )), \
  79. $(Package/$(dep2)/depends) $(dep2) \
  80. )), \
  81. $(Package/$(dep3)/depends) $(dep3) \
  82. )), \
  83. $(Package/$(dep4)/depends) $(dep4) \
  84. ))) \
  85. ))
  86. PKG_DIR_NAME:=$(lastword $(subst /,$(space),$(CURDIR)))
  87. STAMP_NO_AUTOREBUILD=$(wildcard $(PKG_BUILD_DIR)/.no_autorebuild)
  88. PREV_STAMP_PREPARED:=$(if $(STAMP_NO_AUTOREBUILD),$(wildcard $(PKG_BUILD_DIR)/.prepared*))
  89. ifneq ($(PREV_STAMP_PREPARED),)
  90. STAMP_PREPARED:=$(PREV_STAMP_PREPARED)
  91. CONFIG_AUTOREBUILD:=
  92. else
  93. STAMP_PREPARED=$(PKG_BUILD_DIR)/.prepared$(if $(QUILT)$(DUMP),,_$(shell $(call $(if $(CONFIG_AUTOREMOVE),find_md5_reproducible,find_md5),${CURDIR} $(PKG_FILE_DEPENDS),))_$(call confvar,CONFIG_AUTOREMOVE $(PKG_PREPARED_DEPENDS)))
  94. endif
  95. STAMP_CONFIGURED=$(PKG_BUILD_DIR)/.configured$(if $(DUMP),,_$(call confvar,$(PKG_CONFIG_DEPENDS)))
  96. STAMP_CONFIGURED_WILDCARD=$(PKG_BUILD_DIR)/.configured_*
  97. STAMP_BUILT:=$(PKG_BUILD_DIR)/.built
  98. STAMP_INSTALLED:=$(STAGING_DIR)/stamp/.$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),)_installed
  99. STAGING_FILES_LIST:=$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),).list
  100. define CleanStaging
  101. rm -f $(STAMP_INSTALLED)
  102. @-(\
  103. if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
  104. $(SCRIPT_DIR)/clean-package.sh \
  105. "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
  106. "$(STAGING_DIR)"; \
  107. fi; \
  108. )
  109. endef
  110. PKG_INSTALL_STAMP:=$(PKG_INFO_DIR)/$(PKG_DIR_NAME).$(if $(BUILD_VARIANT),$(BUILD_VARIANT),default).install
  111. include $(INCLUDE_DIR)/package-defaults.mk
  112. include $(INCLUDE_DIR)/package-dumpinfo.mk
  113. include $(INCLUDE_DIR)/package-ipkg.mk
  114. include $(INCLUDE_DIR)/package-bin.mk
  115. include $(INCLUDE_DIR)/autotools.mk
  116. _pkg_target:=$(if $(QUILT),,.)
  117. override MAKEFLAGS=
  118. CONFIG_SITE:=$(INCLUDE_DIR)/site/$(ARCH)
  119. CUR_MAKEFILE:=$(filter-out Makefile,$(firstword $(MAKEFILE_LIST)))
  120. SUBMAKE:=$(NO_TRACE_MAKE) $(if $(CUR_MAKEFILE),-f $(CUR_MAKEFILE))
  121. PKG_CONFIG_PATH=$(STAGING_DIR)/usr/lib/pkgconfig:$(STAGING_DIR)/usr/share/pkgconfig
  122. unexport QUIET CONFIG_SITE
  123. ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
  124. ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
  125. define Build/Autoclean
  126. $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
  127. $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
  128. $(if $(filter prepare,$(MAKECMDGOALS)),,$(call rdep,$(PKG_BUILD_DIR),$(STAMP_BUILT),,-x "*/.dep_*" -x "*/ipkg*"))
  129. endef
  130. endif
  131. endif
  132. ifdef USE_GIT_SRC_CHECKOUT
  133. define Build/Prepare/Default
  134. mkdir -p $(PKG_BUILD_DIR)
  135. ln -s $(TOPDIR)/git-src/$(PKG_NAME)/.git $(PKG_BUILD_DIR)/.git
  136. ( cd $(PKG_BUILD_DIR); \
  137. git checkout .; \
  138. git submodule update --recursive; \
  139. git submodule foreach git config --unset core.worktree; \
  140. git submodule foreach git checkout .; \
  141. )
  142. endef
  143. endif
  144. ifdef USE_GIT_TREE
  145. define Build/Prepare/Default
  146. mkdir -p $(PKG_BUILD_DIR)
  147. ln -s $(CURDIR)/git-src $(PKG_BUILD_DIR)/.git
  148. ( cd $(PKG_BUILD_DIR); \
  149. git checkout .; \
  150. git submodule update --recursive; \
  151. git submodule foreach git config --unset core.worktree; \
  152. git submodule foreach git checkout .; \
  153. )
  154. endef
  155. endif
  156. ifdef USE_SOURCE_DIR
  157. define Build/Prepare/Default
  158. rm -rf $(PKG_BUILD_DIR)
  159. $(if $(wildcard $(USE_SOURCE_DIR)/*),,@echo "Error: USE_SOURCE_DIR=$(USE_SOURCE_DIR) path not found"; false)
  160. ln -snf $(USE_SOURCE_DIR) $(PKG_BUILD_DIR)
  161. touch $(PKG_BUILD_DIR)/.source_dir
  162. endef
  163. endif
  164. define Build/Exports/Default
  165. $(1) : export ACLOCAL_INCLUDE=$$(foreach p,$$(wildcard $$(STAGING_DIR)/usr/share/aclocal $$(STAGING_DIR)/usr/share/aclocal-* $$(STAGING_DIR_HOSTPKG)/share/aclocal $$(STAGING_DIR_HOSTPKG)/share/aclocal-* $$(STAGING_DIR)/host/share/aclocal $$(STAGING_DIR)/host/share/aclocal-*),-I $$(p))
  166. $(1) : export STAGING_PREFIX=$$(STAGING_DIR)/usr
  167. $(1) : export PATH=$$(TARGET_PATH_PKG)
  168. $(1) : export CONFIG_SITE:=$$(CONFIG_SITE)
  169. $(1) : export PKG_CONFIG_PATH:=$$(PKG_CONFIG_PATH)
  170. $(1) : export PKG_CONFIG_LIBDIR:=$$(PKG_CONFIG_PATH)
  171. $(1) : export GIT_CEILING_DIRECTORIES:=$$(BUILD_DIR)
  172. endef
  173. Build/Exports=$(Build/Exports/Default)
  174. define Build/CoreTargets
  175. STAMP_PREPARED:=$$(STAMP_PREPARED)
  176. STAMP_CONFIGURED:=$$(STAMP_CONFIGURED)
  177. $(if $(QUILT),$(Build/Quilt))
  178. $(call Build/Autoclean)
  179. $(call DefaultTargets)
  180. $(call check_download_integrity)
  181. download:
  182. $(foreach hook,$(Hooks/Download),
  183. $(call $(hook))$(sep)
  184. )
  185. $(STAMP_PREPARED) : export PATH=$$(TARGET_PATH_PKG)
  186. $(STAMP_PREPARED): $(STAMP_PREPARED_DEPENDS)
  187. @-rm -rf $(PKG_BUILD_DIR)
  188. @mkdir -p $(PKG_BUILD_DIR)
  189. touch $$@_check
  190. $(foreach hook,$(Hooks/Prepare/Pre),$(call $(hook))$(sep))
  191. $(Build/Prepare)
  192. $(foreach hook,$(Hooks/Prepare/Post),$(call $(hook))$(sep))
  193. touch $$@
  194. $(call Build/Exports,$(STAMP_CONFIGURED))
  195. $(STAMP_CONFIGURED): $(STAMP_PREPARED) $(STAMP_CONFIGURED_DEPENDS)
  196. rm -f $(STAMP_CONFIGURED_WILDCARD)
  197. $(CleanStaging)
  198. $(foreach hook,$(Hooks/Configure/Pre),$(call $(hook))$(sep))
  199. $(Build/Configure)
  200. $(foreach hook,$(Hooks/Configure/Post),$(call $(hook))$(sep))
  201. touch $$@
  202. $(call Build/Exports,$(STAMP_BUILT))
  203. $(STAMP_BUILT): $(STAMP_CONFIGURED) $(STAMP_BUILT_DEPENDS)
  204. rm -f $$@
  205. touch $$@_check
  206. $(foreach hook,$(Hooks/Compile/Pre),$(call $(hook))$(sep))
  207. $(Build/Compile)
  208. $(foreach hook,$(Hooks/Compile/Post),$(call $(hook))$(sep))
  209. $(Build/Install)
  210. $(foreach hook,$(Hooks/Install/Post),$(call $(hook))$(sep))
  211. touch $$@
  212. $(STAMP_INSTALLED) : export PATH=$$(TARGET_PATH_PKG)
  213. $(STAMP_INSTALLED): $(STAMP_BUILT)
  214. rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
  215. mkdir -p $(TMP_DIR)/stage-$(PKG_DIR_NAME)/host $(STAGING_DIR)/packages
  216. $(foreach hook,$(Hooks/InstallDev/Pre),\
  217. $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
  218. )
  219. $(call Build/InstallDev,$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)
  220. $(foreach hook,$(Hooks/InstallDev/Post),\
  221. $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
  222. )
  223. if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
  224. $(SCRIPT_DIR)/clean-package.sh \
  225. "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
  226. "$(STAGING_DIR)"; \
  227. fi
  228. if [ -d $(TMP_DIR)/stage-$(PKG_DIR_NAME) ]; then \
  229. (cd $(TMP_DIR)/stage-$(PKG_DIR_NAME); find ./ > $(TMP_DIR)/stage-$(PKG_DIR_NAME).files); \
  230. $(call locked, \
  231. mv $(TMP_DIR)/stage-$(PKG_DIR_NAME).files $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) && \
  232. $(CP) $(TMP_DIR)/stage-$(PKG_DIR_NAME)/* $(STAGING_DIR)/; \
  233. ,staging-dir); \
  234. fi
  235. rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
  236. touch $$@
  237. ifdef Build/InstallDev
  238. $(_pkg_target)compile: $(STAMP_INSTALLED)
  239. endif
  240. $(_pkg_target)prepare: $(STAMP_PREPARED)
  241. $(_pkg_target)configure: $(STAMP_CONFIGURED)
  242. $(_pkg_target)dist: $(STAMP_CONFIGURED)
  243. $(_pkg_target)distcheck: $(STAMP_CONFIGURED)
  244. ifneq ($(CONFIG_AUTOREMOVE),)
  245. compile:
  246. -touch -r $(PKG_BUILD_DIR)/.built $(PKG_BUILD_DIR)/.autoremove 2>/dev/null >/dev/null
  247. $(FIND) $(PKG_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -and -not -name '.pkgdir' -print0 | \
  248. $(XARGS) -0 rm -rf
  249. endif
  250. endef
  251. define Build/DefaultTargets
  252. $(if $(PKG_SKIP_DOWNLOAD),,$(if $(strip $(PKG_SOURCE_URL)),$(call Download,default)))
  253. $(if $(DUMP),,$(Build/CoreTargets))
  254. define Build/DefaultTargets
  255. endef
  256. endef
  257. define BuildPackage
  258. $(eval $(Package/Default))
  259. $(eval $(Package/$(1)))
  260. ifdef DESCRIPTION
  261. $$(error DESCRIPTION:= is obsolete, use Package/PKG_NAME/description)
  262. endif
  263. ifndef Package/$(1)/description
  264. define Package/$(1)/description
  265. $(TITLE)
  266. endef
  267. endif
  268. BUILD_PACKAGES += $(1)
  269. $(STAMP_PREPARED): $$(if $(QUILT)$(DUMP),,$(call find_library_dependencies,$(1)))
  270. $(foreach FIELD, TITLE CATEGORY SECTION VERSION,
  271. ifeq ($($(FIELD)),)
  272. $$(error Package/$(1) is missing the $(FIELD) field)
  273. endif
  274. )
  275. $(if $(DUMP), \
  276. $(if $(CHECK),,$(Dumpinfo/Package)), \
  277. $(foreach target, \
  278. $(if $(Package/$(1)/targets),$(Package/$(1)/targets), \
  279. $(if $(PKG_TARGETS),$(PKG_TARGETS), ipkg) \
  280. ), $(BuildTarget/$(target)) \
  281. ) \
  282. )
  283. $(if $(PKG_HOST_ONLY),,$(call Build/DefaultTargets,$(1)))
  284. endef
  285. define pkg_install_files
  286. $(foreach install_file,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_file)`; $(INSTALL_DATA) $(2)/$(install_file) $(3)/`dirname $(install_file)`;)
  287. endef
  288. define pkg_install_bin
  289. $(foreach install_apps,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_apps)`; $(INSTALL_BIN) $(2)/$(install_apps) $(3)/`dirname $(install_apps)`;)
  290. endef
  291. Build/Prepare=$(call Build/Prepare/Default,)
  292. Build/Configure=$(call Build/Configure/Default,)
  293. Build/Compile=$(call Build/Compile/Default,)
  294. Build/Install=$(if $(PKG_INSTALL),$(call Build/Install/Default,))
  295. Build/Dist=$(call Build/Dist/Default,)
  296. Build/DistCheck=$(call Build/DistCheck/Default,)
  297. .NOTPARALLEL:
  298. .PHONY: prepare-package-install
  299. prepare-package-install:
  300. @mkdir -p $(PKG_INFO_DIR)
  301. @rm -f $(PKG_INSTALL_STAMP)
  302. @echo "$(filter-out essential nonshared,$(PKG_FLAGS))" > $(PKG_INSTALL_STAMP).flags
  303. $(PACKAGE_DIR):
  304. mkdir -p $@
  305. compile:
  306. .install: .compile
  307. install: compile
  308. force-clean-build: FORCE
  309. rm -rf $(PKG_BUILD_DIR)
  310. clean-build: $(if $(wildcard $(PKG_BUILD_DIR)/.autoremove),force-clean-build)
  311. clean: force-clean-build
  312. $(CleanStaging)
  313. $(call Build/UninstallDev,$(STAGING_DIR),$(STAGING_DIR)/host)
  314. $(Build/Clean)
  315. rm -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST)
  316. dist:
  317. $(Build/Dist)
  318. distcheck:
  319. $(Build/DistCheck)