b69e5040ffe17245e22199dd7d720f70ddb5dae2
[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 allbareconfig \
13         clean distclean \
14         release tags
15
16 # the toplevel sourcedir
17 ifndef top_srcdir
18 top_srcdir=$(CURDIR)
19 endif
20 # toplevel directory of the object-tree
21 ifndef top_builddir
22 top_builddir=$(CURDIR)
23 endif
24
25 export srctree=$(top_srcdir)
26 vpath %/Config.in $(srctree)
27
28 DIRS:=applets archival archival/libunarchive coreutils console-tools \
29         debianutils editors findutils init miscutils modutils networking \
30         networking/libiproute networking/udhcp procps loginutils shell \
31         sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils libbb
32
33 SRC_DIRS:=$(patsubst %,$(top_srcdir)/%,$(DIRS))
34
35 # That's our default target when none is given on the command line
36 .PHONY: _all
37 _all:
38
39 CONFIG_CONFIG_IN = $(top_srcdir)/Config.in
40 CONFIG_DEFCONFIG = $(top_srcdir)/defconfig
41
42 ifeq ($(KBUILD_SRC),)
43
44 ifdef O
45   ifeq ("$(origin O)", "command line")
46     KBUILD_OUTPUT := $(O)
47     top_builddir := $(O)
48   endif
49 else
50 # If no alternate output-dir was specified, we build in cwd
51 # We are using KBUILD_OUTPUT nevertheless to make sure that we create
52 # Rules.mak and the toplevel Makefile, in case they don't exist.
53   KBUILD_OUTPUT := $(top_builddir)
54 endif
55
56 ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
57 # pull in OS specific commands like cp, mkdir, etc. early
58 -include $(top_srcdir)/Rules.mak
59 endif
60
61 # All object directories.
62 OBJ_DIRS := $(DIRS)
63 all_tree := $(patsubst %,$(top_builddir)/%,$(OBJ_DIRS) scripts scripts/config include)
64 all_tree: $(all_tree)
65 $(all_tree):
66         @mkdir -p "$@"
67
68 ifneq ($(KBUILD_OUTPUT),)
69 # Invoke a second make in the output directory, passing relevant variables
70 # Check that the output directory actually exists
71 saved-output := $(KBUILD_OUTPUT)
72 KBUILD_OUTPUT := $(shell cd $(KBUILD_OUTPUT) && /bin/pwd)
73 $(if $(wildcard $(KBUILD_OUTPUT)),, \
74      $(error output directory "$(saved-output)" does not exist))
75
76 .PHONY: $(MAKECMDGOALS)
77
78 $(filter-out _all,$(MAKECMDGOALS)) _all: $(KBUILD_OUTPUT)/Rules.mak $(KBUILD_OUTPUT)/Makefile all_tree
79         $(Q)$(MAKE) -C $(KBUILD_OUTPUT) \
80         top_srcdir=$(top_srcdir) \
81         top_builddir=$(top_builddir) \
82         KBUILD_SRC=$(top_srcdir) \
83         -f $(CURDIR)/Makefile $@
84
85 $(KBUILD_OUTPUT)/Rules.mak:
86         @echo > $@
87         @echo top_srcdir=$(top_srcdir) >> $@
88         @echo top_builddir=$(KBUILD_OUTPUT) >> $@
89         @echo include $(top_srcdir)/Rules.mak >> $@
90
91 $(KBUILD_OUTPUT)/Makefile:
92         @echo > $@
93         @echo top_srcdir=$(top_srcdir) >> $@
94         @echo top_builddir=$(KBUILD_OUTPUT) >> $@
95         @echo KBUILD_SRC='$$(top_srcdir)' >> $@
96         @echo include '$$(KBUILD_SRC)'/Makefile >> $@
97
98 # Leave processing to above invocation of make
99 skip-makefile := 1
100 endif # ifneq ($(KBUILD_OUTPUT),)
101 endif # ifeq ($(KBUILD_SRC),)
102
103 ifeq ($(skip-makefile),)
104
105 # We only need a copy of the Makefile for the config targets and reuse
106 # the rest from the source directory, i.e. we do not cp ALL_MAKEFILES.
107 scripts/config/Makefile: $(top_srcdir)/scripts/config/Makefile
108         cp $< $@
109
110 _all: all
111
112 help:
113         @echo 'Cleaning:'
114         @echo '  clean                  - delete temporary files created by build'
115         @echo '  distclean              - delete all non-source files (including .config)'
116         @echo
117         @echo 'Build:'
118         @echo '  all                    - Executable and documentation'
119         @echo '  busybox                - the swiss-army executable'
120         @echo '  doc                    - docs/BusyBox.{txt,html,1}'
121         @echo
122         @echo 'Configuration:'
123         @echo '  allnoconfig            - disable all symbols in .config'
124         @echo '  allyesconfig           - enable all symbols in .config (see defconfig)'
125         @echo '  allbareconfig          - enable all applets without any sub-features'
126         @echo '  config         - text based configurator (of last resort)'
127         @echo '  defconfig              - set .config to largest generic configuration'
128         @echo '  menuconfig             - interactive curses-based configurator'
129         @echo '  oldconfig              - resolve any unresolved symbols in .config'
130         @echo
131         @echo 'Installation:'
132         @echo '  install                - install busybox into $prefix'
133         @echo '  uninstall'
134         @echo
135         @echo 'Development:'
136         @echo '  check                  - run the test suite for all applets'
137         @echo '  randconfig             - generate a random configuration'
138         @echo '  release                - create a distribution tarball'
139         @echo '  sizes                  - show size of all enabled busybox symbols'
140         @echo
141
142
143 include $(top_srcdir)/Rules.mak
144
145 ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
146
147 # Default target if none was requested explicitly
148 all: menuconfig
149
150 # warn if no configuration exists and we are asked to build a non-config target
151 .config:
152         @echo ""
153         @echo "No $(top_builddir)/$@ found!"
154         @echo "Please refer to 'make  help', section Configuration."
155         @echo ""
156         @exit 1
157
158 # configuration
159 # ---------------------------------------------------------------------------
160
161 scripts/config/conf: scripts/config/Makefile
162         $(Q)$(MAKE) -C scripts/config conf
163         -@if [ ! -f .config ] ; then \
164                 cp $(CONFIG_DEFCONFIG) .config; \
165         fi
166
167 scripts/config/mconf: scripts/config/Makefile
168         $(Q)$(MAKE) -C scripts/config ncurses conf mconf
169         -@if [ ! -f .config ] ; then \
170                 cp $(CONFIG_DEFCONFIG) .config; \
171         fi
172
173 menuconfig: scripts/config/mconf
174         @./scripts/config/mconf $(CONFIG_CONFIG_IN)
175
176 config: scripts/config/conf
177         @./scripts/config/conf $(CONFIG_CONFIG_IN)
178
179 oldconfig: scripts/config/conf
180         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
181
182 randconfig: scripts/config/conf
183         @./scripts/config/conf -r $(CONFIG_CONFIG_IN)
184
185 allyesconfig: scripts/config/conf
186         @./scripts/config/conf -y $(CONFIG_CONFIG_IN)
187         sed -i -r -e "s/^(USING_CROSS_COMPILER)=.*/# \1 is not set/" .config
188         echo "CONFIG_FEATURE_SHARED_BUSYBOX=y" >> .config
189         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
190
191 allnoconfig: scripts/config/conf
192         @./scripts/config/conf -n $(CONFIG_CONFIG_IN)
193
194 # defconfig is allyesconfig minus any features that are specialized enough
195 # or cause enough behavior change that the user really should switch them on
196 # manually if that's what they want.  Sort of "maximum sane config".
197
198 defconfig: scripts/config/conf
199         @./scripts/config/conf -y $(CONFIG_CONFIG_IN)
200         sed -i -r -e "s/^(USING_CROSS_COMPILER|CONFIG_(DEBUG.*|STATIC|SELINUX|BUILD_(AT_ONCE|LIBBUSYBOX)|FEATURE_(DEVFS|FULL_LIBBUSYBOX|SHARED_BUSYBOX|MTAB_SUPPORT|CLEAN_UP)))=.*/# \1 is not set/" .config
201         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
202
203
204 allbareconfig: scripts/config/conf
205         @./scripts/config/conf -y $(CONFIG_CONFIG_IN)
206         @sed -i -r -e "s/^(USING_CROSS_COMPILER|CONFIG_(DEBUG|STATIC|SELINUX|DEVFSD|NC_GAPING_SECURITY_HOLE|BUILD_AT_ONCE)).*/# \1 is not set/" .config
207         @sed -i -e "/FEATURE/s/=.*//;/^[^#]/s/.*FEATURE.*/# \0 is not set/;" .config
208         @echo "CONFIG_FEATURE_BUFFERS_GO_ON_STACK=y" >> .config
209         @./scripts/config/conf -o $(CONFIG_CONFIG_IN)
210
211 else # ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
212
213 all: busybox busybox.links doc
214
215 # In this section, we need .config
216 -include $(top_builddir)/.config.cmd
217 include $(patsubst %,%/Makefile.in, $(SRC_DIRS))
218
219 endif # ifneq ($(strip $(HAVE_DOT_CONFIG)),y)
220
221 -include $(top_builddir)/.config
222 -include $(top_builddir)/.depend
223
224
225 ifeq ($(strip $(CONFIG_BUILD_AT_ONCE)),y)
226 libraries-y:=
227 # Which parts of the internal libs are requested?
228 # Per default we only want what was actually selected.
229 # -a denotes all while -y denotes the selected ones.
230 ifeq ($(strip $(CONFIG_FEATURE_FULL_LIBBUSYBOX)),y)
231 LIBRARY_DEFINE:=$(LIBRARY_DEFINE-a)
232 LIBRARY_SRC   :=$(LIBRARY_SRC-a)
233 else # CONFIG_FEATURE_FULL_LIBBUSYBOX
234 LIBRARY_DEFINE:=$(LIBRARY_DEFINE-y)
235 LIBRARY_SRC   :=$(LIBRARY_SRC-y)
236 endif # CONFIG_FEATURE_FULL_LIBBUSYBOX
237 APPLET_SRC:=$(APPLET_SRC-y)
238 APPLETS_DEFINE:=$(APPLETS_DEFINE-y)
239 else  # CONFIG_BUILD_AT_ONCE
240 # no --combine, build archives out of the individual .o
241 # This was the old way the binary was built.
242 libbusybox-obj:=archival/libunarchive/libunarchive.a \
243         networking/libiproute/libiproute.a \
244         libpwdgrp/libpwdgrp.a \
245         coreutils/libcoreutils/libcoreutils.a \
246         libbb/libbb.a
247 libbusybox-obj:=$(patsubst %,$(top_builddir)/%,$(libbusybox-obj))
248
249 ifeq ($(strip $(CONFIG_FEATURE_SHARED_BUSYBOX)),y)
250 # linking against libbusybox, so don't build the .a already contained in the .so
251 libraries-y:=$(filter-out $(libbusybox-obj),$(libraries-y))
252 endif # CONFIG_FEATURE_SHARED_BUSYBOX
253 endif # CONFIG_BUILD_AT_ONCE
254
255
256 ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
257 LD_LIBBUSYBOX:=libbusybox.so
258 LIBBUSYBOX_SONAME:=$(LD_LIBBUSYBOX).$(MAJOR_VERSION).$(MINOR_VERSION).$(SUBLEVEL_VERSION)
259 DO_INSTALL_LIBS:=$(LD_LIBBUSYBOX) \
260         $(LD_LIBBUSYBOX).$(MAJOR_VERSION) \
261         $(LD_LIBBUSYBOX).$(MAJOR_VERSION).$(MINOR_VERSION)
262
263 ifeq ($(strip $(CONFIG_BUILD_AT_ONCE)),y)
264 ifneq ($(strip $(CONFIG_FEATURE_SHARED_BUSYBOX)),y)
265 # --combine but not linking against libbusybox, so compile all
266 BUSYBOX_SRC   := $(LIBRARY_SRC)
267 BUSYBOX_DEFINE:= $(LIBRARY_DEFINE)
268 endif # !CONFIG_FEATURE_SHARED_BUSYBOX
269 $(LIBBUSYBOX_SONAME): $(LIBRARY_SRC)
270 else # CONFIG_BUILD_AT_ONCE
271 $(LIBBUSYBOX_SONAME): $(libbusybox-obj)
272 endif # CONFIG_BUILD_AT_ONCE
273 endif # CONFIG_BUILD_LIBBUSYBOX
274
275 ifeq ($(strip $(CONFIG_FEATURE_SHARED_BUSYBOX)),y)
276 LDBUSYBOX:=-L$(top_builddir) -lbusybox
277 endif
278
279 ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
280 $(LIBBUSYBOX_SONAME):
281 ifndef MAJOR_VERSION
282         $(error MAJOR_VERSION needed for $@ is not defined)
283 endif
284         $(do_link) $(LIB_CFLAGS) $(LIB_LDFLAGS) \
285         -Wl,-soname=$(LD_LIBBUSYBOX).$(MAJOR_VERSION) \
286         -Wl,-z,combreloc $(LIB_LDFLAGS) \
287         -o $(@) \
288         -Wl,--start-group -Wl,--whole-archive \
289         $(LIBRARY_DEFINE) $(^) \
290         -Wl,--no-whole-archive -Wl,--end-group
291         $(RM_F) $(DO_INSTALL_LIBS)
292         for i in $(DO_INSTALL_LIBS); do $(LN_S) -v $(@) $$i ; done
293         $(do_strip)
294
295 endif # ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
296
297 busybox: .depend $(LIBBUSYBOX_SONAME) $(BUSYBOX_SRC) $(libraries-y)
298         $(do_link) $(PROG_CFLAGS) \
299         -o $@ -Wl,--start-group  \
300         $(APPLETS_DEFINE) $(APPLET_SRC) \
301         $(BUSYBOX_DEFINE) $(BUSYBOX_SRC) $(libraries-y) \
302         $(LDBUSYBOX) $(LIBRARIES) \
303         -Wl,--end-group
304         $(do_strip)
305
306 busybox.links: $(top_srcdir)/applets/busybox.mkll include/bb_config.h $(top_srcdir)/include/applets.h
307         $(Q)-$(SHELL) $^ >$@
308
309 install: $(top_srcdir)/applets/install.sh busybox busybox.links
310         DO_INSTALL_LIBS="$(strip $(LIBBUSYBOX_SONAME) $(DO_INSTALL_LIBS))" \
311                 $(SHELL) $< $(PREFIX) $(INSTALL_OPTS)
312 ifeq ($(strip $(CONFIG_FEATURE_SUID)),y)
313         @echo
314         @echo
315         @echo --------------------------------------------------
316         @echo You will probably need to make your busybox binary
317         @echo setuid root to ensure all configured applets will
318         @echo work properly.
319         @echo --------------------------------------------------
320         @echo
321 endif
322
323 uninstall: busybox.links
324         $(RM_F) $(PREFIX)/bin/busybox
325         for i in `cat busybox.links` ; do $(RM_F) $(PREFIX)$$i; done
326 ifneq ($(strip $(DO_INSTALL_LIBS)),n)
327         for i in $(LIBBUSYBOX_SONAME) $(DO_INSTALL_LIBS); do \
328                 $(RM_F) $(PREFIX)$$i; \
329         done
330 endif
331
332 # see if we are in verbose mode
333 KBUILD_VERBOSE :=
334 ifdef V
335   ifeq ("$(origin V)", "command line")
336     KBUILD_VERBOSE := $(V)
337   endif
338 endif
339 ifneq ($(strip $(KBUILD_VERBOSE)),)
340   CHECK_VERBOSE := -v
341 # ARFLAGS+=v
342 endif
343 check test: busybox
344         bindir=$(top_builddir) srcdir=$(top_srcdir)/testsuite \
345         $(top_srcdir)/testsuite/runtest $(CHECK_VERBOSE)
346
347 sizes:
348         -$(RM_F) busybox
349         $(MAKE) top_srcdir=$(top_srcdir) top_builddir=$(top_builddir) \
350                 -f $(top_srcdir)/Makefile STRIPCMD=/bin/true
351         $(NM) --size-sort busybox
352
353 # Documentation Targets
354 doc: docs/busybox.pod docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html
355
356 docs/busybox.pod : $(top_srcdir)/docs/busybox_header.pod $(top_srcdir)/include/usage.h $(top_srcdir)/docs/busybox_footer.pod $(top_srcdir)/docs/autodocifier.pl
357         $(disp_doc)
358         $(Q)-mkdir -p docs
359         $(Q)-( cat $(top_srcdir)/docs/busybox_header.pod; \
360             $(top_srcdir)/docs/autodocifier.pl $(top_srcdir)/include/usage.h; \
361             cat $(top_srcdir)/docs/busybox_footer.pod ) > docs/busybox.pod
362
363 docs/BusyBox.txt: docs/busybox.pod
364         $(disp_doc)
365         $(Q)-mkdir -p docs
366         $(Q)-pod2text $< > $@
367
368 docs/BusyBox.1: docs/busybox.pod
369         $(disp_doc)
370         $(Q)-mkdir -p docs
371         $(Q)-pod2man --center=BusyBox --release="version $(VERSION)" \
372                 $< > $@
373
374 docs/BusyBox.html: docs/busybox.net/BusyBox.html
375         $(disp_doc)
376         $(Q)-mkdir -p docs
377         $(Q)-$(RM_F) docs/BusyBox.html
378         $(Q)-cp docs/busybox.net/BusyBox.html docs/BusyBox.html
379
380 docs/busybox.net/BusyBox.html: docs/busybox.pod
381         $(Q)-mkdir -p docs/busybox.net
382         $(Q)-pod2html --noindex $< > \
383             docs/busybox.net/BusyBox.html
384         $(Q)-$(RM_F) pod2htm*
385
386 # The nifty new buildsystem stuff
387 scripts/bb_mkdep: $(top_srcdir)/scripts/bb_mkdep.c
388         $(HOSTCC) $(HOSTCFLAGS) -o $@ $<
389
390 DEP_INCLUDES := include/config.h include/bb_config.h include/_usage.h
391
392 ifeq ($(strip $(CONFIG_BBCONFIG)),y)
393 DEP_INCLUDES += include/bbconfigopts.h
394
395 include/bbconfigopts.h: .config
396         $(disp_gen)
397         $(Q)$(top_srcdir)/scripts/config/mkconfigs > $@
398 endif
399
400 depend dep: .depend
401 .depend: scripts/bb_mkdep $(DEP_INCLUDES)
402         $(disp_gen)
403         $(Q)$(RM_F) .depend
404         $(Q)mkdir -p include/config
405         $(Q)scripts/bb_mkdep -c include/config.h -c include/bb_config.h \
406                         -I $(top_srcdir)/include $(top_srcdir) > $@.tmp
407         $(Q)mv $@.tmp $@
408
409 include/config.h: .config
410         @if [ ! -x $(top_builddir)/scripts/config/conf ] ; then \
411             $(MAKE) -C scripts/config conf; \
412         fi;
413         @$(top_builddir)/scripts/config/conf -o $(CONFIG_CONFIG_IN)
414
415 include/bb_config.h: include/config.h
416         @echo -e "#ifndef BB_CONFIG_H\n#define BB_CONFIG_H" > $@
417         @sed -e 's/#undef CONFIG_\(.*\)/#define ENABLE_\1 0/' \
418             -e 's/#define CONFIG_\(.*\)/#define CONFIG_\1\n#define ENABLE_\1/' \
419                 < $< >> $@
420         @echo "#endif" >> $@
421
422 # Create macros for usage.h, e.g.:
423 #if ENABLE_HAVE_DOT_CONFIG
424 #define USAGE_HAVE_DOT_CONFIG(a) a
425 #else
426 #define USAGE_HAVE_DOT_CONFIG(a)
427 #endif
428 include/_usage.h: .config
429         $(disp_gen)
430         $(Q)awk '/CONFIG|BB_APPLET/{gsub("#[[:space:]]*|=y|.*CONFIG_|.*BB_APPLET_","");if(!/=/){print("#if ENABLE_"$$1"\n#define USAGE_"$$1"(a) a\n#else\n#define USAGE_"$$1"(a)\n#endif");}}' $(<) > $(@)
431
432 clean:
433         - $(MAKE) -C scripts/config $@
434         - $(RM_F) docs/busybox.dvi docs/busybox.ps \
435             docs/busybox.pod docs/busybox.net/busybox.html \
436             docs/busybox pod2htm* *.gdb *.elf *~ core .*config.log \
437             docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html \
438             docs/busybox.net/BusyBox.html busybox.links \
439             $(DO_INSTALL_LIBS) $(LIBBUSYBOX_SONAME) \
440             .config.old busybox
441         - rm -rf _install testsuite/links
442         - find . -name .\*.flags -exec $(RM_F) {} \;
443         - find . -name \*.o -exec $(RM_F) {} \;
444         - find . -name \*.a -exec $(RM_F) {} \;
445         - find . -name \*.so -exec $(RM_F) {} \;
446
447 distclean: clean
448         - $(RM_F) scripts/bb_mkdep
449         - rm -rf include/config $(DEP_INCLUDES)
450         - find . -name .depend -exec $(RM_F) {} \;
451         $(RM_F) .config .config.old .config.cmd
452
453 release: distclean #doc
454         cd ..; \
455         rm -rf $(PROG)-$(VERSION); \
456         cp -a busybox $(PROG)-$(VERSION); \
457         \
458         find $(PROG)-$(VERSION)/ -type d \
459                 -name .svn \
460                 -print \
461                 -exec rm -rf {} \; ; \
462         \
463         find $(PROG)-$(VERSION)/ -type f \
464                 -name .\#* \
465                 -print \
466                 -exec $(RM_F) {} \; ; \
467         \
468         tar -cvzf $(PROG)-$(VERSION).tar.gz $(PROG)-$(VERSION)/;
469
470 tags:
471         ctags -R .
472
473
474 endif # ifeq ($(skip-makefile),)
475
476 .PHONY: dummy subdirs release distclean clean config oldconfig \
477         menuconfig tags check test depend dep buildtree