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