summaryrefslogtreecommitdiff
path: root/mk/macros.mk
blob: 4ae316492eba1bcc184a368ceffc2e5887ab0dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
################################################################################
# Embtoolkit
# Copyright(C) 2009-2011 Abdoulaye Walsimou GAYE.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
# \file         macros.mk
# \brief	macros.mk of Embtoolkit
# \author       Abdoulaye Walsimou GAYE <awg@embtoolkit.org>
# \date         May 2009
################################################################################

# Embtoolkit colors
__embtk_color_red = "\E[1;31m"
__embtk_color_green = "\E[1;32m"
__embtk_color_yellow = "\E[1;33m"
__embtk_color_blue = "\E[1;34m"
__embtk_no_color = "\E[0m"

# echo colored text
#usage $(call embtk_echo_red,$(TEXT))
define embtk_echo_red
	@echo -e $(__embtk_color_red)$(1)$(__embtk_no_color)
endef
#usage $(call embtk_echo_green,$(TEXT))
define embtk_echo_green
	@echo -e $(__embtk_color_green)$(1)$(__embtk_no_color)
endef
#usage $(call echo_yellow,$(TEXT))
define echo_yellow
	@echo -e $(__embtk_color_yellow)$(1)$(__embtk_no_color)
endef
#usage $(call embtk_echo_blue,$(TEXT))
define embtk_echo_blue
	@echo -e $(__embtk_color_blue)$(1)$(__embtk_no_color)
endef

#
# __embtk_mk_strcmp:
# A macro for two strings comparison. It returns y if the strings are identical
# and nothing if not.
# Note: This macro strips passed parameters
# Usage:
# $(call __embtk_mk_strcmp,str1,str2)
#
__embtk_mk_strcmp=$(shell [ $(strip $(1)) = $(strip $(2)) ] && echo y)

#
# wget wrapper
# usage: $(call embtk_wget,$(OUTPUT_FILE),$(SITE),$(FOREIGN_FILE))
#
__wget_outfile		= $(patsubst %/,%,$(DOWNLOAD_DIR))/$(strip $(1))
__wget_remotesite	= $(patsubst %/,%,$(strip $(2)))
__wget_foreignfiles	= $(strip $(3))
__wget_opts		= --tries=5 --timeout=10 --waitretry=5
define embtk_wget
	wget $(__wget_opts) -O $(__wget_outfile)				\
	$(__wget_remotesite)/$(__wget_foreignfiles) ||				\
	wget $(__wget_opts) --no-passive-ftp -O $(__wget_outfile)		\
	$(__wget_remotesite)/$(__wget_foreignfiles)
endef

#Decompress message
#usage $(call EMBTK_DECOMPRESS_MSG,$(NAME_PACKAGE))
define EMBTK_DECOMPRESS_MSG
	$(call embtk_echo_blue,"################################################################################")
	$(call embtk_echo_blue,"# EmbToolkit # Decompressing $(1)")
	$(call embtk_echo_blue,"################################################################################")
endef

#Configure message
#usage $(call EMBTK_CONFIGURE_MSG,$(NAME_PACKAGE))
define EMBTK_CONFIGURE_MSG
	$(call embtk_echo_blue,"################################################################################")
	$(call embtk_echo_blue,"# EmbToolkit # Configuring $(1)")
	$(call embtk_echo_blue,"################################################################################")
endef

#Build message
#usage $(call EMBTK_BUILD_MSG,$(NAME_PACKAGE))
define EMBTK_BUILD_MSG
	$(call embtk_echo_blue,"################################################################################")
	$(call embtk_echo_blue,"# EmbToolkit # Building $(1)")
	$(call embtk_echo_blue,"################################################################################")
endef

#Install message
#usage $(call EMBTK_INSTALL_MSG,$(NAME_PACKAGE))
define EMBTK_INSTALL_MSG
	$(call embtk_echo_blue,"################################################################################")
	$(call embtk_echo_blue,"# EmbToolkit # Installing $(1)")
	$(call embtk_echo_blue,"################################################################################")
endef

#Install message
#usage $(call embtk_error_msg,$(MESSAGE))
define embtk_error_msg
	$(call embtk_echo_red,"################################################################################")
	$(call embtk_echo_red,"# EmbToolkit # ERROR: $(1)")
	$(call embtk_echo_red,"################################################################################")
endef

