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