btrfs-progs: build, update the clean rule
[platform/upstream/btrfs-progs.git] / Makefile
1 # Export all variables to sub-makes by default
2 export
3
4 CC = gcc
5 LN = ln
6 AR = ar
7 AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -DBTRFS_FLAT_INCLUDES -fno-strict-aliasing -fPIC
8 CFLAGS = -g -O1 -fno-strict-aliasing -rdynamic
9 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
10           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
11           extent-cache.o extent_io.o volumes.o utils.o repair.o \
12           qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
13           ulist.o qgroup-verify.o backref.o
14 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
15                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
16                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
17                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
18                cmds-property.o
19 libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
20                    uuid-tree.o utils-lib.o rbtree-utils.o
21 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
22                crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
23                extent_io.h ioctl.h ctree.h btrfsck.h version.h
24 TESTS = fsck-tests.sh convert-tests.sh
25
26 INSTALL = install
27 prefix ?= /usr/local
28 bindir = $(prefix)/bin
29 lib_LIBS = -luuid -lblkid -lm -lz -llzo2 -L.
30 libdir ?= $(prefix)/lib
31 incdir = $(prefix)/include/btrfs
32 LIBS = $(lib_LIBS) $(libs_static)
33
34 ifeq ("$(origin V)", "command line")
35   BUILD_VERBOSE = $(V)
36 endif
37 ifndef BUILD_VERBOSE
38   BUILD_VERBOSE = 0
39 endif
40
41 ifeq ($(BUILD_VERBOSE),1)
42   Q =
43 else
44   Q = @
45 endif
46
47 MAKEOPTS = --no-print-directory Q=$(Q)
48
49 progs = mkfs.btrfs btrfs-debug-tree btrfsck \
50         btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
51         btrfs-find-root btrfstune btrfs-show-super
52
53 progs_extra = btrfs-corrupt-block btrfs-fragments btrfs-calc-size \
54               btrfs-select-super
55
56 progs_static = $(foreach p,$(progs),$(p).static)
57
58 # external libs required by various binaries; for btrfs-foo,
59 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
60 btrfs_convert_libs = -lext2fs -lcom_err
61 btrfs_image_libs = -lpthread
62 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
63
64 SUBDIRS =
65 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
66 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
67 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
68
69 ifeq ($(DISABLE_BACKTRACE),1)
70 AM_CFLAGS += -DBTRFS_DISABLE_BACKTRACE
71 endif
72
73 ifneq ($(DISABLE_DOCUMENTATION),1)
74 BUILDDIRS += build-Documentation
75 INSTALLDIRS += install-Documentation
76 endif
77
78 .PHONY: $(SUBDIRS)
79 .PHONY: $(BUILDDIRS)
80 .PHONY: $(INSTALLDIRS)
81 .PHONY: $(TESTDIRS)
82 .PHONY: $(CLEANDIRS)
83 .PHONY: all install clean
84
85 # Create all the static targets
86 static_objects = $(patsubst %.o, %.static.o, $(objects))
87 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
88 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
89
90 # Define static compilation flags
91 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
92 STATIC_LDFLAGS = -static -Wl,--gc-sections
93 STATIC_LIBS = $(lib_LIBS) -lpthread
94
95 libs_shared = libbtrfs.so.0.1
96 libs_static = libbtrfs.a
97 libs = $(libs_shared) $(libs_static)
98 lib_links = libbtrfs.so.0 libbtrfs.so
99 headers = $(libbtrfs_headers)
100
101 # make C=1 to enable sparse
102 check_defs := .cc-defines.h 
103 ifdef C
104         #
105         # We're trying to use sparse against glibc headers which go wild
106         # trying to use internal compiler macros to test features.  We
107         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
108         # 'cause sparse defines that one.
109         #
110         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
111                         grep -v __SIZE_TYPE__ > $(check_defs))
112         check = sparse -include $(check_defs) -D__CHECKER__ \
113                 -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef
114         check_echo = echo
115         # don't use FORTIFY with sparse because glibc with FORTIFY can
116         # generate so many sparse errors that sparse stops parsing,
117         # which masks real errors that we want to see.
118 else
119         check = true
120         check_echo = true
121         AM_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
122 endif
123
124 %.o.d: %.c
125         $(Q)$(CC) -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(AM_CFLAGS) $(CFLAGS) $<
126
127 .c.o:
128         @$(check_echo) "    [SP]     $<"
129         $(Q)$(check) $(AM_CFLAGS) $(CFLAGS) $<
130         @echo "    [CC]     $@"
131         $(Q)$(CC) $(AM_CFLAGS) $(CFLAGS) -c $<
132
133 %.static.o: %.c
134         @echo "    [CC]     $@"
135         $(Q)$(CC) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
136
137 all: $(progs) $(BUILDDIRS)
138 $(SUBDIRS): $(BUILDDIRS)
139 $(BUILDDIRS):
140         @echo "Making all in $(patsubst build-%,%,$@)"
141         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
142
143 test:
144         $(Q)for t in $(TESTS); do \
145                 echo "     [TEST]    $$t"; \
146                 bash tests/$$t || exit 1; \
147         done
148
149 #
150 # NOTE: For static compiles, you need to have all the required libs
151 #       static equivalent available
152 #
153 static: $(progs_static)
154
155 version.h:
156         @echo "    [SH]     $@"
157         $(Q)bash version.sh
158
159 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
160         @echo "    [LD]     $@"
161         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(lib_LIBS) \
162                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
163
164 $(libs_static): $(libbtrfs_objects)
165         @echo "    [AR]     $@"
166         $(Q)$(AR) cru libbtrfs.a $(libbtrfs_objects)
167
168 $(lib_links):
169         @echo "    [LN]     $@"
170         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so.0
171         $(Q)$(LN) -sf libbtrfs.so.0.1 libbtrfs.so
172
173 # keep intermediate files from the below implicit rules around
174 .PRECIOUS: $(addsuffix .o,$(progs))
175
176 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
177 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
178 # turns them into a list of libraries to link against if they exist
179 #
180 # For static variants, use an extra $(subst) to get rid of the ".static"
181 # from the target name before translating to list of libs
182
183 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects)
184         @echo "    [LD]     $@"
185         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
186                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
187                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
188
189 btrfs-%: $(objects) $(libs) btrfs-%.o
190         @echo "    [LD]     $@"
191         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
192
193 btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs)
194         @echo "    [LD]     $@"
195         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
196                 $(objects) $(LDFLAGS) $(LIBS) -lpthread
197
198 btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
199         @echo "    [LD]     $@"
200         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
201                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
202
203 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
204 btrfsck: btrfs
205         @echo "    [LN]     $@"
206         $(Q)$(LN) -f btrfs btrfsck
207
208 btrfsck.static: btrfs.static
209         @echo "    [LN]     $@"
210         $(Q)$(LN) -f $^ $@
211
212 mkfs.btrfs: $(objects) $(libs) mkfs.o
213         @echo "    [LD]     $@"
214         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
215
216 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
217         @echo "    [LD]     $@"
218         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
219                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
220
221 btrfstune: $(objects) $(libs) btrfstune.o
222         @echo "    [LD]     $@"
223         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
224
225 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
226         @echo "    [LD]     $@"
227         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
228                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
229
230 dir-test: $(objects) $(libs) dir-test.o
231         @echo "    [LD]     $@"
232         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS)
233
234 quick-test: $(objects) $(libs) quick-test.o
235         @echo "    [LD]     $@"
236         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS)
237
238 ioctl-test: $(objects) $(libs) ioctl-test.o
239         @echo "    [LD]     $@"
240         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
241
242 send-test: $(objects) $(libs) send-test.o
243         @echo "    [LD]     $@"
244         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) send-test.o $(LDFLAGS) $(LIBS) -lpthread
245
246 library-test: $(libs_shared) library-test.o
247         @echo "    [LD]     $@"
248         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
249
250 library-test.static: $(libs_static) library-test.o
251         @echo "    [LD]     $@"
252         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
253
254 test-build:
255         $(MAKE) clean-all
256         $(MAKE) library-test
257         -$(MAKE) library-test.static
258         $(MAKE) -j 8 all
259         -$(MAKE) -j 8 static
260         $(MAKE) -j 8 $(progs_extra)
261
262 manpages:
263         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
264
265 clean-all: clean-doc clean
266
267 clean: $(CLEANDIRS)
268         @echo "Cleaning"
269         $(Q)rm -f $(progs) cscope.out *.o *.o.d \
270               dir-test ioctl-test quick-test send-test library-test library-test-static \
271               btrfs.static mkfs.btrfs.static \
272               version.h $(check_defs) \
273               $(libs) $(lib_links) \
274               $(progs_static) $(progs_extra)
275
276 clean-doc:
277         @echo "Cleaning Documentation"
278         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
279
280 $(CLEANDIRS):
281         @echo "Cleaning $(patsubst clean-%,%,$@)"
282         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
283
284 install: $(libs) $(progs) $(INSTALLDIRS)
285         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
286         $(INSTALL) $(progs) $(DESTDIR)$(bindir)
287         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
288         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
289         $(LN) -f $(DESTDIR)$(bindir)/btrfs $(DESTDIR)$(bindir)/btrfsck
290         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
291         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
292         cp -a $(lib_links) $(DESTDIR)$(libdir)
293         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
294         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
295
296 install-static: $(progs_static) $(INSTALLDIRS)
297         for p in $(progs_static) ; do \
298                 $(INSTALL) -D -m755 $$p $(DESTDIR)$(bindir)/`basename $$p .static` ; \
299         done
300
301 $(INSTALLDIRS):
302         @echo "Making install in $(patsubst install-%,%,$@)"
303         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
304
305 uninstall:
306         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
307         cd $(DESTDIR)$(incdir); rm -f $(headers)
308         rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
309         cd $(DESTDIR)$(libdir); rm -f $(lib_links) $(libs)
310         cd $(DESTDIR)$(bindir); rm -f btrfsck fsck.btrfs $(progs)
311
312 ifneq ($(MAKECMDGOALS),clean)
313 -include $(objects:.o=.o.d) $(cmd-objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
314 endif