#Generic message
#usage $(call embtk_generic_message,$(GENERIC_MESSAGE))
define embtk_generic_message
	$(call embtk_echo_blue,"################################################################################")
	$(call embtk_echo_blue,"# EmbToolkit # $(1)")
	$(call embtk_echo_blue,"################################################################################")
endef
define embtk_generic_msg
	$(call embtk_generic_message,$(1))
endef

#Successful build of EmbToolkit message
successful_build:
	$(call embtk_echo_blue," --------------------- ")
	$(call embtk_echo_blue,"| Toolchain build log |")
	$(call embtk_echo_blue," --------------------- ")
	$(call embtk_echo_blue,"You successfully build your toolchain for $(GNU_TARGET)")
	$(call embtk_echo_blue,"Tools built (GCC compiler, Binutils, etc.) are located in:")
	$(call embtk_echo_blue,"    $(TOOLS)/bin")
	@echo
	$(call embtk_echo_blue," ---------------------------- ")
	$(call embtk_echo_blue,"| Root file system build log |")
	$(call embtk_echo_blue," ---------------------------- ")
ifeq ($(CONFIG_EMBTK_HAVE_ROOTFS),y)
	$(call embtk_echo_blue,"You also successfully build root filesystem(s) located in the")
	$(call embtk_echo_blue,"'generated' sub-directory of EmbToolkit.")
else
	$(call embtk_echo_green,"Build of root filesystem not selected.")
endif
	@echo
	$(call embtk_echo_blue," ---------------------------- ")
	$(call embtk_echo_blue,"| Embedded systems Toolkit   |")
	$(call embtk_echo_blue," ---------------------------- ")
	$(call embtk_echo_blue,"Hope that EmbToolkit will be useful for your project !!!")
	$(call embtk_echo_blue,"Please report any bugs/suggestion at:")
	$(call embtk_echo_blue,"   http://www.embtoolkit.org/issues/projects/show/embtoolkit")
	$(call embtk_echo_blue,"You can also visit the wiki at:")
	$(call embtk_echo_blue,"   http://www.embtoolkit.org")
	@echo
	$(call embtk_echo_blue,"################################################################################")

#
# embtk_pkgconfig_getlibs:
# A macro to get pkg-config libs entry for a target package
# Usage: $(call embtk_pkgconfig_getlibs,pkgname)
#
define embtk_pkgconfig_getlibs
	$(shell									\
		PKG_CONFIG_PATH=$(EMBTK_PKG_CONFIG_PATH)			\
		PKG_CONFIG_LIBDIR="$(EMBTK_PKG_CONFIG_LIBDIR)"			\
		$(PKGCONFIG_BIN) $(strip $(1)) --libs)
endef

#
# embtk_pkgconfig_getcflags:
# A macro to get pkg-config cflags entry for a target package
# Usage: $(call embtk_pkgconfig_getcflags,pkgname)
#
define embtk_pkgconfig_getcflags
	$(shell									\
		PKG_CONFIG_PATH=$(EMBTK_PKG_CONFIG_PATH)			\
		PKG_CONFIG_LIBDIR="$(EMBTK_PKG_CONFIG_LIBDIR)"			\
		$(PKGCONFIG_BIN) $(strip $(1)) --cflags)
endef

#Macro to adapt libtool files (*.la) for cross compiling
__ltlibdirold=libdir='\/usr\/$(LIBDIR)'
__ltlibdirnew=libdir='$(SYSROOT)\/usr\/$(LIBDIR)'
define __embtk_fix_libtool_files
	@LIBTOOLS_LA_FILES=`find $(SYSROOT)/usr/$(LIBDIR) -name *.la`;		\
	for i in $$LIBTOOLS_LA_FILES; do					\
	sed -i "s;$(__ltlibdirold);$(__ltlibdirnew);" $$i;			\
	done
endef
libtool_files_adapt:
	$(Q)$(call __embtk_fix_libtool_files)

#Macro to adapt pkg-config files for cross compiling
define __embtk_fix_pkgconfig_files
	@PKGCONFIG_FILES=`find $(SYSROOT)/usr/$(LIBDIR)/pkgconfig -name *.pc`;	\
	for i in $$PKGCONFIG_FILES; do						\
	sed -e 's;prefix=.*;prefix=$(SYSROOT)/usr;'				\
	-e 's;includedir=$${prefix}/include;includedir=$(SYSROOT)/usr/include;'	\
	-e 's;libdir=.*;libdir=$(SYSROOT)/usr/$(LIBDIR);' < $$i > $$i.new;	\
	mv $$i.new $$i;								\
	done
