summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: ace0db223713c9b581a3c3f6baab12396d04ddc5 (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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#
# $Id$
#
# Copyright (C) 2002-2005 by The PTXdist Team - See CREDITS for Details
#

PROJECT			:= PTXdist
VERSION			:= 0
PATCHLEVEL		:= 9
SUBLEVEL		:= 0
EXTRAVERSION		:=-svn

FULLVERSION		:= $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)

export PROJECT VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION FULLVERSION

#
# For out-of-tree builds we have to find out where the Makefile
# _reall_ lives; it may happen that somebody has created a softlink
#
# FIXME: how to make sure that we have at least make 3.80?
#

ifneq ($(MAKEFILE_LIST),)
 # Since 3.80, we can find out which Makefile is currently processed,
 # and infere the location of the source tree using MAKEFILE_LIST.
 TOPLEVEL_MAKEFILE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
 TOPLEVEL_MAKEFILE := $(shell \
	if [ -L "$(TOPLEVEL_MAKEFILE)" ]; then \
		readlink $(TOPLEVEL_MAKEFILE); \
	else \
		echo "$(TOPLEVEL_MAKEFILE)"; \
	fi \
 )
 OUTOFTREE := $(shell [ -L "$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))" ] && echo 1)
endif

PTXDIST_TOPDIR := $(shell dirname $(TOPLEVEL_MAKEFILE))
ifeq ($(PTXDIST_TOPDIR),)
PTXDIST_TOPDIR := $(shell test -d rules && pwd)
endif
ifeq ($(PTXDIST_TOPDIR),)
$(error Please specify the location of your source tree: make PTXDIST_TOPDIR=...)
endif
override PTXDIST_TOPDIR	:= $(shell cd $(PTXDIST_TOPDIR) && pwd)

#
# Now define the rest of the directories
#
# Make has the directory of the Makefile in $(CURDIR) 
#

HOME			:= $(shell echo $$HOME)

PTXDIST_WORKSPACE	:= $(CURDIR)

PATCHDIR		:= $(PTXDIST_TOPDIR)/patches
MISCDIR			:= $(PTXDIST_TOPDIR)/misc
RULESDIR		:= $(PTXDIST_TOPDIR)/rules

BUILDDIR		:= $(PTXDIST_WORKSPACE)/build-target
CROSS_BUILDDIR		:= $(PTXDIST_WORKSPACE)/build-cross
HOST_BUILDDIR		:= $(PTXDIST_WORKSPACE)/build-host
STATEDIR		:= $(PTXDIST_WORKSPACE)/state
IMAGEDIR		:= $(PTXDIST_WORKSPACE)/images
ROOTDIR			:= $(PTXDIST_WORKSPACE)/root

include $(RULESDIR)/Definitions.make

ifneq ($(wildcard $(HOME)/.ptxdistrc),)
include $(HOME)/.ptxdistrc
else
include $(PTXDIST_TOPDIR)/config/setup/ptxdistrc.default
endif

-include $(PTXDIST_WORKSPACE)/.config

SRCDIR			:= $(strip $(call remove_quotes, $(PTXCONF_SETUP_SRCDIR)))

# ----------------------------------------------------------------------------
# Setup a list of project directories
# ----------------------------------------------------------------------------

PROJECTDIRS		:= 
ifeq (exists, $(shell test -e $(PTXCONF_SETUP_PROJECTDIR1) && echo exists))
PROJECTDIRS		+= $(PTXCONF_SETUP_PROJECTDIR1)/
endif
ifeq (exists, $(shell test -e $(PTXCONF_SETUP_PROJECTDIR2) && echo exists))
PROJECTDIRS		+= $(PTXCONF_SETUP_PROJECTDIR2)/
endif
PROJECTDIRS		+= $(wildcard $(PTXDIST_WORKSPACE)/project-*)

PROJECTDIRS		:= $(strip $(PROJECTDIRS))
 
