ACL plugin enable macip for ip4/ip6 traffic
[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
262 ## BURT
263 # we will deprecate INSTALL_DIR shortly for DFLT_INSTALL_DIR
264 INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)$(ARCH)
265 # DFLT_INSTALL_DIR used in platforms.mk for $(PLATFORM)_DESTDIR_BASE
266 DFLT_INSTALL_DIR := $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)$(ARCH)
267 ## BURT
268
269 PLATFORM_IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)$(PLATFORM)
270
271 # $(call VAR,DEFAULT)
272 override_var_with_default_fn = $(if $($(1)),$($(1)),$(2))
273
274 # $(call if_directory_exists_fn,D1,D2) returns D1 if it exists else D2
275 define if_directory_exists_fn
276 $(shell if test -d $(1); then echo $(1); else echo $(2); fi)
277 endef
278
279 # $(call if_file_exists_fn,F1,F2) returns F1 if it exists else F2
280 define if_file_exists_fn
281 $(shell if test -f $(1); then echo $(1); else echo $(2); fi)
282 endef
283
284 # Default VAR, package specified override of default PACKAGE_VAR
285 package_var_fn = $(call override_var_with_default_fn,$(1)_$(2),$(1))
286
287 package_build_dir_fn = $(call package_var_fn,$(1),build_dir)
288
289 package_install_dir_fn = \
290   $(if $(is_build_tool),$(TOOL_INSTALL_DIR),$(INSTALL_DIR)/$(call package_build_dir_fn,$(1)))
291
292 PACKAGE_BUILD_DIR = \
293   $(BUILD_DIR)/$(call package_build_dir_fn,$(PACKAGE))
294 PACKAGE_INSTALL_DIR = \
295   $(call package_install_dir_fn,$(PACKAGE))
296
297 # Tools (gcc, binutils, glibc...) are installed here
298 TOOL_INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/tools
299
300 # Target specific tools go here e.g. mu-build/tools/ppc-mu-linux
301 TARGET_TOOL_INSTALL_DIR = $(TOOL_INSTALL_DIR)/$(TARGET)
302
303 # Set BUILD_DEBUG to vx or x enable shell command tracing.
304 BUILD_DEBUG =
305
306 # Message from build system itself (as opposed to make or shell commands)
307 build_msg_fn = echo "@@@@ $(1) @@@@"
308
309 # Allow CCACHE_DIR to be overridden, e.g. in .../build-root/build-config.mk
310 ifeq ($(CCACHE_DIR),)
311   CCACHE_DIR=$(MU_BUILD_ROOT_DIR)/.ccache
312 endif
313
314 # Always prefer our own tools to those installed on system.
315 # Note: ccache-bin must be before tool bin.
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     export LD_LIBRARY_PATH=$(TOOL_INSTALL_DIR)/lib64:$(TOOL_INSTALL_DIR)/lib ;          \
322     set -eu$(BUILD_DEBUG) ;                                                             \
323     set -o pipefail
324
325 ######################################################################
326 # Package build generic definitions
327 ######################################################################
328
329 package_dir_fn = \
330   $(call find_build_data_dir_for_package_fn,$(1))/packages
331
332 package_mk_fn = $(call package_dir_fn,$(1))/$(1).mk
333
334 ### BURT
335
336 #next version
337 #pkgPhaseDependMacro = $(foreach x,configure build install,                  \
338                         $(eval $(1)_$(x)_depend := $($(1)_depend:%=%-$(x))))
339 #version equivalent to original code
340 pkgPhaseDependMacro = $(eval $(1)_configure_depend := $($(1)_depend:%=%-install))
341
342 ### BURT
343
344 # Pick up built-root/pre-package-include.mk for all source directories
345 $(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),     \
346   $(eval -include $(d)/pre-package-include.mk))
347
348 $(foreach d,$(addsuffix /packages,$(FIND_SOURCE_PATH)),                 \
349   $(eval -include $(d)/*.mk)                                            \
350   $(eval ALL_PACKAGES += $(patsubst $(d)/%.mk,%,$(wildcard $(d)/*.mk))) \
351 )
352
353 # Pick up built-root/post-package-include.mk for all source directories
354 $(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),     \
355   $(eval -include $(d)/post-package-include.mk))
356
357 # Linux specific native build tools
358 NATIVE_TOOLS_LINUX =                            \
359   e2fsimage                                     \
360   e2fsprogs                                     \
361   fakeroot                                      \
362   jffs2                                         \
363   mkimage                                       \
364   zlib                                          \
365   xz                                            \
366   squashfs
367
368 IS_LINUX = $(if $(findstring no,$($(PLATFORM)_uses_linux)),no,yes)
369
370 NATIVE_TOOLS_$(IS_LINUX) += $(NATIVE_TOOLS_LINUX)
371
372 # only build glibc for linux installs
373 CROSS_TOOLS_$(IS_LINUX) += glibc gcc
374
375 # must be first for bootstrapping
376 NATIVE_TOOLS = findutils make
377
378 # basic tools needed for build system
379 NATIVE_TOOLS += git automake autoconf libtool texinfo bison flex tar
380
381 # needed to compile gcc
382 NATIVE_TOOLS += mpfr gmp mpc
383
384 # Tool to sign binaries
385 NATIVE_TOOLS += sign
386
387 # ccache
388 NATIVE_TOOLS += ccache
389
390 # Tools needed on native host to build for platform
391 NATIVE_TOOLS += $(call ifdef_fn,$(PLATFORM)_native_tools,)
392
393 # Tools for cross-compiling from native -> ARCH
394 CROSS_TOOLS = binutils gcc-bootstrap gdb
395
396 # Tools needed on native host to build for platform
397 CROSS_TOOLS += $(call ifdef_fn,$(PLATFORM)_cross_tools,)
398
399 NATIVE_TOOLS += $(NATIVE_TOOLS_yes)
400 CROSS_TOOLS += $(CROSS_TOOLS_yes)
401
402 timestamp_name_fn = .mu_build_$(1)_timestamp
403 CONFIGURE_TIMESTAMP = $(call timestamp_name_fn,configure)
404 BUILD_TIMESTAMP = $(call timestamp_name_fn,build)
405 INSTALL_TIMESTAMP = $(call timestamp_name_fn,install)
406
407 TIMESTAMP_DIR = $(PACKAGE_BUILD_DIR)
408
409 find_newer_files_fn =                                           \
410   "`for i in $(2) ; do                                          \
411       [[ -f $$i && $$i -nt $(1) ]] && echo "$$i" && exit 0;     \
412     done ;                                                      \
413     exit 0;`"
414
415 find_filter = -not -name '*~'
416 find_filter += -and -not -path '*/.git*'
417 find_filter += -and -not -path '*/.svn*'
418 find_filter += -and -not -path '*/.CVS*'
419 find_filter += -and -not -path '*/manual/*'
420 find_filter += -and -not -path '*/autom4te.cache/*'
421 find_filter += -and -not -path '*/doc/all-cfg.texi'
422 find_filter += -and -not -path '*/.mu_build_*'
423
424 find_newer_filtered_fn =                        \
425   (! -f $(1)                                    \
426     || -n $(call find_newer_files_fn,$(1),$(3)) \
427     || -n "`find -H $(2)                        \
428               -type f                           \
429               -and -newer $(1)                  \
430               -and \( $(4) \)                   \
431               -print -quit`")
432
433 find_newer_fn =                                                 \
434   $(call find_newer_filtered_fn,$(1),$(2),$(3),$(find_filter))
435
436 ######################################################################
437 # Package dependencies
438 ######################################################################
439
440 # This must come before %-configure, %-build, %-install pattern rules
441 # or else dependencies will not work.
442
443 package_dependencies_fn =                               \
444   $(patsubst %-install, %,                              \
445     $(filter %-install,$($(1)_configure_depend)))
446
447 PACKAGE_DEPENDENCIES = $(call package_dependencies_fn,$(PACKAGE))
448
449 # package specific configure, build, install dependencies
450 add_package_dependency_fn = \
451   $(if $($(1)_$(2)_depend), \
452        $(eval $(1)-$(2) : $($(1)_$(2)_depend)))
453
454 $(foreach p,$(ALL_PACKAGES), \
455     $(call add_package_dependency_fn,$(p),configure) \
456     $(call add_package_dependency_fn,$(p),build) \
457     $(call add_package_dependency_fn,$(p),install))
458
459 TARGETS_RESPECTING_DEPENDENCIES = image_install wipe diff push-all pull-all find-source
460
461 # carry over packages dependencies to image install, wipe, pull-all, push-all
462 $(foreach p,$(ALL_PACKAGES),                                                    \
463   $(if $($(p)_configure_depend),                                                \
464     $(foreach s,$(TARGETS_RESPECTING_DEPENDENCIES),                             \
465       $(eval $(p)-$(s):                                                         \
466              $(addsuffix -$(s), $(call package_dependencies_fn,$(p)))))))
467
468 # recursively resolve dependencies
469 resolve_dependencies2_fn = $(strip                                      \
470   $(eval __added = $(filter-out $(4),                                   \
471     $(call uniq_fn,                                                     \
472       $(foreach l,$(3),                                                 \
473        $(call ifdef3_fn,$(l)$(1),,$(call $(2),$($(l)$(1))))             \
474       ))))                                                              \
475   $(eval __known = $(call uniq_fn,$(4) $(3) $(__added)))                \
476   $(if $(__added),                                                      \
477     $(call resolve_dependencies2_fn,$(1),$(2),$(__added),$(__known)),   \
478     $(__known))                                                         \
479 )
480
481 resolve_dependencies_null_fn = $(1)
482
483 resolve_dependencies_fn = $(call resolve_dependencies2_fn,$(1),resolve_dependencies_null_fn,$(2))
484
485 ######################################################################
486 # Package configure
487 ######################################################################
488
489 # x86_64 can be either 32/64.  set BIACH=32 to get 32 bit libraries.
490 BIARCH = 64
491
492 x86_64_libdir = $(BIARCH)
493 native_libdir = $($(NATIVE_ARCH)_libdir)
494
495 # lib or lib64 depending
496 arch_lib_dir = lib$($(BASIC_ARCH)_libdir)
497
498 # find dynamic linker as absolute path
499 TOOL_INSTALL_LIB_DIR=$(TOOL_INSTALL_DIR)/$(TARGET)/$(arch_lib_dir)
500 DYNAMIC_LINKER=${shell cd $(TOOL_INSTALL_LIB_DIR); echo ld*.so.*}
501
502 # Pad dynamic linker & rpath so elftool will never have to change ELF section sizes.
503 # Yes, this is a kludge.
504 lots_of_slashes_to_pad_names = "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"
505
506 # When PLATFORM != native we *always* use our own versions of GLIBC and dynamic linker
507 # Allow per-platform overrides
508 CROSS_LDFLAGS = $(strip $($(PLATFORM)_cross_ldflags))
509 ifeq ($(CROSS_LDFLAGS),)
510   CROSS_LDFLAGS =                                                                                       \
511     -Wl,--dynamic-linker=$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)/$(DYNAMIC_LINKER)       \
512     -Wl,-rpath -Wl,$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)
513 endif
514
515 cross_ldflags = $(if $(is_native)$(is_build_tool),,$(CROSS_LDFLAGS) )
516
517 # $(call installed_libs_fn,PACKAGE)
518 # Return install library directory for given package.
519 # Some packages (e.g. openssl) don't install under lib64; instead they use lib
520 define installed_lib_fn
521 $(call if_directory_exists_fn,
522   $(call package_install_dir_fn,$(1))/$(arch_lib_dir),
523   $(call package_install_dir_fn,$(1))/lib)
524 endef
525
526 # Set -L and rpath to point to dependent libraries previously built by us.
527 installed_libs_fn =                                     \
528   $(foreach i,$(1),                                     \
529     -L$(call installed_lib_fn,$(i))                     \
530     -Wl,-rpath -Wl,$(call installed_lib_fn,$(i)))
531
532 # As above for include files
533 installed_include_fn = $(call package_install_dir_fn,$(1))/include
534
535 installed_includes_fn = $(foreach i,$(1),-I$(call installed_include_fn,$(i)))
536
537 # By default package CPPFLAGS (to set include path -I) and LDFLAGS (to set link path -L)
538 # point at dependent install directories.
539 DEFAULT_CPPFLAGS = $(call installed_includes_fn, $(PACKAGE_DEPENDENCIES))
540 DEFAULT_LDFLAGS = $(call installed_libs_fn, $(PACKAGE_DEPENDENCIES))
541
542 configure_var_fn = \
543   $(call tag_var_with_added_space_fn,$(1))$(call override_var_with_default_fn,$(PACKAGE)_$(1),$(DEFAULT_$(1)))
544 configure_ldflags_fn = \
545   $(cross_ldflags)$(call configure_var_fn,LDFLAGS)
546
547 # Allow packages to override CPPFLAGS, CFLAGS, and LDFLAGS
548 CONFIGURE_ENV =                                                         \
549     $(if $(call configure_var_fn,CPPFLAGS),                             \
550          CPPFLAGS="$(CPPFLAGS) $(call configure_var_fn,CPPFLAGS)")      \
551     $(if $(call configure_var_fn,CFLAGS),                               \
552          CFLAGS="$(CFLAGS) $(call configure_var_fn,CFLAGS)")            \
553     $(if $(call configure_var_fn,CCASFLAGS),                            \
554          CCASFLAGS="$(CCASFLAGS) $(call configure_var_fn,CCASFLAGS)")   \
555     $(if $(call configure_ldflags_fn),                                  \
556          LDFLAGS="$(LDFLAGS) $(call configure_ldflags_fn)")             \
557     $(if $($(PACKAGE)_configure_env),$($(PACKAGE)_configure_env))
558
559 ### BURT
560 # only partially used now (used in a few .mk files)
561 ifeq ($(is_build_tool),yes)
562 prefix     = $(PACKAGE_INSTALL_DIR)
563 libdir     = $(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)
564 libexecdir = $(PACKAGE_INSTALL_DIR)/usr/libexec
565 DESTDIR     = /
566 else
567 # Eventually simplify this with no per package DESTDIR or prefix
568 ppdMacro = $(if $(PER_PACKAGE_DESTDIR),$(call package_build_dir_fn,$(1)))
569 pppMacro = $(if $(PER_PACKAGE_PREFIX),$(call package_build_dir_fn,$(1)))
570 prefixMacro     = $($(PLATFORM)_PREFIX_BASE)/$(pppMacro)
571 prefix = $(call prefixMacro,$(PACKAGE))
572 libdir     = $($(PLATFORM)_LIBDIR)
573 libexecdir = $($(PLATFORM)_LIBEXECDIR)
574 destdirMacro  = $($(PLATFORM)_DESTDIR_BASE)$(ppdMacro)
575 DESTDIR  = $(call destdirMacro,$(PACKAGE))
576 endif
577 ### BURT
578 ### dbarach
579 image_extra_dependencies = $($(PLATFORM)_image_extra_dependencies)
580 ### dbarach
581
582 configure_package_gnu =                                         \
583   s=$(call find_source_fn,$(PACKAGE_SOURCE))$(PACKAGE_SUBDIR) ; \
584   if [ ! -f $$s/configure ] ; then                              \
585     autoreconf -i -f $$s ;                                      \
586   fi ;                                                          \
587   cd $(PACKAGE_BUILD_DIR) ;                                     \
588   env $(CONFIGURE_ENV)                                          \
589     $$s/configure                                               \
590       $(if $($(PACKAGE)_configure_host_and_target),             \
591            $($(PACKAGE)_configure_host_and_target),             \
592            $(if $(not_native),--host=$(TARGET),))               \
593       $(if $($(PACKAGE)_configure_prefix),                      \
594            $($(PACKAGE)_configure_prefix),                      \
595            --libdir=$(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)      \
596            --prefix=$(PACKAGE_INSTALL_DIR))                     \
597       $($(PACKAGE)_configure_args)                              \
598       $($(PACKAGE)_configure_args_$(PLATFORM))
599
600 configure_package =                                                     \
601   $(call build_msg_fn,Configuring $(PACKAGE) in $(PACKAGE_BUILD_DIR)) ; \
602   mkdir -p $(PACKAGE_BUILD_DIR) ;                                       \
603   $(if $($(PACKAGE)_configure),                                         \
604        $($(PACKAGE)_configure),                                         \
605        $(configure_package_gnu))
606
607 # Tools (e.g. gcc, binutils, gdb) required a platform to build for
608 check_platform =                                                                \
609   is_tool="$(is_build_tool)" ;                                                  \
610   is_cross_package="$(findstring $(PACKAGE),$(CROSS_TOOLS))" ;                  \
611   is_arch_native="$(if $(subst native,,$(ARCH)),,yes)" ;                        \
612   if [ "$${is_tool}" == "yes"                                                   \
613        -a "$${is_cross_package}" != ""                                          \
614        -a "$${is_arch_native}" != "" ]; then                                    \
615     $(call build_msg_fn,You must specify PLATFORM for building tools) ;         \
616     exit 1 ;                                                                    \
617   fi ;                                                                          \
618   : check that platform gcc can be found ;                                      \
619   target_gcc=gcc ;                                                              \
620   if [ "$${is_arch_native}" != "yes" ] ; then                                   \
621     target_gcc=$(TARGET)-gcc ;                                                  \
622   fi ;                                                                          \
623   if [ "$${is_tool}" != "yes"                                                   \
624        -a "$${is_arch_native}" != "yes"                                         \
625        -a ! -x "`which 2> /dev/null $${target_gcc}`" ] ; then                   \
626     $(call build_msg_fn,                                                        \
627            No cross-compiler found for platform $(PLATFORM) target $(TARGET);   \
628              try make PLATFORM=$(PLATFORM) install-tools) ;                     \
629     exit 1 ;                                                                    \
630   fi
631     
632 configure_check_timestamp =                                             \
633   @$(BUILD_ENV) ;                                                       \
634   $(check_platform) ;                                                   \
635   mkdir -p $(PACKAGE_BUILD_DIR) ;                                       \
636   mkdir -p $(PACKAGE_INSTALL_DIR) ;                                     \
637   conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;                      \
638   dirs="$(call package_mk_fn,$(PACKAGE))                                \
639         $(wildcard $(call find_source_fn,                               \
640                      $(PACKAGE_SOURCE))$(PACKAGE_SUBDIR)/configure)     \
641        $(MU_BUILD_ROOT_DIR)/config.site" ;                              \
642   if [[ $(call find_newer_fn, $${conf}, $${dirs}, $?) ]]; then          \
643     $(configure_package) ;                                              \
644     touch $${conf} ;                                                    \
645   else                                                                  \
646     $(call build_msg_fn,Configuring $(PACKAGE): nothing to do) ;        \
647   fi
648
649 .PHONY: %-configure
650 %-configure: %-find-source
651         $(configure_check_timestamp)
652
653 ######################################################################
654 # Package build
655 ######################################################################
656
657 # /proc/cpuinfo does not exist on platforms without a /proc and on some
658 # platforms, notably inside containers, it has no content. In those cases
659 # we assume there's 1 processor; we use 2*ncpu for the -j option.
660 # NB: GNU Make 4.2 will let us use '$(file </proc/cpuinfo)' to both test
661 # for file presence and content; for now this will have to do.
662 MAKE_PARALLEL_JOBS = -j $(if $(shell [ -f /proc/cpuinfo ] && head /proc/cpuinfo), \
663         $(shell expr 2 '*' $$(grep -c ^processor /proc/cpuinfo)), 2)
664 MAKE_PARALLEL_FLAGS = $(if $($(PACKAGE)_make_parallel_fails),,$(MAKE_PARALLEL_JOBS))
665
666 # Make command shorthand for packages & tools.
667 PACKAGE_MAKE =                                  \
668   $(MAKE)                                       \
669     -C $(PACKAGE_BUILD_DIR)                     \
670     $($(PACKAGE)_make_args)                     \
671     $(MAKE_PARALLEL_FLAGS)
672
673 build_package =                                                 \
674   $(call build_msg_fn,Building $* in $(PACKAGE_BUILD_DIR)) ;    \
675   mkdir -p $(PACKAGE_BUILD_DIR) ;                               \
676   cd $(PACKAGE_BUILD_DIR) ;                                     \
677   $(if $($(PACKAGE)_build),                                     \
678        $($(PACKAGE)_build),                                     \
679        $(PACKAGE_MAKE))
680
681 build_check_timestamp =                                                                 \
682   @$(BUILD_ENV) ;                                                                       \
683   comp="$(TIMESTAMP_DIR)/$(BUILD_TIMESTAMP)" ;                                          \
684   conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;                                      \
685   dirs="$(call find_source_fn,$(PACKAGE_SOURCE))                                        \
686         $($(PACKAGE)_build_timestamp_depends)                                           \
687         $(if $(is_build_tool),,$(addprefix $(INSTALL_DIR)/,$(PACKAGE_DEPENDENCIES)))" ; \
688   if [[ $${conf} -nt $${comp}                                                           \
689         || $(call find_newer_fn, $${comp}, $${dirs}, $?) ]]; then                       \
690     $(build_package) ;                                                                  \
691     touch $${comp} ;                                                                    \
692   else                                                                                  \
693     $(call build_msg_fn,Building $(PACKAGE): nothing to do) ;                           \
694   fi
695
696 .PHONY: %-build
697 %-build: %-configure
698         $(build_check_timestamp)
699
700 .PHONY: %-rebuild
701 %-rebuild: %-wipe %-build
702         @ :
703
704 ######################################################################
705 # Package install
706 ######################################################################
707
708 install_package =                                                               \
709     : by default, for non-tools, remove any previously installed bits ;         \
710     $(if $(is_build_tool)$($(PACKAGE)_keep_instdir),                            \
711          true,                                                                  \
712          rm -rf $(PACKAGE_INSTALL_DIR));                                        \
713     mkdir -p $(PACKAGE_INSTALL_DIR) ;                                           \
714     $(if $($(PACKAGE)_pre_install),$($(PACKAGE)_pre_install),true);             \
715     $(if $($(PACKAGE)_install),                                                 \
716          $($(PACKAGE)_install),                                                 \
717          $(PACKAGE_MAKE)                                                        \
718             $($(PACKAGE)_install_args)                                          \
719             install) ;                                                          \
720     $(if $($(PACKAGE)_post_install),$($(PACKAGE)_post_install),true)
721
722 install_check_timestamp =                                       \
723   @$(BUILD_ENV) ;                                               \
724   inst=$(TIMESTAMP_DIR)/$(INSTALL_TIMESTAMP) ;                  \
725   dirs="$(PACKAGE_BUILD_DIR)                                    \
726         $($(PACKAGE)_install_dependencies)" ;                   \
727   if [[ $(call find_newer_fn, $${inst}, $${dirs}, $?) ]]; then  \
728     $(call build_msg_fn,Installing $(PACKAGE)) ;                \
729     $(install_package) ;                                        \
730     touch $${inst} ;                                            \
731   else                                                          \
732     $(call build_msg_fn,Installing $(PACKAGE): nothing to do) ; \
733   fi
734
735 .PHONY: %-install
736 %-install: %-build
737         $(install_check_timestamp)
738
739 ######################################################################
740 # Source code managment
741 ######################################################################
742
743 GIT = git
744
745 # Maps package name to source directory root.
746 # Multiple packages may use a single source tree.
747 # For example, gcc-bootstrap package shares gcc source.
748 PACKAGE_SOURCE = $(if $($(PACKAGE)_source),$($(PACKAGE)_source),$(PACKAGE))
749 PACKAGE_SUBDIR = $(if $($(PACKAGE)_configure_subdir),/$($(PACKAGE)_configure_subdir),)
750
751 # Use git to download source if directory is not found
752 find_source_for_package =                                                                       \
753   @$(BUILD_ENV) ;                                                                               \
754   $(call build_msg_fn,Arch for platform '$(PLATFORM)' is $(ARCH)) ;                             \
755   $(call build_msg_fn,Finding source for $(PACKAGE)) ;                                          \
756   s="$(call find_source_fn,$(PACKAGE_SOURCE))" ;                                                \
757   [[ -z "$${s}" ]]                                                                              \
758     && $(call build_msg_fn,Package $(PACKAGE) not found with path $(SOURCE_PATH))               \
759     && exit 1;                                                                                  \
760   mk="$(call find_build_data_dir_for_package_fn,$(PACKAGE_SOURCE))/packages/$(PACKAGE).mk";     \
761   $(call build_msg_fn,Makefile fragment found in $${mk}) ;                                      \
762   if [ ! -d "$${s}" ] ; then                                                                    \
763     d=`dirname $${mk}` ;                                                                        \
764     i=`cd $${d}/.. && ($(GIT) config remote.origin.url ||                                       \
765                     awk '/URL/ { print $$2; }' .git/remotes/origin)`;                           \
766     g=`dirname $${i}` ;                                                                         \
767     $(call build_msg_fn,Fetching source: $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s) ;            \
768     if ! $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s; then                                         \
769       $(call build_msg_fn,No source for $(PACKAGE) in $${g});                                   \
770       exit 1;                                                                                   \
771     fi ;                                                                                        \
772     $(call build_msg_fn,Fix file dates in $${g}/$(PACKAGE_SOURCE)) ;                                    \
773     (cd $${s} ; $(MU_BUILD_ROOT_DIR)/autowank --touch) ;                                        \
774   fi ;                                                                                          \
775   s=`cd $${s} && pwd` ;                                                                         \
776   $(call build_msg_fn,Source found in $${s})
777
778 .PHONY: %-find-source
779 %-find-source:
780         $(find_source_for_package)
781
782 .PHONY: %-push %-pull %-push-all %-pull-all
783 %-push %-pull %-push-all %-pull-all:
784         @$(BUILD_ENV) ;                                                         \
785         push_or_pull=$(patsubst %-all,%,$(subst $(PACKAGE)-,,$@)) ;             \
786         $(call build_msg_fn,Git $${push_or_pull} source for $(PACKAGE)) ;       \
787         s=$(call find_source_fn,$(PACKAGE_SOURCE)) ;                            \
788         if [ "x$$s" = "x" ]; then                                               \
789              $(call build_msg_fn,No source for $(PACKAGE)) ;                    \
790              exit 1;                                                            \
791         fi ;                                                                    \
792         cd $$s && $(GIT) $${push_or_pull}
793
794 # Pull all packages for platform
795 .PHONY: pull-all
796 pull-all:
797         @$(BUILD_ENV) ;                                                         \
798         $(call build_msg_fn,Git pull build system) ;                            \
799         for d in $(MU_BUILD_ROOT_DIR)                                           \
800                  $(SOURCE_PATH_BUILD_ROOT_DIRS)                                 \
801                  $(SOURCE_PATH_BUILD_DATA_DIRS); do                             \
802           $(call build_msg_fn,Git pull $${d}) ;                                 \
803           pushd $${d} >& /dev/null && $(GIT) pull && popd >& /dev/null ;        \
804         done ;                                                                  \
805         $(call build_msg_fn,Git pull build tools) ;                             \
806         $(call tool_make_target_fn,pull-all) ;                                  \
807         $(call build_msg_fn,Git pull packages for platform $(PLATFORM)) ;       \
808         make PLATFORM=$(PLATFORM) $(patsubst %,%-pull-all,$(ROOT_PACKAGES))
809
810 .PHONY: %-diff
811 %-diff:                                                                 
812         @$(BUILD_ENV) ;                                                 \
813         d=$(call find_source_fn,$(PACKAGE_SOURCE)) ;                    \
814         $(call build_msg_fn,Git diff $(PACKAGE)) ;                      \
815         if [ -d $${d}/.git ] ; then                                     \
816          cd $${d} && $(GIT) --no-pager diff 2>/dev/null;                \
817         else                                                            \
818          $(call build_msg_fn, $(PACKAGE) not a git directory) ;         \
819         fi
820             
821
822
823 # generate diffs for everything in source path
824 .PHONY: diff-all
825 diff-all:                                                               
826         @$(BUILD_ENV) ;                                                 \
827         $(call build_msg_fn,Generate diffs) ;                           \
828         for r in $(ABSOLUTE_SOURCE_PATH); do                            \
829           for d in $${r}/* ; do                                         \
830             if [ -d $${d} ] ; then                                      \
831               $(call build_msg_fn,Git diff $${d}) ;                     \
832               if [ -d $${d}/.git ] ; then                               \
833                 cd $${d} && $(GIT) --no-pager diff 2>/dev/null;         \
834               else                                                      \
835                 $(call build_msg_fn, $${d} not a git directory) ;       \
836               fi ;                                                      \
837             fi ;                                                        \
838           done ;                                                        \
839         done
840
841 ######################################################################
842 # System images
843 ######################################################################
844
845 IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/image-$(PLATFORM)
846
847 # Reports shared libraries in given directory
848 find_shared_libs_fn =                           \
849   find $(1)                                     \
850     -maxdepth 1                                 \
851        -regex '.*/lib[a-z0-9_]+\+?\+?.so'               \
852     -o -regex '.*/lib[a-z0-9_]+-[0-9.]+\+?\+?.so'       \
853     -o -regex '.*/lib[a-z0-9_]+\+?\+?.so.[0-9.]+'
854
855 # By default pick up files from binary directories and /etc.
856 # Also include shared libraries.
857 DEFAULT_IMAGE_INCLUDE =                                 \
858   for d in bin sbin libexec                             \
859            usr/bin usr/sbin usr/libexec                 \
860            etc; do                                      \
861     [[ -d $$d ]] && echo $$d;                           \
862   done ;                                                \
863   [[ -d $(arch_lib_dir) ]]                              \
864     && $(call find_shared_libs_fn,$(arch_lib_dir))
865
866 # Define any shell functions needed by install scripts
867 image_install_functions =                       \
868   $(foreach p,$(ALL_PACKAGES),                  \
869     $(if $($(p)_image_install_functions),       \
870          $($(p)_image_install_functions)))
871
872 # Should always be over-written by temp dir in %-root-image rule
873 IMAGE_INSTALL_DIR = $(error you need to set IMAGE_INSTALL_DIR)
874
875 image_install_fn =                                                              \
876   @$(BUILD_ENV) ;                                                               \
877   $(call build_msg_fn,Image-install $(1) for platform $(PLATFORM)) ;            \
878   inst_dir=$(IMAGE_INSTALL_DIR) ;                                               \
879   mkdir -p $${inst_dir} ;                                                       \
880   cd $(2) ;                                                                     \
881   : select files to include in image ;                                          \
882   image_include_files="                                                         \
883     `$(call ifdef_fn,$(1)_image_include,$(DEFAULT_IMAGE_INCLUDE)) ;             \
884      echo "" ;                                                                  \
885      exit 0 ; `";                                                               \
886   : select files regexps to exclude from image ;                                \
887   image_exclude_files="" ;                                                      \
888   if [ ! -z "$($(1)_image_exclude)" ] ; then                                    \
889     image_exclude_files="${image_exclude_files}                                 \
890                          $(patsubst %,--exclude=%,$($(1)_image_exclude))" ;     \
891   fi ;                                                                          \
892   [[ -z "$${image_include_files}" || $${image_include_files} == " " ]]          \
893     || tar cf - $${image_include_files} $${image_exclude_files}                 \
894        | tar xf - -C $${inst_dir} ;                                             \
895   : copy files from copyimg directories on source path if present ;             \
896   for build_data_dir in $(SOURCE_PATH_BUILD_DATA_DIRS) ; do                     \
897     d="$${build_data_dir}/packages/$(1).copyimg" ;                              \
898     if [ -d "$${d}" ] ; then                                                    \
899       env $($(PLATFORM)_copyimg_env)                                            \
900         $(MU_BUILD_ROOT_DIR)/copyimg $${d} $${inst_dir} ;                       \
901     fi ;                                                                        \
902   done ;                                                                        \
903   : run package dependent install script ;                                      \
904   $(if $($(1)_image_install),                                                   \
905        $(image_install_functions)                                               \
906        cd $${inst_dir} ;                                                        \
907        $($(1)_image_install))
908
909 .PHONY: %-image_install
910 %-image_install: %-install
911         $(call image_install_fn,$(PACKAGE),$(PACKAGE_INSTALL_DIR))
912
913 basic_system_image_include =                                    \
914   $(call ifdef_fn,$(PLATFORM)_basic_system_image_include,       \
915   echo bin/ldd ;                                                \
916   echo $(arch_lib_dir)/ld*.so* ;                                \
917   $(call find_shared_libs_fn, $(arch_lib_dir)))
918
919 basic_system_image_install =                            \
920   mkdir -p bin lib mnt proc root sbin sys tmp etc ;     \
921   mkdir -p usr usr/{bin,sbin} usr/lib ;                 \
922   mkdir -p var var/{lib,lock,log,run,tmp} ;             \
923   mkdir -p var/lock/subsys var/lib/urandom 
924
925 .PHONY: basic_system-image_install
926 basic_system-image_install: # linuxrc-install
927         $(if $(not_native),                                                     \
928              $(call image_install_fn,basic_system,$(TARGET_TOOL_INSTALL_DIR)),)
929
930 ROOT_PACKAGES = $(if $($(PLATFORM)_root_packages),$($(PLATFORM)_root_packages),$(default_root_packages))
931
932 .PHONY: install-packages
933 install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES))  
934         @$(BUILD_ENV) ;                                                         \
935         set -eu$(BUILD_DEBUG) ;                                                 \
936         d=$(MU_BUILD_ROOT_DIR)/packages-$(PLATFORM) ;                           \
937         rm -rf $${d} ;                                                          \
938         mkdir -p $${d};                                                         \
939         $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${d}                 \
940             $(patsubst %,%-image_install,                                       \
941               basic_system                                                      \
942               $(ROOT_PACKAGES)) || exit 1;                                      \
943         $(call build_msg_fn, Relocating ELF executables to run in $${d}) ;      \
944         find $${d} -type f                                                      \
945             -exec elftool quiet in '{}' out '{}'                                \
946                 set-interpreter                                                 \
947                     $${d}/$(arch_lib_dir)/$(DYNAMIC_LINKER)                     \
948                 set-rpath $${d}/$(arch_lib_dir):$${d}/lib ';' ;                 \
949         : strip symbols from files ;                                            \
950         if [ $${strip_symbols:-no} = 'yes' ] ; then                             \
951             $(call build_msg_fn, Stripping symbols from files) ;                \
952             find $${d} -type f                                                  \
953                 -exec                                                           \
954                   $(TARGET_PREFIX)strip                                         \
955                     --strip-unneeded '{}' ';'                                   \
956                     >& /dev/null ;                                              \
957         else                                                                    \
958             $(call build_msg_fn, NOT stripping symbols) ;                       \
959         fi 
960
961 # readonly root squashfs image
962 # Note: $(call build_msg_fn) does not seem to work inside of fakeroot so we use echo
963 .PHONY: ro-image
964 $(PLATFORM_IMAGE_DIR)/ro.img ro-image: $(patsubst %,%-find-source,$(ROOT_PACKAGES))
965         @$(BUILD_ENV) ;                                                 \
966         d=$(PLATFORM_IMAGE_DIR) ;                                       \
967         mkdir -p $$d;                                                   \
968         ro_image=$$d/ro.img ;                                           \
969         rm -f $${ro_image} ;                                            \
970         tmp_dir="`mktemp -d $$d/ro-image-XXXXXX`" ;                     \
971         chmod 0755 $${tmp_dir} ;                                        \
972         cd $${tmp_dir} ;                                                \
973         trap "rm -rf $${tmp_dir}" err ;                                 \
974         fakeroot /bin/bash -c "{                                        \
975           set -eu$(BUILD_DEBUG) ;                                       \
976           $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${tmp_dir} \
977             $(patsubst %,%-image_install,                               \
978               basic_system                                              \
979               $(ROOT_PACKAGES)) ;                                       \
980           : make dev directory ;                                        \
981           $(linuxrc_makedev) ;                                          \
982           echo @@@@ Relocating ELF executables to run in / @@@@ ;       \
983           find $${d} -type f                                            \
984               -exec elftool quiet in '{}' out '{}'                      \
985                 set-interpreter                                         \
986                     /$(arch_lib_dir)/$(DYNAMIC_LINKER)                  \
987                 set-rpath /$(arch_lib_dir):/lib ';' ;                   \
988           : strip symbols from files ;                                  \
989           if [ '$${strip_symbols:-yes}' = 'yes' ] ; then                \
990               echo @@@@ Stripping symbols from files @@@@ ;             \
991               find $${tmp_dir} -type f                                  \
992                 -exec                                                   \
993                   $(TARGET_PREFIX)strip                                 \
994                     --strip-unneeded '{}' ';'                           \
995                     >& /dev/null ;                                      \
996           else                                                          \
997               echo @@@@ NOT stripping symbols @@@@ ;                    \
998           fi ;                                                          \
999           if [ $${sign_executables:-yes} = 'yes'                        \
1000                -a -n "$($(PLATFORM)_public_key)" ] ; then               \
1001               echo @@@@ Signing executables @@@@ ;                      \
1002               find $${tmp_dir} -type f                                  \
1003                 | xargs sign $($(PLATFORM)_public_key)                  \
1004                              $($(PLATFORM)_private_key_passphrase) ;    \
1005           fi ;                                                          \
1006           : make read-only file system ;                                \
1007           mksquashfs                                                    \
1008             $${tmp_dir} $${ro_image}                                    \
1009             -no-exports -no-progress -no-recovery ;                     \
1010         }" ;                                                            \
1011         : cleanup tmp directory ;                                       \
1012         rm -rf $${tmp_dir}
1013
1014 MKFS_JFFS2_BYTE_ORDER_x86_64 = -l
1015 MKFS_JFFS2_BYTE_ORDER_i686 = -l
1016 MKFS_JFFS2_BYTE_ORDER_ppc = -b
1017 MKFS_JFFS2_BYTE_ORDER_mips = -b
1018 MKFS_JFFS2_BYTE_ORDER_native = $(MKFS_JFFS2_BYTE_ORDER_$(NATIVE_ARCH))
1019
1020 MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES = \
1021   $(call ifdef_fn,$(PLATFORM)_jffs2_sector_size_in_kbytes,256)
1022
1023 mkfs_fn_jffs2 = mkfs.jffs2                              \
1024   --eraseblock=$(MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES)KiB   \
1025   --root=$(1) --output=$(2)                             \
1026   $(MKFS_JFFS2_BYTE_ORDER_$(BASIC_ARCH))
1027
1028 # As things stand the actual initrd size parameter
1029 # is set in .../open-repo/build-data/packages/linuxrc.mk.
1030 EXT2_RW_IMAGE_SIZE=notused
1031
1032 mkfs_fn_ext2 = \
1033   e2fsimage -d $(1) -f $(2) -s $(EXT2_RW_IMAGE_SIZE)
1034
1035 RW_IMAGE_TYPE=jffs2
1036
1037 make_rw_image_fn = \
1038   $(call mkfs_fn_$(RW_IMAGE_TYPE),$(1),$(2))
1039
1040 rw_image_embed_ro_image_fn =                                    \
1041   mkdir -p proc initrd images ro rw union ;                     \
1042   cp $(PLATFORM_IMAGE_DIR)/$(1) images/$(1) ;                   \
1043   md5sum images/$(1) > images/$(1).md5 ;                        \
1044   echo Built by $(LOGNAME) at `date` > images/$(1).stamp ;      \
1045   mkdir -p changes/$(1)
1046
1047 # make sure RW_IMAGE_TYPE is a type we know how to build
1048 .PHONY: rw-image-check-type
1049 rw-image-check-type:
1050         @$(BUILD_ENV) ;                                                         \
1051         if [ -z "$(make_rw_image_fn)" ] ; then                                  \
1052           $(call build_msg_fn,Unknown read/write fs image type;                 \
1053                               try RW_IMAGE_TYPE=ext2 or RW_IMAGE_TYPE=jffs2) ;  \
1054           exit 1;                                                               \
1055         fi
1056
1057 # read write image
1058 .PHONY: rw-image
1059 rw-image: rw-image-check-type ro-image
1060         @$(BUILD_ENV) ;                                         \
1061         d=$(PLATFORM_IMAGE_DIR) ;                               \
1062         mkdir -p $$d ;                                          \
1063         rw_image="$$d/rw.$(RW_IMAGE_TYPE)" ;                    \
1064         ro_image="ro.img" ;                                     \
1065         rm -f $$rw_image ;                                      \
1066         tmp_dir="`mktemp -d $$d/rw-image-XXXXXX`" ;             \
1067         chmod 0755 $${tmp_dir} ;                                \
1068         cd $${tmp_dir} ;                                        \
1069         trap "rm -rf $${tmp_dir}" err ;                         \
1070         fakeroot /bin/bash -c "{                                \
1071           set -eu$(BUILD_DEBUG) ;                               \
1072           $(linuxrc_makedev) ;                                  \
1073           $(call rw_image_embed_ro_image_fn,$${ro_image}) ;     \
1074           $(call make_rw_image_fn,$${tmp_dir},$${rw_image}) ;   \
1075         }" ;                                                    \
1076         : cleanup tmp directory ;                               \
1077         rm -rf $${tmp_dir}
1078
1079 images: linuxrc-install linux-install $(image_extra_dependencies) rw-image
1080         @$(BUILD_ENV) ;                                         \
1081         d=$(PLATFORM_IMAGE_DIR) ;                               \
1082         cd $(BUILD_DIR)/linux-$(PLATFORM) ;                     \
1083         i="" ;                                                  \
1084         [[ -z $$i && -f bzImage ]] && i=bzImage ;               \
1085         [[ -z $$i && -f zImage ]] && i=zImage ;                 \
1086         [[ -z $$i && -f linux ]] && i=linux ;                   \
1087         [[ -z $$i && -f vmlinux ]] && i=vmlinux ;               \
1088         [[ -z $$i ]]                                            \
1089           && $(call build_msg_fn,no linux image to install      \
1090                 in $(BUILD_DIR)/linux-$(PLATFORM))              \
1091           && exit 1 ;                                           \
1092         cp $$i $$d
1093
1094 ######################################################################
1095 # Tool chain build/install
1096 ######################################################################
1097
1098 .PHONY: ccache-install
1099 ccache-install:
1100         $(MAKE) -C $(MU_BUILD_ROOT_DIR) ccache-build
1101         mkdir -p $(TOOL_INSTALL_DIR)/ccache-bin
1102         ln -sf $(MU_BUILD_ROOT_DIR)/build-tool-native/ccache/ccache \
1103                 $(TOOL_INSTALL_DIR)/ccache-bin/$(TARGET_PREFIX)gcc 
1104
1105 TOOL_MAKE = $(MAKE) is_build_tool=yes
1106
1107 tool_make_target_fn =                                                   \
1108   $(if $(strip $(NATIVE_TOOLS)),                                        \
1109     $(TOOL_MAKE) $(patsubst %,%-$(1),$(NATIVE_TOOLS)) ARCH=native || exit 1 ;) \
1110   $(TOOL_MAKE) $(patsubst %,%-$(1),$(CROSS_TOOLS))
1111
1112 .PHONY: install-tools
1113 install-tools:
1114         $(call tool_make_target_fn,install)
1115
1116 .PHONY: bootstrap-tools
1117 bootstrap-tools:
1118         $(TOOL_MAKE) make-install findutils-install git-install \
1119         automake-install autoconf-install libtool-install fakeroot-install
1120
1121
1122 ######################################################################
1123 # Clean
1124 ######################################################################
1125
1126 package_clean_script =                                                  \
1127   @$(call build_msg_fn, Cleaning $* in $(PACKAGE_INSTALL_DIR)) ;        \
1128   $(BUILD_ENV) ;                                                        \
1129   $(if $(is_build_tool),,rm -rf $(PACKAGE_INSTALL_DIR) ;)               \
1130   rm -rf $(TIMESTAMP_DIR)/$(call timestamp_name_fn,*) ;                 \
1131   $(if $($(PACKAGE)_clean),                                             \
1132     $($(PACKAGE)_clean),                                                \
1133     $(PACKAGE_MAKE) clean)
1134
1135 .PHONY: %-clean
1136 %-clean:
1137         $(package_clean_script)
1138
1139 # Wipe e.g. remove build and install directories for packages.
1140 package_wipe_script =                                                                                   \
1141   @message=$(if $(is_build_tool),"Wiping build $(PACKAGE)","Wiping build/install $(PACKAGE)") ;         \
1142   $(call build_msg_fn,$$message) ;                                                                      \
1143   $(BUILD_ENV) ;                                                                                        \
1144   rm -rf $(if $(is_build_tool),$(PACKAGE_BUILD_DIR),$(PACKAGE_INSTALL_DIR) $(PACKAGE_BUILD_DIR))
1145
1146 .PHONY: %-wipe
1147 %-wipe:
1148         $(package_wipe_script)
1149
1150 # Wipe entire build/install area for TAG and PLATFORM
1151 .PHONY: wipe-all
1152 wipe-all:
1153         @$(call build_msg_fn, Wiping $(BUILD_DIR) $(INSTALL_DIR)) ;     \
1154         $(BUILD_ENV) ;                                                  \
1155         rm -rf $(BUILD_DIR) $(INSTALL_DIR)
1156
1157 # Clean everything
1158 distclean:
1159         rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_package)*/
1160         rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_tool)*
1161         rm -rf $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)*
1162         rm -rf $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)*
1163         rm -rf $(TOOL_INSTALL_DIR)
1164         rm -rf $(MU_BUILD_ROOT_DIR)/*.deb
1165         rm -rf $(MU_BUILD_ROOT_DIR)/*.rpm
1166         rm -rf $(MU_BUILD_ROOT_DIR)/*.changes
1167         rm -rf $(MU_BUILD_ROOT_DIR)/python
1168         if [ -e /usr/bin/dh ];then (cd $(MU_BUILD_ROOT_DIR)/deb/;debian/rules clean); fi
1169         rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/*.install
1170         rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/changelog