build: simplify building *config targets
[oweals/openwrt.git] / include / toplevel.mk
1 # Makefile for OpenWrt
2 #
3 # Copyright (C) 2007-2012 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 PREP_MK= OPENWRT_BUILD= QUIET=0
10
11 export IS_TTY=$(shell tty -s && echo 1 || echo 0)
12
13 include $(TOPDIR)/include/verbose.mk
14
15 ifeq ($(SDK),1)
16   include $(TOPDIR)/include/version.mk
17 else
18   REVISION:=$(shell $(TOPDIR)/scripts/getver.sh)
19   SOURCE_DATE_EPOCH:=$(shell $(TOPDIR)/scripts/get_source_date_epoch.sh)
20 endif
21
22 HOSTCC ?= $(CC)
23 export REVISION
24 export SOURCE_DATE_EPOCH
25 export GIT_CONFIG_PARAMETERS='core.autocrlf=false'
26 export GIT_ASKPASS:=/bin/true
27 export MAKE_JOBSERVER=$(filter --jobserver%,$(MAKEFLAGS))
28 export GNU_HOST_NAME:=$(shell $(TOPDIR)/scripts/config.guess)
29 export HOST_OS:=$(shell uname)
30 export HOST_ARCH:=$(shell uname -m)
31
32 # prevent perforce from messing with the patch utility
33 unexport P4PORT P4USER P4CONFIG P4CLIENT
34
35 # prevent user defaults for quilt from interfering
36 unexport QUILT_PATCHES QUILT_PATCH_OPTS
37
38 unexport C_INCLUDE_PATH CROSS_COMPILE ARCH
39
40 # prevent distro default LPATH from interfering
41 unexport LPATH
42
43 # make sure that a predefined CFLAGS variable does not disturb packages
44 export CFLAGS=
45 export LDFLAGS=
46
47 empty:=
48 space:= $(empty) $(empty)
49 path:=$(subst :,$(space),$(PATH))
50 path:=$(filter-out .%,$(path))
51 path:=$(subst $(space),:,$(path))
52 export PATH:=$(path)
53
54 unexport TAR_OPTIONS
55
56 ifneq ($(shell $(HOSTCC) 2>&1 | grep clang),)
57   export HOSTCC_REAL?=$(HOSTCC)
58   export HOSTCC_WRAPPER:=$(TOPDIR)/scripts/clang-gcc-wrapper
59 else
60   export HOSTCC_WRAPPER:=$(HOSTCC)
61 endif
62
63 ifeq ($(FORCE),)
64   .config scripts/config/conf scripts/config/mconf: staging_dir/host/.prereq-build
65 endif
66
67 SCAN_COOKIE?=$(shell echo $$$$)
68 export SCAN_COOKIE
69
70 SUBMAKE:=umask 022; $(SUBMAKE)
71
72 ULIMIT_FIX=_limit=`ulimit -n`; [ "$$_limit" = "unlimited" -o "$$_limit" -ge 1024 ] || ulimit -n 1024;
73
74 prepare-mk: staging_dir/host/.prereq-build FORCE ;
75
76 ifdef SDK
77   IGNORE_PACKAGES = linux
78 endif
79
80 _ignore = $(foreach p,$(IGNORE_PACKAGES),--ignore $(p))
81
82 prepare-tmpinfo: FORCE
83         @+$(MAKE) -r -s staging_dir/host/.prereq-build $(PREP_MK)
84         mkdir -p tmp/info
85         $(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f include/scan.mk SCAN_TARGET="packageinfo" SCAN_DIR="package" SCAN_NAME="package" SCAN_DEPTH=5 SCAN_EXTRA=""
86         $(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f include/scan.mk SCAN_TARGET="targetinfo" SCAN_DIR="target/linux" SCAN_NAME="target" SCAN_DEPTH=2 SCAN_EXTRA="" SCAN_MAKEOPTS="TARGET_BUILD=1"
87         for type in package target; do \
88                 f=tmp/.$${type}info; t=tmp/.config-$${type}.in; \
89                 [ "$$t" -nt "$$f" ] || ./scripts/$${type}-metadata.pl $(_ignore) config "$$f" > "$$t" || { rm -f "$$t"; echo "Failed to build $$t"; false; break; }; \
90         done
91         [ tmp/.config-feeds.in -nt tmp/.packageauxvars ] || ./scripts/feeds feed_config > tmp/.config-feeds.in
92         ./scripts/package-metadata.pl mk tmp/.packageinfo > tmp/.packagedeps || { rm -f tmp/.packagedeps; false; }
93         ./scripts/package-metadata.pl pkgaux tmp/.packageinfo > tmp/.packageauxvars || { rm -f tmp/.packageauxvars; false; }
94         touch $(TOPDIR)/tmp/.build
95
96 .config: ./scripts/config/conf $(if $(CONFIG_HAVE_DOT_CONFIG),,prepare-tmpinfo)
97         @+if [ \! -e .config ] || ! grep CONFIG_HAVE_DOT_CONFIG .config >/dev/null; then \
98                 [ -e $(HOME)/.openwrt/defconfig ] && cp $(HOME)/.openwrt/defconfig .config; \
99                 $(_SINGLE)$(NO_TRACE_MAKE) menuconfig $(PREP_MK); \
100         fi
101
102 ifneq ($(DISTRO_PKG_CONFIG),)
103 scripts/config/%onf: export PATH:=$(dir $(DISTRO_PKG_CONFIG)):$(PATH)
104 endif
105 scripts/config/%onf: CFLAGS+= -O2
106 scripts/config/%onf:
107         @$(_SINGLE)$(SUBMAKE) -s -C scripts/config $(notdir $@) CC="$(HOSTCC_WRAPPER)"
108
109 $(eval $(call rdep,scripts/config,scripts/config/mconf))
110
111 config: scripts/config/conf prepare-tmpinfo FORCE
112         [ -L .config ] && export KCONFIG_OVERWRITECONFIG=1; \
113                 $< Config.in
114
115 config-clean: FORCE
116         $(_SINGLE)$(NO_TRACE_MAKE) -C scripts/config clean
117
118 defconfig: scripts/config/conf prepare-tmpinfo FORCE
119         touch .config
120         @if [ ! -s .config -a -e $(HOME)/.openwrt/defconfig ]; then cp $(HOME)/.openwrt/defconfig .config; fi
121         [ -L .config ] && export KCONFIG_OVERWRITECONFIG=1; \
122                 $< --defconfig=.config Config.in
123
124 confdefault-y=allyes
125 confdefault-m=allmod
126 confdefault-n=allno
127 confdefault:=$(confdefault-$(CONFDEFAULT))
128
129 oldconfig: scripts/config/conf prepare-tmpinfo FORCE
130         [ -L .config ] && export KCONFIG_OVERWRITECONFIG=1; \
131                 $< --$(if $(confdefault),$(confdefault),old)config Config.in
132
133 menuconfig: scripts/config/mconf prepare-tmpinfo FORCE
134         if [ \! -e .config -a -e $(HOME)/.openwrt/defconfig ]; then \
135                 cp $(HOME)/.openwrt/defconfig .config; \
136         fi
137         [ -L .config ] && export KCONFIG_OVERWRITECONFIG=1; \
138                 $< Config.in
139
140 xconfig: scripts/config/qconf prepare-tmpinfo FORCE
141         if [ \! -e .config -a -e $(HOME)/.openwrt/defconfig ]; then \
142                 cp $(HOME)/.openwrt/defconfig .config; \
143         fi
144         $< Config.in
145
146 prepare_kernel_conf: .config toolchain/install FORCE
147
148 ifeq ($(wildcard staging_dir/host/bin/quilt),)
149   prepare_kernel_conf:
150         @+$(SUBMAKE) -r tools/quilt/compile
151 else
152   prepare_kernel_conf: ;
153 endif
154
155 kernel_oldconfig: prepare_kernel_conf
156         $(_SINGLE)$(NO_TRACE_MAKE) -C target/linux oldconfig
157
158 ifneq ($(DISTRO_PKG_CONFIG),)
159 kernel_menuconfig: export PATH:=$(dir $(DISTRO_PKG_CONFIG)):$(PATH)
160 kernel_nconfig: export PATH:=$(dir $(DISTRO_PKG_CONFIG)):$(PATH)
161 endif
162 kernel_menuconfig: prepare_kernel_conf
163         $(_SINGLE)$(NO_TRACE_MAKE) -C target/linux menuconfig
164
165 kernel_nconfig: prepare_kernel_conf
166         $(_SINGLE)$(NO_TRACE_MAKE) -C target/linux nconfig
167
168 staging_dir/host/.prereq-build: include/prereq-build.mk
169         mkdir -p tmp
170         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prereq-build.mk prereq 2>/dev/null || { \
171                 echo "Prerequisite check failed. Use FORCE=1 to override."; \
172                 false; \
173         }
174   ifneq ($(realpath $(TOPDIR)/include/prepare.mk),)
175         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prepare.mk prepare 2>/dev/null || { \
176                 echo "Preparation failed."; \
177                 false; \
178         }
179   endif
180         touch $@
181
182 printdb: FORCE
183         @$(_SINGLE)$(NO_TRACE_MAKE) -p $@ V=99 DUMP_TARGET_DB=1 2>&1
184
185 ifndef SDK
186   DOWNLOAD_DIRS = tools/download toolchain/download package/download target/download
187 else
188   DOWNLOAD_DIRS = package/download
189 endif
190
191 download: .config FORCE $(if $(wildcard $(TOPDIR)/staging_dir/host/bin/flock),,tools/flock/compile)
192         @+$(foreach dir,$(DOWNLOAD_DIRS),$(SUBMAKE) $(dir);)
193
194 clean dirclean: .config
195         @+$(SUBMAKE) -r $@
196
197 prereq:: prepare-tmpinfo .config
198         @+$(NO_TRACE_MAKE) -r -s $@
199
200 check: .config FORCE
201         @+$(NO_TRACE_MAKE) -r -s $@ QUIET= V=s
202
203 val.%: FORCE
204         @+$(NO_TRACE_MAKE) -r -s $@ QUIET= V=s
205
206 WARN_PARALLEL_ERROR = $(if $(BUILD_LOG),,$(and $(filter -j,$(MAKEFLAGS)),$(findstring s,$(OPENWRT_VERBOSE))))
207
208 ifeq ($(SDK),1)
209
210 %::
211         @+$(PREP_MK) $(NO_TRACE_MAKE) -r -s prereq
212         @./scripts/config/conf --defconfig=.config Config.in
213         @+$(ULIMIT_FIX) $(SUBMAKE) -r $@
214
215 else
216
217 %::
218         @+$(PREP_MK) $(NO_TRACE_MAKE) -r -s prereq
219         @( \
220                 cp .config tmp/.config; \
221                 ./scripts/config/conf --defconfig=tmp/.config -w tmp/.config Config.in > /dev/null 2>&1; \
222                 if ./scripts/kconfig.pl '>' .config tmp/.config | grep -q CONFIG; then \
223                         printf "$(_R)WARNING: your configuration is out of sync. Please run make menuconfig, oldconfig or defconfig!$(_N)\n" >&2; \
224                 fi \
225         )
226         @+$(ULIMIT_FIX) $(SUBMAKE) -r $@ $(if $(WARN_PARALLEL_ERROR), || { \
227                 printf "$(_R)Build failed - please re-run with -j1 to see the real error message$(_N)\n" >&2; \
228                 false; \
229         } )
230
231 endif
232
233 # update all feeds, re-create index files, install symlinks
234 package/symlinks:
235         ./scripts/feeds update -a
236         ./scripts/feeds install -a
237
238 # re-create index files, install symlinks
239 package/symlinks-install:
240         ./scripts/feeds update -i
241         ./scripts/feeds install -a
242
243 # remove all symlinks, don't touch ./feeds
244 package/symlinks-clean:
245         ./scripts/feeds uninstall -a
246
247 help:
248         cat README
249
250 distclean:
251         rm -rf bin build_dir .config* dl feeds key-build* logs package/feeds package/openwrt-packages staging_dir tmp
252         @$(_SINGLE)$(SUBMAKE) -C scripts/config clean
253
254 ifeq ($(findstring v,$(DEBUG)),)
255   .SILENT: symlinkclean clean dirclean distclean config-clean download help tmpinfo-clean .config scripts/config/mconf scripts/config/conf menuconfig staging_dir/host/.prereq-build tmp/.prereq-package prepare-tmpinfo
256 endif
257 .PHONY: help FORCE
258 .NOTPARALLEL:
259