blob: 9a717dd2fa79ca1016cead8721a6fb933aecbe96 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# SPDX-License-Identifier: GPL-2.0-only
2#
3# Copyright (C) 2007-2008 OpenWrt.org
4# Copyright (C) 2016 LEDE Project
5
6ifneq ($(__target_inc),1)
7__target_inc=1
8
9
10##@
11# @brief Default device type ( basic | nas | router ).
12##
13DEVICE_TYPE?=router
14
15##@
16# @brief Default packages.
17#
18# The really basic set. Additional packages are added based on @DEVICE_TYPE and
19# @CONFIG_* values.
20##
21DEFAULT_PACKAGES:=\
22 base-files \
23 ca-bundle \
24 dropbear \
25 fstools \
26 libc \
27 libgcc \
28 libustream-mbedtls \
29 logd \
30 mtd \
31 netifd \
32 uci \
33 uclient-fetch \
34 urandom-seed \
35 urngd
36
37##@
38# @brief Default packages for @DEVICE_TYPE basic.
39##
40DEFAULT_PACKAGES.basic:=
41##@
42# @brief Default packages for @DEVICE_TYPE nas.
43##
44DEFAULT_PACKAGES.nas:=\
45 block-mount \
46 fdisk \
47 lsblk \
48 mdadm
49##@
50# @brief Default packages for @DEVICE_TYPE router.
51##
52DEFAULT_PACKAGES.router:=\
53 dnsmasq \
54 firewall \
55 ip6tables \
56 iptables \
57 kmod-ipt-offload \
58 odhcp6c \
59 odhcpd-ipv6only \
60 ppp \
61 ppp-mod-pppoe
62
63ifneq ($(DUMP),)
64 all: dumpinfo
65endif
66
67target_conf=$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
68ifeq ($(DUMP),)
69 PLATFORM_DIR:=$(firstword $(wildcard $(TOPDIR)/target/linux/feeds/$(BOARD) $(TOPDIR)/target/linux/$(BOARD)))
70 SUBTARGET:=$(strip $(foreach subdir,$(patsubst $(PLATFORM_DIR)/%/target.mk,%,$(wildcard $(PLATFORM_DIR)/*/target.mk)),$(if $(CONFIG_TARGET_$(call target_conf,$(BOARD)_$(subdir))),$(subdir))))
71else
72 PLATFORM_DIR:=${CURDIR}
73 ifeq ($(SUBTARGETS),)
74 SUBTARGETS:=$(strip $(patsubst $(PLATFORM_DIR)/%/target.mk,%,$(wildcard $(PLATFORM_DIR)/*/target.mk)))
75 endif
76endif
77
78TARGETID:=$(BOARD)$(if $(SUBTARGET),/$(SUBTARGET))
79PLATFORM_SUBDIR:=$(PLATFORM_DIR)$(if $(SUBTARGET),/$(SUBTARGET))
80
81ifneq ($(TARGET_BUILD),1)
82 ifndef DUMP
83 include $(PLATFORM_DIR)/Makefile
84 ifneq ($(PLATFORM_DIR),$(PLATFORM_SUBDIR))
85 include $(PLATFORM_SUBDIR)/target.mk
86 endif
87 endif
88else
89 ifneq ($(SUBTARGET),)
90 -include ./$(SUBTARGET)/target.mk
91 endif
92endif
93
94# include ujail on systems with enough storage
95ifeq ($(filter small_flash,$(FEATURES)),)
96 DEFAULT_PACKAGES+=procd-ujail
97endif
98
99# Add device specific packages (here below to allow device type set from subtarget)
100DEFAULT_PACKAGES += $(DEFAULT_PACKAGES.$(DEVICE_TYPE))
101
102##@
103# @brief Filter out packages, prepended with `-`.
104#
105# @param 1: Package list.
106##
107filter_packages = $(filter-out -% $(patsubst -%,%,$(filter -%,$(1))),$(1))
108
109##@
110# @brief Append extra package dependencies.
111#
112# @param 1: Package list.
113##
114extra_packages = $(if $(filter wpad wpad-% nas,$(1)),iwinfo)
115
116define ProfileDefault
117 NAME:=
118 PRIORITY:=
119 PACKAGES:=
120endef
121
122ifndef Profile
123define Profile
124 $(eval $(call ProfileDefault))
125 $(eval $(call Profile/$(1)))
126 dumpinfo : $(call shexport,Profile/$(1)/Description)
127 PACKAGES := $(filter-out -%,$(PACKAGES))
128 DUMPINFO += \
129 echo "Target-Profile: $(1)"; \
130 $(if $(PRIORITY), echo "Target-Profile-Priority: $(PRIORITY)"; ) \
131 echo "Target-Profile-Name: $(NAME)"; \
132 echo "Target-Profile-Packages: $(PACKAGES) $(call extra_packages,$(DEFAULT_PACKAGES) $(PACKAGES))"; \
133 echo "Target-Profile-Description:"; \
134 echo "$$$$$$$$$(call shvar,Profile/$(1)/Description)"; \
135 echo "@@"; \
136 echo;
137endef
138endif
139
140ifneq ($(PLATFORM_DIR),$(PLATFORM_SUBDIR))
141 define IncludeProfiles
142 -include $(sort $(wildcard $(PLATFORM_DIR)/profiles/*.mk))
143 -include $(sort $(wildcard $(PLATFORM_SUBDIR)/profiles/*.mk))
144 endef
145else
146 define IncludeProfiles
147 -include $(sort $(wildcard $(PLATFORM_DIR)/profiles/*.mk))
148 endef
149endif
150
151PROFILE?=$(call qstrip,$(CONFIG_TARGET_PROFILE))
152
153ifeq ($(TARGET_BUILD),1)
154 ifneq ($(DUMP),)
155 $(eval $(call IncludeProfiles))
156 endif
157endif
158
159ifneq ($(TARGET_BUILD)$(if $(DUMP),,1),)
160 include $(INCLUDE_DIR)/kernel-version.mk
161endif
162
163GENERIC_PLATFORM_DIR := $(TOPDIR)/target/linux/generic
164GENERIC_BACKPORT_DIR := $(GENERIC_PLATFORM_DIR)/backport$(if $(wildcard $(GENERIC_PLATFORM_DIR)/backport-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
165GENERIC_PATCH_DIR := $(GENERIC_PLATFORM_DIR)/pending$(if $(wildcard $(GENERIC_PLATFORM_DIR)/pending-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
166GENERIC_HACK_DIR := $(GENERIC_PLATFORM_DIR)/hack$(if $(wildcard $(GENERIC_PLATFORM_DIR)/hack-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
167GENERIC_FILES_DIR := $(foreach dir,$(wildcard $(GENERIC_PLATFORM_DIR)/files $(GENERIC_PLATFORM_DIR)/files-$(KERNEL_PATCHVER)),"$(dir)")
168
169__config_name_list = $(1)/$(KERNEL_CONFIG_FILE_NAME_APPENDIX)config-$(KERNEL_PATCHVER) $(1)/config-$(KERNEL_PATCHVER) $(1)/config-default
170__config_list = $(firstword $(wildcard $(call __config_name_list,$(1))))
171find_kernel_config=$(if $(__config_list),$(__config_list),$(lastword $(__config_name_list)))
172
173GENERIC_LINUX_CONFIG = $(call find_kernel_config,$(GENERIC_PLATFORM_DIR))
174LINUX_TARGET_CONFIG = $(call find_kernel_config,$(PLATFORM_DIR))
175ifneq ($(PLATFORM_DIR),$(PLATFORM_SUBDIR))
176 LINUX_SUBTARGET_CONFIG = $(call find_kernel_config,$(PLATFORM_SUBDIR))
177endif
178
179# config file list used for compiling
180LINUX_KCONFIG_LIST = $(wildcard $(GENERIC_LINUX_CONFIG) $(LINUX_TARGET_CONFIG) $(LINUX_SUBTARGET_CONFIG) $(TOPDIR)/env/kernel-config)
181
182# default config list for reconfiguring
183# defaults to subtarget if subtarget exists and target does not
184# defaults to target otherwise
185USE_SUBTARGET_CONFIG = $(if $(wildcard $(LINUX_TARGET_CONFIG)),,$(if $(LINUX_SUBTARGET_CONFIG),1))
186
187LINUX_RECONFIG_LIST = $(wildcard $(GENERIC_LINUX_CONFIG) $(LINUX_TARGET_CONFIG) $(if $(USE_SUBTARGET_CONFIG),$(LINUX_SUBTARGET_CONFIG)))
188LINUX_RECONFIG_TARGET = $(if $(USE_SUBTARGET_CONFIG),$(LINUX_SUBTARGET_CONFIG),$(LINUX_TARGET_CONFIG))
189
190CFG_TARGET = $(CONFIG_TARGET)
191ifeq ($(CFG_TARGET),platform)
192 CFG_TARGET = target
193 $(warning Deprecation warning: use CONFIG_TARGET=target instead.)
194else ifeq ($(CFG_TARGET),subtarget_platform)
195 CFG_TARGET = subtarget_target
196 $(warning Deprecation warning: use CONFIG_TARGET=subtarget_target instead.)
197endif
198
199# select the config file to be changed by kernel_menuconfig/kernel_oldconfig
200ifeq ($(CFG_TARGET),target)
201 LINUX_RECONFIG_LIST = $(wildcard $(GENERIC_LINUX_CONFIG) $(LINUX_TARGET_CONFIG))
202 LINUX_RECONFIG_TARGET = $(LINUX_TARGET_CONFIG)
203else ifeq ($(CFG_TARGET),subtarget)
204 LINUX_RECONFIG_LIST = $(wildcard $(GENERIC_LINUX_CONFIG) $(LINUX_TARGET_CONFIG) $(LINUX_SUBTARGET_CONFIG))
205 LINUX_RECONFIG_TARGET = $(LINUX_SUBTARGET_CONFIG)
206else ifeq ($(CFG_TARGET),subtarget_target)
207 LINUX_RECONFIG_LIST = $(wildcard $(GENERIC_LINUX_CONFIG) $(LINUX_SUBTARGET_CONFIG) $(LINUX_TARGET_CONFIG))
208 LINUX_RECONFIG_TARGET = $(LINUX_TARGET_CONFIG)
209else ifeq ($(CFG_TARGET),env)
210 LINUX_RECONFIG_LIST = $(LINUX_KCONFIG_LIST)
211 LINUX_RECONFIG_TARGET = $(TOPDIR)/env/kernel-config
212else ifneq ($(strip $(CFG_TARGET)),)
213 $(error CONFIG_TARGET=$(CFG_TARGET) is invalid. Valid: target|subtarget|subtarget_target|env)
214endif
215
216__linux_confcmd = $(2) $(patsubst %,+,$(wordlist 2,9999,$(1))) $(1)
217
218LINUX_CONF_CMD = $(SCRIPT_DIR)/kconfig.pl $(call __linux_confcmd,$(LINUX_KCONFIG_LIST))
219LINUX_RECONF_CMD = $(SCRIPT_DIR)/kconfig.pl $(call __linux_confcmd,$(LINUX_RECONFIG_LIST))
220LINUX_RECONF_DIFF = $(SCRIPT_DIR)/kconfig.pl - '>' $(call __linux_confcmd,$(filter-out $(LINUX_RECONFIG_TARGET),$(LINUX_RECONFIG_LIST))) $(1) $(GENERIC_PLATFORM_DIR)/config-filter
221
222ifeq ($(DUMP),1)
223 BuildTarget=$(BuildTargets/DumpCurrent)
224
225 CPU_CFLAGS = -Os -pipe
226 ifneq ($(findstring mips,$(ARCH)),)
227 ifneq ($(findstring mips64,$(ARCH)),)
228 CPU_TYPE ?= mips64
229 else
230 CPU_TYPE ?= mips32
231 endif
232 CPU_CFLAGS += -mno-branch-likely
233 CPU_CFLAGS_mips32 = -mips32 -mtune=mips32
234 CPU_CFLAGS_mips64 = -mips64 -mtune=mips64 -mabi=64
235 CPU_CFLAGS_mips64r2 = -mips64r2 -mtune=mips64r2 -mabi=64
236 CPU_CFLAGS_4kec = -mips32r2 -mtune=4kec
237 CPU_CFLAGS_24kc = -mips32r2 -mtune=24kc
238 CPU_CFLAGS_74kc = -mips32r2 -mtune=74kc
239 CPU_CFLAGS_octeonplus = -march=octeon+ -mabi=64
240 endif
241 ifeq ($(ARCH),i386)
242 CPU_TYPE ?= pentium-mmx
243 CPU_CFLAGS_pentium-mmx = -march=pentium-mmx
244 CPU_CFLAGS_pentium4 = -march=pentium4
245 endif
246 ifneq ($(findstring arm,$(ARCH)),)
247 CPU_TYPE ?= xscale
248 endif
249 ifeq ($(ARCH),powerpc)
250 CPU_CFLAGS_603e:=-mcpu=603e
251 CPU_CFLAGS_8540:=-mcpu=8540
252 CPU_CFLAGS_8548:=-mcpu=8548
253 CPU_CFLAGS_405:=-mcpu=405
254 CPU_CFLAGS_440:=-mcpu=440
255 CPU_CFLAGS_464fp:=-mcpu=464fp
256 endif
257 ifeq ($(ARCH),powerpc64)
258 CPU_TYPE ?= powerpc64
259 CPU_CFLAGS_e5500:=-mcpu=e5500
260 CPU_CFLAGS_powerpc64:=-mcpu=powerpc64
261 endif
262 ifeq ($(ARCH),sparc)
263 CPU_TYPE = sparc
264 CPU_CFLAGS_ultrasparc = -mcpu=ultrasparc
265 endif
266 ifeq ($(ARCH),aarch64)
267 CPU_TYPE ?= generic
268 CPU_CFLAGS_generic = -mcpu=generic
269 CPU_CFLAGS_cortex-a53 = -mcpu=cortex-a53
270 endif
271 ifeq ($(ARCH),arc)
272 CPU_TYPE ?= arc700
273 CPU_CFLAGS += -matomic
274 CPU_CFLAGS_arc700 = -mcpu=arc700
275 CPU_CFLAGS_archs = -mcpu=archs
276 endif
277 ifeq ($(ARCH),riscv64)
278 CPU_TYPE ?= riscv64
279 CPU_CFLAGS_riscv64:=-mabi=lp64d -march=rv64imafdc
280 endif
281 ifeq ($(ARCH),loongarch64)
282 CPU_TYPE ?= generic
283 CPU_CFLAGS := -O2 -pipe
284 CPU_CFLAGS_generic:=-march=loongarch64
285 endif
286 ifneq ($(CPU_TYPE),)
287 ifndef CPU_CFLAGS_$(CPU_TYPE)
288 $(warning CPU_TYPE "$(CPU_TYPE)" doesn't correspond to a known type)
289 endif
290 endif
291 DEFAULT_CFLAGS=$(strip $(CPU_CFLAGS) $(CPU_CFLAGS_$(CPU_TYPE)) $(CPU_CFLAGS_$(CPU_SUBTYPE)))
292
293 ifneq ($(BOARD),)
294 TMP_CONFIG:=$(TMP_DIR)/.kconfig-$(call target_conf,$(TARGETID))
295 $(TMP_CONFIG): $(LINUX_KCONFIG_LIST)
296 $(LINUX_CONF_CMD) > $@ || rm -f $@
297 -include $(TMP_CONFIG)
298 .SILENT: $(TMP_CONFIG)
299 .PRECIOUS: $(TMP_CONFIG)
300
301 ifdef KERNEL_TESTING_PATCHVER
302 ifneq ($(KERNEL_TESTING_PATCHVER),$(KERNEL_PATCHVER))
303 FEATURES += testing-kernel
304 endif
305 endif
306 ifneq ($(CONFIG_OF),)
307 FEATURES += dt
308 endif
309 ifneq ($(CONFIG_GENERIC_GPIO)$(CONFIG_GPIOLIB),)
310 FEATURES += gpio
311 endif
312 ifneq ($(CONFIG_PCI),)
313 FEATURES += pci
314 endif
315 ifneq ($(CONFIG_PCIEPORTBUS),)
316 FEATURES += pcie
317 endif
318 ifneq ($(CONFIG_USB)$(CONFIG_USB_SUPPORT),)
319 ifneq ($(CONFIG_USB_ARCH_HAS_HCD)$(CONFIG_USB_EHCI_HCD),)
320 FEATURES += usb
321 endif
322 endif
323 ifneq ($(CONFIG_PCMCIA)$(CONFIG_PCCARD),)
324 FEATURES += pcmcia
325 endif
326 ifneq ($(CONFIG_VGA_CONSOLE)$(CONFIG_FB),)
327 FEATURES += display
328 endif
329 ifneq ($(CONFIG_RTC_CLASS),)
330 FEATURES += rtc
331 endif
332 ifneq ($(CONFIG_VIRTIO),)
333 FEATURES += virtio
334 endif
335 ifneq ($(CONFIG_CPU_MIPS32_R2),)
336 FEATURES += mips16
337 endif
338 ifneq ($(CONFIG_CPU_V6),)
339 FEATURES += arm_v6
340 endif
341 ifneq ($(CONFIG_CPU_V6K),)
342 FEATURES += arm_v6
343 endif
344 ifneq ($(CONFIG_CPU_V7),)
345 FEATURES += arm_v7
346 endif
347
348 # remove duplicates
349 FEATURES:=$(sort $(FEATURES))
350 endif
351endif
352
353CUR_SUBTARGET:=$(SUBTARGET)
354ifeq ($(SUBTARGETS),)
355 CUR_SUBTARGET := default
356endif
357
358define BuildTargets/DumpCurrent
359 .PHONY: dumpinfo
360 dumpinfo : export DESCRIPTION=$$(Target/Description)
361 dumpinfo:
362 @echo 'Target: $(TARGETID)'; \
363 echo 'Target-Board: $(BOARD)'; \
364 echo 'Target-Name: $(BOARDNAME)$(if $(SUBTARGETS),$(if $(SUBTARGET),))'; \
365 echo 'Target-Arch: $(ARCH)'; \
366 echo 'Target-Arch-Packages: $(if $(ARCH_PACKAGES),$(ARCH_PACKAGES),$(ARCH)$(if $(CPU_TYPE),_$(CPU_TYPE))$(if $(CPU_SUBTYPE),_$(CPU_SUBTYPE)))'; \
367 echo 'Target-Features: $(FEATURES)'; \
368 echo 'Target-Depends: $(DEPENDS)'; \
369 echo 'Target-Optimization: $(if $(CFLAGS),$(CFLAGS),$(DEFAULT_CFLAGS))'; \
370 echo 'CPU-Type: $(CPU_TYPE)$(if $(CPU_SUBTYPE),+$(CPU_SUBTYPE))'; \
371 echo 'Linux-Version: $(LINUX_VERSION)'; \
372 $(if $(LINUX_TESTING_VERSION),echo 'Linux-Testing-Version: $(LINUX_TESTING_VERSION)';) \
373 echo 'Linux-Release: $(LINUX_RELEASE)'; \
374 echo 'Linux-Kernel-Arch: $(LINUX_KARCH)'; \
375 $(if $(SUBTARGET),,$(if $(DEFAULT_SUBTARGET), echo 'Default-Subtarget: $(DEFAULT_SUBTARGET)'; )) \
376 echo 'Target-Description:'; \
377 echo "$$$$DESCRIPTION"; \
378 echo '@@'; \
379 echo 'Default-Packages: $(DEFAULT_PACKAGES) $(call extra_packages,$(DEFAULT_PACKAGES))'; \
380 $(DUMPINFO)
381 $(if $(CUR_SUBTARGET),$(SUBMAKE) -r --no-print-directory -C image -s DUMP=1 SUBTARGET=$(CUR_SUBTARGET))
382 $(if $(SUBTARGET),,@$(foreach SUBTARGET,$(SUBTARGETS),$(SUBMAKE) -s DUMP=1 SUBTARGET=$(SUBTARGET); ))
383endef
384
385include $(INCLUDE_DIR)/kernel.mk
386ifeq ($(TARGET_BUILD),1)
387 include $(INCLUDE_DIR)/kernel-build.mk
388 BuildTarget?=$(BuildKernel)
389endif
390
391endif #__target_inc