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