btrfs-progs: build: fix building libbtrfs.so with tsan
[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
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
125 TESTS = fsck-tests.sh convert-tests.sh
126
127 udev_rules = 64-btrfs-dm.rules
128
129 ifeq ("$(origin V)", "command line")
130   BUILD_VERBOSE = $(V)
131 endif
132 ifndef BUILD_VERBOSE
133   BUILD_VERBOSE = 0
134 endif
135
136 ifeq ($(BUILD_VERBOSE),1)
137   Q =
138 else
139   Q = @
140 endif
141
142 ifeq ("$(origin D)", "command line")
143   DEBUG_CFLAGS_INTERNAL = $(DEBUG_CFLAGS_DEFAULT) $(DEBUG_CFLAGS)
144   DEBUG_LDFLAGS_INTERNAL = $(DEBUG_LDFLAGS_DEFAULT) $(DEBUG_LDFLAGS)
145 endif
146
147 ifneq (,$(findstring verbose,$(D)))
148   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
149 endif
150
151 ifneq (,$(findstring trace,$(D)))
152   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
153 endif
154
155 ifneq (,$(findstring abort,$(D)))
156   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
157 endif
158
159 ifneq (,$(findstring all,$(D)))
160   DEBUG_CFLAGS_INTERNAL += -DDEBUG_VERBOSE_ERROR=1
161   DEBUG_CFLAGS_INTERNAL += -DDEBUG_TRACE_ON_ERROR=1
162   DEBUG_CFLAGS_INTERNAL += -DDEBUG_ABORT_ON_ERROR=1
163 endif
164
165 ifneq (,$(findstring asan,$(D)))
166   DEBUG_CFLAGS_INTERNAL += -fsanitize=address
167 endif
168
169 ifneq (,$(findstring tsan,$(D)))
170   DEBUG_CFLAGS_INTERNAL += -fsanitize=thread -fPIC
171   DEBUG_LDFLAGS_INTERNAL += -fsanitize=thread -ltsan -pie
172 endif
173
174 ifneq (,$(findstring ubsan,$(D)))
175   DEBUG_CFLAGS_INTERNAL += -fsanitize=undefined
176 endif
177
178 ifneq (,$(findstring bcheck,$(D)))
179   DEBUG_CFLAGS_INTERNAL += -DDEBUG_BUILD_CHECKS
180 endif
181
182 MAKEOPTS = --no-print-directory Q=$(Q)
183
184 # build all by default
185 progs = $(progs_install) btrfsck btrfs-corrupt-block
186
187 # install only selected
188 progs_install = btrfs mkfs.btrfs btrfs-debug-tree \
189         btrfs-map-logical btrfs-image btrfs-zero-log \
190         btrfs-find-root btrfstune \
191         btrfs-select-super
192
193 # other tools, not built by default
194 progs_extra = btrfs-fragments btrfs-calc-size btrfs-show-super
195
196 progs_static = $(foreach p,$(progs),$(p).static)
197
198 ifneq ($(DISABLE_BTRFSCONVERT),1)
199 progs_install += btrfs-convert
200 endif
201
202 # external libs required by various binaries; for btrfs-foo,
203 # specify btrfs_foo_libs = <list of libs>; see $($(subst...)) rules below
204 btrfs_convert_cflags = -DBTRFSCONVERT_EXT2=$(BTRFSCONVERT_EXT2)
205 btrfs_convert_cflags += -DBTRFSCONVERT_REISERFS=$(BTRFSCONVERT_REISERFS)
206 btrfs_fragments_libs = -lgd -lpng -ljpeg -lfreetype
207 btrfs_debug_tree_objects = cmds-inspect-dump-tree.o
208 btrfs_show_super_objects = cmds-inspect-dump-super.o
209 btrfs_calc_size_objects = cmds-inspect-tree-stats.o
210
211 # collect values of the variables above
212 standalone_deps = $(foreach dep,$(patsubst %,%_objects,$(subst -,_,$(filter btrfs-%, $(progs)))),$($(dep)))
213
214 SUBDIRS =
215 BUILDDIRS = $(patsubst %,build-%,$(SUBDIRS))
216 INSTALLDIRS = $(patsubst %,install-%,$(SUBDIRS))
217 CLEANDIRS = $(patsubst %,clean-%,$(SUBDIRS))
218
219 ifneq ($(DISABLE_DOCUMENTATION),1)
220 BUILDDIRS += build-Documentation
221 INSTALLDIRS += install-Documentation
222 endif
223
224 .PHONY: $(SUBDIRS)
225 .PHONY: $(BUILDDIRS)
226 .PHONY: $(INSTALLDIRS)
227 .PHONY: $(TESTDIRS)
228 .PHONY: $(CLEANDIRS)
229 .PHONY: all install clean
230 .PHONY: FORCE
231
232 # Create all the static targets
233 static_objects = $(patsubst %.o, %.static.o, $(objects))
234 static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
235 static_libbtrfs_objects = $(patsubst %.o, %.static.o, $(libbtrfs_objects))
236 static_convert_objects = $(patsubst %.o, %.static.o, $(convert_objects))
237 static_mkfs_objects = $(patsubst %.o, %.static.o, $(mkfs_objects))
238
239 libs_shared = libbtrfs.so.0.1
240 libs_static = libbtrfs.a
241 libs = $(libs_shared) $(libs_static)
242 lib_links = libbtrfs.so.0 libbtrfs.so
243 headers = $(libbtrfs_headers)
244
245 # make C=1 to enable sparse
246 ifdef C
247         # We're trying to use sparse against glibc headers which go wild
248         # trying to use internal compiler macros to test features.  We
249         # copy gcc's and give them to sparse.  But not __SIZE_TYPE__
250         # 'cause sparse defines that one.
251         #
252         dummy := $(shell $(CC) -dM -E -x c - < /dev/null | \
253                         grep -v __SIZE_TYPE__ > $(check_defs))
254         check = $(CHECKER)
255         check_echo = echo
256 else
257         check = true
258         check_echo = true
259 endif
260
261 %.o.d: %.c
262         $(Q)$(CC) -MD -MM -MG -MF $@ -MT $(@:.o.d=.o) -MT $(@:.o.d=.static.o) -MT $@ $(CFLAGS) $<
263
264 #
265 # Pick from per-file variables, btrfs_*_cflags
266 #
267 .c.o:
268         @$(check_echo) "    [SP]     $<"
269         $(Q)$(check) $(CFLAGS) $(CHECKER_FLAGS) $<
270         @echo "    [CC]     $@"
271         $(Q)$(CC) $(CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.o=%)-cflags)) \
272                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
273
274 %.static.o: %.c
275         @echo "    [CC]     $@"
276         $(Q)$(CC) $(STATIC_CFLAGS) -c $< -o $@ $($(subst -,_,$(@:%.static.o=%)-cflags)) \
277                 $($(subst -,_,btrfs-$(@:%/$(notdir $@)=%)-cflags))
278
279 all: $(progs) libbtrfs $(BUILDDIRS)
280 $(SUBDIRS): $(BUILDDIRS)
281 $(BUILDDIRS):
282         @echo "Making all in $(patsubst build-%,%,$@)"
283         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
284
285 test-convert: btrfs btrfs-convert
286         @echo "    [TEST]   convert-tests.sh"
287         $(Q)bash tests/convert-tests.sh
288
289 test-check: test-fsck
290 test-fsck: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune
291         @echo "    [TEST]   fsck-tests.sh"
292         $(Q)bash tests/fsck-tests.sh
293
294 test-misc: btrfs btrfs-image btrfs-corrupt-block mkfs.btrfs btrfstune fssum \
295                 btrfs-zero-log btrfs-find-root btrfs-select-super
296         @echo "    [TEST]   misc-tests.sh"
297         $(Q)bash tests/misc-tests.sh
298
299 test-mkfs: btrfs mkfs.btrfs
300         @echo "    [TEST]   mkfs-tests.sh"
301         $(Q)bash tests/mkfs-tests.sh
302
303 test-fuzz: btrfs
304         @echo "    [TEST]   fuzz-tests.sh"
305         $(Q)bash tests/fuzz-tests.sh
306
307 test-cli: btrfs
308         @echo "    [TEST]   cli-tests.sh"
309         $(Q)bash tests/cli-tests.sh
310
311 test-clean:
312         @echo "Cleaning tests"
313         $(Q)bash tests/clean-tests.sh
314
315 test-inst: all
316         @tmpdest=`mktemp --tmpdir -d btrfs-inst.XXXXXX` && \
317                 echo "Test installation to $$tmpdest" && \
318                 $(MAKE) $(MAKEOPTS) DESTDIR=$$tmpdest install && \
319                 $(RM) -rf -- $$tmpdest
320
321 test: test-fsck test-mkfs test-convert test-misc test-fuzz test-cli
322
323 #
324 # NOTE: For static compiles, you need to have all the required libs
325 #       static equivalent available
326 #
327 static: $(progs_static)
328
329 version.h: version.sh version.h.in configure.ac
330         @echo "    [SH]     $@"
331         $(Q)bash ./config.status --silent $@
332
333 mktables: kernel-lib/mktables.c
334         @echo "    [CC]     $@"
335         $(Q)$(CC) $(CFLAGS) $< -o $@
336
337 # the target can be regenerated manually using mktables, but a local copy is
338 # kept so the build process is simpler
339 kernel-lib/tables.c:
340         @echo "    [TABLE]  $@"
341         $(Q)./mktables > $@ || ($(RM) -f $@ && exit 1)
342
343 libbtrfs: $(libs_shared) $(lib_links)
344
345 $(libs_shared): $(libbtrfs_objects) $(lib_links) send.h
346         @echo "    [LD]     $@"
347         $(Q)$(CC) $(CFLAGS) $(libbtrfs_objects) $(LDFLAGS) $(LIBBTRFS_LIBS) \
348                 -shared -Wl,-soname,libbtrfs.so.0 -o libbtrfs.so.0.1
349
350 $(libs_static): $(libbtrfs_objects)
351         @echo "    [AR]     $@"
352         $(Q)$(AR) cr libbtrfs.a $(libbtrfs_objects)
353
354 $(lib_links):
355         @echo "    [LN]     $@"
356         $(Q)$(LN_S) -f libbtrfs.so.0.1 $@
357
358 # keep intermediate files from the below implicit rules around
359 .PRECIOUS: $(addsuffix .o,$(progs))
360
361 # Make any btrfs-foo out of btrfs-foo.o, with appropriate libs.
362 # The $($(subst...)) bits below takes the btrfs_*_libs definitions above and
363 # turns them into a list of libraries to link against if they exist
364 #
365 # For static variants, use an extra $(subst) to get rid of the ".static"
366 # from the target name before translating to list of libs
367
368 btrfs-%.static: btrfs-%.static.o $(static_objects) $(patsubst %.o,%.static.o,$(standalone_deps)) $(static_libbtrfs_objects)
369         @echo "    [LD]     $@"
370         $(Q)$(CC) -o $@ $@.o $(static_objects) \
371                 $(patsubst %.o, %.static.o, $($(subst -,_,$(subst .static,,$@)-objects))) \
372                 $(static_libbtrfs_objects) $(STATIC_LDFLAGS) \
373                 $($(subst -,_,$(subst .static,,$@)-libs)) $(STATIC_LIBS)
374
375 btrfs-%: btrfs-%.o $(objects) $(standalone_deps) $(libs_static)
376         @echo "    [LD]     $@"
377         $(Q)$(CC) -o $@ $(objects) $@.o \
378                 $($(subst -,_,$@-objects)) \
379                 $(libs_static) \
380                 $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
381
382 btrfs: btrfs.o $(objects) $(cmds_objects) $(libs_static)
383         @echo "    [LD]     $@"
384         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
385
386 btrfs.static: btrfs.static.o $(static_objects) $(static_cmds_objects) $(static_libbtrfs_objects)
387         @echo "    [LD]     $@"
388         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
389
390 # For backward compatibility, 'btrfs' changes behaviour to fsck if it's named 'btrfsck'
391 btrfsck: btrfs
392         @echo "    [LN]     $@"
393         $(Q)$(LN_S) -f btrfs btrfsck
394
395 btrfsck.static: btrfs.static
396         @echo "    [LN]     $@"
397         $(Q)$(LN_S) -f $^ $@
398
399 mkfs.btrfs: $(mkfs_objects) $(objects) $(libs_static)
400         @echo "    [LD]     $@"
401         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
402
403 mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
404         @echo "    [LD]     $@"
405         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
406
407 btrfstune: btrfstune.o $(objects) $(libs_static)
408         @echo "    [LD]     $@"
409         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
410
411 btrfstune.static: btrfstune.static.o $(static_objects) $(static_libbtrfs_objects)
412         @echo "    [LD]     $@"
413         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
414
415 btrfs-image: image/main.o $(objects) $(libs_static)
416         @echo "    [LD]     $@"
417         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
418
419 btrfs-image.static: image/main.static.o $(static_objects) $(static_libbtrfs_objects)
420         @echo "    [LD]     $@"
421         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
422
423 btrfs-convert: $(convert_objects) $(objects) $(libs_static)
424         @echo "    [LD]     $@"
425         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(btrfs_convert_libs) $(LIBS)
426
427 btrfs-convert.static: $(static_convert_objects) $(static_objects) $(static_libbtrfs_objects)
428         @echo "    [LD]     $@"
429         $(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(btrfs_convert_libs) $(STATIC_LIBS)
430
431 dir-test: dir-test.o $(objects) $(libs)
432         @echo "    [LD]     $@"
433         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
434
435 quick-test: quick-test.o $(objects) $(libs)
436         @echo "    [LD]     $@"
437         $(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
438
439 ioctl-test.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
440         @echo "    [CC]   $@"
441         $(Q)$(CC) $(CFLAGS) -c $< -o $@
442
443 ioctl-test-32.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
444         @echo "    [CC32]   $@"
445         $(Q)$(CC) $(CFLAGS) -m32 -c $< -o $@
446
447 ioctl-test-64.o: ioctl-test.c ioctl.h kerncompat.h ctree.h
448         @echo "    [CC64]   $@"
449         $(Q)$(CC) $(CFLAGS) -m64 -c $< -o $@
450
451 ioctl-test: ioctl-test.o
452         @echo "    [LD]   $@"
453         $(Q)$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
454         @echo "   ?[PAHOLE] $@.pahole"
455         -$(Q)pahole $@ > $@.pahole
456
457 ioctl-test-32: ioctl-test-32.o
458         @echo "    [LD32]   $@"
459         $(Q)$(CC) -m32 -o $@ $< $(LDFLAGS)
460         @echo "   ?[PAHOLE] $@.pahole"
461         -$(Q)pahole $@ > $@.pahole
462
463 ioctl-test-64: ioctl-test-64.o
464         @echo "    [LD64]   $@"
465         $(Q)$(CC) -m64 -o $@ $< $(LDFLAGS)
466         @echo "   ?[PAHOLE] $@.pahole"
467         -$(Q)pahole $@ > $@.pahole
468
469 test-ioctl: ioctl-test ioctl-test-32 ioctl-test-64
470         @echo "    [TEST/ioctl]"
471         $(Q)./ioctl-test > ioctl-test.log
472         $(Q)./ioctl-test-32 > ioctl-test-32.log
473         $(Q)./ioctl-test-64 > ioctl-test-64.log
474
475 library-test: library-test.c $(libs_shared)
476         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
477         $(Q)mkdir -p $(TMPD)/include/btrfs && \
478         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
479         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(TOPDIR)/,$^) -Wl,-rpath=$(TOPDIR) -lbtrfs
480         @echo "    [TEST RUN]   $@"
481         $(Q)cd $(TMPD) && ./$@
482         @echo "    [TEST CLEAN] $@"
483         $(Q)$(RM) -rf -- $(TMPD)
484
485 library-test.static: library-test.c $(libs_static)
486         @echo "    [TEST PREP]  $@"$(eval TMPD=$(shell mktemp -d))
487         $(Q)mkdir -p $(TMPD)/include/btrfs && \
488         cp $(libbtrfs_headers) $(TMPD)/include/btrfs && \
489         cd $(TMPD) && $(CC) -I$(TMPD)/include -o $@ $(addprefix $(TOPDIR)/,$^) $(STATIC_LDFLAGS) $(STATIC_LIBS)
490         @echo "    [TEST RUN]   $@"
491         $(Q)cd $(TMPD) && ./$@
492         @echo "    [TEST CLEAN] $@"
493         $(Q)$(RM) -rf -- $(TMPD)
494
495 fssum: tests/fssum.c tests/sha224-256.c
496         @echo "    [LD]   $@"
497         $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
498
499 test-build: test-build-pre test-build-real
500
501 test-build-pre:
502         $(MAKE) $(MAKEOPTS) clean-all
503         ./autogen.sh
504         ./configure
505
506 test-build-real:
507         $(MAKE) $(MAKEOPTS) library-test
508         -$(MAKE) $(MAKEOPTS) library-test.static
509         $(MAKE) $(MAKEOPTS) -j 8 all
510         -$(MAKE) $(MAKEOPTS) -j 8 static
511         $(MAKE) $(MAKEOPTS) -j 8 $(progs_extra)
512
513 manpages:
514         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation
515
516 tags: FORCE
517         @echo "    [TAGS]   $(TAGS_CMD)"
518         $(Q)$(TAGS_CMD) *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch]
519
520 cscope: FORCE
521         @echo "    [CSCOPE] $(CSCOPE_CMD)"
522         $(Q)ls -1 *.[ch] image/*.[ch] convert/*.[ch] mkfs/*.[ch] > cscope.files
523         $(Q)$(CSCOPE_CMD)
524
525 clean-all: clean clean-doc clean-gen
526
527 clean: $(CLEANDIRS)
528         @echo "Cleaning"
529         $(Q)$(RM) -f -- $(progs) *.o *.o.d \
530                 kernel-lib/*.o kernel-lib/*.o.d \
531                 kernel-shared/*.o kernel-shared/*.o.d \
532                 image/*.o image/*.o.d \
533                 convert/*.o convert/*.o.d \
534                 mkfs/*.o mkfs/*.o.d \
535               dir-test ioctl-test quick-test library-test library-test-static \
536               mktables btrfs.static mkfs.btrfs.static fssum \
537               $(check_defs) \
538               $(libs) $(lib_links) \
539               $(progs_static) $(progs_extra)
540
541 clean-doc:
542         @echo "Cleaning Documentation"
543         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation clean
544
545 clean-gen:
546         @echo "Cleaning Generated Files"
547         $(Q)$(RM) -rf -- version.h config.status config.cache connfig.log \
548                 configure.lineno config.status.lineno Makefile.inc \
549                 Documentation/Makefile tags \
550                 cscope.files cscope.out cscope.in.out cscope.po.out \
551                 config.log config.h config.h.in~ aclocal.m4 \
552                 configure autom4te.cache/ config/
553
554 $(CLEANDIRS):
555         @echo "Cleaning $(patsubst clean-%,%,$@)"
556         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst clean-%,%,$@) clean
557
558 install: $(libs) $(progs_install) $(INSTALLDIRS)
559         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
560         $(INSTALL) $(progs_install) $(DESTDIR)$(bindir)
561         $(INSTALL) fsck.btrfs $(DESTDIR)$(bindir)
562         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
563         $(LN_S) -f btrfs $(DESTDIR)$(bindir)/btrfsck
564         $(INSTALL) -m755 -d $(DESTDIR)$(libdir)
565         $(INSTALL) $(libs) $(DESTDIR)$(libdir)
566         cp -a $(lib_links) $(DESTDIR)$(libdir)
567         $(INSTALL) -m755 -d $(DESTDIR)$(incdir)
568         $(INSTALL) -m644 $(headers) $(DESTDIR)$(incdir)
569 ifneq ($(udevdir),)
570         $(INSTALL) -m755 -d $(DESTDIR)$(udevruledir)
571         $(INSTALL) -m644 $(udev_rules) $(DESTDIR)$(udevruledir)
572 endif
573
574 install-static: $(progs_static) $(INSTALLDIRS)
575         $(INSTALL) -m755 -d $(DESTDIR)$(bindir)
576         $(INSTALL) $(progs_static) $(DESTDIR)$(bindir)
577         # btrfsck is a link to btrfs in the src tree, make it so for installed file as well
578         $(LN_S) -f btrfs.static $(DESTDIR)$(bindir)/btrfsck.static
579
580 $(INSTALLDIRS):
581         @echo "Making install in $(patsubst install-%,%,$@)"
582         $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst install-%,%,$@) install
583
584 uninstall:
585         $(Q)$(MAKE) $(MAKEOPTS) -C Documentation uninstall
586         cd $(DESTDIR)$(incdir); $(RM) -f -- $(headers)
587         $(RMDIR) -p --ignore-fail-on-non-empty -- $(DESTDIR)$(incdir)
588         cd $(DESTDIR)$(libdir); $(RM) -f -- $(lib_links) $(libs)
589         cd $(DESTDIR)$(bindir); $(RM) -f -- btrfsck fsck.btrfs $(progs_install)
590
591 ifneq ($(MAKECMDGOALS),clean)
592 -include $(objects:.o=.o.d) $(cmds_objects:.o=.o.d) $(subst .btrfs,, $(filter-out btrfsck.o.d, $(progs:=.o.d)))
593 endif