cfa5e52de5d64e13ba155f6b17641516e1cd9f9b
[oweals/busybox.git] / Makefile
1 # Makefile for busybox
2 #
3 # Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
4 #
5 # Licensed under GPLv2, see the file LICENSE in this tarball for details.
6 #
7
8 #--------------------------------------------------------------
9 # You shouldn't need to mess with anything beyond this point...
10 #--------------------------------------------------------------
11 noconfig_targets := menuconfig config oldconfig randconfig \
12         defconfig allyesconfig allnoconfig clean distclean \
13         release tags
14
15 # the toplevel sourcedir
16 ifndef top_srcdir
17 top_srcdir=$(CURDIR)
18 endif
19 # toplevel directory of the object-tree
20 ifndef top_builddir
21 top_builddir=$(CURDIR)
22 endif
23
24 export srctree=$(top_srcdir)
25 vpath %/Config.in $(srctree)
26
27 DIRS:=applets archival archival/libunarchive coreutils console-tools \
28         debianutils editors findutils init miscutils modutils networking \
29         networking/libiproute networking/udhcp procps loginutils shell \
30         sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils libbb
31
32 SRC_DIRS:=$(patsubst %,$(top_srcdir)/%,$(DIRS))
33
34 # That's our default target when none is given on the command line
35 .PHONY: _all
36 _all:
37
38 CONFIG_CONFIG_IN = $(top_srcdir)/sysdeps/$(TARGET_OS)/Config.in
39 CONFIG_DEFCONFIG = $(top_srcdir)/sysdeps/$(TARGET_OS)/defconfig
40
41 ifeq ($(KBUILD_SRC),)
42
43 ifdef O
44   ifeq ("$(origin O)", "command line")
45     KBUILD_OUTPUT := $(O)
46     top_builddir := $(O)
47   endif
48 else
49 # If no alternate output-dir was specified, we build in cwd
50 # We are using KBUILD_OUTPUT nevertheless to make sure that we create
51 # Rules.mak and the toplevel Makefile, in case they don't exist.
52   KBUILD_OUTPUT := $(top_builddir)
53 endif
54
55 # All object directories.
56 OBJ_DIRS := $(DIRS)
57 all_tree := $(patsubst %,$(top_builddir)/%,$(OBJ_DIRS) scripts scripts/config include)
58 all_tree: $(all_tree)
59 $(all_tree):
60         @mkdir -p "$@"
61
62 ifneq ($(KBUILD_OUTPUT),)
63 # Invoke a second make in the output directory, passing relevant variables
64 # Check that the output directory actually exists
65 saved-output := $(KBUILD_OUTPUT)
66 KBUILD_OUTPUT := $(shell cd $(KBUILD_OUTPUT) && /bin/pwd)
67 $(if $(wildcard $(KBUILD_OUTPUT)),, \
68      $(error output directory "$(saved-output)" does not exist))
69
70 .PHONY: $(MAKECMDGOALS)
71
72 $(filter-out _all,$(MAKECMDGOALS)) _all: $(KBUILD_OUTPUT)/Rules.mak $(KBUILD_OUTPUT)/Makefile all_tree
73         $(MAKE) -C $(KBUILD_OUTPUT) \
74         top_srcdir=$(top_srcdir) \
75         top_builddir=$(top_builddir) \
76         KBUILD_SRC=$(top_srcdir) \
77         PREFIX=$(PREFIX) \
78         CROSS=$(CROSS) \
79         -f $(CURDIR)/Makefile $@
80
81 $(KBUILD_OUTPUT)/Rules.mak:
82         @echo > $@
83         @echo top_srcdir=$(top_srcdir) >> $@
84         @echo top_builddir=$(KBUILD_OUTPUT) >> $@
85         @echo include $(top_srcdir)/Rules.mak >> $@
86
87 $(KBUILD_OUTPUT)/Makefile:
88         @echo > $@
89         @echo top_srcdir=$(top_srcdir) >> $@
90         @echo top_builddir=$(KBUILD_OUTPUT) >> $@
91         @echo KBUILD_SRC='$$(top_srcdir)' >> $@
92         @echo include '$$(KBUILD_SRC)'/Makefile >> $@
93
94 # Leave processing to above invocation of make
95 skip-makefile := 1
96 endif # ifneq ($(KBUILD_OUTPUT),)
97 endif # ifeq ($(KBUILD_SRC),)
98
99 ifeq ($(skip-makefile),)
100
101 # We only need a copy of the Makefile for the config targets and reuse
102 # the rest from the source directory, i.e. we do not cp ALL_MAKEFILES.
103 scripts/config/Makefile: $(top_srcdir)/scripts/config/Makefile
104         cp $< $@
105
106 _all: all
107
108 help:
109         @echo 'Cleaning:'
110         @echo '  clean                  - delete temporary files created by build'
111         @echo '  distclean              - delete all non-source files (including .config)'
112         @echo
113         @echo 'Build:'
114         @echo '  all                    - Executable and documentation'
115         @echo '  busybox                - the swiss-army executable'
116         @echo '  doc                    - docs/BusyBox.{txt,html,1}'
117         @echo
118         @echo 'Configuration:'
119         @echo '  allnoconfig            - disable all symbols in .config'
120         @echo '  allyesconfig           - enable (almost) all symbols in .config'
121         @echo '  config         - text based configurator (of last resort).'
122         @echo '  defconfig              - set .config to defaults'
123         @echo '  menuconfig             - interactive curses-based configurator'
124         @echo '  oldconfig              - resolve any unresolved symbols in .config'
125         @echo
126         @echo 'Installation:'
127         @echo '  install                - install busybox and symlinks into $prefix'
128         @echo '  install-hardlinks      - install busybox and hardlinks into $prefix'
129         @echo '  uninstall'
130         @echo
131         @echo 'Development:'
132         @echo '  check                  - run the test suite for all applets'
133         @echo '  randconfig             - generate a random configuration'
134         @echo '  release                - create a distribution tarball'
135         @echo '  sizes                  - show size of all enabled busybox symbols'
136         @echo
137
138
139 include $(top_srcdir)/Rules.mak
140
141 ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
142
143 # Default target if none was requested explicitly
144 all: menuconfig
145
146 # warn if no configuration exists and we are asked to build a non-config target
147 .config:
148         @echo ""
149         @echo "No $(top_builddir)/$@ found!"
150         @echo "Please refer to 'make  help', section Configuration."
151         @echo ""
152         @exit 1
153
154 # configuration
155 # ---------------------------------------------------------------------------
156
157 scripts/config/conf: scripts/config/Makefile
158         $(MAKE) -C scripts/config conf
159         -@if [ ! -f .config ] ; then \
160                 cp $(CONFIG_DEFCONFIG) .config; \
161         fi
162
163 scripts/config/mconf: scripts/config/Makefile
164         $(MAKE) -C scripts/config ncurses conf mconf
165         -@if [ ! -f .config ] ; then \
166                 cp $(CONFIG_DEFCONFIG) .config; \
167         fi
168
169 menuconfig: scripts/config/mconf
170         @./scripts/config/mconf $(CONFIG_CONFIG_IN)
171
172 config: scripts/config/conf
173         @./scripts/config/conf $(CONFIG_CONFIG_IN)
174
175 oldconfig: scripts/config/conf
176         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
177
178 randconfig: scripts/config/conf
179         @./scripts/config/conf -r $(CONFIG_CONFIG_IN)
180
181 allyesconfig: scripts/config/conf
182         @./scripts/config/conf -y $(CONFIG_CONFIG_IN)
183         sed -i -r -e "s/^(CONFIG_DEBUG|USING_CROSS_COMPILER|CONFIG_STATIC|CONFIG_SELINUX).*/# \1 is not set/" .config
184         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
185
186 allnoconfig: scripts/config/conf
187         @./scripts/config/conf -n $(CONFIG_CONFIG_IN)
188
189 defconfig: scripts/config/conf
190         @./scripts/config/conf -d $(CONFIG_CONFIG_IN)
191
192 else # ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
193
194 all: busybox busybox.links doc
195
196 # In this section, we need .config
197 -include $(top_builddir)/.config.cmd
198 include $(patsubst %,%/Makefile.in, $(SRC_DIRS))
199 -include $(top_builddir)/.depend
200
201 endif # ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
202
203 busybox: .depend $(libraries-y)
204         $(CC) $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ -Wl,--start-group $(libraries-y) $(LIBRARIES) -Wl,--end-group
205         $(STRIPCMD) $@
206
207 busybox.links: $(top_srcdir)/applets/busybox.mkll include/config.h $(top_srcdir)/include/applets.h
208         - $(SHELL) $^ >$@
209
210 install: $(top_srcdir)/applets/install.sh busybox busybox.links
211         $(SHELL) $< $(PREFIX)
212 ifeq ($(strip $(CONFIG_FEATURE_SUID)),y)
213         @echo
214         @echo
215         @echo --------------------------------------------------
216         @echo You will probably need to make your busybox binary
217         @echo setuid root to ensure all configured applets will
218         @echo work properly.
219         @echo --------------------------------------------------
220         @echo
221 endif
222
223 uninstall: busybox.links
224         rm -f $(PREFIX)/bin/busybox
225         for i in `cat busybox.links` ; do rm -f $(PREFIX)$$i; done
226
227 install-hardlinks: $(top_srcdir)/applets/install.sh busybox busybox.links
228         $(SHELL) $< $(PREFIX) --hardlinks
229
230 # see if we are in verbose mode
231 KBUILD_VERBOSE :=
232 ifdef V
233   ifeq ("$(origin V)", "command line")
234     KBUILD_VERBOSE := $(V)
235   endif
236 endif
237 ifneq ($(strip $(KBUILD_VERBOSE)),)
238   CHECK_VERBOSE := -v
239 endif
240 check test: busybox
241         bindir=$(top_builddir) srcdir=$(top_srcdir)/testsuite \
242         $(top_srcdir)/testsuite/runtest $(CHECK_VERBOSE)
243
244 sizes:
245         -rm -f busybox
246         $(MAKE) top_srcdir=$(top_srcdir) top_builddir=$(top_builddir) \
247                 -f $(top_srcdir)/Makefile STRIPCMD=/bin/true
248         nm --size-sort busybox
249
250 # Documentation Targets
251 doc: docs/busybox.pod docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html
252
253 docs/busybox.pod : $(top_srcdir)/docs/busybox_header.pod $(top_srcdir)/include/usage.h $(top_srcdir)/docs/busybox_footer.pod
254         -mkdir -p docs
255         - ( cat $(top_srcdir)/docs/busybox_header.pod; \
256             $(top_srcdir)/docs/autodocifier.pl $(top_srcdir)/include/usage.h; \
257             cat $(top_srcdir)/docs/busybox_footer.pod ) > docs/busybox.pod
258
259 docs/BusyBox.txt: docs/busybox.pod
260         $(SECHO)
261         $(SECHO) BusyBox Documentation
262         $(SECHO)
263         -mkdir -p docs
264         -pod2text $< > $@
265
266 docs/BusyBox.1: docs/busybox.pod
267         - mkdir -p docs
268         - pod2man --center=BusyBox --release="version $(VERSION)" \
269                 $< > $@
270
271 docs/BusyBox.html: docs/busybox.net/BusyBox.html
272         - mkdir -p docs
273         -@ rm -f docs/BusyBox.html
274         -@ cp docs/busybox.net/BusyBox.html docs/BusyBox.html
275
276 docs/busybox.net/BusyBox.html: docs/busybox.pod
277         -@ mkdir -p docs/busybox.net
278         -  pod2html --noindex $< > \
279             docs/busybox.net/BusyBox.html
280         -@ rm -f pod2htm*
281
282 # The nifty new buildsystem stuff
283 scripts/bb_mkdep: $(top_srcdir)/scripts/bb_mkdep.c
284         $(HOSTCC) $(HOSTCFLAGS) -o $@ $<
285
286 DEP_INCLUDES := include/config.h include/bb_config.h
287
288 ifeq ($(strip $(CONFIG_BBCONFIG)),y)
289 DEP_INCLUDES += include/bbconfigopts.h
290
291 include/bbconfigopts.h: .config
292         $(top_srcdir)/scripts/config/mkconfigs > $@
293 endif
294
295 depend dep $(top_builddir)/.depend: .depend
296 .depend: scripts/bb_mkdep $(DEP_INCLUDES)
297         @rm -f .depend
298         @mkdir -p include/config
299         scripts/bb_mkdep -c include/config.h -c include/bb_config.h -I $(top_srcdir)/include $(top_srcdir)/ > $@.tmp
300         mv $@.tmp $@
301
302 include/config.h: .config
303         @if [ ! -x $(top_builddir)/scripts/config/conf ] ; then \
304             $(MAKE) -C scripts/config conf; \
305         fi;
306         @$(top_builddir)/scripts/config/conf -o $(CONFIG_CONFIG_IN)
307
308 include/bb_config.h: include/config.h
309         @echo -e "#ifndef BB_CONFIG_H\n#define BB_CONFIG_H" > $@
310         @sed -e 's/#undef CONFIG_\(.*\)/#define ENABLE_\1 0/' \
311             -e 's/#define CONFIG_\(.*\)/#define CONFIG_\1\n#define ENABLE_\1/' \
312                 < $< >> $@
313         @echo "#endif" >> $@
314
315 clean:
316         - $(MAKE) -C scripts/config $@
317         - rm -f docs/busybox.dvi docs/busybox.ps \
318             docs/busybox.pod docs/busybox.net/busybox.html \
319             docs/busybox pod2htm* *.gdb *.elf *~ core .*config.log \
320             docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html \
321             docs/busybox.net/BusyBox.html busybox.links libbb/loop.h \
322             .config.old busybox
323         - rm -rf _install testsuite/links
324         - find . -name .\*.flags -exec rm -f {} \;
325         - find . -name \*.o -exec rm -f {} \;
326         - find . -name \*.a -exec rm -f {} \;
327
328 distclean: clean
329         - rm -f scripts/bb_mkdep
330         - rm -rf include/config include/config.h include/bb_config.h include/bbconfigopts.h
331         - find . -name .depend -exec rm -f {} \;
332         rm -f .config .config.old .config.cmd
333
334 release: distclean #doc
335         cd ..; \
336         rm -rf $(PROG)-$(VERSION); \
337         cp -a busybox $(PROG)-$(VERSION); \
338         \
339         find $(PROG)-$(VERSION)/ -type d \
340                 -name .svn \
341                 -print \
342                 -exec rm -rf {} \; ; \
343         \
344         find $(PROG)-$(VERSION)/ -type f \
345                 -name .\#* \
346                 -print \
347                 -exec rm -f {} \; ; \
348         \
349         tar -cvzf $(PROG)-$(VERSION).tar.gz $(PROG)-$(VERSION)/;
350
351 tags:
352         ctags -R .
353
354
355 endif # ifeq ($(skip-makefile),)
356
357 .PHONY: dummy subdirs release distclean clean config oldconfig \
358         menuconfig tags check test depend buildtree