3cabc7afa960a1b8046d886a0c1fa7a434a16555
[oweals/busybox.git] / Makefile
1 # Makefile for busybox
2 #
3 # Copyright (C) 1999,2000,2001 Erik Andersen <andersee@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19
20 PROG      := busybox
21 VERSION   := 0.61.pre
22 BUILDTIME := $(shell TZ=UTC date -u "+%Y.%m.%d-%H:%M%z")
23 export VERSION
24
25 # With a modern GNU make(1) (highly recommended, that's what all the
26 # developers use), all of the following configuration values can be
27 # overridden at the command line.  For example:
28 #   make CROSS=powerpc-linux- BB_SRC_DIR=$HOME/busybox PREFIX=/mnt/app
29
30 # If you want to add some simple compiler switches (like -march=i686),
31 # especially from the command line, use this instead of CFLAGS directly.
32 # For optimization overrides, it's better still to set OPTIMIZATION.
33 CFLAGS_EXTRA =
34  
35 # If you want a static binary, turn this on.
36 DOSTATIC = false
37
38 # Set the following to `true' to make a debuggable build.
39 # Leave this set to `false' for production use.
40 DODEBUG = false
41
42 # Setting this to `true' will cause busybox to directly use the system's
43 # password and group functions.  Assuming you use GNU libc, when this is
44 # `true', you will need to install the /etc/nsswitch.conf configuration file
45 # and the required libnss_* libraries. This generally makes your embedded
46 # system quite a bit larger... If you leave this off, busybox will directly use
47 # the /etc/password, /etc/group files (and your system will be smaller, and I
48 # will get fewer emails asking about how glibc NSS works).  Enabling this adds
49 # just 1.4k to the binary size (which is a _lot_ less then glibc NSS costs).
50 # Note that if you want hostname resolution to work with glibc, you still need
51 # the libnss_* libraries.  
52 USE_SYSTEM_PWD_GRP = true
53
54 # This enables compiling with dmalloc ( http://dmalloc.com/ )
55 # which is an excellent public domain mem leak and malloc problem
56 # detector.  To enable dmalloc, before running busybox you will
57 # want to first set up your environment.
58 # eg: `export DMALLOC_OPTIONS=debug=0x14f47d83,inter=100,log=logfile`
59 # Do not enable this for production builds...
60 DODMALLOC = false
61
62 # Electric-fence is another very useful malloc debugging library.
63 # Do not enable this for production builds...
64 DOEFENCE  = false
65
66 # If you want large file summit support, turn this on.
67 # This has no effect if you don't have a kernel with lfs
68 # support, and a system with libc-2.1.3 or later.
69 # Some of the programs that can benefit from lfs support
70 # are dd, gzip, mount, tar, and mkfs_minix.
71 # LFS allows you to use the above programs for files
72 # larger than 2GB!
73 DOLFS = false
74
75 # If you have a "pristine" source directory, point BB_SRC_DIR to it.
76 # Experimental and incomplete; tell the mailing list
77 # <busybox@opensource.lineo.com> if you do or don't like it so far.
78 BB_SRC_DIR =
79
80 # If you are running a cross compiler, you may want to set this
81 # to something more interesting, like "powerpc-linux-".
82 CROSS =
83 CC = $(CROSS)gcc
84 AR = $(CROSS)ar
85 STRIPTOOL = $(CROSS)strip
86
87 # To compile vs uClibc, just use the compiler wrapper built by uClibc...
88 # Everything should compile and work as expected these days...
89 #CC = /usr/i386-linux-uclibc/usr/bin/i386-uclibc-gcc
90
91 # To compile vs some other alternative libc, you may need to use/adjust
92 # the following lines to meet your needs...
93 #
94 # If you are using Red Hat 6.x with the compatible RPMs (for developing under
95 # Red Hat 5.x and glibc 2.0) uncomment the following.  Be sure to read about
96 # using the compatible RPMs (compat-*) at http://www.redhat.com !
97 #LIBCDIR=/usr/i386-glibc20-linux
98 #
99 # The following is used for libc5 (if you install altgcc and libc5-altdev
100 # on a Debian system).  
101 #LIBCDIR=/usr/i486-linuxlibc1
102 #
103 # For other libraries, you are on your own...
104 #LDFLAGS+=-nostdlib
105 #LIBRARIES = $(LIBCDIR)/lib/libc.a -lgcc
106 #CROSS_CFLAGS+=-nostdinc -I$(LIBCDIR)/include -I$(GCCINCDIR)
107 #GCCINCDIR = $(shell gcc -print-search-dirs | sed -ne "s/install: \(.*\)/\1include/gp")
108
109 # use '-Os' optimization if available, else use -O2
110 OPTIMIZATION := $(shell if $(CC) -Os -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \
111     then echo "-Os"; else echo "-O2" ; fi)
112
113 WARNINGS=-Wall -Wstrict-prototypes -Wshadow
114 ARFLAGS = -r
115
116 #
117 #--------------------------------------------------------
118 # If you're going to do a lot of builds with a non-vanilla configuration,
119 # it makes sense to adjust parameters above, so you can type "make"
120 # by itself, instead of following it by the same half-dozen overrides
121 # every time.  The stuff below, on the other hand, is probably less
122 # prone to casual user adjustment.
123
124
125 ifeq ($(strip $(DOLFS)),true)
126     # For large file summit support
127     CFLAGS+=-D_FILE_OFFSET_BITS=64 -D__USE_FILE_OFFSET64
128 endif
129 ifeq ($(strip $(DODMALLOC)),true)
130     # For testing mem leaks with dmalloc
131     CFLAGS+=-DDMALLOC
132     LIBRARIES = -ldmalloc
133     # Force debug=true, since this is useless when not debugging...
134     DODEBUG = true
135 else
136     ifeq ($(strip $(DOEFENCE)),true)
137         LIBRARIES = -lefence
138         # Force debug=true, since this is useless when not debugging...
139         DODEBUG = true
140     endif
141 endif
142 ifeq ($(strip $(DODEBUG)),true)
143     CFLAGS  += $(WARNINGS) -g -D_GNU_SOURCE
144     LDFLAGS += -Wl,-warn-common
145     STRIP    =
146 else
147     CFLAGS  += $(WARNINGS) $(OPTIMIZATION) -fomit-frame-pointer -D_GNU_SOURCE
148     LDFLAGS += -s -Wl,-warn-common
149     STRIP    = $(STRIPTOOL) --remove-section=.note --remove-section=.comment $(PROG)
150 endif
151 ifeq ($(strip $(DOSTATIC)),true)
152     LDFLAGS += --static
153     #
154     #use '-ffunction-sections -fdata-sections' and '--gc-sections' (if they 
155     # work) to try and strip out any unused junk.  Doesn't do much for me, 
156     # but you may want to give it a shot...
157     #
158     #ifeq ($(shell $(CC) -ffunction-sections -fdata-sections -S \
159     #   -o /dev/null -xc /dev/null 2>/dev/null && $(LD) \
160     #                   --gc-sections -v >/dev/null && echo 1),1)
161     #   CFLAGS += -ffunction-sections -fdata-sections
162     #   LDFLAGS += --gc-sections
163     #endif
164 endif
165
166 ifndef $(PREFIX)
167     PREFIX = `pwd`/_install
168 endif
169
170 # Additional complications due to support for pristine source dir.
171 # Include files in the build directory should take precedence over
172 # the copy in BB_SRC_DIR, both during the compilation phase and the
173 # shell script that finds the list of object files.
174 # Work in progress by <ldoolitt@recycle.lbl.gov>.
175 #
176 ifneq ($(strip $(BB_SRC_DIR)),)
177     VPATH = $(BB_SRC_DIR)
178 endif
179 #ifneq ($(strip $(VPATH)),)
180 #    CFLAGS += -I- -I. $(patsubst %,-I%,$(subst :, ,$(VPATH)))
181 #endif
182
183 # We need to set APPLET_SOURCES to something like
184 #   $(shell busybox.sh Config.h)
185 # but in a manner that works with VPATH and BB_SRC_DIR.
186 # Possible ways to approach this:
187 #
188 #   1. Explicitly search through .:$(VPATH) for busybox.sh and config.h,
189 #      then $(shell $(BUSYBOX_SH) $(CONFIG_H) $(BB_SRC_DIR))
190 #
191 #   2. Explicity search through .:$(VPATH) for slist.mk,
192 #      then $(shell $(MAKE) -f $(SLIST_MK) VPATH=$(VPATH) BB_SRC_DIR=$(BB_SRC_DIR))
193 #
194 #   3. Create slist.mk in this directory, with commands embedded in
195 #      a $(shell ...) command, and $(MAKE) it immediately.
196 #
197 #   4. Use a real rule within this makefile to create a file that sets 
198 #      APPLET_SOURCE_LIST, then include that file.  Has complications
199 #      with the first trip through the makefile (before processing the
200 #      include) trying to do too much, and a spurious warning the first
201 #      time make is run.
202 #
203 # This is option 3:
204 #
205 #APPLET_SOURCES = $(shell \
206 #   echo -e 'all: busybox.sh Config.h\n\t@ $$(SHELL) $$^ $$(BB_SRC_DIR)' >slist.mk; \
207 #   make -f slist.mk VPATH=$(VPATH) BB_SRC_DIR=$(BB_SRC_DIR) \
208 #)
209 # And option 4:
210 -include applet_source_list
211
212 OBJECTS   = $(APPLET_SOURCES:.c=.o) busybox.o usage.o applets.o
213 CFLAGS    += $(CROSS_CFLAGS)
214 CFLAGS    += -DBB_VER='"$(VERSION)"'
215 CFLAGS    += -DBB_BT='"$(BUILDTIME)"'
216 ifdef BB_INIT_SCRIPT
217     CFLAGS += -DINIT_SCRIPT='"$(BB_INIT_SCRIPT)"'
218 endif
219
220 ifneq ($(strip $(USE_SYSTEM_PWD_GRP)),true)
221     PWD_GRP     = pwd_grp
222     PWD_GRP_DIR = $(BB_SRC_DIR:=/)$(PWD_GRP)
223     PWD_LIB     = libpwd.a
224     PWD_CSRC=__getpwent.c pwent.c getpwnam.c getpwuid.c putpwent.c getpw.c \
225             fgetpwent.c __getgrent.c grent.c getgrnam.c getgrgid.c fgetgrent.c \
226             initgroups.c setgroups.c
227     PWD_OBJS=$(patsubst %.c,$(PWD_GRP)/%.o, $(PWD_CSRC))
228 ifneq ($(strip $(BB_SRC_DIR)),)
229     PWD_CFLAGS = -I- -I.
230 endif
231     PWD_CFLAGS += -I$(PWD_GRP_DIR)
232 else
233     CFLAGS    += -DUSE_SYSTEM_PWD_GRP
234 endif
235     
236 LIBBB     = libbb
237 LIBBB_LIB = libbb.a
238 LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \
239 copy_file_chunk.c libc5.c device_open.c error_msg.c \
240 error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \
241 find_root_device.c full_read.c full_write.c get_console.c \
242 get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \
243 isdirectory.c kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c \
244 mtab_file.c my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c \
245 my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \
246 print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \
247 safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \
248 trim.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
249 xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \
250 copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \
251 dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \
252 simplify_path.c
253 LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
254 ifeq ($(strip $(BB_SRC_DIR)),)
255     LIBBB_CFLAGS += -I$(LIBBB)
256 else
257     LIBBB_CFLAGS = -I- -I. -I./$(LIBBB) -I$(BB_SRC_DIR)/$(LIBBB) -I$(BB_SRC_DIR)
258 endif
259
260 LIBBB_MSRC=libbb/messages.c
261 LIBBB_MESSAGES= full_version name_too_long omitting_directory not_a_directory \
262 memory_exhausted invalid_date invalid_option io_error dash_dash_help \
263 write_error too_few_args name_longer_than_foo unknown can_not_create_raw_socket
264 LIBBB_MOBJ=$(patsubst %,$(LIBBB)/%.o, $(LIBBB_MESSAGES))
265
266 LIBBB_ARCSRC=libbb/unarchive.c
267 LIBBB_ARCOBJ= archive_offset seek_sub_file extract_archive unarchive \
268 get_header_ar get_header_cpio get_header_tar deb_extract
269 LIBBB_AROBJS=$(patsubst %,$(LIBBB)/%.o, $(LIBBB_ARCOBJ))
270
271
272 # Put user-supplied flags at the end, where they
273 # have a chance of winning.
274 CFLAGS += $(CFLAGS_EXTRA)
275
276 .EXPORT_ALL_VARIABLES:
277
278 all: applet_source_list busybox busybox.links doc
279
280 applet_source_list: busybox.sh Config.h
281         (echo -n "APPLET_SOURCES := "; CC="$(CC)" BB_SRC_DIR="$(BB_SRC_DIR)" $(SHELL) $^) > $@
282
283 doc: olddoc
284
285 # Old Docs...
286 olddoc: docs/busybox.pod docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html
287
288 docs/busybox.pod : docs/busybox_header.pod usage.h docs/busybox_footer.pod
289         - ( cat docs/busybox_header.pod; \
290             docs/autodocifier.pl usage.h; \
291             cat docs/busybox_footer.pod ) > docs/busybox.pod
292
293 docs/BusyBox.txt: docs/busybox.pod
294         @echo
295         @echo BusyBox Documentation
296         @echo
297         -mkdir -p docs
298         -pod2text $< > $@
299
300 docs/BusyBox.1: docs/busybox.pod
301         - mkdir -p docs
302         - pod2man --center=BusyBox --release="version $(VERSION)" \
303                 $< > $@
304
305 docs/BusyBox.html: docs/busybox.lineo.com/BusyBox.html
306         - mkdir -p docs
307         -@ rm -f docs/BusyBox.html
308         -@ ln -s busybox.lineo.com/BusyBox.html docs/BusyBox.html
309
310 docs/busybox.lineo.com/BusyBox.html: docs/busybox.pod
311         -@ mkdir -p docs/busybox.lineo.com
312         -  pod2html --noindex $< > \
313             docs/busybox.lineo.com/BusyBox.html
314         -@ rm -f pod2htm*
315
316
317 # New docs based on DOCBOOK SGML
318 newdoc: docs/busybox.txt docs/busybox.pdf docs/busybox/busyboxdocumentation.html
319
320 docs/busybox.txt: docs/busybox.sgml
321         @echo
322         @echo BusyBox Documentation
323         @echo
324         - mkdir -p docs
325         (cd docs; sgmltools -b txt busybox.sgml)
326
327 docs/busybox.dvi: docs/busybox.sgml
328         - mkdir -p docs
329         (cd docs; sgmltools -b dvi busybox.sgml)
330
331 docs/busybox.ps: docs/busybox.sgml
332         - mkdir -p docs
333         (cd docs; sgmltools -b ps busybox.sgml)
334
335 docs/busybox.pdf: docs/busybox.ps
336         - mkdir -p docs
337         (cd docs; ps2pdf busybox.ps)
338
339 docs/busybox/busyboxdocumentation.html: docs/busybox.sgml
340         - mkdir -p docs
341         (cd docs/busybox.lineo.com; sgmltools -b html ../busybox.sgml)
342
343
344 busybox: $(PWD_LIB) $(LIBBB_LIB) $(OBJECTS) 
345         $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBBB_LIB) $(PWD_LIB) $(LIBRARIES)
346         $(STRIP)
347
348 # Without VPATH, rule expands to "/bin/sh busybox.mkll Config.h applets.h"
349 # but with VPATH, some or all of those file names are resolved to the
350 # directories in which they live.
351 busybox.links: busybox.mkll Config.h applets.h
352         - $(SHELL) $^ >$@
353
354 nfsmount.o cmdedit.o: %.o: %.h
355 ash.o hush.o lash.o msh.o: cmdedit.h
356 $(OBJECTS): %.o: %.c Config.h busybox.h applets.h Makefile
357 ifeq ($(strip $(BB_SRC_DIR)),)
358         $(CC) $(CFLAGS) -I. $(patsubst %,-I%,$(subst :, ,$(BB_SRC_DIR))) -c $< -o $*.o
359 else
360         $(CC) $(CFLAGS) -I- -I. $(patsubst %,-I%,$(subst :, ,$(BB_SRC_DIR))) -c $< -o $*.o
361 endif
362
363 $(PWD_OBJS): %.o: %.c Config.h busybox.h applets.h Makefile
364         - mkdir -p $(PWD_GRP)
365         $(CC) $(CFLAGS) $(PWD_CFLAGS) -c $< -o $*.o
366
367 $(LIBBB_OBJS): %.o: %.c Config.h busybox.h applets.h Makefile libbb/libbb.h
368         - mkdir -p $(LIBBB)
369         $(CC) $(CFLAGS) $(LIBBB_CFLAGS) -c $< -o $*.o
370
371 $(LIBBB_MOBJ): $(LIBBB_MSRC)
372         - mkdir -p $(LIBBB)
373         $(CC) $(CFLAGS) $(LIBBB_CFLAGS) -DL_$(patsubst libbb/%,%,$*) -c $< -o $*.o
374
375 $(LIBBB_AROBJS): $(LIBBB_ARCSRC)
376         - mkdir -p $(LIBBB)
377         $(CC) $(CFLAGS) $(LIBBB_CFLAGS) -DL_$(patsubst libbb/%,%,$*) -c $< -o $*.o
378
379 libpwd.a: $(PWD_OBJS)
380         $(AR) $(ARFLAGS) $@ $^
381
382 libbb.a:  $(LIBBB_MOBJ) $(LIBBB_AROBJS) $(LIBBB_OBJS)
383         $(AR) $(ARFLAGS) $@ $^
384
385 usage.o: usage.h
386
387 libbb/loop.o: libbb/loop.h
388
389 libbb/loop.h: mk_loop_h.sh
390         @ $(SHELL) $< > $@
391
392 test tests:
393         # old way of doing it
394         #cd tests && $(MAKE) all
395         # new way of doing it
396         cd tests && ./tester.sh
397
398 clean:
399         - cd tests && $(MAKE) clean
400         - rm -f docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html \
401             docs/busybox.lineo.com/BusyBox.html
402         - rm -f docs/busybox.txt docs/busybox.dvi docs/busybox.ps \
403             docs/busybox.pdf docs/busybox.lineo.com/busybox.html
404         - rm -f multibuild.log Config.h.orig *.gdb *.elf
405         - rm -rf docs/busybox _install libpwd.a libbb.a pod2htm*
406         - rm -f busybox.links libbb/loop.h *~ slist.mk core applet_source_list
407         - find -name \*.o -exec rm -f {} \;
408
409 distclean: clean
410         - rm -f busybox applet_source_list
411         - cd tests && $(MAKE) distclean
412
413 install: install.sh busybox busybox.links
414         $(SHELL) $< $(PREFIX)
415
416 install-hardlinks: install.sh busybox busybox.links
417         $(SHELL) $< $(PREFIX) --hardlinks
418
419 debug_pristine:
420         @ echo VPATH=\"$(VPATH)\"
421         @ echo OBJECTS=\"$(OBJECTS)\"
422
423 dist release: distclean doc
424         cd ..;                                  \
425         rm -rf busybox-$(VERSION);              \
426         cp -a busybox busybox-$(VERSION);       \
427                                                 \
428         find busybox-$(VERSION)/ -type d        \
429                                  -name CVS      \
430                                  -print         \
431                 -exec rm -rf {} \; ;            \
432                                                 \
433         find busybox-$(VERSION)/ -type f        \
434                                  -name .\#*     \
435                                  -print         \
436                 -exec rm -f {} \;  ;            \
437                                                 \
438         tar -cvzf busybox-$(VERSION).tar.gz busybox-$(VERSION)/;
439
440 .PHONY: tags
441 tags:
442         ctags -R .