PROJECTCONFFILE		:= $(shell find $(PROJECTDIRS) -name $(PTXCONF_PROJECT).ptxconfig)
PROJECTDIR		:= $(strip $(shell test -z "$(PROJECTCONFFILE)" || dirname $(PROJECTCONFFILE)))
PROJECTRULES		:= $(wildcard $(PROJECTDIR)/rules/*.make)
ifneq ($(PROJECTDIR),)
PROJECTRULESDIR		:= $(PROJECTDIR)/rules
PROJECTPATCHDIR		:= $(PROJECTDIR)/patches
endif

MENU			:=  $(shell 						\
				if [ -e $(PROJECTDIR)/Kconfig ]; then		\
					echo $(PROJECTDIR)/Kconfig; 		\
				else 						\
					echo $(PTXDIST_TOPDIR)/config/Kconfig;	\
				fi						\
			   )

# ----------------------------------------------------------------------------
# Packets for host, cross and target
# ----------------------------------------------------------------------------

PACKAGES           =
PACKAGES-y         =
CROSS_PACKAGES     =
CROSS_PACKAGES-y   =
HOST_PACKAGES      =
HOST_PACKAGES-y    =
VIRTUAL            =

export TAR PTXDIST_TOPDIR BUILDDIR ROOTDIR SRCDIR PTXSRCDIR STATEDIR HOST_PACKAGES-y CROSS_PACKAGES-y PACKAGES-y

# ----------------------------------------------------------------------------
# PTXCONF_PREFIX can be overwritten from the make var PREFIX 
# ----------------------------------------------------------------------------

ifdef PREFIX
PTXCONF_PREFIX=$(PREFIX)
PREFIX=
endif

ifeq ($(PTXCONF_PREFIX),)
PTXCONF_PREFIX=$(PTXDIST_WORKSPACE)/local
endif

# FIXME: this should be removed some day...
PTXCONF_TARGET_CONFIG_FILE ?= none
ifeq ("", $(PTXCONF_TARGET_CONFIG_FILE))
PTXCONF_TARGET_CONFIG_FILE = none
endif
-include config/arch/$(call remove_quotes,$(PTXCONF_TARGET_CONFIG_FILE))
# /FIXME

# ----------------------------------------------------------------------------
# Include all rule files
# ----------------------------------------------------------------------------

all: help

include $(RULESDIR)/Rules.make
include $(RULESDIR)/Version.make

TMP_PROJECTRULES_IN = $(filter-out 			\
		$(RULESDIR)/Virtual.make 		\
		$(RULESDIR)/Rules.make 			\
		$(RULESDIR)/Version.make 		\
		$(RULESDIR)/Definitions.make,		\
		$(wildcard $(RULESDIR)/*.make)		\
) $(PROJECTRULES)

TMP_PROJECTRULES_FINAL = $(shell 			\
	$(PTXDIST_TOPDIR)/scripts/select_projectrules 	\
	"$(PTXDIST_TOPDIR)/rules" 			\
	"$(PROJECTDIR)/rules" 				\
	"$(TMP_PROJECTRULES_IN)" 			\
)

include $(TMP_PROJECTRULES_FINAL)

include $(RULESDIR)/Virtual.make

PACKAGES = $(PACKAGES-y)
CROSS_PACKAGES = $(CROSS_PACKAGES-y)
HOST_PACKAGES = $(HOST_PACKAGES-y)
VIRTUAL = $(VIRTUAL-y)

ALL_PACKAGES = $(PACKAGES-y) $(PACKAGES-) $(CROSS_PACKAGES) $(CROSS_PACKAGES-) $(HOST_PACKAGES) $(HOST_PACKAGES-)

# ----------------------------------------------------------------------------
# Install targets 
# ----------------------------------------------------------------------------

PACKAGES_TARGETINSTALL 	:= $(addsuffix _targetinstall,$(PACKAGES)) $(addsuffix _targetinstall,$(VIRTUAL))
PACKAGES_GET		:= $(addsuffix _get,$(PACKAGES))
PACKAGES_EXTRACT	:= $(addsuffix _extract,$(PACKAGES))
PACKAGES_PREPARE	:= $(addsuffix _prepare,$(PACKAGES))
PACKAGES_COMPILE	:= $(addsuffix _compile,$(PACKAGES))
HOST_PACKAGES_INSTALL	:= $(addsuffix _install,$(HOST_PACKAGES))
HOST_PACKAGES_GET	:= $(addsuffix _get,$(HOST_PACKAGES))
HOST_PACKAGES_EXTRACT	:= $(addsuffix _extract,$(HOST_PACKAGES))
HOST_PACKAGES_PREPARE	:= $(addsuffix _prepare,$(HOST_PACKAGES))
HOST_PACKAGES_COMPILE	:= $(addsuffix _compile,$(HOST_PACKAGES))
CROSS_PACKAGES_INSTALL	:= $(addsuffix _install,$(CROSS_PACKAGES))
CROSS_PACKAGES_GET	:= $(addsuffix _get,$(CROSS_PACKAGES))
CROSS_PACKAGES_EXTRACT	:= $(addsuffix _extract,$(CROSS_PACKAGES))
CROSS_PACKAGES_PREPARE	:= $(addsuffix _prepare,$(CROSS_PACKAGES))
CROSS_PACKAGES_COMPILE	:= $(addsuffix _compile,$(CROSS_PACKAGES))

# FIXME: should probably go somewhere else (separate images.make?)
ifdef PTXCONF_MKNBI_NBI
MKNBI_EXT = "nbi"
else
MKNBI_EXT = "elf"
endif

ifdef PTXCONF_USE_EXTERNAL_KERNEL
MKNBI_KERNEL = $(PTXCONF_IMAGE_MKNBI_EXT_KERNEL)
else
MKNBI_KERNEL = $(KERNEL_TARGET_PATH)
endif

MKNBI_ROOTFS = $(IMAGEDIR)/root.ext2
ifdef PTXCONF_IMAGE_EXT2_GZIP
MKNBI_ROOTFS = $(IMAGEDIR)/root.ext2.gz
endif

# ----------------------------------------------------------------------------
# Targets
# ----------------------------------------------------------------------------

help:
	@echo
	@echo "PTXdist - Build System for Embedded Linux Systems"
	@echo
	@echo "Most Useful Targets:"
	@echo
	@echo "  make projects                     show predefined project configs"
	@echo "  make setup                        setup PTXdist per-user-preferences"
	@echo "  make menuconfig|xconfig|gconfig   configure the root filesystem"
	@echo
	@echo "  make world                        make-everything-and-be-happy"
	@echo "  make run                          run simulation (only works in NATIVE=1 mode)"
	@echo
	@echo "  make imagesclean                  cleanup images directory"
	@echo "  make rootclean                    cleanup root directory for target"
	@echo "  make projectclean                 cleanup build-target directory"
	@echo "  make clean                        cleanup build-host and build-cross dirs"
	@echo "  make distclean                    cleanup everything"
	@echo
	@echo "  make images                       build root filesystem images"
	@echo "  make svn-up                       run \"svn update\" in topdir and project dir"
	@echo "  make svn-stat                     run \"svn stat\" in topdir and project dir"
	@echo
	@echo "Targets for Testing & QA:"
	@echo
	@echo "  make cuckoo-test                  search for cuckoo-eggs in root system"
	@echo "  make config-test                  run oldconfig on all ptxconfig files"
	@echo "  make ipkg-test                    check if ipkg packets are consistent "
	@echo "                                    with ROOTDIR"
	@echo "  make qa-static	                   run qa checks from scripts/qa-static which"
	@echo "                                    perform static analysis steps"
	@echo "  make qa-autobuild                 run autobuild scripts from"
	@echo "                                    scripts/qa-autobuild"
	@echo
	@echo "Targets for Autobuilding:"
	@echo
	@echo "  make toolchains                   build all supported toolchains"
	@echo "  make compile-test                 compile all supported targets, with report"
	@echo
#	@echo "Temporarily Broken:"
#	@echo
#	@echo "  make get                          download (most) of the needed packets"
#	@echo "  make extract                      extract all needed archives"
#	@echo "  make prepare                      prepare the configured system "
#	@echo "                                    for compilation"
#	@echo "  make compile                      compile the packages"
#	@echo "  make install                      install to rootdirectory"
#	@echo
	@echo "Make Variables:"
	@echo
	@echo "  NATIVE=1                          build with native compiler "
	@echo "                                    instead of cross compiler"
	@echo "  PREFIX=<path>                     build into this directory, instead of"
	@echo "                                    building into PTXCONF_PREFIX from config"
	@echo


# FIXME: add check_tools getclean here
get: $(PACKAGES_GET) $(HOST_PACKAGES_GET) $(CROSS_PACKAGES_GET)

# extract: check_tools $(PACKAGES_EXTRACT)
# prepare: check_tools $(PACKAGES_PREPARE)
# compile: check_tools $(PACKAGES_COMPILE)
# install: check_tools $(PACKAGES_TARGETINSTALL)

dep_output_clean:
#	if [ -e $(DEP_OUTPUT) ]; then rm -f $(DEP_OUTPUT); fi
	touch $(DEP_OUTPUT)

dep_tree: $(STATEDIR)/dep_tree

$(STATEDIR)/dep_tree:
ifndef NATIVE
	@echo "Launching cuckoo-test"
	@$(PTXDIST_TOPDIR)/scripts/cuckoo-test $(PTXCONF_ARCH) $(ROOTDIR) $(PTXCONF_COMPILER_PREFIX)
ifdef PTXCONF_IMAGE_IPKG
	@echo "Launching ipkg-test"
	@IMAGES=$(IMAGEDIR) ROOT=$(ROOTDIR) IPKG=$(PTXCONF_PREFIX)/bin/ipkg-cl $(PTXDIST_TOPDIR)/scripts/ipkg-test
endif
endif
	@if dot -V 2> /dev/null; then \
		echo "creating dependency graph..."; \
		sort $(DEP_OUTPUT) | uniq | $(PTXDIST_TOPDIR)/scripts/makedeptree | $(DOT) -Tps > $(DEP_TREE_PS); \
		if [ -x "`which poster`" ]; then \
			echo "creating A4 version..."; \
			poster -v -c0\% -mA4 -o$(DEP_TREE_A4_PS) $(DEP_TREE_PS); \
		fi;\
	else \
		echo "Install 'dot' from graphviz packet if you want to have a nice dependency tree"; \
	fi
	$(call touch, $@)

dep_world: $(HOST_PACKAGES_INSTALL) \
	   $(CROSS_PACKAGES_INSTALL) \
	   $(PACKAGES_TARGETINSTALL)
	@echo $@ : $^ | sed  -e 's/\([^ ]*\)_\([^_]*\)/\1.\2/g' >> $(DEP_OUTPUT)

world: check_tools dep_output_clean dep_world $(BOOTDISK_TARGETINSTALL) dep_tree 

host-tools:    check_tools dep_output_clean $(HOST_PACKAGES_INSTALL)  dep_tree
host-get:      check_tools getclean $(HOST_PACKAGES_GET) 
host-extract:  check_tools $(HOST_PACKAGES_EXTRACT)
host-prepare:  check_tools $(HOST_PACKAGES_PREPARE)
host-compile:  check_tools $(HOST_PACKAGES_COMPILE)
host-install:  check_tools $(HOST_PACKAGES_INSTALL)

cross-tools:   check_tools dep_output_clean $(CROSS_PACKAGES_INSTALL)  dep_tree
cross-get:     check_tools getclean $(CROSS_PACKAGES_GET) 
cross-extract: check_tools $(CROSS_PACKAGES_EXTRACT)
cross-prepare: check_tools $(CROSS_PACKAGES_PREPARE)
cross-compile: check_tools $(CROSS_PACKAGES_COMPILE)
cross-install: check_tools $(CROSS_PACKAGES_INSTALL)

# Robert-is-faster-typing-than-thinking shortcut
owrld: world

#
# Things which have to be done before _any_ suffix rule is executed
# (especially PTXDIST_PATH handling)
#

$(PACKAGES_PREPARE): before_prepare
before_prepare:
	@for path in `echo $(PTXDIST_PATH) | awk -F: '{for (i=1; i<=NF; i++) {print $$i}}'`; do \
		if [ ! -d $$path ]; then							\
			echo "warning: PTXDIST_PATH component \"$$path\" is no directory";	\
		fi;										\
	done;

# ----------------------------------------------------------------------------
# Images
# ----------------------------------------------------------------------------

DOPERMISSIONS = '{	\
	if ($$1 == "f")	\
		printf("chmod %s .%s; chown %s.%s .%s;\n", $$5, $$2, $$3, $$4, $$2);	\
	if ($$1 == "n")	\
		printf("mknod -m %s .%s %s %s %s; chown %s.%s .%s;\n", $$5, $$2, $$6, $$7, $$8, $$3, $$4, $$2);}'

images: $(STATEDIR)/images

ipkg-push: $(STATEDIR)/ipkg-push

$(STATEDIR)/ipkg-push:
	@$(call targetinfo, $@)
	$(PTXDIST_TOPDIR)/scripts/ipkg-push \
		--ipkgdir  $(call remove_quotes,$(IMAGEDIR)) \
		--repodir  $(call remove_quotes,$(PTXCONF_SETUP_IPKG_REPOSITORY)) \
		--revision $(call remove_quotes,$(FULLVERSION)) \
		--project  $(call remove_quotes,$(PTXCONF_PROJECT)) \
		--dist     $(call remove_quotes,$(PTXCONF_PROJECT)$(PTXCONF_PROJECT_VERSION))
	@echo
	$(call touch, $@)

images_deps =  world
ifdef PTXCONF_IMAGE_IPKG_IMAGE_FROM_REPOSITORY
images_deps += $(STATEDIR)/ipkg-push
endif

$(STATEDIR)/images: $(images_deps)
ifdef PTXCONF_IMAGE_TGZ
	cd $(ROOTDIR); \
	($(AWK) -F: $(DOPERMISSIONS) $(IMAGEDIR)/permissions && \
	echo "tar -zcvf $(IMAGEDIR)/root.tgz . ") | $(FAKEROOT) --
endif
ifdef PTXCONF_IMAGE_JFFS2
ifdef PTXCONF_IMAGE_IPKG
	@imagesfrom=$(IMAGEDIR);								\
	echo "Creating rootfs using packages from $$imagesfrom";			\
	PATH=$(PTXCONF_PREFIX)/bin:$$PATH $(PTXDIST_TOPDIR)/scripts/make_image_root.sh 	\
		-i $$imagesfrom								\
		-r $(ROOTDIR)								\
		-p $(IMAGEDIR)/permissions						\
		-e $(PTXCONF_IMAGE_JFFS2_BLOCKSIZE)					\
		-j $(PTXCONF_IMAGE_JFFS2_EXTRA_ARGS)					\
		-o $(IMAGEDIR)/root.jffs2
else
	PATH=$(PTXCONF_PREFIX)/bin:$$PATH $(PTXDIST_TOPDIR)/scripts/make_image_root.sh	\
		-r $(ROOTDIR)								\
		-p $(IMAGEDIR)/permissions						\
		-e $(PTXCONF_IMAGE_JFFS2_BLOCKSIZE)					\
		-j $(PTXCONF_IMAGE_JFFS2_EXTRA_ARGS)					\
		-o $(IMAGEDIR)/root.jffs2
endif
endif
ifdef PTXCONF_IMAGE_HD
	$(PTXDIST_TOPDIR)/scripts/genhdimg \
	-m $(GRUB_DIR)/stage1/stage1 \
	-n $(GRUB_DIR)/stage2/stage2 \
	-r $(ROOTDIR) \
	-i images \
	-f $(PTXCONF_IMAGE_HD_CONF)
endif
ifdef PTXCONF_IMAGE_EXT2
	cd $(ROOTDIR); \
	($(AWK) -F: $(DOPERMISSIONS) $(IMAGEDIR)/permissions && \
	( \
		echo -n "$(PTXCONF_HOST_PREFIX)/bin/genext2fs "; \
		echo -n "-b $(PTXCONF_IMAGE_EXT2_SIZE) "; \
		echo -n "$(PTXCONF_IMAGE_EXT2_EXTRA_ARGS) "; \
		echo -n "-d $(ROOTDIR) "; \
		echo "$(IMAGEDIR)/root.ext2" ) \
	) | $(FAKEROOT) --
endif
ifdef PTXCONF_IMAGE_EXT2_GZIP
	rm -f $(IMAGEDIR)/root.ext2.gz
	gzip -v9 $(IMAGEDIR)/root.ext2
endif
ifdef PTXCONF_IMAGE_UIMAGE
	$(PTXCONF_PREFIX)/bin/u-boot-mkimage \
		-A $(PTXCONF_ARCH) \
		-O Linux \
		-T ramdisk \
		-C gzip \
		-n $(PTXCONF_IMAGE_UIMAGE_NAME) \
		-d $(IMAGEDIR)/root.ext2.gz \
		$(IMAGEDIR)/uRamdisk
endif
ifdef PTXCONF_IMAGE_MKNBI
	PERL5LIB=$(PTXCONF_PREFIX)/usr/local/lib/mknbi \
		 $(PTXCONF_PREFIX)/usr/local/lib/mknbi/mknbi \
		--format=$(MKNBI_EXT) \
		--target=linux \
		--output=$(IMAGEDIR)/$(PTXCONF_PROJECT).$(MKNBI_EXT) \
		-a $(PTXCONF_IMAGE_MKNBI_APPEND) \
		$(MKNBI_KERNEL) \
		$(MKNBI_ROOTFS)
endif
	touch $@

# ----------------------------------------------------------------------------
# Simulation
# ----------------------------------------------------------------------------

uml_cmdline=
ifdef PTXCONF_KERNEL_HOST_ROOT_HOSTFS
uml_cmdline += root=/dev/root rootflags=$(ROOTDIR) rootfstype=hostfs
endif
ifdef PTXCONF_KERNEL_HOST_CONSOLE_STDSERIAL
uml_cmdline += ssl0=fd:0,fd:1
endif
#uml_cmdline += eth0=slirp
uml_cmdline += $(PTXCONF_KERNEL_HOST_CMDLINE)

run: world
	@$(call targetinfo, "Run Simulation")
ifdef NATIVE
	@echo "starting Linux..."
	@echo
	@$(ROOTDIR)/boot/linux $(call remove_quotes,$(uml_cmdline))
else
	@echo
	@echo "cannot run simulation if not in NATIVE=1 mode"
	@echo
	@exit 1
endif

# ----------------------------------------------------------------------------
# Configuration system
# ----------------------------------------------------------------------------

# FIXME: move this to a saner place, now that lxdialog is a host tool

ptx_lxdialog:
	@echo -e "#include \"ncurses.h\"\nint main(void){}" | gcc -E - > /dev/null; 	\
	if [ "$$?" = "1" ]; then							\
		echo;									\
		echo "Error: you don't seem to have ncurses.h; this probably means"; 	\
		echo "       that you'll have to install some ncurses-devel packet";	\
		echo "       from your distribution.";					\
		echo;									\
		exit 1;									\
	fi

check_problematic_configs = 								\
	$(call targetinfo,checking problematic configs);				\
	echo "checking \$$PTXDIST_WORKSPACE/.config";					\
	if [ -f "$(PTXDIST_WORKSPACE)/.config" ] &&					\
	   [ -n "`grep "DONT_COMPILE_KERNEL" $(PTXDIST_WORKSPACE)/.config`" ] ;	then	\
		echo;									\
		echo "error: your .config file contains PTXCONF_DONT_COMPILE_KERNEL (obsolete)";\
		echo "error: please set PTXCONF_COMPILE_KERNEL correctly and re-run!";	\
		echo;									\
		echo "example: old: '\# PTXCONF_DONT_COMPILE_KERNEL is not set'";	\
		echo "         new: 'PTXCONF_COMPILE_KERNEL=y'";			\
		echo "or:      old: 'PTXCONF_DONT_COMPILE_KERNEL=y'";			\
		echo "         new: '\# PTXCONF_COMPILE_KERNEL is not set'";		\
		echo;									\
		echo "The PTXdist team apologizes for any inconvenience :-)";		\
		echo;									\
		exit 1;									\
	fi;										\
	echo -n "checking \$$PTXDIST_WORKSPACE/config/setup";				\
	if [ -n "$(OUTOFTREE)" ] && [ ! -d "$(PTXDIST_WORKSPACE)/config/setup" ]; then	\
		echo "-> out-of-tree build, creating setup dir";			\
		rm -fr $(PTXDIST_WORKSPACE)/config/setup;				\
		mkdir -p $(PTXDIST_WORKSPACE)/config; 					\
		cp -a $(PTXDIST_TOPDIR)/config/setup $(PTXDIST_WORKSPACE)/config; 	\
		for i in $(PTXDIST_TOPDIR)/config/uClibc* $(PTXDIST_TOPDIR)/config/busybox*; do \
			ln -sf $$i $(PTXDIST_WORKSPACE)/config/`basename $$i`; 		\
		done; 									\
	else										\
		echo;									\
	fi;										\
	echo "checking \$$PTXDIST_WORKSPACE/rules";					\
	test -e "$(PTXDIST_WORKSPACE)/rules" || ln -sf $(PTXDIST_TOPDIR)/rules $(PTXDIST_WORKSPACE)/rules


menuconfig: $(STATEDIR)/host-lxdialog.install $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/mconf $(MENU)
	# automatic silentoldconfig for consistent .config files 
	@if [ -f $(PTXDIST_WORKSPACE)/.config ]; then cd $(PTXDIST_WORKSPACE) && make silentoldconfig; fi

xconfig: $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/qconf $(MENU)
	# automatic silentoldconfig for consistent .config files 
	cd $(PTXDIST_WORKSPACE) && make silentoldconfig

gconfig: $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	LD_LIBRARY_PATH=$(PTXDIST_TOPDIR)/scripts/kconfig cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/gconf $(MENU)
	# automatic silentoldconfig for consistent .config files 
	cd $(PTXDIST_WORKSPACE) && make silentoldconfig

oldconfig: $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/conf -o $(MENU)

silentoldconfig: $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/conf -s $(MENU)

allyesconfig: $(STATEDIR)/host-kconfig.install
	@$(call check_problematic_configs)
	$(call findout_config)
	cd $(PTXDIST_WORKSPACE) && $(PTXDIST_WORKSPACE)/scripts/kconfig/conf -y $(MENU)

configdeps_deps := $(wildcard $(RULESDIR)/*.in) 
ifndef ($(PROJECTRULESDIR),)
configdeps_deps += $(wildcard $(PROJECTRULESDIR)/*.in)
endif
ifneq ($(shell test -e $(PTXDIST_WORKSPACE)/.config && echo "y"),)
configdeps_deps += $(PTXDIST_WORKSPACE)/.config
endif

configdeps: $(STATEDIR)/configdeps

$(STATEDIR)/configdeps: $(STATEDIR)/host-kconfig.install $(configdeps_deps)
	@$(call check_problematic_configs)
	@$(call findout_config)
	@$(call targetinfo,generating dependencies from kconfig)
	@mkdir -p $(IMAGEDIR)
	@cd $(PTXDIST_WORKSPACE) && \
		yes "" | $(PTXDIST_WORKSPACE)/scripts/kconfig/conf -O $(MENU) | grep -e "^DEP:.*:.*" \
			2> /dev/null > $(STATEDIR)/configdeps;
	@#if [ "$?" != "0" ]; then						\
	#	echo "error: configdeps failed!";				\
	#	exit 1;								\
	#fi
	@echo

setup: before_config $(STATEDIR)/host-lxdialog.install $(STATEDIR)/host-kconfig.install
	@rm -f $(PTXDIST_WORKSPACE)/config/setup/.config
	@ln -sf $(PTXDIST_WORKSPACE)/scripts $(PTXDIST_WORKSPACE)/config/setup/scripts
	@if [ -f $(HOME)/.ptxdistrc ]; then 					\
		echo "using \$$HOME/.ptxdistrc"; 				\
		cp $(HOME)/.ptxdistrc $(PTXDIST_WORKSPACE)/config/setup/.config;\
	else									\
		cp $(PTXDIST_WORKSPACE)/config/setup/ptxdistrc.default		\
		   $(PTXDIST_WORKSPACE)/config/setup/.config;			\
	fi
	@(cd $(PTXDIST_WORKSPACE)/config/setup && $(PTXDIST_WORKSPACE)/scripts/kconfig/mconf Kconfig)
	@echo "cleaning up after setup..."
	@for i in .tmpconfig.h .config.old .config.cmd; do			\
		rm -f $(PTXDIST_WORKSPACE)/config/setup/$$i; 			\
	done
	@if [ -f $(PTXDIST_WORKSPACE)/config/setup/.config ]; then 		\
		echo "copying new .ptxdistrc to $(HOME)...";			\
		mv $(PTXDIST_WORKSPACE)/config/setup/.config $(HOME)/.ptxdistrc;\
		echo "done.";							\
	fi

# ----------------------------------------------------------------------------
# Config Targets
# ----------------------------------------------------------------------------

%_config:
	@echo; \
	echo "[Searching for Config File:]"; 						\
	CFG="`find $(PROJECTDIRS) -name $(subst _config,.ptxconfig,$@) 2> /dev/null`";	\
	if [ `echo $$CFG | wc -w` -gt 1 ]; then						\
		echo "ERROR: more than one config file found:"; 		\
		echo $$CFG; echo; 						\
		exit 1;								\
	fi;									\
	if [ -n "$$CFG" ]; then 						\
		echo "config file: \"$$CFG\""; 					\
		cp $$CFG $(PTXDIST_WORKSPACE)/.config; 				\
		echo "workspace:   \"$(PTXDIST_WORKSPACE)\"";			\
	else 									\
		echo "could not find config file \"$@\""; 			\
		exit 1;								\
	fi; 									\
	echo

# ----------------------------------------------------------------------------
# Test
# ----------------------------------------------------------------------------

config-test: 
	@for i in `find $(PROJECTDIRS) -name "*.ptxconfig"`; do 	\
		OUT=`basename $$i`;					\
		$(call targetinfo,$$OUT);				\
		cp $$i .config;						\
		make oldconfig;						\
		cp .config $$i;						\
	done

compile-test:
	@echo 
	@echo "compile-test is obsolete, run qa-autobuild target instead."
	@echo

cuckoo-test: world
	@scripts/cuckoo-test $(PTXCONF_ARCH) $(ROOTDIR) $(PTXCONF_COMPILER_PREFIX)

ipkg-test: world
	@$(call targetinfo,ipkg-test)
	@IMAGES=$(IMAGEDIR) ROOT=$(ROOTDIR) \
	IPKG=$(call remove_quotes,$(PTXCONF_PREFIX))/bin/ipkg-cl \
		$(PTXDIST_TOPDIR)/scripts/ipkg-test

# ----------------------------------------------------------------------------

toolchains:
	cd $(PTXDIST_WORKSPACE); 					\
	rm -f TOOLCHAINS;						\
	echo "Automatic Toolchain Compilation" >> TOOLCHAINS;		\
	echo "--------------------------" >> TOOLCHAINS;		\
	echo >> TOOLCHAINS;						\
	echo start: `date` >> TOOLCHAINS;				\
	echo >> TOOLCHAINS;						\
	scripts/compile-test /usr/bin toolchain_arm-softfloat-linux-gnu-2.95.3_glibc_2.2.5     TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_arm-softfloat-linux-gnu-3.3.3_glibc_2.3.2      TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_arm-softfloat-linux-gnu-3.3.6_glibc_2.3.2_linux_2.6.14 TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_arm-softfloat-linux-gnu-3.4.5_glibc_2.3.6      TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_arm-softfloat-linux-uclibc-3.3.3_uClibc-0.9.27 TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_i586-unknown-linux-gnu-2.95.3_glibc-2.2.5      TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_i586-unknown-linux-gnu-3.4.5_glibc-2.3.6       TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_i586-unknown-linux-uclibc-3.3.3_uClibc-0.9.27  TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_m68k-unknown-linux-uclibc-3.3.3_uClibc-0.9.27  TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_powerpc-405-linux-gnu-3.2.3_glibc-2.2.5        TOOLCHAINS;\
	scripts/compile-test /usr/bin toolchain_powerpc-604-linux-gnu-3.4.1_glibc-2.3.3        TOOLCHAINS;\
	echo >> TOOLCHAINS;						\
	echo stop: `date` >> TOOLCHAINS;				\
	echo >> TOOLCHAINS;
	
#	scripts/compile-test /usr/bin toolchain_i586-unknown-linux-gnu-3.4.2_glibc-2.3.3       TOOLCHAINS;\
#	scripts/compile-test /usr/bin toolchain_i586-unknown-linux-gnu-3.4.4_glibc-2.3.5       TOOLCHAINS;\

# ----------------------------------------------------------------------------

qa-static:
	@cd $(PTXDIST_WORKSPACE);					\
	rm -f QA.log;							\
	echo "QA: Static Analysis Report" >> QA-static.log;		\
	echo start: `date` >> QA-static.log;                   		\
	echo >> QA-static.log;						\
	scripts/qa-static/master >> QA-static.log 2>&1;			\
	echo >> QA-static.log;						\
	echo stop: `date` >> QA-static.log;				\
	echo >> QA-static.log;
	@cat QA-static.log;

# ----------------------------------------------------------------------------

qa-autobuild:
	@cd $(PTXDIST_WORKSPACE);							\
	rm -f QA-autobuild.log;								\
	echo | tee -a QA-autobuild.log;							\
	echo "QA: Autobuild Report" | tee -a QA-autobuild.log;				\
	echo "start: `date`" | tee -a QA-autobuild.log;                			\
	echo | tee -a QA-autobuild.log;							\
											\
	for i in `find $(PROJECTDIRS) -name "*.ptxconfig"`; do 				\
		autobuild=`echo $$i | perl -p -e "s/.ptxconfig/.autobuild/g"`; 		\
		if [ -x "$$autobuild" ]; then						\
			PTXDIST_TOPDIR=$(PTXDIST_TOPDIR)				\
			PTXDIST_WORKSPACE=$(PTXDIST_WORKSPACE)				\
			$$autobuild | tee -a QA-autobuild.log;				\
		else									\
			echo "skipping `basename $$autobuild`|tee -a QA-autobuild.log";	\
		fi;									\
	done;										\
	echo | tee -a QA-autobuild.log;							\
	echo stop: `date` | tee -a QA-autobuild.log;					\
	echo | tee -a QA-autobuild.log

# ----------------------------------------------------------------------------
# Cleaning
# ----------------------------------------------------------------------------

imagesclean:
	@echo -n "cleaning images dir.............. "
	@rm -fr $(IMAGEDIR)
	@rm -f $(STATEDIR)/images
	@echo "done."
	@echo

rootclean: imagesclean
	@echo -n "cleaning root dir................ "
	@rm -fr $(ROOTDIR)
	@echo "done."
	@echo -n "cleaning state/*.targetinstall... "
	@rm -f $(STATEDIR)/*.targetinstall
	@echo "done."	
	@echo -n "cleaning permissions............. "
	@rm -f $(IMAGEDIR)/permissions
	@echo "done."
	@echo

projectclean: rootclean
	@echo -n "cleaning state dir............... "
	@for i in $(notdir $(wildcard $(STATEDIR)/*)); do 		\
		[ -n "`echo \"$$i\" | grep host-`" ] || rm -fr $(STATEDIR)/$$i;\
	done
	@echo "done."
	@echo -n "cleaning host build dir.......... "
	@for i in $(wildcard $(BUILDDIR)/*); do 			\
		echo -n $$i; 						\
		rm -rf $$i; 						\
		echo; echo -n "                                  ";	\
	done
	@rm -fr $(BUILDDIR)
	@echo "done."
	@echo -n "cleaning dependency tree ........ "
	@rm -f $(DEP_OUTPUT) $(DEP_TREE_PS) $(DEP_TREE_A4_PS)
	@echo "done."
	@if [ -d $(PTXDIST_TOPDIR)/Documentation/manual ]; then				\
		echo -n "cleaning manual.................. ";				\
		make -C $(PTXDIST_TOPDIR)/Documentation/manual clean > /dev/null;	\
		echo "done.";								\
	fi;
	@echo

clean: projectclean
	@echo -n "cleaning build-host dir.......... "
	@for i in $(wildcard $(STATEDIR)/*); do rm -rf $$i; done
	@for i in $(wildcard $(HOST_BUILDDIR)/*); do 			\
		echo -n $$i; 						\
		rm -rf $$i; 						\
		echo; echo -n "                                  ";	\
	done
	@rm -fr $(HOST_BUILDDIR)
	@echo "done."
	@echo -n "cleaning build-cross dir......... "
	@for i in $(wildcard $(CROSS_BUILDDIR)/*); do 			\
		echo -n $$i; 						\
		rm -rf $$i; 						\
		echo; echo -n "                                  ";	\
	done
	@rm -fr $(CROSS_BUILDDIR)
	@echo "done."
	@echo -n "cleaning scripts dir............. "
	@if [ -n "$(OUTOFTREE)" ]; then 				\
		rm -fr $(PTXDIST_WORKSPACE)/scripts; 			\
	else								\
		make -s -C $(PTXDIST_WORKSPACE)/scripts/kconfig clean;	\
		rm -f $(PTXDIST_WORKSPACE)/scripts/lxdialog/lxdialog;	\
		rm -f $(PTXDIST_WORKSPACE)/scripts/lxdialog/Makefile;	\
	fi
	@echo "done."
	@echo

distclean: clean
	@echo -n "cleaning logfile................. "
	@rm -f logfile* 
	@echo "done."
	@echo -n "cleaning .config................. "
	@cd $(PTXDIST_WORKSPACE) && rm -f .config* .tmp*
	@cd $(PTXDIST_WORKSPACE) && rm -f config/setup/scripts config/setup/.config scripts/scripts
	@echo "done."
	@echo -n "cleaning logs.................... "
	@rm -fr $(PTXDIST_WORKSPACE)/logs/root-orig.txt $(PTXDIST_WORKSPACE)/logs/root-ipkg.txt $(PTXDIST_WORKSPACE)/logs/root.diff
	@echo "done."
	@if [ -n "$(OUTOFTREE)" ]; then 				\
		rm -fr $(PTXDIST_WORKSPACE)/config;			\
		rm -fr $(PTXDIST_WORKSPACE)/rules;			\
		rm -fr $(PTXDIST_WORKSPACE)/state;			\
	fi
	@echo

# ----------------------------------------------------------------------------
# SVN Targets
# ----------------------------------------------------------------------------

svn-up:
	@$(call targetinfo, Updating in Toplevel)
	@cd $(PTXDIST_TOPDIR) && svn update
	@if [ -d "$(PROJECTDIR)" ]; then				\
		$(call targetinfo, Updating in PROJECTDIR);		\
		cd $(PROJECTDIR);					\
		[ -d .svn ] && svn update; 				\
	fi;
	@echo "done."

svn-stat:
	@$(call targetinfo, svn stat in Toplevel)
	@cd $(PTXDIST_TOPDIR) && svn stat
	@if [ -d "$(PROJECTDIR)" ]; then				\
		$(call targetinfo, svn stat in PROJECTDIR);		\
		cd $(PROJECTDIR);					\
		[ -d .svn ] && svn stat; 				\
	fi;
	@echo "done."

# ----------------------------------------------------------------------------
# Misc other targets
# ----------------------------------------------------------------------------

archive:  
	@echo
	@echo -n "packaging additional sources ...... "
	scripts/collect_sources.sh $(PTXDIST_TOPDIR) $(shell basename $(PTXDIST_TOPDIR))

archive-toolchain: virtual-xchain_install
	$(TAR) -C $(PTXCONF_PREFIX)/.. -jcvf $(PTXDIST_TOPDIR)/$(PTXCONF_GNU_TARGET).tar.bz2 \
		$(shell basename $(PTXCONF_PREFIX))

.PHONY: projects
projects:
	@for dir in $(call remove_quotes,$(PROJECTDIRS)); do 						\
		(cd $$dir); 										\
		if [ "$$?" != "0" ]; then								\
			echo;										\
			echo "Error: PTXCONF_SETUP_PROJECTDIR macros point to something which";		\
			echo "       is no directory. Check your .ptxdistrc file. ";			\
			echo;										\
			echo "Directory: $$dir";							\
			echo;										\
			exit 1;										\
		fi											\
	done
	@echo
	@echo "---------------------- Available PTXdist Projects: ----------------------------"
	@echo
	@for i in `find $(PROJECTDIRS) -name "*.ptxconfig"`; do 					\
		basename `echo $$i | perl -p -e "s/.ptxconfig/_config/g"`; 				\
	done | sort 
	@echo
	@echo "-------------------------------------------------------------------------------"
	@echo

configs:
	@echo
	@echo "Please use 'make projects' instead of 'make configs'. Thanks."
	@echo

%_recompile:
	@rm -f $(STATEDIR)/$*.compile
	@make -C $(PTXDIST_WORKSPACE) $*_compile

print-%:
	@echo "$* is \"$($*)\""

# ----------------------------------------------------------------------------
# Autogenerate Dependencies
# ----------------------------------------------------------------------------

%.dep: $(STATEDIR)/configdeps
	@$(PTXDIST_TOPDIR)/scripts/create_dependencies.sh \
		--action defaults \
		--rulesdir $(RULESDIR) \
		`test -n "$(PROJECTRULESDIR)" && echo "--projectrulesdir $(PROJECTRULESDIR)"` \
		--imagedir $(IMAGEDIR) \
		--statedir $(STATEDIR) \
		--dependency-file $@

# ----------------------------------------------------------------------------

.PHONY: dep_output_clean dep_tree dep_world before_config

# vim600:set foldmethod=marker: