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