Makefile: make_parallel_flags via env variable
[vpp.git] / build-root / Makefile
1 # Copyright (c) 2015 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 #
15 # Copyright (c) 2007-2008 Eliot Dresselhaus
16 #
17 #  Permission is hereby granted, free of charge, to any person obtaining
18 #  a copy of this software and associated documentation files (the
19 #  "Software"), to deal in the Software without restriction, including
20 #  without limitation the rights to use, copy, modify, merge, publish,
21 #  distribute, sublicense, and/or sell copies of the Software, and to
22 #  permit persons to whom the Software is furnished to do so, subject to
23 #  the following conditions:
24 #
25 #  The above copyright notice and this permission notice shall be
26 #  included in all copies or substantial portions of the Software.
27 #
28 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 #  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 #  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 #  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 #  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 #  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 #  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #
36
37 ######################################################################
38 # Collect makefile fragments
39 ######################################################################
40
41 # Scripts require non-POSIX parts of bash
42 SHELL := /bin/bash
43
44 # Where this makefile lives
45 MU_BUILD_ROOT_DIR = $(shell pwd)
46 MU_BUILD_NAME = $(shell basename $(MU_BUILD_ROOT_DIR))
47
48 # Search path (e.g. multiple directories) where sources are found.
49 SOURCE_PATH =
50
51 # Pick up user's definitions for variables e.g. SOURCE_PATH, etc.
52 -include build-config.mk
53
54 MU_BUILD_ROOT_NAME = $(shell basename $(MU_BUILD_ROOT_DIR))
55 MU_BUILD_DATA_DIR_NAME = build-data
56
57 ABSOLUTE_SOURCE_PATH = $(foreach d,$(SOURCE_PATH),$(shell cd $(d) && pwd))
58
59 SOURCE_PATH_BUILD_ROOT_DIRS = $(addsuffix /$(MU_BUILD_NAME),$(ABSOLUTE_SOURCE_PATH))
60 SOURCE_PATH_BUILD_DATA_DIRS = $(addsuffix /$(MU_BUILD_DATA_DIR_NAME),$(ABSOLUTE_SOURCE_PATH))
61
62 # For tools use build-root as source path, otherwise use given source path
63 FIND_SOURCE_PATH =                                              \
64   $(if $(is_build_tool),                                        \
65     $(SOURCE_PATH_BUILD_ROOT_DIRS) $(MU_BUILD_ROOT_DIR),        \
66     $(SOURCE_PATH_BUILD_DATA_DIRS))
67
68 # First search given source path, then default to build-root
69 FULL_SOURCE_PATH = $(SOURCE_PATH_BUILD_DATA_DIRS) $(MU_BUILD_ROOT_DIR)
70
71 # Misc functions
72 is_in_fn = $(strip $(filter $(1),$(2)))
73 last_fn = $(lastword $1)
74 chop_fn = $(wordlist 2,$(words $1),x $1)
75 uniq_fn = $(strip $(if $1,$(call uniq_fn,$(call chop_fn,$1)) \
76             $(if $(filter $(call last_fn,$1),$(call chop_fn,$1)),,$(call last_fn,$1))))
77 ifdef3_fn = $(if $(patsubst undefined,,$(origin $(1))),$(3),$(2))
78 ifdef_fn = $(call ifdef3_fn,$(1),$(2),$($(1)))
79
80 _mu_debug = $(warning "$(1) = $($(1))")
81
82 $(foreach d,$(FIND_SOURCE_PATH),                                        \
83   $(eval _mu_package_mk_in_$(d) = $(shell find $(d)/packages/*.mk 2> /dev/null))        \
84   $(eval _mu_srcdirs_in_$(d) =                                          \
85     $(shell find $(d)/..                                                \
86       -maxdepth 1                                                       \
87       -type d                                                           \
88       -and -not -name ".."                                              \
89       -and -not -name $(MU_BUILD_ROOT_NAME)                             \
90       -and -not -name $(MU_BUILD_DATA_DIR_NAME)))                       \
91   $(eval _mu_non_package_files_in_$(d) =                                \
92     $(shell find $(d)/packages                                          \
93       -type f                                                           \
94       -and -not -name '*.mk'                                            \
95       -and -not -name '*~' 2> /dev/null))                               \
96   $(foreach p,$(patsubst %.mk,%,$(notdir $(_mu_package_mk_in_$(d)))),   \
97     $(eval _mu_package_dir_$(p) = $(d))                                 \
98     $(eval _mu_package_mk_$(p) = $(d)/packages/$(p).mk)                 \
99   )                                                                     \
100   $(foreach p,$(notdir $(_mu_srcdirs_in_$(d))),                         \
101     $(eval _mu_package_srcdir_$(p) = $(shell cd $(d)/../$(p) && pwd))   \
102   )                                                                     \
103 )
104
105 # Find root directory for package based on presence of package .mk
106 # makefile fragment on source path.
107 _find_build_data_dir_for_package_fn = $(shell                   \
108   set -eu$(BUILD_DEBUG) ;                                       \
109   for d in $(FIND_SOURCE_PATH) ; do                             \
110     f="$${d}/packages/$(1).mk" ;                                \
111     [[ -f $${f} ]] && echo `cd $${d} && pwd` && exit 0 ;        \
112   done ;                                                        \
113   echo "")
114 find_build_data_dir_for_package_fn = $(call ifdef_fn,_mu_package_dir_$(1),)
115
116 # dir/PACKAGE
117 _find_source_fn = $(shell                               \
118   set -eu$(BUILD_DEBUG) ;                               \
119   d="$(call find_build_data_dir_for_package_fn,$(1))" ; \
120   [[ -n "$${d}" ]] && d="$${d}/../$(1)" ;               \
121   echo "$${d}")
122 find_source_fn = $(call ifdef3_fn,_mu_package_dir_$(1),,$(_mu_package_dir_$(1))/../$(1))
123
124 # Find given FILE in source path as build-data/packages/FILE
125 find_package_file_fn = $(shell                          \
126   set -eu$(BUILD_DEBUG) ;                               \
127   d="$(call find_build_data_dir_for_package_fn,$(1))" ; \
128   [[ -n "$${d}" ]] && d="$${d}/packages/$(2)" ;         \
129   [[ -f "$${d}" ]] && echo "$${d}")
130
131 # Find first FILE in source path with name PATH/build-data/FILE
132 find_build_data_file_fn = $(shell                               \
133   set -eu$(BUILD_DEBUG) ;                                       \
134   for d in $(FIND_SOURCE_PATH) ; do                             \
135     f="$${d}/$(1)" ;                                            \
136     [[ -f $${f} ]] && echo `cd $${d} && pwd`/$(1) && exit 0 ;   \
137   done ;                                                        \
138   echo "")
139
140 ######################################################################
141 # ARCH, PLATFORM
142 ######################################################################
143
144 NATIVE_ARCH = $(shell gcc -dumpmachine | sed -e 's/\([a-zA-Z_0-9]*\)-.*/\1/')
145
146 # Find all platforms.mk that we can, including those from build-root
147 $(foreach d,$(FULL_SOURCE_PATH), \
148   $(eval -include $(d)/platforms.mk))
149
150 # Platform should be defined somewhere by specifying $($(PLATFORM)_arch)
151 ARCH = $(strip $($(PLATFORM)_arch))
152 ifeq ($(ARCH),)
153   $(error "Unknown platform `$(PLATFORM)'")
154 endif
155
156 # map e.g. ppc7450 -> ppc
157 BASIC_ARCH = \
158    ${shell case '$(ARCH)' in \
159       (native) echo $(NATIVE_ARCH) ;; \
160       (i*86*) echo i386 ;; \
161       (ppc*|powerpc*) echo ppc ;; \
162       (*) echo '$(ARCH)' ;; \
163      esac }
164
165 # x86_64 can be either 32/64.  set BIACH=32 to get 32 bit libraries.
166 BIARCH = 64
167
168 aarch64_libdir = 64
169 x86_64_libdir = $(BIARCH)
170 native_libdir = $($(NATIVE_ARCH)_libdir)
171
172 # lib or lib64 depending
173 arch_lib_dir = lib$($(BASIC_ARCH)_libdir)
174
175 # OS to configure for.  configure --host will be set to $(ARCH)-$(OS)
176 # Allow per-platform overrides
177
178 OS = $(strip $($(PLATFORM)_os))
179 ifeq ($(OS),)
180   OS = mu-linux
181 endif
182
183 spu_target = spu
184 native_target =
185
186 is_native = $(if $(ARCH:native=),,true)
187 not_native = $(if $(ARCH:native=),true,)
188
189 ARCH_TARGET_tmp = $(call ifdef_fn,$(ARCH)_target,$(ARCH)-$(OS))
190 TARGET = $(call ifdef_fn,$(PLATFORM)_target,$(ARCH_TARGET_tmp))
191 TARGET_PREFIX = $(if $(not_native),$(TARGET)-,)
192
193 # CPU microarchitecture detection. 
194 # Either set <platform>_march in build-data/platforms/<platform>.mk,
195 # or detect and use the build-host instruction set
196
197 MARCH = $(strip $($(PLATFORM)_march))
198 ifeq ($(MARCH),)
199   ifneq ($(wildcard $(TOOL_INSTALL_DIR)/bin/$(TARGET)-gcc),)
200     TARGET_GCC = $(TOOL_INSTALL_DIR)/bin/$(TARGET)-gcc
201   else ifneq ($(wildcard $(MU_BUILD_ROOT_DIR)/tools/bin/$(TARGET)-gcc),)
202     TARGET_GCC = $(MU_BUILD_ROOT_DIR)/tools/bin/$(TARGET)-gcc
203   endif
204   ifneq ($(TARGET_GCC),)
205     MARCH = $(shell $(TARGET_GCC) -Q --help=target -march=native | grep march | sed -e 's/.*march=[[:space:]]*//')
206   else
207     MARCH = native
208   endif
209 else
210   ifeq ($(MARCH),nehalem)
211     override MARCH = corei7
212   else ifeq ($(MARCH),westmere)
213     override MARCH = corei7
214   else ifeq ($(MARCH),sandybridge)
215     override MARCH = corei7-avx
216   else ifeq ($(MARCH),ivybridge)
217     override MARCH = core-avx-i
218   else ifeq ($(MARCH),haswell)
219     override MARCH = core-avx2
220   endif
221 endif
222 export MARCH
223
224 MTUNE = $(strip $($(PLATFORM)_mtune))
225 ifeq ($(MTUNE),)
226   MTUNE = generic
227 endif
228
229 ######################################################################
230 # Generic build stuff
231 ######################################################################
232
233 # The package we are currently working on
234 PACKAGE = $*
235
236 # Build/install tags.  This lets you have different CFLAGS/CPPFLAGS/LDFLAGS
237 # for e.g. debug versus optimized compiles.  Each tag has its own set of build/install
238 # areas.
239 TAG = 
240 TAG_PREFIX = $(if $(TAG),$(TAG)-)
241
242 # yes you need the space
243 tag_var_with_added_space_fn = $(if $($(TAG)_TAG_$(1)),$($(TAG)_TAG_$(1)) )
244
245 # TAG=debug for debugging
246 debug_TAG_CFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
247                    -fstack-protector-all -fPIC
248 debug_TAG_LDFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
249                    -fstack-protector-all -fPIC
250
251 BUILD_PREFIX_package = build-$(TAG_PREFIX)
252 BUILD_PREFIX_tool = build-tool-$(TAG_PREFIX)
253 INSTALL_PREFIX = install-$(TAG_PREFIX)
254 IMAGES_PREFIX = images-$(TAG_PREFIX)
255
256 # Whether we are building a tool or not
257 tool_or_package_fn = $(if $(is_build_tool),tool,package)
258
259 # Directory where packages are built & installed
260 BUILD_DIR = $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_$(call tool_or_package_fn))$(ARCH)
261 INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)$(ARCH)
262
263 PLATFORM_IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)$(PLATFORM)
264
265 # $(call VAR,DEFAULT)
266 override_var_with_default_fn = $(if $($(1)),$($(1)),$(2))
267
268 # $(call if_directory_exists_fn,D1,D2) returns D1 if it exists else D2
269 define if_directory_exists_fn
270 $(shell if test -d $(1); then echo $(1); else echo $(2); fi)
271 endef
272
273 # $(call if_file_exists_fn,F1,F2) returns F1 if it exists else F2
274 define if_file_exists_fn
275 $(shell if test -f $(1); then echo $(1); else echo $(2); fi)
276 endef
277
278 # Default VAR, package specified override of default PACKAGE_VAR
279 package_var_fn = $(call override_var_with_default_fn,$(1)_$(2),$(1))
280
281 package_build_dir_fn = $(call package_var_fn,$(1),build_dir)
282
283 package_install_dir_fn = \
284   $(if $(is_build_tool),$(TOOL_INSTALL_DIR),$(INSTALL_DIR)/$(call package_build_dir_fn,$(1)))
285
286 PACKAGE_BUILD_DIR = \
287   $(BUILD_DIR)/$(call package_build_dir_fn,$(PACKAGE))
288 PACKAGE_INSTALL_DIR = \
289   $(call package_install_dir_fn,$(PACKAGE))
290
291 # Tools (gcc, binutils, glibc...) are installed here
292 TOOL_INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/tools
293
294 # Target specific tools go here e.g. mu-build/tools/ppc-mu-linux
295 TARGET_TOOL_INSTALL_DIR = $(TOOL_INSTALL_DIR)/$(TARGET)
296
297 # Set BUILD_DEBUG to vx or x enable shell command tracing.
298 BUILD_DEBUG =
299
300 # Message from build system itself (as opposed to make or shell commands)
301 build_msg_fn = echo "@@@@ $(1) @@@@"
302
303 # Allow CCACHE_DIR to be overridden, e.g. in .../build-root/build-config.mk
304 ifeq ($(CCACHE_DIR),)
305   CCACHE_DIR=$(MU_BUILD_ROOT_DIR)/.ccache
306 endif
307
308 # Always prefer our own tools to those installed on system.
309 # Note: ccache-bin must be before tool bin.
310
311 # Removed LD_LIBRARY_PATH from BUILD_ENV (drb, 10/31/17):  
312 # export LD_LIBRARY_PATH=$(TOOL_INSTALL_DIR)/lib64:$(TOOL_INSTALL_DIR)/lib
313 #   Reported to cause trouble. Only of historical interest, since we no longer
314 #   build a full tool chain from source.
315
316 BUILD_ENV =                                                                             \
317     export CCACHE_DIR=$(CCACHE_DIR) ;                                   \
318     export PATH=$(TOOL_INSTALL_DIR)/ccache-bin:$(TOOL_INSTALL_DIR)/bin:$${PATH} ;       \
319     export PATH="`echo $${PATH} | sed -e s/[.]://`" ;                                   \
320     $(if $(not_native),export CONFIG_SITE=$(MU_BUILD_ROOT_DIR)/config.site ;,)  \
321     set -eu$(BUILD_DEBUG) ;                                                             \
322     set -o pipefail
323
324 ######################################################################
325 # Package build generic definitions
326 ######################################################################
327
328 package_dir_fn = \
329   $(call find_build_data_dir_for_package_fn,$(1))/packages
330
331 package_mk_fn = $(call package_dir_fn,$(1))/$(1).mk
332
333 # Pick up built-root/pre-package-include.mk for all source directories
334 $(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),     \
335   $(eval -include $(d)/pre-package-include.mk))
336
337 $(foreach d,$(addsuffix /packages,$(FIND_SOURCE_PATH)),                 \
338   $(eval -include $(d)/*.mk)                                            \
339   $(eval ALL_PACKAGES += $(patsubst $(d)/%.mk,%,$(wildcard $(d)/*.mk))) \
340 )
341
342 # Pick up built-root/post-package-include.mk for all source directories
343 $(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),     \
344   $(eval -include $(d)/post-package-include.mk))
345
346 # Linux specific native build tools
347 NATIVE_TOOLS_LINUX =                            \
348   e2fsimage                                     \
349   e2fsprogs                                     \
350   fakeroot                                      \
351   jffs2                                         \
352   mkimage                                       \
353   zlib                                          \
354   xz                                            \
355   squashfs
356
357 IS_LINUX = $(if $(findstring no,$($(PLATFORM)_uses_linux)),no,yes)
358
359 NATIVE_TOOLS_$(IS_LINUX) += $(NATIVE_TOOLS_LINUX)
360
361 # only build glibc for linux installs
362 CROSS_TOOLS_$(IS_LINUX) += glibc gcc
363
364 # must be first for bootstrapping
365 NATIVE_TOOLS = findutils make
366
367 # basic tools needed for build system
368 NATIVE_TOOLS += git automake autoconf libtool texinfo bison flex tar
369
370 # needed to compile gcc
371 NATIVE_TOOLS += mpfr gmp mpc
372
373 # Tool to sign binaries
374 NATIVE_TOOLS += sign
375
376 # ccache
377 NATIVE_TOOLS += ccache
378
379 # Tools needed on native host to build for platform
380 NATIVE_TOOLS += $(call ifdef_fn,$(PLATFORM)_native_tools,)
381
382 # Tools for cross-compiling from native -> ARCH
383 CROSS_TOOLS = binutils gcc-bootstrap gdb
384
385 # Tools needed on native host to build for platform
386 CROSS_TOOLS += $(call ifdef_fn,$(PLATFORM)_cross_tools,)
387
388 NATIVE_TOOLS += $(NATIVE_TOOLS_yes)
389 CROSS_TOOLS += $(CROSS_TOOLS_yes)
390
391 timestamp_name_fn = .mu_build_$(1)_timestamp
392 CONFIGURE_TIMESTAMP = $(call timestamp_name_fn,configure)
393 BUILD_TIMESTAMP = $(call timestamp_name_fn,build)
394 INSTALL_TIMESTAMP = $(call timestamp_name_fn,install)
395
396 TIMESTAMP_DIR = $(PACKAGE_BUILD_DIR)
397
398 find_newer_files_fn =                                           \
399   "`for i in $(2) ; do                                          \
400       [[ -f $$i && $$i -nt $(1) ]] && echo "$$i" && exit 0;     \
401     done ;                                                      \
402     exit 0;`"
403
404 find_filter = -not -name '*~'
405 find_filter += -and -not -path '*/.git*'
406 find_filter += -and -not -path '*/.svn*'
407 find_filter += -and -not -path '*/.CVS*'
408 find_filter += -and -not -path '*/manual/*'
409 find_filter += -and -not -path '*/autom4te.cache/*'
410 find_filter += -and -not -path '*/doc/all-cfg.texi'
411 find_filter += -and -not -path '*/.mu_build_*'
412
413 find_newer_filtered_fn =                        \
414   (! -f $(1)                                    \
415     || -n $(call find_newer_files_fn,$(1),$(3)) \
416     || -n "`find -H $(2)                        \
417               -type f                           \
418               -and -newer $(1)                  \
419               -and \( $(4) \)                   \
420               -print -quit`")
421
422 find_newer_fn =                                                 \
423   $(call find_newer_filtered_fn,$(1),$(2),$(3),$(find_filter))
424
425 ######################################################################
426 # Package dependencies
427 ######################################################################
428
429 # This must come before %-configure, %-build, %-install pattern rules
430 # or else dependencies will not work.
431
432 package_dependencies_fn =                               \
433   $(patsubst %-install, %,                              \
434     $(filter %-install,$($(1)_configure_depend)))
435
436 PACKAGE_DEPENDENCIES = $(call package_dependencies_fn,$(PACKAGE))
437
438 # package specific configure, build, install dependencies
439 add_package_dependency_fn = \
440   $(if $($(1)_$(2)_depend), \
441        $(eval $(1)-$(2) : $($(1)_$(2)_depend)))
442
443 $(foreach p,$(ALL_PACKAGES), \
444     $(call add_package_dependency_fn,$(p),configure) \
445     $(call add_package_dependency_fn,$(p),build) \
446     $(call add_package_dependency_fn,$(p),install))
447
448 TARGETS_RESPECTING_DEPENDENCIES = image_install wipe diff push-all pull-all find-source
449
450 # carry over packages dependencies to image install, wipe, pull-all, push-all
451 $(foreach p,$(ALL_PACKAGES),                                                    \
452   $(if $($(p)_configure_depend),                                                \
453     $(foreach s,$(TARGETS_RESPECTING_DEPENDENCIES),                             \
454       $(eval $(p)-$(s):                                                         \
455              $(addsuffix -$(s), $(call package_dependencies_fn,$(p)))))))
456
457 # recursively resolve dependencies
458 resolve_dependencies2_fn = $(strip                                      \
459   $(eval __added = $(filter-out $(4),                                   \
460     $(call uniq_fn,                                                     \
461       $(foreach l,$(3),                                                 \
462        $(call ifdef3_fn,$(l)$(1),,$(call $(2),$($(l)$(1))))             \
463       ))))                                                              \
464   $(eval __known = $(call uniq_fn,$(4) $(3) $(__added)))                \
465   $(if $(__added),                                                      \
466     $(call resolve_dependencies2_fn,$(1),$(2),$(__added),$(__known)),   \
467     $(__known))                                                         \
468 )
469
470 resolve_dependencies_null_fn = $(1)
471
472 resolve_dependencies_fn = $(call resolve_dependencies2_fn,$(1),resolve_dependencies_null_fn,$(2))
473
474 ######################################################################
475 # Package configure
476 ######################################################################
477
478 # x86_64 can be either 32/64.  set BIACH=32 to get 32 bit libraries.
479 BIARCH = 64
480
481 x86_64_libdir = $(BIARCH)
482 native_libdir = $($(NATIVE_ARCH)_libdir)
483
484 # lib or lib64 depending
485 arch_lib_dir = lib$($(BASIC_ARCH)_libdir)
486
487 # find dynamic linker as absolute path
488 TOOL_INSTALL_LIB_DIR=$(TOOL_INSTALL_DIR)/$(TARGET)/$(arch_lib_dir)
489 DYNAMIC_LINKER=${shell cd $(TOOL_INSTALL_LIB_DIR); echo ld*.so.*}
490
491 # Pad dynamic linker & rpath so elftool will never have to change ELF section sizes.
492 # Yes, this is a kludge.
493 lots_of_slashes_to_pad_names = "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"
494
495 # When PLATFORM != native we *always* use our own versions of GLIBC and dynamic linker
496 # Allow per-platform overrides
497 CROSS_LDFLAGS = $(strip $($(PLATFORM)_cross_ldflags))
498 ifeq ($(CROSS_LDFLAGS),)
499   CROSS_LDFLAGS =                                                                                       \
500     -Wl,--dynamic-linker=$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)/$(DYNAMIC_LINKER)       \
501     -Wl,-rpath -Wl,$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)
502 endif
503
504 cross_ldflags = $(if $(is_native)$(is_build_tool),,$(CROSS_LDFLAGS) )
505
506 # $(call installed_libs_fn,PACKAGE)
507 # Return install library directory for given package.
508 # Some packages (e.g. openssl) don't install under lib64; instead they use lib
509 define installed_lib_fn
510 $(call if_directory_exists_fn,
511   $(call package_install_dir_fn,$(1))/$(arch_lib_dir),
512   $(call package_install_dir_fn,$(1))/lib)
513 endef
514
515 # Set -L and rpath to point to dependent libraries previously built by us.
516 installed_libs_fn =                                     \
517   $(foreach i,$(1),                                     \
518     -L$(call installed_lib_fn,$(i))                     \
519     -Wl,-rpath -Wl,$(call installed_lib_fn,$(i)))
520
521 # As above for include files
522 installed_include_fn = $(call package_install_dir_fn,$(1))/include
523
524 installed_includes_fn = $(foreach i,$(1),-I$(call installed_include_fn,$(i)))
525
526 # By default package CPPFLAGS (to set include path -I) and LDFLAGS (to set link path -L)
527 # point at dependent install directories.
528 DEFAULT_CPPFLAGS = $(call installed_includes_fn, $(PACKAGE_DEPENDENCIES))
529 DEFAULT_LDFLAGS = $(call installed_libs_fn, $(PACKAGE_DEPENDENCIES))
530
531 configure_var_fn = \
532   $(call tag_var_with_added_space_fn,$(1))$(call override_var_with_default_fn,$(PACKAGE)_$(1),$(DEFAULT_$(1)))
533 configure_ldflags_fn = \
534   $(cross_ldflags)$(call configure_var_fn,LDFLAGS)
535
536 # Allow packages to override CPPFLAGS, CFLAGS, and LDFLAGS
537 CONFIGURE_ENV =                                                         \
538     $(if $(call configure_var_fn,CPPFLAGS),                             \
539          CPPFLAGS="$(CPPFLAGS) $(call configure_var_fn,CPPFLAGS)")      \
540     $(if $(call configure_var_fn,CFLAGS),                               \
541          CFLAGS="$(CFLAGS) $(call configure_var_fn,CFLAGS)")            \
542     $(if $(call configure_var_fn,CCASFLAGS),                            \
543          CCASFLAGS="$(CCASFLAGS) $(call configure_var_fn,CCASFLAGS)")   \
544     $(if $(call configure_ldflags_fn),                                  \
545          LDFLAGS="$(LDFLAGS) $(call configure_ldflags_fn)")             \
546     $(if $($(PACKAGE)_configure_env),$($(PACKAGE)_configure_env))
547
548 # only partially used now (used in a few .mk files)
549 ifeq ($(is_build_tool),yes)
550 prefix     = $(PACKAGE_INSTALL_DIR)
551 libdir     = $(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)
552 libexecdir = $(PACKAGE_INSTALL_DIR)/usr/libexec
553 DESTDIR     = /
554 else
555 # Eventually simplify this with no per package DESTDIR or prefix
556 ppdMacro = $(if $(PER_PACKAGE_DESTDIR),$(call package_build_dir_fn,$(1)))
557 pppMacro = $(if $(PER_PACKAGE_PREFIX),$(call package_build_dir_fn,$(1)))
558 prefixMacro     = $($(PLATFORM)_PREFIX_BASE)/$(pppMacro)
559 prefix = $(call prefixMacro,$(PACKAGE))
560 libdir     = $($(PLATFORM)_LIBDIR)
561 libexecdir = $($(PLATFORM)_LIBEXECDIR)
562 destdirMacro  = $($(PLATFORM)_DESTDIR_BASE)$(ppdMacro)
563 DESTDIR  = $(call destdirMacro,$(PACKAGE))
564 endif
565
566 configure_package_gnu =                                         \
567   s=$(call find_source_fn,$(PACKAGE_SOURCE))$(PACKAGE_SUBDIR) ; \
568   if [ ! -f $$s/configure ] ; then                              \
569     autoreconf -i -f $$s ;                                      \
570   fi ;                                                          \
571   cd $(PACKAGE_BUILD_DIR) ;                                     \
572   env $(CONFIGURE_ENV)                                          \
573     $$s/configure                                               \
574       $(if $($(PACKAGE)_configure_host_and_target),             \
575            $($(PACKAGE)_configure_host_and_target),             \
576            $(if $(not_native),--host=$(TARGET),))               \
577       $(if $($(PACKAGE)_configure_prefix),                      \
578            $($(PACKAGE)_configure_prefix),                      \
579            --libdir=$(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)      \
580            --prefix=$(PACKAGE_INSTALL_DIR))                     \
581       $($(PACKAGE)_configure_args)                              \
582       $($(PACKAGE)_configure_args_$(PLATFORM))
583
584 configure_package =                                                     \
585   $(call build_msg_fn,Configuring $(PACKAGE) in $(PACKAGE_BUILD_DIR)) ; \
586   mkdir -p $(PACKAGE_BUILD_DIR) ;                                       \
587   $(if $($(PACKAGE)_configure),                                         \
588        $($(PACKAGE)_configure),                                         \
589        $(configure_package_gnu))
590
591 # Tools (e.g. gcc, binutils, gdb) required a platform to build for
592 check_platform =                                                                \
593   is_tool="$(is_build_tool)" ;                                                  \
594   is_cross_package="$(findstring $(PACKAGE),$(CROSS_TOOLS))" ;                  \
595   is_arch_native="$(if $(subst native,,$(ARCH)),,yes)" ;                        \
596   if [ "$${is_tool}" == "yes"                                                   \
597        -a "$${is_cross_package}" != ""                                          \
598        -a "$${is_arch_native}" != "" ]; then                                    \
599     $(call build_msg_fn,You must specify PLATFORM for building tools) ;         \
600     exit 1 ;                                                                    \
601   fi ;                                                                          \
602   : check that platform gcc can be found ;                                      \
603   target_gcc=gcc ;                                                              \
604   if [ "$${is_arch_native}" != "yes" ] ; then                                   \
605     target_gcc=$(TARGET)-gcc ;                                                  \
606   fi ;                                                                          \
607   if [ "$${is_tool}" != "yes"                                                   \
608        -a "$${is_arch_native}" != "yes"                                         \
609        -a ! -x "`which 2> /dev/null $${target_gcc}`" ] ; then                   \
610     $(call build_msg_fn,                                                        \
611            No cross-compiler found for platform $(PLATFORM) target $(TARGET);   \
612              try make PLATFORM=$(PLATFORM) install-tools) ;                     \
613     exit 1 ;                                                                    \
614   fi
615     
616 configure_check_timestamp =                                             \
617   @$(BUILD_ENV) ;                                                       \
618   $(check_platform) ;                                                   \
619   mkdir -p $(PACKAGE_BUILD_DIR) ;                                       \
620   mkdir -p $(PACKAGE_INSTALL_DIR) ;                                     \
621   conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;                      \
622   dirs="$(call package_mk_fn,$(PACKAGE))                                \
623         $(wildcard $(call find_source_fn,                               \
624                      $(PACKAGE_SOURCE))$(PACKAGE_SUBDIR)/configure)     \
625        $(MU_BUILD_ROOT_DIR)/config.site" ;                              \
626   if [[ $(call find_newer_fn, $${conf}, $${dirs}, $?) ]]; then          \
627     $(configure_package) ;                                              \
628     touch $${conf} ;                                                    \
629   else                                                                  \
630     $(call build_msg_fn,Configuring $(PACKAGE): nothing to do) ;        \
631   fi
632
633 .PHONY: %-configure
634 %-configure: %-find-source
635         $(configure_check_timestamp)
636
637 ######################################################################
638 # Package build
639 ######################################################################
640
641 # /proc/cpuinfo does not exist on platforms without a /proc and on some
642 # platforms, notably inside containers, it has no content. In those cases
643 # we assume there's 1 processor; we use 2*ncpu for the -j option.
644 # NB: GNU Make 4.2 will let us use '$(file </proc/cpuinfo)' to both test
645 # for file presence and content; for now this will have to do.
646 MAKE_PARALLEL_JOBS = -j $(if $(shell [ -f /proc/cpuinfo ] && head /proc/cpuinfo), \
647         $(shell grep -c ^processor /proc/cpuinfo), 2)
648 MAKE_PARALLEL_FLAGS ?= $(if $($(PACKAGE)_make_parallel_fails),,$(MAKE_PARALLEL_JOBS))
649
650 # Make command shorthand for packages & tools.
651 PACKAGE_MAKE =                                  \
652   $(MAKE)                                       \
653     -C $(PACKAGE_BUILD_DIR)                     \
654     $($(PACKAGE)_make_args)                     \
655     $(MAKE_PARALLEL_FLAGS)
656
657 build_package =                                                 \
658   $(call build_msg_fn,Building $* in $(PACKAGE_BUILD_DIR)) ;    \
659   mkdir -p $(PACKAGE_BUILD_DIR) ;                               \
660   cd $(PACKAGE_BUILD_DIR) ;                                     \
661   $(if $($(PACKAGE)_build),                                     \
662        $($(PACKAGE)_build),                                     \
663        $(PACKAGE_MAKE))
664
665 build_check_timestamp =                                                                 \
666   @$(BUILD_ENV) ;                                                                       \
667   comp="$(TIMESTAMP_DIR)/$(BUILD_TIMESTAMP)" ;                                          \
668   conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;                                      \
669   dirs="$(call find_source_fn,$(PACKAGE_SOURCE))                                        \
670         $($(PACKAGE)_build_timestamp_depends)                                           \
671         $(if $(is_build_tool),,$(addprefix $(INSTALL_DIR)/,$(PACKAGE_DEPENDENCIES)))" ; \
672   if [[ $${conf} -nt $${comp}                                                           \
673         || $(call find_newer_fn, $${comp}, $${dirs}, $?) ]]; then                       \
674     $(build_package) ;                                                                  \
675     touch $${comp} ;                                                                    \
676   else                                                                                  \
677     $(call build_msg_fn,Building $(PACKAGE): nothing to do) ;                           \
678   fi
679
680 .PHONY: %-build
681 %-build: %-configure
682         $(build_check_timestamp)
683
684 .PHONY: %-rebuild
685 %-rebuild: %-wipe %-build
686         @ :
687
688 ######################################################################
689 # Package install
690 ######################################################################
691
692 install_package =                                                               \
693     : by default, for non-tools, remove any previously installed bits ;         \
694     $(if $(is_build_tool)$($(PACKAGE)_keep_instdir),                            \
695          true,                                                                  \
696          rm -rf $(PACKAGE_INSTALL_DIR));                                        \
697     mkdir -p $(PACKAGE_INSTALL_DIR) ;                                           \
698     $(if $($(PACKAGE)_pre_install),$($(PACKAGE)_pre_install),true);             \
699     $(if $($(PACKAGE)_install),                                                 \
700          $($(PACKAGE)_install),                                                 \
701          $(PACKAGE_MAKE)                                                        \
702             $($(PACKAGE)_install_args)                                          \
703             install) ;                                                          \
704     $(if $($(PACKAGE)_post_install),$($(PACKAGE)_post_install),true)
705
706 install_check_timestamp =                                       \
707   @$(BUILD_ENV) ;                                               \
708   inst=$(TIMESTAMP_DIR)/$(INSTALL_TIMESTAMP) ;                  \
709   dirs="$(PACKAGE_BUILD_DIR)                                    \
710         $($(PACKAGE)_install_dependencies)" ;                   \
711   if [[ $(call find_newer_fn, $${inst}, $${dirs}, $?) ]]; then  \
712     $(call build_msg_fn,Installing $(PACKAGE)) ;                \
713     $(install_package) ;                                        \
714     touch $${inst} ;                                            \
715   else                                                          \
716     $(call build_msg_fn,Installing $(PACKAGE): nothing to do) ; \
717   fi
718
719 .PHONY: %-install
720 %-install: %-build
721         $(install_check_timestamp)
722
723 ######################################################################
724 # Source code managment
725 ######################################################################
726
727 GIT = git
728
729 # Maps package name to source directory root.
730 # Multiple packages may use a single source tree.
731 # For example, gcc-bootstrap package shares gcc source.
732 PACKAGE_SOURCE = $(if $($(PACKAGE)_source),$($(PACKAGE)_source),$(PACKAGE))
733 PACKAGE_SUBDIR = $(if $($(PACKAGE)_configure_subdir),/$($(PACKAGE)_configure_subdir),)
734
735 # Use git to download source if directory is not found
736 find_source_for_package =                                                                       \
737   @$(BUILD_ENV) ;                                                                               \
738   $(call build_msg_fn,Arch for platform '$(PLATFORM)' is $(ARCH)) ;                             \
739   $(call build_msg_fn,Finding source for $(PACKAGE)) ;                                          \
740   s="$(call find_source_fn,$(PACKAGE_SOURCE))" ;                                                \
741   [[ -z "$${s}" ]]                                                                              \
742     && $(call build_msg_fn,Package $(PACKAGE) not found with path $(SOURCE_PATH))               \
743     && exit 1;                                                                                  \
744   mk="$(call find_build_data_dir_for_package_fn,$(PACKAGE_SOURCE))/packages/$(PACKAGE).mk";     \
745   $(call build_msg_fn,Makefile fragment found in $${mk}) ;                                      \
746   if [ ! -d "$${s}" ] ; then                                                                    \
747     d=`dirname $${mk}` ;                                                                        \
748     i=`cd $${d}/.. && ($(GIT) config remote.origin.url ||                                       \
749                     awk '/URL/ { print $$2; }' .git/remotes/origin)`;                           \
750     g=`dirname $${i}` ;                                                                         \
751     $(call build_msg_fn,Fetching source: $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s) ;            \
752     if ! $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s; then                                         \
753       $(call build_msg_fn,No source for $(PACKAGE) in $${g});                                   \
754       exit 1;                                                                                   \
755     fi ;                                                                                        \
756     $(call build_msg_fn,Fix file dates in $${g}/$(PACKAGE_SOURCE)) ;                                    \
757     (cd $${s} ; $(MU_BUILD_ROOT_DIR)/autowank --touch) ;                                        \
758   fi ;                                                                                          \
759   s=`cd $${s} && pwd` ;                                                                         \
760   $(call build_msg_fn,Source found in $${s})
761
762 .PHONY: %-find-source
763 %-find-source:
764         $(find_source_for_package)
765
766 .PHONY: %-push %-pull %-push-all %-pull-all
767 %-push %-pull %-push-all %-pull-all:
768         @$(BUILD_ENV) ;                                                         \
769         push_or_pull=$(patsubst %-all,%,$(subst $(PACKAGE)-,,$@)) ;             \
770         $(call build_msg_fn,Git $${push_or_pull} source for $(PACKAGE)) ;       \
771         s=$(call find_source_fn,$(PACKAGE_SOURCE)) ;                            \
772         if [ "x$$s" = "x" ]; then                                               \
773              $(call build_msg_fn,No source for $(PACKAGE)) ;                    \
774              exit 1;                                                            \
775         fi ;                                                                    \
776         cd $$s && $(GIT) $${push_or_pull}
777
778 # Pull all packages for platform
779 .PHONY: pull-all
780 pull-all:
781         @$(BUILD_ENV) ;                                                         \
782         $(call build_msg_fn,Git pull build system) ;                            \
783         for d in $(MU_BUILD_ROOT_DIR)                                           \
784                  $(SOURCE_PATH_BUILD_ROOT_DIRS)                                 \
785                  $(SOURCE_PATH_BUILD_DATA_DIRS); do                             \
786           $(call build_msg_fn,Git pull $${d}) ;                                 \
787           pushd $${d} >& /dev/null && $(GIT) pull && popd >& /dev/null ;        \
788         done ;                                                                  \
789         $(call build_msg_fn,Git pull build tools) ;                             \
790         $(call tool_make_target_fn,pull-all) ;                                  \
791         $(call build_msg_fn,Git pull packages for platform $(PLATFORM)) ;       \
792         make PLATFORM=$(PLATFORM) $(patsubst %,%-pull-all,$(ROOT_PACKAGES))
793
794 .PHONY: %-diff
795 %-diff:                                                                 
796         @$(BUILD_ENV) ;                                                 \
797         d=$(call find_source_fn,$(PACKAGE_SOURCE)) ;                    \
798         $(call build_msg_fn,Git diff $(PACKAGE)) ;                      \
799         if [ -d $${d}/.git ] ; then                                     \
800          cd $${d} && $(GIT) --no-pager diff 2>/dev/null;                \
801         else                                                            \
802          $(call build_msg_fn, $(PACKAGE) not a git directory) ;         \
803         fi
804             
805
806
807 # generate diffs for everything in source path
808 .PHONY: diff-all
809 diff-all:                                                               
810         @$(BUILD_ENV) ;                                                 \
811         $(call build_msg_fn,Generate diffs) ;                           \
812         for r in $(ABSOLUTE_SOURCE_PATH); do                            \
813           for d in $${r}/* ; do                                         \
814             if [ -d $${d} ] ; then                                      \
815               $(call build_msg_fn,Git diff $${d}) ;                     \
816               if [ -d $${d}/.git ] ; then                               \
817                 cd $${d} && $(GIT) --no-pager diff 2>/dev/null;         \
818               else                                                      \
819                 $(call build_msg_fn, $${d} not a git directory) ;       \
820               fi ;                                                      \
821             fi ;                                                        \
822           done ;                                                        \
823         done
824
825 ######################################################################
826 # System images
827 ######################################################################
828
829 IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/image-$(PLATFORM)
830
831 # Reports shared libraries in given directory
832 find_shared_libs_fn =                           \
833   find $(1)                                     \
834     -maxdepth 1                                 \
835        -regex '.*/lib[a-z0-9_]+\+?\+?.so'               \
836     -o -regex '.*/lib[a-z0-9_]+-[0-9.]+\+?\+?.so'       \
837     -o -regex '.*/lib[a-z0-9_]+\+?\+?.so.[0-9.]+'
838
839 # By default pick up files from binary directories and /etc.
840 # Also include shared libraries.
841 DEFAULT_IMAGE_INCLUDE =                                 \
842   for d in bin sbin libexec                             \
843            usr/bin usr/sbin usr/libexec                 \
844            etc; do                                      \
845     [[ -d $$d ]] && echo $$d;                           \
846   done ;                                                \
847   [[ -d $(arch_lib_dir) ]]                              \
848     && $(call find_shared_libs_fn,$(arch_lib_dir))
849
850 # Define any shell functions needed by install scripts
851 image_install_functions =                       \
852   $(foreach p,$(ALL_PACKAGES),                  \
853     $(if $($(p)_image_install_functions),       \
854          $($(p)_image_install_functions)))
855
856 # Should always be over-written by temp dir in %-root-image rule
857 IMAGE_INSTALL_DIR = $(error you need to set IMAGE_INSTALL_DIR)
858
859 image_install_fn =                                                              \
860   @$(BUILD_ENV) ;                                                               \
861   $(call build_msg_fn,Image-install $(1) for platform $(PLATFORM)) ;            \
862   inst_dir=$(IMAGE_INSTALL_DIR) ;                                               \
863   mkdir -p $${inst_dir} ;                                                       \
864   cd $(2) ;                                                                     \
865   : select files to include in image ;                                          \
866   image_include_files="                                                         \
867     `$(call ifdef_fn,$(1)_image_include,$(DEFAULT_IMAGE_INCLUDE)) ;             \
868      echo "" ;                                                                  \
869      exit 0 ; `";                                                               \
870   : select files regexps to exclude from image ;                                \
871   image_exclude_files="" ;                                                      \
872   if [ ! -z "$($(1)_image_exclude)" ] ; then                                    \
873     image_exclude_files="${image_exclude_files}                                 \
874                          $(patsubst %,--exclude=%,$($(1)_image_exclude))" ;     \
875   fi ;                                                                          \
876   [[ -z "$${image_include_files}" || $${image_include_files} == " " ]]          \
877     || tar cf - $${image_include_files} $${image_exclude_files}                 \
878        | tar xf - -C $${inst_dir} ;                                             \
879   : copy files from copyimg directories on source path if present ;             \
880   for build_data_dir in $(SOURCE_PATH_BUILD_DATA_DIRS) ; do                     \
881     d="$${build_data_dir}/packages/$(1).copyimg" ;                              \
882     if [ -d "$${d}" ] ; then                                                    \
883       env $($(PLATFORM)_copyimg_env)                                            \
884         $(MU_BUILD_ROOT_DIR)/copyimg $${d} $${inst_dir} ;                       \
885     fi ;                                                                        \
886   done ;                                                                        \
887   : run package dependent install script ;                                      \
888   $(if $($(1)_image_install),                                                   \
889        $(image_install_functions)                                               \
890        cd $${inst_dir} ;                                                        \
891        $($(1)_image_install))
892
893 .PHONY: %-image_install
894 %-image_install: %-install
895         $(call image_install_fn,$(PACKAGE),$(PACKAGE_INSTALL_DIR))
896
897 basic_system_image_include =                                    \
898   $(call ifdef_fn,$(PLATFORM)_basic_system_image_include,       \
899   echo bin/ldd ;                                                \
900   echo $(arch_lib_dir)/ld*.so* ;                                \
901   $(call find_shared_libs_fn, $(arch_lib_dir)))
902
903 basic_system_image_install =                            \
904   mkdir -p bin lib mnt proc root sbin sys tmp etc ;     \
905   mkdir -p usr usr/{bin,sbin} usr/lib ;                 \
906   mkdir -p var var/{lib,lock,log,run,tmp} ;             \
907   mkdir -p var/lock/subsys var/lib/urandom 
908
909 .PHONY: basic_system-image_install
910 basic_system-image_install: # linuxrc-install
911         $(if $(not_native),                                                     \
912              $(call image_install_fn,basic_system,$(TARGET_TOOL_INSTALL_DIR)),)
913
914 ROOT_PACKAGES = $(if $($(PLATFORM)_root_packages),$($(PLATFORM)_root_packages),$(default_root_packages))
915
916 .PHONY: install-packages
917 install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES))  
918         @$(BUILD_ENV) ;                                                         \
919         set -eu$(BUILD_DEBUG) ;                                                 \
920         d=$(MU_BUILD_ROOT_DIR)/packages-$(PLATFORM) ;                           \
921         rm -rf $${d} ;                                                          \
922         mkdir -p $${d};                                                         \
923         $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${d}                 \
924             $(patsubst %,%-image_install,                                       \
925               basic_system                                                      \
926               $(ROOT_PACKAGES)) || exit 1;                                      \
927         $(call build_msg_fn, Relocating ELF executables to run in $${d}) ;      \
928         find $${d} -type f                                                      \
929             -exec elftool quiet in '{}' out '{}'                                \
930                 set-interpreter                                                 \
931                     $${d}/$(arch_lib_dir)/$(DYNAMIC_LINKER)                     \
932                 set-rpath $${d}/$(arch_lib_dir):$${d}/lib ';' ;                 \
933         : strip symbols from files ;                                            \
934         if [ $${strip_symbols:-no} = 'yes' ] ; then                             \
935             $(call build_msg_fn, Stripping symbols from files) ;                \
936             find $${d} -type f                                                  \
937                 -exec                                                           \
938                   $(TARGET_PREFIX)strip                                         \
939                     --strip-unneeded '{}' ';'                                   \
940                     >& /dev/null ;                                              \
941         else                                                                    \
942             $(call build_msg_fn, NOT stripping symbols) ;                       \
943         fi 
944
945 # readonly root squashfs image
946 # Note: $(call build_msg_fn) does not seem to work inside of fakeroot so we use echo
947 .PHONY: ro-image
948 $(PLATFORM_IMAGE_DIR)/ro.img ro-image: $(patsubst %,%-find-source,$(ROOT_PACKAGES))
949         @$(BUILD_ENV) ;                                                 \
950         d=$(PLATFORM_IMAGE_DIR) ;                                       \
951         mkdir -p $$d;                                                   \
952         ro_image=$$d/ro.img ;                                           \
953         rm -f $${ro_image} ;                                            \
954         tmp_dir="`mktemp -d $$d/ro-image-XXXXXX`" ;                     \
955         chmod 0755 $${tmp_dir} ;                                        \
956         cd $${tmp_dir} ;                                                \
957         trap "rm -rf $${tmp_dir}" err ;                                 \
958         fakeroot /bin/bash -c "{                                        \
959           set -eu$(BUILD_DEBUG) ;                                       \
960           $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${tmp_dir} \
961             $(patsubst %,%-image_install,                               \
962               basic_system                                              \
963               $(ROOT_PACKAGES)) ;                                       \
964           : make dev directory ;                                        \
965           $(linuxrc_makedev) ;                                          \
966           echo @@@@ Relocating ELF executables to run in / @@@@ ;       \
967           find $${d} -type f                                            \
968               -exec elftool quiet in '{}' out '{}'                      \
969                 set-interpreter                                         \
970                     /$(arch_lib_dir)/$(DYNAMIC_LINKER)                  \
971                 set-rpath /$(arch_lib_dir):/lib ';' ;                   \
972           : strip symbols from files ;                                  \
973           if [ '$${strip_symbols:-yes}' = 'yes' ] ; then                \
974               echo @@@@ Stripping symbols from files @@@@ ;             \
975               find $${tmp_dir} -type f                                  \
976                 -exec                                                   \
977                   $(TARGET_PREFIX)strip                                 \
978                     --strip-unneeded '{}' ';'                           \
979                     >& /dev/null ;                                      \
980           else                                                          \
981               echo @@@@ NOT stripping symbols @@@@ ;                    \
982           fi ;                                                          \
983           if [ $${sign_executables:-yes} = 'yes'                        \
984                -a -n "$($(PLATFORM)_public_key)" ] ; then               \
985               echo @@@@ Signing executables @@@@ ;                      \
986               find $${tmp_dir} -type f                                  \
987                 | xargs sign $($(PLATFORM)_public_key)                  \
988                              $($(PLATFORM)_private_key_passphrase) ;    \
989           fi ;                                                          \
990           : make read-only file system ;                                \
991           mksquashfs                                                    \
992             $${tmp_dir} $${ro_image}                                    \
993             -no-exports -no-progress -no-recovery ;                     \
994         }" ;                                                            \
995         : cleanup tmp directory ;                                       \
996         rm -rf $${tmp_dir}
997
998 MKFS_JFFS2_BYTE_ORDER_x86_64 = -l
999 MKFS_JFFS2_BYTE_ORDER_i686 = -l
1000 MKFS_JFFS2_BYTE_ORDER_ppc = -b
1001 MKFS_JFFS2_BYTE_ORDER_mips = -b
1002 MKFS_JFFS2_BYTE_ORDER_native = $(MKFS_JFFS2_BYTE_ORDER_$(NATIVE_ARCH))
1003
1004 MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES = \
1005   $(call ifdef_fn,$(PLATFORM)_jffs2_sector_size_in_kbytes,256)
1006
1007 mkfs_fn_jffs2 = mkfs.jffs2                              \
1008   --eraseblock=$(MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES)KiB   \
1009   --root=$(1) --output=$(2)                             \
1010   $(MKFS_JFFS2_BYTE_ORDER_$(BASIC_ARCH))
1011
1012 # As things stand the actual initrd size parameter
1013 # is set in .../open-repo/build-data/packages/linuxrc.mk.
1014 EXT2_RW_IMAGE_SIZE=notused
1015
1016 mkfs_fn_ext2 = \
1017   e2fsimage -d $(1) -f $(2) -s $(EXT2_RW_IMAGE_SIZE)
1018
1019 RW_IMAGE_TYPE=jffs2
1020
1021 make_rw_image_fn = \
1022   $(call mkfs_fn_$(RW_IMAGE_TYPE),$(1),$(2))
1023
1024 rw_image_embed_ro_image_fn =                                    \
1025   mkdir -p proc initrd images ro rw union ;                     \
1026   cp $(PLATFORM_IMAGE_DIR)/$(1) images/$(1) ;                   \
1027   md5sum images/$(1) > images/$(1).md5 ;                        \
1028   echo Built by $(LOGNAME) at `date` > images/$(1).stamp ;      \
1029   mkdir -p changes/$(1)
1030
1031 # make sure RW_IMAGE_TYPE is a type we know how to build
1032 .PHONY: rw-image-check-type
1033 rw-image-check-type:
1034         @$(BUILD_ENV) ;                                                         \
1035         if [ -z "$(make_rw_image_fn)" ] ; then                                  \
1036           $(call build_msg_fn,Unknown read/write fs image type;                 \
1037                               try RW_IMAGE_TYPE=ext2 or RW_IMAGE_TYPE=jffs2) ;  \
1038           exit 1;                                                               \
1039         fi
1040
1041 # read write image
1042 .PHONY: rw-image
1043 rw-image: rw-image-check-type ro-image
1044         @$(BUILD_ENV) ;                                         \
1045         d=$(PLATFORM_IMAGE_DIR) ;                               \
1046         mkdir -p $$d ;                                          \
1047         rw_image="$$d/rw.$(RW_IMAGE_TYPE)" ;                    \
1048         ro_image="ro.img" ;                                     \
1049         rm -f $$rw_image ;                                      \
1050         tmp_dir="`mktemp -d $$d/rw-image-XXXXXX`" ;             \
1051         chmod 0755 $${tmp_dir} ;                                \
1052         cd $${tmp_dir} ;                                        \
1053         trap "rm -rf $${tmp_dir}" err ;                         \
1054         fakeroot /bin/bash -c "{                                \
1055           set -eu$(BUILD_DEBUG) ;                               \
1056           $(linuxrc_makedev) ;                                  \
1057           $(call rw_image_embed_ro_image_fn,$${ro_image}) ;     \
1058           $(call make_rw_image_fn,$${tmp_dir},$${rw_image}) ;   \
1059         }" ;                                                    \
1060         : cleanup tmp directory ;                               \
1061         rm -rf $${tmp_dir}
1062
1063 images: linuxrc-install linux-install $(image_extra_dependencies) rw-image
1064         @$(BUILD_ENV) ;                                         \
1065         d=$(PLATFORM_IMAGE_DIR) ;                               \
1066         cd $(BUILD_DIR)/linux-$(PLATFORM) ;                     \
1067         i="" ;                                                  \
1068         [[ -z $$i && -f bzImage ]] && i=bzImage ;               \
1069         [[ -z $$i && -f zImage ]] && i=zImage ;                 \
1070         [[ -z $$i && -f linux ]] && i=linux ;                   \
1071         [[ -z $$i && -f vmlinux ]] && i=vmlinux ;               \
1072         [[ -z $$i ]]                                            \
1073           && $(call build_msg_fn,no linux image to install      \
1074                 in $(BUILD_DIR)/linux-$(PLATFORM))              \
1075           && exit 1 ;                                           \
1076         cp $$i $$d
1077
1078 ######################################################################
1079 # Tool chain build/install
1080 ######################################################################
1081
1082 .PHONY: ccache-install
1083 ccache-install:
1084         $(MAKE) -C $(MU_BUILD_ROOT_DIR) ccache-build
1085         mkdir -p $(TOOL_INSTALL_DIR)/ccache-bin
1086         ln -sf $(MU_BUILD_ROOT_DIR)/build-tool-native/ccache/ccache \
1087                 $(TOOL_INSTALL_DIR)/ccache-bin/$(TARGET_PREFIX)gcc 
1088
1089 TOOL_MAKE = $(MAKE) is_build_tool=yes
1090
1091 tool_make_target_fn =                                                   \
1092   $(if $(strip $(NATIVE_TOOLS)),                                        \
1093     $(TOOL_MAKE) $(patsubst %,%-$(1),$(NATIVE_TOOLS)) ARCH=native || exit 1 ;) \
1094   $(TOOL_MAKE) $(patsubst %,%-$(1),$(CROSS_TOOLS))
1095
1096 .PHONY: install-tools
1097 install-tools:
1098         $(call tool_make_target_fn,install)
1099
1100 .PHONY: bootstrap-tools
1101 bootstrap-tools:
1102         $(TOOL_MAKE) make-install findutils-install git-install \
1103         automake-install autoconf-install libtool-install fakeroot-install
1104
1105
1106 ######################################################################
1107 # Clean
1108 ######################################################################
1109
1110 package_clean_script =                                                  \
1111   @$(call build_msg_fn, Cleaning $* in $(PACKAGE_INSTALL_DIR)) ;        \
1112   $(BUILD_ENV) ;                                                        \
1113   $(if $(is_build_tool),,rm -rf $(PACKAGE_INSTALL_DIR) ;)               \
1114   rm -rf $(TIMESTAMP_DIR)/$(call timestamp_name_fn,*) ;                 \
1115   $(if $($(PACKAGE)_clean),                                             \
1116     $($(PACKAGE)_clean),                                                \
1117     $(PACKAGE_MAKE) clean)
1118
1119 .PHONY: %-clean
1120 %-clean:
1121         $(package_clean_script)
1122
1123 # Wipe e.g. remove build and install directories for packages.
1124 package_wipe_script =                                                                                   \
1125   @message=$(if $(is_build_tool),"Wiping build $(PACKAGE)","Wiping build/install $(PACKAGE)") ;         \
1126   $(call build_msg_fn,$$message) ;                                                                      \
1127   $(BUILD_ENV) ;                                                                                        \
1128   rm -rf $(if $(is_build_tool),$(PACKAGE_BUILD_DIR),$(PACKAGE_INSTALL_DIR) $(PACKAGE_BUILD_DIR))
1129
1130 .PHONY: %-wipe
1131 %-wipe:
1132         $(package_wipe_script)
1133
1134 # Wipe entire build/install area for TAG and PLATFORM
1135 .PHONY: wipe-all
1136 wipe-all:
1137         @$(call build_msg_fn, Wiping $(BUILD_DIR) $(INSTALL_DIR)) ;     \
1138         $(BUILD_ENV) ;                                                  \
1139         rm -rf $(BUILD_DIR) $(INSTALL_DIR)
1140
1141 # Clean everything
1142 distclean:
1143         rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_package)*/
1144         rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_tool)*
1145         rm -rf $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)*
1146         rm -rf $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)*
1147         rm -rf $(TOOL_INSTALL_DIR)
1148         rm -rf $(MU_BUILD_ROOT_DIR)/*.deb
1149         rm -rf $(MU_BUILD_ROOT_DIR)/*.rpm
1150         rm -rf $(MU_BUILD_ROOT_DIR)/*.changes
1151         rm -rf $(MU_BUILD_ROOT_DIR)/python
1152         if [ -e /usr/bin/dh ];then (cd $(MU_BUILD_ROOT_DIR)/deb/;debian/rules clean); fi
1153         rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/*.install
1154         rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/changelog