endef
pkgconfig_files_adapt:
	$(Q)$(call __embtk_fix_pkgconfig_files)

#A macro to remove rpath in packages that use libtool -rpath
define __embtk_kill_lt_rpath
	@cd $(strip $(1)); \
	LOCAL_LT_FILES=`find -type f -name libtool`; \
	for i in $$LOCAL_LT_FILES; \
	do \
	sed -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' $$i; \
	sed -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' $$i; \
	done
endef

#
# Get passed package variables prefix
#
PKGV=$(strip $(shell echo $(1) | tr a-z A-Z))

#
# A macro to get packages version from .config file.
# usage: $(call embtk_get_pkgversion,PACKAGE)
#
embtk_get_pkgversion = $(subst ",,$(strip $(CONFIG_EMBTK_$(PKGV)_VERSION_STRING)))

#
# A macro to test if a package is already decompressed.
# It returns y if decompressed and nothing if not.
#
__embtk_pkg_decompressed-y = $(shell test -e $($(PKGV)_SRC_DIR)/.decompressed && echo y)

#
# A macro to test if a package is already patched.
# It returns y if patched and nothing if not.
#
__embtk_pkg_patched-y = $(shell test -e $($(PKGV)_SRC_DIR)/.patched && echo y)

#
# A macro to test if a package is already configured using autotools configure
# script. It returns y if configured and nothing if not.
#
__embtk_pkg_configured-y = $(shell test -e $($(PKGV)_BUILD_DIR)/.configured && echo y)

#
# A macro to test if a package is already installed.
# It returns y if installed and nothing if not.
#
__installed_f=$($(PKGV)_BUILD_DIR)/.installed
__pkgkconfig_f=$($(PKGV)_BUILD_DIR)/.embtk.$($(PKGV)_NAME).kconfig
__pkgkconfig_f_old=$($(PKGV)_BUILD_DIR)/.embtk.$($(PKGV)_NAME).kconfig.old
__embtk_pkg_installed-y = $(shell						\
	if [ -e $(__installed_f) ] && [ -e $(__pkgkconfig_f) ]; then		\
		cp $(__pkgkconfig_f) $(__pkgkconfig_f_old);			\
		grep 'CONFIG_EMBTK_.*$(PKGV)_.*' $(EMBTK_DOTCONFIG)		\
							> $(__pkgkconfig_f);	\
		cmp -s $(__pkgkconfig_f) $(__pkgkconfig_f_old);			\
		if [ "x$$?" = "x0" ]; then					\
			echo y;							\
		fi;								\
	else									\
		mkdir -p $($(PKGV)_BUILD_DIR);					\
		grep 'CONFIG_EMBTK_.*$(PKGV)_.*' $(EMBTK_DOTCONFIG)		\
							> $(__pkgkconfig_f);	\
	fi;)

#
# A macro which runs configure script (conpatible with autotools configure)
# for a package and sets environment variables correctly.
# Usage:
# $(call embtk_configure_pkg,PACKAGE)
#
define __embtk_configure_autoreconfpkg
@if [ "x$(CONFIG_EMBTK_$(PKGV)_NEED_AUTORECONF)" == "xy" ]; then		\
	test -e $($(PKGV)_SRC_DIR)/configure.ac ||				\
	test -e $($(PKGV)_SRC_DIR)/configure.in || exit 1;			\
	cd $($(PKGV)_SRC_DIR);							\
	$(AUTORECONF) --install -f;						\
fi
endef
define __embtk_print_configure_opts
	$(call embtk_echo_blue,"Configure options:")
	@for i in `echo $(1) | tr " " "\n"`; \
	do echo -e $(__embtk_color_blue)$$i$(__embtk_no_color); done
endef
define embtk_configure_pkg
	$(call embtk_generic_msg,"Configure $($(PKGV)_PACKAGE)...")
	$(call __embtk_configure_autoreconfpkg,$(1))
	@test -e $($(PKGV)_SRC_DIR)/configure || exit 1
	$(call __embtk_print_configure_opts,"$($(PKGV)_CONFIGURE_OPTS)")
	$(Q)cd $($(PKGV)_BUILD_DIR);						\
	CC=$(TARGETCC_CACHED)							\
	CXX=$(TARGETCXX_CACHED)							\
	AR=$(TARGETAR)								\
	RANLIB=$(TARGETRANLIB)							\
	AS=$(CROSS_COMPILE)as							\
	LD=$(TARGETLD)								\
	NM=$(TARGETNM)								\
	STRIP=$(TARGETSTRIP)							\
	OBJDUMP=$(TARGETOBJDUMP)						\
	OBJCOPY=$(TARGETOBJCOPY)						\
	CFLAGS="$(TARGET_CFLAGS)"						\
	CXXFLAGS="$(TARGET_CFLAGS)"						\
	LDFLAGS="-L$(SYSROOT)/$(LIBDIR) -L$(SYSROOT)/usr/$(LIBDIR)"		\
	CPPFLAGS="-I$(SYSROOT)/usr/include"					\
	PKG_CONFIG="$(PKGCONFIG_BIN)"						\
	PKG_CONFIG_PATH="$(EMBTK_PKG_CONFIG_PATH)"				\
	PKG_CONFIG_LIBDIR="$(EMBTK_PKG_CONFIG_LIBDIR)"				\
	ac_cv_func_malloc_0_nonnull=yes						\
	ac_cv_func_realloc_0_nonnull=yes					\
	$($(PKGV)_CONFIGURE_ENV)						\
	$(CONFIG_SHELL) $($(PKGV)_SRC_DIR)/configure				\
	--build=$(HOST_BUILD) --host=$(STRICT_GNU_TARGET)			\
	--target=$(STRICT_GNU_TARGET) --libdir=/usr/$(LIBDIR)			\
	--prefix=/usr --sysconfdir=/etc --disable-rpath				\
	$($(PKGV)_CONFIGURE_OPTS)
	@touch $($(PKGV)_BUILD_DIR)/.configured
	$(call __embtk_kill_lt_rpath,$($(PKGV)_BUILD_DIR))
endef

#
# A macro which runs configure script (conpatible with autotools configure)
# for a package for host development machine and sets environment variables
# correctly.
# Usage:
# $(call embtk_configure_hostpkg,PACKAGE)
#
define embtk_configure_hostpkg
	$(call embtk_generic_msg,"Configure $($(PKGV)_PACKAGE) for host...")
	$(call __embtk_configure_autoreconfpkg,$(1))
	@test -e $($(PKGV)_SRC_DIR)/configure || exit 1
	$(call __embtk_print_configure_opts,"$($(PKGV)_CONFIGURE_OPTS)")
	@cd $($(PKGV)_BUILD_DIR);						\
	CPPFLAGS="-I$(HOSTTOOLS)/usr/include"					\
	LDFLAGS="-L$(HOSTTOOLS)/usr/lib -Wl,-rpath,$(HOSTTOOLS)/usr/lib"	\
	PKG_CONFIG="$(PKGCONFIG_BIN)"						\
	PKG_CONFIG_PATH="$(EMBTK_HOST_PKG_CONFIG_PATH)"				\
	$(if $(call __embtk_mk_strcmp,$(PKGV),CCACHE),,CC=$(HOSTCC_CACHED))	\
	$(if $(call __embtk_mk_strcmp,$(PKGV),CCACHE),,CXX=$(HOSTCXX_CACHED))	\
	$($(PKGV)_CONFIGURE_ENV)						\
	$(CONFIG_SHELL) $($(PKGV)_SRC_DIR)/configure				\
	--build=$(HOST_BUILD) --host=$(HOST_ARCH)				\
	--prefix=$(if $($(PKGV)_PREFIX),$($(PKGV)_PREFIX),$(HOSTTOOLS)/usr)	\
	$($(PKGV)_CONFIGURE_OPTS)
	@touch $($(PKGV)_BUILD_DIR)/.configured
endef

#
# Various helpers macros for different steps while installing packages.
#
__embtk_multi_make = $(foreach builddir,$($(PKGV)_MAKE_DIRS),			\
				$($(PKGV)_MAKE_ENV)				\
				$(MAKE) -C $($(PKGV)_BUILD_DIR)/$(builddir)	\
				$($(PKGV)_MAKE_OPTS) $(J);)

__embtk_single_make = $($(PKGV)_MAKE_ENV) $(MAKE) -C $($(PKGV)_BUILD_DIR)	\
			$($(PKGV)_MAKE_OPTS) $(J)

__embtk_multi_make_install = $(foreach builddir,$($(PKGV)_MAKE_DIRS),		\
	$($(PKGV)_MAKE_ENV) $(MAKE) -C $($(PKGV)_BUILD_DIR)/$(builddir)		\
	DESTDIR=$(SYSROOT)$(if $($(PKGV)_SYSROOT_SUFFIX),/$($(PKGV)_SYSROOT_SUFFIX)) \
	$($(PKGV)_MAKE_OPTS) install;)

__embtk_single_make_install = $($(PKGV)_MAKE_ENV)				\
	$(MAKE) -C $($(PKGV)_BUILD_DIR)						\
	DESTDIR=$(SYSROOT)$(if $($(PKGV)_SYSROOT_SUFFIX),/$($(PKGV)_SYSROOT_SUFFIX)) \
	$($(PKGV)_MAKE_OPTS) install

__embtk_multi_make_hostinstall = $(foreach builddir,$($(PKGV)_MAKE_DIRS),	\
	$($(PKGV)_MAKE_ENV)							\
	$(MAKE) -C $($(PKGV)_BUILD_DIR)/$(builddir)				\
	$(if $($(PKGV)_DESTDIR),DESTDIR=$($(PKGV)_DESTDIR))			\
	$($(PKGV)_MAKE_OPTS) install;)

__embtk_single_make_hostinstall = $($(PKGV)_MAKE_ENV)				\
	$(MAKE) -C $($(PKGV)_BUILD_DIR)						\
	$(if $($(PKGV)_DESTDIR),DESTDIR=$($(PKGV)_DESTDIR))			\
	$($(PKGV)_MAKE_OPTS) install

__embtk_autotolspkg-y=$(2)
define __embtk_install_pkg_make
	$(call embtk_generic_msg,"Compiling and installing $($(PKGV)_NAME)-$($(PKGV)_VERSION) in your root filesystem...")
	$(Q)$(if $(strip $($(PKGV)_DEPS)),$(MAKE) $($(PKGV)_DEPS))
	$(Q)$(call embtk_download_pkg,$(1))
	$(Q)$(call embtk_decompress_pkg,$(1))
	$(Q)$(if $(__embtk_autotolspkg-y),$(call embtk_configure_pkg,$(1)))
	$(Q)$(if $($(PKGV)_MAKE_DIRS),						\
		$(__embtk_multi_make),						\
		$(__embtk_single_make))
	$(Q)$(if $($(PKGV)_MAKE_DIRS),						\
		$(__embtk_multi_make_install),					\
		$(__embtk_single_make_install))
	$(Q)$(if $(__embtk_autotolspkg-y),$(call __embtk_fix_libtool_files))
	$(Q)$(if $(__embtk_autotolspkg-y),$(call __embtk_fix_pkgconfig_files))
	@touch $($(PKGV)_BUILD_DIR)/.installed
endef
define __embtk_install_hostpkg_make
	$(call embtk_generic_msg,"Compiling and installing $($(PKGV)_NAME)-$($(PKGV)_VERSION) for host...")
	$(Q)$(if $(strip $($(PKGV)_DEPS)),$(MAKE) $($(PKGV)_DEPS))
	$(Q)$(call embtk_download_pkg,$(1))
	$(Q)$(call embtk_decompress_hostpkg,$(1))
	$(Q)$(if $(__embtk_autotolspkg-y),$(call embtk_configure_hostpkg,$(1)))
	$(Q)$(if $($(PKGV)_MAKE_DIRS),						\
		$(__embtk_multi_make),						\
		$(__embtk_single_make))
	$(Q)$(if $($(PKGV)_MAKE_DIRS),						\
		$(__embtk_multi_make_hostinstall),				\
		$(__embtk_single_make_hostinstall))
	@touch $($(PKGV)_BUILD_DIR)/.installed
endef

#
# A macro to install automatically a package, using autotools scripts, intended
# to run on the target
# Usage:
# $(call embtk_install_pkg,package)
#
define embtk_install_pkg
	$(Q)$(if $(__embtk_pkg_installed-y),					\
		true,$(call __embtk_install_pkg_make,$(1),autotools))
endef

#
# A macro to install automatically a package, using simple Makefile and an
# install target, intented to run on the target.
# Usage:
# $(call embtk_makeinstall_pkg,package)
#
define embtk_makeinstall_pkg
	$(Q)$(if $(__embtk_pkg_installed-y),					\
		true,$(call __embtk_install_pkg_make,$(1)))
endef

#
# A macro to install automatically a package, using autotools scripts, intended
# to run on the host development machine.
# Usage:
# $(call embtk_install_hostpkg,package)
#
define embtk_install_hostpkg
	$(Q)$(if $(__embtk_pkg_installed-y),true,				\
		$(call __embtk_install_hostpkg_make,$(1),autotools))
endef

#
# A macro to install automatically a package, using simple Makefile and an
# install target, intended to run on the host development machine.
# Usage:
# $(call embtk_makeinstall_hostpkg,package)
#
define embtk_makeinstall_hostpkg
	$(Q)$(if $(__embtk_pkg_installed-y),true,				\
		$(call __embtk_install_hostpkg_make,$(1)))
endef

#
# A macro which downloads a package.
# Usage:
# $(call embtk_download_pkg,PACKAGE)
#
define __embtk_download_pkg_patches
@if [ "x$(CONFIG_EMBTK_$(PKGV)_NEED_PATCH)" == "xy" ]; then			\
	test -e $(DOWNLOAD_DIR)/$($(PKGV)_NAME)-$($(PKGV)_VERSION).patch ||	\
	$(call embtk_wget,							\
		$($(PKGV)_NAME)-$($(PKGV)_VERSION).patch,			\
		$($(PKGV)_PATCH_SITE),						\
		$($(PKGV)_NAME)-$($(PKGV)_VERSION)-*.patch);			\
fi
endef
define __embtk_download_pkg_from_mirror
if [ "x$($(PKGV)_SITE_MIRROR$(2))" == "x" ]; then 				\
	false;									\
else										\
	$(call embtk_wget,							\
		$($(PKGV)_PACKAGE),						\
		$($(PKGV)_SITE_MIRROR$(2)),					\
		$($(PKGV)_PACKAGE)); 						\
fi
endef
define embtk_download_pkg
	$(call embtk_generic_msg,"Download $($(PKGV)_PACKAGE) if necessary...")
	@test -e $(DOWNLOAD_DIR)/$($(PKGV)_PACKAGE) ||				\
	$(call embtk_wget,							\
		$($(PKGV)_PACKAGE),						\
		$($(PKGV)_SITE),						\
		$($(PKGV)_PACKAGE))||						\
	$(call __embtk_download_pkg_from_mirror,$(1),1) ||			\
	$(call __embtk_download_pkg_from_mirror,$(1),2) ||			\
	$(call __embtk_download_pkg_from_mirror,$(1),3) || exit 1
	$(call __embtk_download_pkg_patches,$(1))
endef

#
# A macro to decompress packages tarball intended to run on target.
# Usage:
# $(call embtk_decompress_pkg,PACKAGE)
#
define embtk_decompress_pkg
	$(call embtk_generic_msg,"Decrompressing $($(PKGV)_PACKAGE) ...")
	@if [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARGZ)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		tar -C $(PACKAGES_BUILD) -xzf					\
		$(DOWNLOAD_DIR)/$($(PKGV)_PACKAGE) &&				\
		mkdir -p $($(PKGV)_BUILD_DIR) &&				\
		touch $($(PKGV)_SRC_DIR)/.decompressed;				\
	elif [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARBZ2)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		tar -C $(PACKAGES_BUILD) -xjf					\
		$(DOWNLOAD_DIR)/$($(PKGV)_PACKAGE) &&				\
		mkdir -p $($(PKGV)_BUILD_DIR) &&				\
		touch $($(PKGV)_SRC_DIR)/.decompressed;				\
	elif [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARBZ2)" == "x" ] &&		\
	[ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARGZ)" == "x" ] &&			\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		echo -e "\E[1;31m!Unknown package compression type!\E[0m";	\
		exit 1;								\
	fi
	@if [ "x$(CONFIG_EMBTK_$(PKGV)_NEED_PATCH)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.patched ]; then				\
		cd $($(PKGV)_SRC_DIR);						\
		patch -p1 <							\
		$(DOWNLOAD_DIR)/$($(PKGV)_NAME)-$($(PKGV)_VERSION).patch &&	\
		touch $($(PKGV)_SRC_DIR)/.patched;				\
	fi
	@mkdir -p $($(PKGV)_BUILD_DIR)
endef

#
# A macro to decompress packages tarball intended to run on host development
# machine.
# Usage:
# $(call embtk_decompress_hostpkg,PACKAGE)
#
define embtk_decompress_hostpkg
	$(call embtk_generic_msg,"Decrompressing $($(PKGV)_PACKAGE) ...")
	@if [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARGZ)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		tar -C $(TOOLS_BUILD) -xzf					\
		$(DOWNLOAD_DIR)/$($(PKGV)_PACKAGE) &&				\
		mkdir -p $($(PKGV)_BUILD_DIR) &&				\
		touch $($(PKGV)_SRC_DIR)/.decompressed;				\
	elif [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARBZ2)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		tar -C $(TOOLS_BUILD) -xjf					\
		$(DOWNLOAD_DIR)/$($(PKGV)_PACKAGE) &&				\
		mkdir -p $($(PKGV)_BUILD_DIR) &&				\
		touch $($(PKGV)_SRC_DIR)/.decompressed;				\
	elif [ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARBZ2)" == "x" ] &&		\
	[ "x$(CONFIG_EMBTK_$(PKGV)_PKG_IS_TARGZ)" == "x" ] &&			\
	[ ! -e $($(PKGV)_SRC_DIR)/.decompressed ]; then				\
		echo -e "\E[1;31m!Unknown package compression type!\E[0m";	\
		exit 1;								\
	fi
	@if [ "x$(CONFIG_EMBTK_$(PKGV)_NEED_PATCH)" == "xy" ] &&		\
	[ ! -e $($(PKGV)_SRC_DIR)/.patched ]; then				\
		cd $($(PKGV)_SRC_DIR);						\
		patch -p1 <							\
		$(DOWNLOAD_DIR)/$($(PKGV)_NAME)-$($(PKGV)_VERSION).patch &&	\
		touch $($(PKGV)_SRC_DIR)/.patched;				\
	fi
	@mkdir -p $($(PKGV)_BUILD_DIR)
endef

#
# A macro to clean installed packages from sysroot.
# Usage:
# $(call embtk_cleanup_pkg,PACKAGE)
#
define embtk_cleanup_pkg
	$(call embtk_generic_message,"Cleanup $($(PKGV)_NAME)...")
	@-if [ "x$($(PKGV)_ETC)" != "x" ] && [ -e $(SYSROOT)/etc ];		\
		then								\
		cd $(SYSROOT)/etc; rm -rf $($(PKGV)_ETC);			\
	fi
	@-if [ "x$($(PKGV)_BINS)" != "x" ] && [ -e $(SYSROOT)/usr/bin ];	\
		then								\
		cd $(SYSROOT)/usr/bin; rm -rf $($(PKGV)_BINS);			\
	fi
	@-if [ "x$($(PKGV)_SBINS)" != "x" ] && [ -e $(SYSROOT)/usr/sbin ];	\
		then								\
		cd $(SYSROOT)/usr/sbin; rm -rf $($(PKGV)_SBINS);		\
	fi
	@-if [ "x$($(PKGV)_INCLUDES)" != "x" ] && [ -e $(SYSROOT)/usr/include ];\
		then								\
		cd $(SYSROOT)/usr/include; rm -rf $($(PKGV)_INCLUDES);		\
	fi
	@-if [ "x$($(PKGV)_LIBS)" != "x" ] && [ -e $(SYSROOT)/usr/$(LIBDIR) ];	\
		then								\
		cd $(SYSROOT)/usr/$(LIBDIR); rm -rf $($(PKGV)_LIBS);		\
	fi
	@-if [ "x$($(PKGV)_LIBEXECS)" != "x" ] &&				\
	[ -e $(SYSROOT)/usr/$(LIBDIR)/libexec ];				\
		then								\
		cd $(SYSROOT)/usr/$(LIBDIR)/libexec;				\
		rm -rf $($(PKGV)_LIBEXECS);					\
	fi
	@-if [ "x$($(PKGV)_PKGCONFIGS)" != "x" ] &&				\
	[ -e $(SYSROOT)/usr/$(LIBDIR)/pkgconfig ];				\
		then								\
		cd $(SYSROOT)/usr/$(LIBDIR)/pkgconfig;				\
		rm -rf $($(PKGV)_PKGCONFIGS);					\
	fi
	@-rm -rf $($(PKGV)_BUILD_DIR)*
endef