btrfs-progs: qgroup: Fix regression leads to corrupted qgroup status
[platform/upstream/btrfs-progs.git] / Makefile.in
1 # btrfs-progs
2 #
3 # Basic build targets:
4 #   all         all main tools
5 #   static      build static bnaries, requires static version of the libraries
6 #   test        run the full testsuite
7 #   install     install to default location (/usr/local)
8 #   clean       clean built binaries (not the documentation)
9 #   clean-all   clean as above, clean docs and generated files
10 #
11 # Tuning by variables (environment or make arguments):
12 #   V=1            verbose, print command lines (default: quiet)
13 #   C=1            run checker before compilation (default checker: sparse)
14 #   D=1            debugging build, turn off optimizations
15 #   D=dflags       dtto, turn on additional debugging features:
16 #                  verbose - print file:line along with error/warning messages
17 #                  trace   - print trace before the error/warning messages
18 #                  abort   - call abort() on first error (dumps core)
19 #                  all     - shortcut for all of the above
20 #                  asan    - enable address sanitizer compiler feature
21 #                  ubsan   - undefined behaviour sanitizer compiler feature
22 #   W=123          build with warnings (default: off)
23 #   DEBUG_CFLAGS   additional compiler flags for debugging build
24 #   EXTRA_CFLAGS   additional compiler flags
25 #   EXTRA_LDFLAGS  additional linker flags
26 #
27 # Static checkers:
28 #   CHECKER        static checker binary to be called (default: sparse)
29 #   CHECKER_FLAGS  flags to pass to CHECKER, can override CFLAGS
30 #
31
32 # Export all variables to sub-makes by default
33 export
34
35 include Makefile.extrawarn
36
37 CC = @CC@
38 LN_S = @LN_S@
39 AR = @AR@
40 RM = @RM@
41 RMDIR = @RMDIR@
42 INSTALL = @INSTALL@
43 DISABLE_DOCUMENTATION = @DISABLE_DOCUMENTATION@
44 DISABLE_BTRFSCONVERT = @DISABLE_BTRFSCONVERT@
45 BTRFSCONVERT_EXT2 = @BTRFSCONVERT_EXT2@
46
47 EXTRA_CFLAGS :=
48 EXTRA_LDFLAGS :=
49
50 DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
51 DEBUG_CFLAGS_INTERNAL =
52 DEBUG_CFLAGS :=
53
54 TOPDIR := $(shell pwd)
55
56 # Common build flags
57 CFLAGS = @CFLAGS@ \
58          -include config.h \
59          -DBTRFS_FLAT_INCLUDES \
60          -D_XOPEN_SOURCE=700  \
61          -fno-strict-aliasing \
62          -fPIC \
63          -I$(TOPDIR) \
64          -I$(TOPDIR)/kernel-lib \
65          $(EXTRAWARN_CFLAGS) \
66          $(DEBUG_CFLAGS_INTERNAL) \
67          $(EXTRA_CFLAGS)
68
69 LDFLAGS = @LDFLAGS@ \
70           -rdynamic $(EXTRA_LDFLAGS)
71
72 LIBS = @UUID_LIBS@ @BLKID_LIBS@ @ZLIB_LIBS@ @LZO2_LIBS@ -L. -pthread
73 LIBBTRFS_LIBS = $(LIBS)
74
75 # Static compilation flags
76 STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
77 STATIC_LDFLAGS = -static -Wl,--gc-sections
78 STATIC_LIBS = @UUID_LIBS_STATIC@ @BLKID_LIBS_STATIC@ \
79               @ZLIB_LIBS_STATIC@ @LZO2_LIBS_STATIC@ -L. -pthread
80
81 # don't use FORTIFY with sparse because glibc with FORTIFY can
82 # generate so many sparse errors that sparse stops parsing,
83 # which masks real errors that we want to see.
84 CHECKER := sparse
85 check_defs := .cc-defines.h
86 CHECKER_FLAGS := -include $(check_defs) -D__CHECKER__ \
87         -D__CHECK_ENDIAN__ -Wbitwise -Wuninitialized -Wshadow -Wundef \
88         -U_FORTIFY_SOURCE
89
90 objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \
91           root-tree.o dir-item.o file-item.o inode-item.o inode-map.o \
92           extent-cache.o extent_io.o volumes.o utils.o repair.o \
93           qgroup.o raid6.o free-space-cache.o kernel-lib/list_sort.o props.o \
94           ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
95           inode.o file.o find-root.o free-space-tree.o help.o
96 cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
97                cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
98                cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
99                cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
100                cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
101                cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o
102 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
103                    kernel-lib/crc32c.o \
104                    uuid-tree.o utils-lib.o rbtree-utils.o
105 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
106                kernel-lib/crc32c.h kernel-lib/list.h kerncompat.h \
107                kernel-lib/radix-tree.h extent-cache.h \
108                extent_io.h ioctl.h ctree.h btrfsck.h version.h
109 TESTS = fsck-tests.sh convert-tests.sh
110
111 udev_rules = 64-btrfs-dm.rules
112
113 prefix ?= @prefix@
114 exec_prefix = @exec_prefix@
115 bindir = @bindir@
116 libdir ?= @libdir@
117 incdir = @includedir@/btrfs
118 udevdir = @UDEVDIR@
119 udevruledir = ${udevdir}/rules.d
120
121 ifeq ("$(origin V)", "command line")
122   BUILD_VERBOSE = $(V)
123 endif
124 ifndef BUILD_VERBOSE
125   BUILD_VERBOSE = 0
126 endif
127
128 ifeq ($(BUILD_VERBOSE),1)
129   Q =
130 else
131   Q = @
132 endif
133
134 ifeq ("$(origin D)", "command line")
135   DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
136 endif
137
138 ifneq (,$(findstring verbose,$(D)))
139   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
140 endif
141
142 ifneq (,$(findstring trace,$(D)))
143   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
144 endif
145
146 ifneq (,$(findstring abort,$(D)))
147   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
148 endif
149
150 ifneq (,$(findstring all,$(D)))
151   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
152   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
153   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
154 endif
155
156 ifneq (,$(findstring asan,$(D)))
157   DEBUG_CFLAGS_INTERNAL += -fsanitize=address
158 endif
159
160 ifneq (,$(findstring ubsan,$(D)))
161   DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
162 endif
163
164 MAKEOPTS = --no-print-directory Q=$(Q)
165
166 # build all by default
167 progs = $(progs_install) btrfsck btrfs-corrupt-block btrfs-calc-size
168
169 # install only selected
170 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
171         btrfs-map-logical btrfs-image btrfs-zero-log \
172         btrfs-find-root btrfstune btrfs-show-super \
173         btrfs-select-super
174
175 progs_extra = btrfs-fragments
176
177 progs_static = $(foreach p,$(progs),$(p).static)
178
179 ifneq ($(DISABLE_BTRFSCONVERT),1)
180 progs_install += btrfs-convert
181 endif
182
183 # external libs required by various binaries; for btrfs-foo,
184 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
185 btrfs_convert_libs = @EXT2FS_LIBS@ @COM_ERR_LIBS@
186 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
187 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
188 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
189 btrfs_show_super_objects = cmds-inspect-dump-super.o
190 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
191
192 # collect values of the variables above
193 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
194
195 SUBDIRS =
196 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
197 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
198 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
199
200 ifneq ($(DISABLE_DOCUMENTATION),1)
201 BUILDDIRS += build-Documentation
202 INSTALLDIRS += install-Documentation
203 endif
204
205 .PHONY: $(SUBDIRS)
206 .PHONY: $(BUILDDIRS)
207 .PHONY: $(INSTALLDIRS)
208 .PHONY: $(TESTDIRS)
209 .PHONY: $(CLEANDIRS)
210 .PHONY: all install clean
211
212 # Create all the static targets
213 static_objects = $(patsubst %.o, %.static.o, $(objects))
214 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
215 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
216
217 libs_shared = libbtrfs.so.0.1
218 libs_static = libbtrfs.a
219 libs = $(libs_shared) $(libs_static)
220 lib_links = libbtrfs.so.0 libbtrfs.so
221 headers = $(libbtrfs_headers)
222
223 # make C=1 to enable sparse
224 ifdef C
225         # We're trying to use sparse against glibc headers which go wild
226         # trying to use internal compiler macros to test features.  We
227         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
228         # 'cause sparse defines that one.
229         #
230         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
231                         grep -v __SIZE_TYPE__ > $(check_defs))
232         check = $(CHECKER)
233         check_echo = echo
234 else
235         check = true
236         check_echo = true
237 endif
238
239 %.o.d: %.c
240         $(Q)$(CC) -MD -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
241
242 #
243 # Pick from per-file variables, btrfs_*_cflags
244 #
245 .c.o:
246         @$(check_echo) "    [SP]     $<"
247         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
248         @echo "    [CC]     $@"
249         $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags))
250
251 %.static.o: %.c
252         @echo "    [CC]     $@"
253         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags))
254
255 all: $(progs) $(BUILDDIRS)
256 $(SUBDIRS): $(BUILDDIRS)
257 $(BUILDDIRS):
258         @echo "Making all in $(patsubst build-%,%,$@)"
259         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
260
261 test-convert: btrfs btrfs-convert
262         @echo "    [TEST]   convert-tests.sh"
263         $(Q)bash tests/convert-tests.sh
264
265 test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
266         @echo "    [TEST]   fsck-tests.sh"
267         $(Q)bash tests/fsck-tests.sh
268
269 test-misc: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs btrfstune
270         @echo "    [TEST]   misc-tests.sh"
271         $(Q)bash tests/misc-tests.sh
272
273 test-mkfs: btrfs mkfs.btrfs
274         @echo "    [TEST]   mkfs-tests.sh"
275         $(Q)bash tests/mkfs-tests.sh
276
277 test-fuzz: btrfs
278         @echo "    [TEST]   fuzz-tests.sh"
279         $(Q)bash tests/fuzz-tests.sh
280
281 test-cli: btrfs
282         @echo "    [TEST]   cli-tests.sh"
283         $(Q)bash tests/cli-tests.sh
284
285 test-clean:
286         @echo "Cleaning tests"
287         $(Q)bash tests/clean-tests.sh
288
289 test-inst: all
290         @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
291                 echo "Test installation to $$tmpdest" && \
292                 $(MAKE) DESTDIR=$$tmpdest install && \
293                 $(RM) -rf -- $$tmpdest
294
295 test: test-fsck test-mkfs test-convert test-misc test-fuzz
296
297 #
298 # NOTE: For static compiles, you need to have all the required libs
299 #       static equivalent available
300 #
301 static: $(progs_static)
302
303 version.h: version.sh version.h.in configure.ac
304         @echo "    [SH]     $@"
305         $(Q)bash ./config.status --silent $@
306
307 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
308         @echo "    [LD]     $@"
309         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
310                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
311
312 $(libs_static): $(libbtrfs_objects)
313         @echo "    [AR]     $@"
314         $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
315
316 $(lib_links):
317         @echo "    [LN]     $@"
318         $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
319
320 # keep intermediate files from the below implicit rules around
321 .PRECIOUS: $(addsuffix .o,$(progs))
322
323 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
324 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
325 # turns them into a list of libraries to link against if they exist
326 #
327 # For static variants, use an extra $(subst) to get rid of the ".static"
328 # from the target name before translating to list of libs
329
330 btrfs-%.static: $(static_objects) btrfs-%.static.o $(static_libbtrfs_objects) $(patsubst %.o,%.static.o,$(standalone_deps))
331         @echo "    [LD]     $@"
332         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ $@.o $(static_objects) \
333                 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
334                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
335                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
336
337 btrfs-%: $(objects) $(libs_static) btrfs-%.o $(standalone_deps)
338         @echo "    [LD]     $@"
339         $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o \
340                 $($(subst -,_,$@-objects)) \
341                 $(libs_static) \
342                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
343
344 btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
345         @echo "    [LD]     $@"
346         $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
347                 $(objects) $(libs_static) $(LDFLAGS) $(LIBS)
348
349 btrfs.static: $(static_objects) btrfs.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
350         @echo "    [LD]     $@"
351         $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o $(static_cmds_objects) \
352                 $(static_objects) $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
353
354 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
355 btrfsck: btrfs
356         @echo "    [LN]     $@"
357         $(Q)$(LN_S) -f btrfs btrfsck
358
359 btrfsck.static: btrfs.static
360         @echo "    [LN]     $@"
361         $(Q)$(LN_S) -f $^ $@
362
363 mkfs.btrfs: $(objects) $(libs_static) mkfs.o
364         @echo "    [LD]     $@"
365         $(Q)$(CC) $(CFLAGS) -o mkfs.btrfs $(objects) $(libs_static) mkfs.o $(LDFLAGS) $(LIBS)
366
367 mkfs.btrfs.static: $(static_objects) mkfs.static.o $(static_libbtrfs_objects)
368         @echo "    [LD]     $@"
369         $(Q)$(CC) $(STATIC_CFLAGS) -o mkfs.btrfs.static mkfs.static.o $(static_objects) \
370                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
371
372 btrfstune: $(objects) $(libs_static) btrfstune.o
373         @echo "    [LD]     $@"
374         $(Q)$(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(libs_static) $(LDFLAGS) $(LIBS)
375
376 btrfstune.static: $(static_objects) btrfstune.static.o $(static_libbtrfs_objects)
377         @echo "    [LD]     $@"
378         $(Q)$(CC) $(STATIC_CFLAGS) -o $@ btrfstune.static.o $(static_objects) \
379                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
380
381 dir-test: $(objects) $(libs) dir-test.o
382         @echo "    [LD]     $@"
383         $(Q)$(CC) $(CFLAGS) -o dir-test $(objects) $(libs) dir-test.o $(LDFLAGS) $(LIBS)
384
385 quick-test: $(objects) $(libs) quick-test.o
386         @echo "    [LD]     $@"
387         $(Q)$(CC) $(CFLAGS) -o quick-test $(objects) $(libs) quick-test.o $(LDFLAGS) $(LIBS)
388
389 ioctl-test: $(objects) $(libs) ioctl-test.o
390         @echo "    [LD]     $@"
391         $(Q)$(CC) $(CFLAGS) -o ioctl-test $(objects) $(libs) ioctl-test.o $(LDFLAGS) $(LIBS)
392
393 send-test: $(objects) $(libs) send-test.o
394         @echo "    [LD]     $@"
395         $(Q)$(CC) $(CFLAGS) -o send-test $(objects) $(libs) send-test.o $(LDFLAGS) $(LIBS)
396
397 library-test: $(libs_shared) library-test.o
398         @echo "    [LD]     $@"
399         $(Q)$(CC) $(CFLAGS) -o library-test library-test.o $(LDFLAGS) -lbtrfs
400
401 library-test.static: $(libs_static) library-test.o
402         @echo "    [LD]     $@"
403         $(Q)$(CC) $(CFLAGS) -o library-test-static library-test.o $(LDFLAGS) $(libs_static)
404
405 test-build: test-build-pre test-build-real
406
407 test-build-pre:
408         $(MAKE) clean-all
409         ./autogen.sh
410         ./configure
411
412 test-build-real:
413         $(MAKE) library-test
414         -$(MAKE) library-test.static
415         $(MAKE) -j 8 all
416         -$(MAKE) -j 8 static
417         $(MAKE) -j 8 $(progs_extra)
418
419 manpages:
420         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
421
422
423 clean-all: clean clean-doc clean-gen
424
425 clean: $(CLEANDIRS)
426         @echo "Cleaning"
427         $(Q)$(RM) -f $(progs) cscope.out *.o *.o.d \
428                 kernel-lib/*.o kernel-lib/*.o.d \
429               dir-test ioctl-test quick-test send-test library-test library-test-static \
430               btrfs.static mkfs.btrfs.static \
431               $(check_defs) \
432               $(libs) $(lib_links) \
433               $(progs_static) $(progs_extra)
434
435 clean-doc:
436         @echo "Cleaning Documentation"
437         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
438
439 clean-gen:
440         @echo "Cleaning Generated Files"
441         $(Q)$(RM) -rf version.h config.status config.cache connfig.log \
442                 configure.lineno config.status.lineno Makefile \
443                 Documentation/Makefile \
444                 config.log config.h config.h.in~ aclocal.m4 \
445                 configure autom4te.cache/ config/
446
447 $(CLEANDIRS):
448         @echo "Cleaning $(patsubst clean-%,%,$@)"
449         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
450
451 install: $(libs) $(progs_install) $(INSTALLDIRS)
452         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
453         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
454         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
455         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
456         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
457         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
458         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
459         cp -a $(lib_links) $(DESTDIR)$(libdir)
460         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
461         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
462 ifneq ($(udevdir),)
463         $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
464         $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
465 endif
466
467 install-static: $(progs_static) $(INSTALLDIRS)
468         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
469         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
470         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
471         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
472
473 $(INSTALLDIRS):
474         @echo "Making install in $(patsubst install-%,%,$@)"
475         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
476
477 uninstall:
478         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
479         cd $(DESTDIR)$(incdir); $(RM) -f $(headers)
480         $(RMDIR) -p --ignore-fail-on-non-empty $(DESTDIR)$(incdir)
481         cd $(DESTDIR)$(libdir); $(RM) -f $(lib_links) $(libs)
482         cd $(DESTDIR)$(bindir); $(RM) -f btrfsck fsck.btrfs $(progs_install)
483
484 ifneq ($(MAKECMDGOALS),clean)
485 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
486 endif