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