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